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