r34: Fixed one minor bug
[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
17 #if SIZEOF_UNSIGNED_SHORT_INT==4
18 typedef unsigned short u32;
19 #elif SIZEOF_UNSIGNED_INT==4
20 typedef unsigned int u32;
21 #elif SIZEOF_UNSIGNED_LONG_INT==4
22 typedef unsigned long u32;
23 #else
24 #error I need at least some 32-bit type
25 #endif
26
27 #if SIZEOF_UNSIGNED_INT==8
28 typedef unsigned int u64;
29 #elif SIZEOF_UNSIGNED_LONG_INT==8
30 typedef unsigned long u64;
31 #elif SIZEOF_UNSIGNED_LONG_LONG_INT==8
32 typedef unsigned long long u64;
33 #else
34 #error I need at least some 64-bit type
35 #endif
36
37 #ifdef HAVE_LINUX_NBD_H
38 #include <linux/nbd.h>
39 #else
40 /* get it from a kernel source somewhere, and dump it in this directory.
41  * Required for people trying to compile nbd-server on a non-linux
42  * system. */
43 #include "nbd.h"
44 #endif
45
46 u64 cliserv_magic = 0x00420281861253LL;
47 #define INIT_PASSWD "NBDMAGIC"
48
49 #define INFO(a) do { } while(0)
50
51 void setmysockopt(int sock)
52 {
53         int size = 1;
54 #if 0
55         if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
56                  INFO("(no sockopt/1: %m)");
57 #endif
58 #ifdef  IPPROTO_TCP
59         size = 1;
60         if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
61                  INFO("(no sockopt/2: %m)");
62 #endif
63 #if 0
64         size = 1024;
65         if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
66                  INFO("(no sockopt/3: %m)");
67 #endif
68 }
69
70 void err(const char *s)
71 {
72         const int maxlen = 150;
73         char s1[maxlen], *s2;
74         int n = 0;
75
76         strncpy(s1, s, maxlen);
77         if (s2 = strstr(s, "%m")) {
78                 strcpy(s1 + (s2 - s), strerror(errno));
79                 s2 += 2;
80                 strcpy(s1 + strlen(s1), s2);
81         }
82 #ifndef sun
83         /* Solaris doesn't have %h in syslog */
84         else if (s2 = strstr(s, "%h")) {
85                 strcpy(s1 + (s2 - s), hstrerror(h_errno));
86                 s2 += 2;
87                 strcpy(s1 + strlen(s1), s2);
88         }
89 #endif
90
91         s1[maxlen-1] = '\0';
92 #ifdef ISSERVER
93         syslog(LOG_ERR, s1);
94 #else
95         fprintf(stderr, "Error: %s\n", s1);
96 #endif
97         exit(1);
98 }
99
100 void logging(void)
101 {
102 #ifdef ISSERVER
103         openlog(MY_NAME, LOG_PID, LOG_DAEMON);
104 #endif
105         setvbuf(stdout, NULL, _IONBF, 0);
106         setvbuf(stderr, NULL, _IONBF, 0);
107 }
108
109 #ifdef WORDS_BIGENDIAN
110 u64 ntohll(u64 a)
111 {
112         return a;
113 }
114 #else
115 u64 ntohll(u64 a)
116 {
117         u32 lo = a & 0xffffffff;
118         u32 hi = a >> 32U;
119         lo = ntohl(lo);
120         hi = ntohl(hi);
121         return ((u64) lo) << 32U | hi;
122 }
123 #endif
124 #define htonll ntohll