00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <errno.h>
00013 #include <string.h>
00014 #include <netdb.h>
00015 #include <netinet/tcp.h>
00016 #include <stdlib.h>
00017
00018 #if SIZEOF_UNSIGNED_SHORT_INT==4
00019 typedef unsigned short u32;
00020 #elif SIZEOF_UNSIGNED_INT==4
00021 typedef unsigned int u32;
00022 #elif SIZEOF_UNSIGNED_LONG_INT==4
00023 typedef unsigned long u32;
00024 #else
00025 #error I need at least some 32-bit type
00026 #endif
00027
00028 #if SIZEOF_UNSIGNED_INT==8
00029 typedef unsigned int u64;
00030 #elif SIZEOF_UNSIGNED_LONG_INT==8
00031 typedef unsigned long u64;
00032 #elif SIZEOF_UNSIGNED_LONG_LONG_INT==8
00033 typedef unsigned long long u64;
00034 #else
00035 #error I need at least some 64-bit type
00036 #endif
00037
00038 #ifdef NBD_H_LOCAL
00039 #include "nbd.h"
00040 #else
00041 #ifdef NBD_H_LINUX
00042 #include <linux/nbd.h>
00043 #endif // NBD_H_LINUX
00044 #endif // NBD_H_LOCAL
00045
00046 #if NBD_LFS==1
00047 #define _LARGEFILE_SOURCE
00048 #define _FILE_OFFSET_BITS 64
00049 #endif
00050
00051 u64 cliserv_magic = 0x00420281861253LL;
00052 #define INIT_PASSWD "NBDMAGIC"
00053
00054 #define INFO(a) do { } while(0)
00055
00056 void setmysockopt(int sock)
00057 {
00058 int size = 1;
00059 #if 0
00060 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
00061 INFO("(no sockopt/1: %m)");
00062 #endif
00063 #ifdef IPPROTO_TCP
00064 size = 1;
00065 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
00066 INFO("(no sockopt/2: %m)");
00067 #endif
00068 #if 0
00069 size = 1024;
00070 if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
00071 INFO("(no sockopt/3: %m)");
00072 #endif
00073 }
00074
00075 void err(const char *s)
00076 {
00077 const int maxlen = 150;
00078 char s1[maxlen], *s2;
00079
00080 strncpy(s1, s, maxlen);
00081 if ((s2 = strstr(s, "%m"))) {
00082 strcpy(s1 + (s2 - s), strerror(errno));
00083 s2 += 2;
00084 strcpy(s1 + strlen(s1), s2);
00085 }
00086 #ifndef sun
00087
00088 else if ((s2 = strstr(s, "%h"))) {
00089 strcpy(s1 + (s2 - s), hstrerror(h_errno));
00090 s2 += 2;
00091 strcpy(s1 + strlen(s1), s2);
00092 }
00093 #endif
00094
00095 s1[maxlen-1] = '\0';
00096 #ifdef ISSERVER
00097 syslog(LOG_ERR, "%s", s1);
00098 #else
00099 fprintf(stderr, "Error: %s\n", s1);
00100 #endif
00101 exit(1);
00102 }
00103
00104 void logging(void)
00105 {
00106 #ifdef ISSERVER
00107 openlog(MY_NAME, LOG_PID, LOG_DAEMON);
00108 #endif
00109 setvbuf(stdout, NULL, _IONBF, 0);
00110 setvbuf(stderr, NULL, _IONBF, 0);
00111 }
00112
00113 #ifdef WORDS_BIGENDIAN
00114 u64 ntohll(u64 a)
00115 {
00116 return a;
00117 }
00118 #else
00119 u64 ntohll(u64 a)
00120 {
00121 u32 lo = a & 0xffffffff;
00122 u32 hi = a >> 32U;
00123 lo = ntohl(lo);
00124 hi = ntohl(hi);
00125 return ((u64) lo) << 32U | hi;
00126 }
00127 #endif
00128 #define htonll ntohll