974352349d5083111b56852c21beab741341266a
[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 #include <linux/nbd.h>
38
39 u64 cliserv_magic = 0x00420281861253LL;
40 #define INIT_PASSWD "NBDMAGIC"
41
42 #define INFO(a) do { } while(0)
43
44 void setmysockopt(int sock)
45 {
46         int size = 1;
47 #if 0
48         if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
49                  INFO("(no sockopt/1: %m)");
50 #endif
51 #ifdef  IPPROTO_TCP
52         size = 1;
53         if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
54                  INFO("(no sockopt/2: %m)");
55 #endif
56 #if 0
57         size = 1024;
58         if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
59                  INFO("(no sockopt/3: %m)");
60 #endif
61 }
62
63 void err(const char *s)
64 {
65         const int maxlen = 150;
66         char s1[maxlen], *s2;
67         int n = 0;
68
69         strncpy(s1, s, maxlen);
70         if (s2 = strstr(s, "%m")) {
71                 strcpy(s1 + (s2 - s), strerror(errno));
72                 s2 += 2;
73                 strcpy(s1 + strlen(s1), s2);
74         }
75 #ifndef sun
76         /* Solaris doesn't have %h in syslog */
77         else if (s2 = strstr(s, "%h")) {
78                 strcpy(s1 + (s2 - s), hstrerror(h_errno));
79                 s2 += 2;
80                 strcpy(s1 + strlen(s1), s2);
81         }
82 #endif
83
84         s1[maxlen-1] = '\0';
85 #ifdef ISSERVER
86         syslog(LOG_ERR, s1);
87 #else
88         fprintf(stderr, "Error: %s\n", s1);
89 #endif
90         exit(1);
91 }
92
93 void logging(void)
94 {
95 #ifdef ISSERVER
96         openlog(MY_NAME, LOG_PID, LOG_DAEMON);
97 #endif
98         setvbuf(stdout, NULL, _IONBF, 0);
99         setvbuf(stderr, NULL, _IONBF, 0);
100 }
101
102 #ifdef WORDS_BIGENDIAN
103 u64 ntohll(u64 a)
104 {
105         return a;
106 }
107 #else
108 u64 ntohll(u64 a)
109 {
110         u32 lo = a & 0xffffffff;
111         u32 hi = a >> 32U;
112         lo = ntohl(lo);
113         hi = ntohl(hi);
114         return ((u64) lo) << 32U | hi;
115 }
116 #endif
117 #define htonll ntohll