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