r42: Reverse the header-checking logic: use a local nbd.h and fall back to
[nbd.git] / cliserv.h
1 /* This header file is shared by client & server. They really have
2  * something to share...
3  * */
4
5 /* Client/server protocol is as follows:
6    Send INIT_PASSWD
7    Send 64-bit cliserv_magic
8    Send 64-bit size of exported device
9    Send 128 bytes of zeros (reserved for future use)
10  */
11
12 #include "config.h"
13 #include <errno.h>
14 #include <string.h>
15 #include <netdb.h>
16 #include <netinet/tcp.h>
17 #include <stdlib.h>
18
19 #if SIZEOF_UNSIGNED_SHORT_INT==4
20 typedef unsigned short u32;
21 #elif SIZEOF_UNSIGNED_INT==4
22 typedef unsigned int u32;
23 #elif SIZEOF_UNSIGNED_LONG_INT==4
24 typedef unsigned long u32;
25 #else
26 #error I need at least some 32-bit type
27 #endif
28
29 #if SIZEOF_UNSIGNED_INT==8
30 typedef unsigned int u64;
31 #elif SIZEOF_UNSIGNED_LONG_INT==8
32 typedef unsigned long u64;
33 #elif SIZEOF_UNSIGNED_LONG_LONG_INT==8
34 typedef unsigned long long u64;
35 #else
36 #error I need at least some 64-bit type
37 #endif
38
39 #ifdef HAVE_LOCAL_NBD_H
40 #include "nbd.h"
41 #else
42 #ifdef HAVE_LINUX_NBD_H
43 #include <linux/nbd.h>
44 #else
45 #error I need an nbd.h somewhere. Either install it in <linux/nbd.h> or put it in the current directory.
46 #endif
47 #endif
48
49 #if NBD_LFS==1
50 #define _LARGEFILE_SOURCE
51 #define _FILE_OFFSET_BITS 64
52 #endif
53
54 u64 cliserv_magic = 0x00420281861253LL;
55 #define INIT_PASSWD "NBDMAGIC"
56
57 #define INFO(a) do { } while(0)
58
59 void setmysockopt(int sock)
60 {
61         int size = 1;
62 #if 0
63         if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
64                  INFO("(no sockopt/1: %m)");
65 #endif
66 #ifdef  IPPROTO_TCP
67         size = 1;
68         if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
69                  INFO("(no sockopt/2: %m)");
70 #endif
71 #if 0
72         size = 1024;
73         if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
74                  INFO("(no sockopt/3: %m)");
75 #endif
76 }
77
78 void err(const char *s)
79 {
80         const int maxlen = 150;
81         char s1[maxlen], *s2;
82         int n = 0;
83
84         strncpy(s1, s, maxlen);
85         if (s2 = strstr(s, "%m")) {
86                 strcpy(s1 + (s2 - s), strerror(errno));
87                 s2 += 2;
88                 strcpy(s1 + strlen(s1), s2);
89         }
90 #ifndef sun
91         /* Solaris doesn't have %h in syslog */
92         else if (s2 = strstr(s, "%h")) {
93                 strcpy(s1 + (s2 - s), hstrerror(h_errno));
94                 s2 += 2;
95                 strcpy(s1 + strlen(s1), s2);
96         }
97 #endif
98
99         s1[maxlen-1] = '\0';
100 #ifdef ISSERVER
101         syslog(LOG_ERR, s1);
102 #else
103         fprintf(stderr, "Error: %s\n", s1);
104 #endif
105         exit(1);
106 }
107
108 void logging(void)
109 {
110 #ifdef ISSERVER
111         openlog(MY_NAME, LOG_PID, LOG_DAEMON);
112 #endif
113         setvbuf(stdout, NULL, _IONBF, 0);
114         setvbuf(stderr, NULL, _IONBF, 0);
115 }
116
117 #ifdef WORDS_BIGENDIAN
118 u64 ntohll(u64 a)
119 {
120         return a;
121 }
122 #else
123 u64 ntohll(u64 a)
124 {
125         u32 lo = a & 0xffffffff;
126         u32 hi = a >> 32U;
127         lo = ntohl(lo);
128         hi = ntohl(hi);
129         return ((u64) lo) << 32U | hi;
130 }
131 #endif
132 #define htonll ntohll