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 #ifndef NBD_H_LINUX
00039
00040 #define __be32 u32
00041 #define __be64 u64
00042 #endif
00043
00044 #ifdef NBD_H_LOCAL
00045 #include "nbd.h"
00046 #else
00047 #ifdef NBD_H_LINUX
00048 #include <linux/types.h>
00049 #include <linux/nbd.h>
00050 #endif // NBD_H_LINUX
00051 #endif // NBD_H_LOCAL
00052
00053 #if NBD_LFS==1
00054 #define _LARGEFILE_SOURCE
00055 #define _FILE_OFFSET_BITS 64
00056 #endif
00057
00058 u64 cliserv_magic = 0x00420281861253LL;
00059 #define INIT_PASSWD "NBDMAGIC"
00060
00061 #define INFO(a) do { } while(0)
00062
00063 void setmysockopt(int sock)
00064 {
00065 int size = 1;
00066 #if 0
00067 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(int)) < 0)
00068 INFO("(no sockopt/1: %m)");
00069 #endif
00070 #ifdef IPPROTO_TCP
00071 size = 1;
00072 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &size, sizeof(int)) < 0)
00073 INFO("(no sockopt/2: %m)");
00074 #endif
00075 #if 0
00076 size = 1024;
00077 if (setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, &size, sizeof(int)) < 0)
00078 INFO("(no sockopt/3: %m)");
00079 #endif
00080 }
00081
00082 void err(const char *s)
00083 {
00084 const int maxlen = 150;
00085 char s1[maxlen], *s2;
00086
00087 strncpy(s1, s, maxlen);
00088 if ((s2 = strstr(s, "%m"))) {
00089 strcpy(s1 + (s2 - s), strerror(errno));
00090 s2 += 2;
00091 strcpy(s1 + strlen(s1), s2);
00092 }
00093 #ifndef sun
00094
00095 else if ((s2 = strstr(s, "%h"))) {
00096 strcpy(s1 + (s2 - s), hstrerror(h_errno));
00097 s2 += 2;
00098 strcpy(s1 + strlen(s1), s2);
00099 }
00100 #endif
00101
00102 s1[maxlen-1] = '\0';
00103 #ifdef ISSERVER
00104 syslog(LOG_ERR, "%s", s1);
00105 #else
00106 fprintf(stderr, "Error: %s\n", s1);
00107 #endif
00108 exit(1);
00109 }
00110
00111 void logging(void)
00112 {
00113 #ifdef ISSERVER
00114 openlog(MY_NAME, LOG_PID, LOG_DAEMON);
00115 #endif
00116 setvbuf(stdout, NULL, _IONBF, 0);
00117 setvbuf(stderr, NULL, _IONBF, 0);
00118 }
00119
00120 #ifdef WORDS_BIGENDIAN
00121 u64 ntohll(u64 a)
00122 {
00123 return a;
00124 }
00125 #else
00126 u64 ntohll(u64 a)
00127 {
00128 u32 lo = a & 0xffffffff;
00129 u32 hi = a >> 32U;
00130 lo = ntohl(lo);
00131 hi = ntohl(hi);
00132 return ((u64) lo) << 32U | hi;
00133 }
00134 #endif
00135 #define htonll ntohll