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