Build system fixes and documentation
[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 #ifdef NBD_H_LOCAL
39 /* 2.6.18 and above use __be* rather than u* */
40 #define __be32 u32
41 #define __be64 u64
42 #include "nbd.h"
43 #endif
44 #ifdef NBD_H_LINUX
45 # ifdef HAVE_LINUX_TYPES_H
46 #  include <linux/types.h>
47 # else
48 #  define __be32 u32
49 #  define __be64 u64
50 # endif
51 # include <linux/nbd.h>
52 #endif
53
54 #if NBD_LFS==1
55 #define _LARGEFILE_SOURCE
56 #define _FILE_OFFSET_BITS 64
57 #endif
58
59 u64 cliserv_magic = 0x00420281861253LL;
60 #define INIT_PASSWD "NBDMAGIC"
61
62 #define INFO(a) do { } while(0)
63
64 void setmysockopt(int sock) {
65         int size = 1;
66 #if 0
67         if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
68                  INFO("(no sockopt/1: %m)");
69 #endif
70 #ifdef  IPPROTO_TCP
71         size = 1;
72         if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
73                  INFO("(no sockopt/2: %m)");
74 #endif
75 #if 0
76         size = 1024;
77         if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
78                  INFO("(no sockopt/3: %m)");
79 #endif
80 }
81
82 #ifndef G_GNUC_NORETURN
83 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
84 #define G_GNUC_NORETURN __attribute__((__noreturn__))
85 #else
86 #define G_GNUC_NORETURN
87 #endif
88 #endif
89
90 void err(const char *s) G_GNUC_NORETURN;
91
92 void err(const char *s) {
93         const int maxlen = 150;
94         char s1[maxlen], *s2;
95
96         strncpy(s1, s, maxlen);
97         if ((s2 = strstr(s, "%m"))) {
98                 strcpy(s1 + (s2 - s), strerror(errno));
99                 s2 += 2;
100                 strcpy(s1 + strlen(s1), s2);
101         }
102 #ifndef sun
103         /* Solaris doesn't have %h in syslog */
104         else if ((s2 = strstr(s, "%h"))) {
105                 strcpy(s1 + (s2 - s), hstrerror(h_errno));
106                 s2 += 2;
107                 strcpy(s1 + strlen(s1), s2);
108         }
109 #endif
110
111         s1[maxlen-1] = '\0';
112 #ifdef ISSERVER
113         syslog(LOG_ERR, "%s", s1);
114 #endif
115         fprintf(stderr, "Error: %s\n", s1);
116         exit(1);
117 }
118
119 void logging(void) {
120 #ifdef ISSERVER
121         openlog(MY_NAME, LOG_PID, LOG_DAEMON);
122 #endif
123         setvbuf(stdout, NULL, _IONBF, 0);
124         setvbuf(stderr, NULL, _IONBF, 0);
125 }
126
127 #ifdef WORDS_BIGENDIAN
128 u64 ntohll(u64 a) {
129         return a;
130 }
131 #else
132 u64 ntohll(u64 a) {
133         u32 lo = a & 0xffffffff;
134         u32 hi = a >> 32U;
135         lo = ntohl(lo);
136         hi = ntohl(hi);
137         return ((u64) lo) << 32U | hi;
138 }
139 #endif
140 #define htonll ntohll
141
142 /* Flags used between the client and server */
143 #define NBD_FLAG_HAS_FLAGS      (1 << 0)        /* Flags are there */
144 #define NBD_FLAG_READ_ONLY      (1 << 1)        /* Device is read-only */