00001 #ifndef __SOCKET_PORTABLE_H__
00002 #define __SOCKET_PORTABLE_H__
00003
00004
00005
00006
00007
00008 const int ALL_OK = 0;
00009 const int BASIC_ERROR = -1;
00010
00011
00012
00013
00014 #if defined(HP_SOCK)
00015
00016 #ifndef _INCLUDE_HPUX_SOURCE
00017 #define _INCLUDE_HPUX_SOURCE
00018 #define _INCLUDE_POSIX_SOURCE
00019 #define _INCLUDE_XOPEN_SOURCE
00020 #endif
00021 #include <sys/socket.h>
00022 #include <sys/time.h>
00023 #include <netinet/in.h>
00024 #include <netdb.h>
00025 #include <errno.h>
00026
00027 #define socket_read read
00028 #define socket_write write
00029 #define socket_close close
00030
00031 #define DO_CONNECT(a,b,c) connect(a,b,c)
00032 #define DO_SOCKET_READ(a,b,c,d) socket_read(a,b,c)
00033 #define DO_SOCKET_WRITE(a,b,c,d) socket_write(a,b,c)
00034
00035 #define GETERROR() errno
00036
00037
00038 #define SOCKIOCTL ioctl
00039 typedef unsigned long SOCKET;
00040 #define BAD_SOCKET 0xffffffff
00041
00042
00043
00044
00045
00046 #elif defined(WIN32) || defined(WIN32_VC) || defined(WIN64_VC)
00047 #include <winsock2.h>
00048 #include <Ws2tcpip.h>
00049
00050
00051 inline int DO_SELECT(SOCKET n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,struct timeval *timeout)
00052 {
00053 return select((int) n, readfds, writefds, exceptfds,timeout);
00054 }
00055
00056 inline int DO_CONNECT( const SOCKET a, const struct sockaddr_in *b)
00057 {
00058 return connect(a, reinterpret_cast<const struct ::sockaddr *>(b), sizeof(sockaddr));
00059 }
00060 inline int DO_SOCKET_READ(const SOCKET a, char * buf, const int size)
00061 {
00062 return recv(a, buf, size, 0);
00063 }
00064 inline int DO_SOCKET_WRITE(const SOCKET a, const char * buff, const int len)
00065 {
00066 return send(a, buff, len, 0);
00067 }
00068 inline int DO_SOCKET_WRITE_TO(const SOCKET a, const char * buffer, const int buf_len, const sockaddr_in * addr)
00069 {
00070 return sendto(a, buffer, buf_len, 0, reinterpret_cast<const struct ::sockaddr *>(addr), sizeof(sockaddr));
00071 }
00072 inline SOCKET DO_NEWUDP()
00073 {
00074 return socket(AF_INET, SOCK_DGRAM, 0);
00075 }
00076 inline SOCKET DO_NEWTCP()
00077 {
00078 return socket(AF_INET, SOCK_STREAM, 0);
00079 }
00080 inline int DO_BIND(const SOCKET a, const sockaddr_in *b)
00081 {
00082 return bind(a, reinterpret_cast<const struct ::sockaddr *>(b), sizeof(sockaddr));
00083 }
00084 inline int DO_CLOSE(const SOCKET a)
00085 {
00086 return closesocket(a);
00087 }
00088 inline SOCKET DO_ACCEPT(SOCKET sck, sockaddr_in * adr)
00089 {
00090 int adrlen = sizeof(sockaddr);
00091 return accept(sck, reinterpret_cast<sockaddr *>(adr), &adrlen);
00092 };
00093 inline int DO_RECV_FROM(SOCKET sck, char * data, int len, sockaddr_in * addr)
00094 {
00095 int plen = sizeof(sockaddr);
00096 return recvfrom(sck, data, len, 0, reinterpret_cast<sockaddr *>(addr), &plen);
00097 }
00098 inline int DO_LISTEN(const SOCKET a, const int size)
00099 {
00100 return listen(a, size);
00101 }
00102
00103 inline int GETERROR()
00104 {
00105 return WSAGetLastError();
00106 }
00107
00108 inline int SOCKIOCTL(const SOCKET s, const long flags, unsigned long * val)
00109 {
00110 return ioctlsocket(s, flags, val);
00111 }
00112
00113 inline int init_network()
00114 {
00115 static struct WSAData mydata;
00116 int answer = WSAStartup(0x0101, &mydata);
00117 if (answer != 0)
00118 return BASIC_ERROR;
00119
00120 return ALL_OK;
00121 }
00122
00123 inline bool do_shutdown_send(SOCKET s)
00124 {
00125 return (shutdown(s,SD_SEND) == 0);
00126 };
00127
00128 typedef int socklen_t ;
00129 const long LOCAL_NONBLOCK = 1;
00130 const long LOCAL_FL_SET = FIONBIO ;
00131 const int LOCAL_BLOCKING_ERROR = WSAEWOULDBLOCK;
00132 const int LOCAL_CONNECT_BLOCKING = WSAEWOULDBLOCK;
00133 const int LOCAL_NOTCONNECTED_ERROR = WSAENOTCONN;
00134 const int LOCAL_TIMEOUT_ERROR = WSAETIMEDOUT;
00135 const SOCKET BAD_SOCKET = (SOCKET)-1;
00136
00137
00138
00139
00140 #elif defined(SunOS) || defined(SUNNEW) || defined(IRIX64)
00141
00142 #include <sys/types.h>
00143 #include <sys/time.h>
00144 #include <sys/socket.h>
00145 #include <sys/ioctl.h>
00146 #include <sys/filio.h>
00147 #include <netinet/in.h>
00148 #include <netdb.h>
00149 #include <errno.h>
00150 #include <fcntl.h>
00151 #include <signal.h>
00152 #include <unistd.h>
00153 #include <string.h>
00154 #include <netinet/in_systm.h>
00155 #include <netinet/ip.h>
00156 #include <netinet/in.h>
00157 #include <arpa/inet.h>
00158
00159 typedef int SOCKET;
00160 const SOCKET BAD_SOCKET = 0xffffffff;
00161
00162
00163
00164
00165
00166 inline int DO_CONNECT(const SOCKET a, const sockaddr_in *b)
00167 {
00168 return connect(a, reinterpret_cast<const struct ::sockaddr *>(b), sizeof(sockaddr));
00169 }
00170 inline int DO_SOCKET_READ(const SOCKET a, char * buf, const int size)
00171 {
00172 return recv(a, buf, size, 0);
00173 }
00174 inline int DO_SOCKET_WRITE(const SOCKET a, const char * buff, const int len)
00175 {
00176 return send(a, buff, len, 0);
00177 }
00178
00179
00180
00181
00182
00183
00184 inline int DO_SOCKET_WRITE_TO(const SOCKET a, const char * buffer, const int buf_len, const sockaddr_in * addr)
00185 {
00186 return sendto(a, buffer, buf_len, 0, reinterpret_cast<const struct ::sockaddr *>(addr), sizeof(sockaddr));
00187 }
00188 inline SOCKET DO_NEWUDP()
00189 {
00190 return socket(AF_INET, SOCK_DGRAM, 0);
00191 }
00192 inline SOCKET DO_NEWTCP()
00193 {
00194 return socket(AF_INET, SOCK_STREAM, 0);
00195 }
00196 inline int DO_BIND(const SOCKET a, const sockaddr_in *b)
00197 {
00198 return bind(a, reinterpret_cast<const struct ::sockaddr *>(b), sizeof(sockaddr));
00199 }
00200 inline int DO_CLOSE(const SOCKET a)
00201 {
00202 return close(a);
00203 }
00204 inline int DO_ACCEPT(SOCKET sck, sockaddr_in * adr)
00205 {
00206 int adrlen = sizeof(sockaddr);
00207 return accept(sck, ( sockaddr *)adr, &adrlen);
00208 };
00209
00210 inline int DO_RECV_FROM(SOCKET sck, char * data, int len, sockaddr_in * addr)
00211 {
00212 int plen = sizeof(sockaddr);
00213 return recvfrom(sck, data, len, 0, (sockaddr *)addr, &plen);
00214 }
00215 inline int DO_LISTEN(const SOCKET a, const int size)
00216 {
00217 return listen(a, size);
00218 }
00219
00220 inline int GETERROR()
00221 {
00222 return errno;
00223 }
00224
00225 inline int SOCKIOCTL(const SOCKET s, const long flags, void * val)
00226 {
00227 return ioctl(s, flags, val);
00228 }
00229
00230 inline int init_network()
00231 {
00232 return ALL_OK;
00233 }
00234 #ifndef INADDR_NONE
00235 const INADDR_NONE = -1;
00236 #endif
00237
00238 const long LOCAL_NONBLOCK = 1;
00239 const long LOCAL_FL_SET = FIONBIO ;
00240 const int LOCAL_BLOCKING_ERROR = EAGAIN;
00241 const int LOCAL_CONNECT_BLOCKING = EINPROGRESS;
00242
00243
00244
00245
00246
00247 #elif defined(IS_LINUX) || defined(IS_OSX) || defined(IS_FREEBSD)
00248
00249 #include <sys/types.h>
00250 #include <sys/time.h>
00251 #include <sys/socket.h>
00252 #include <sys/ioctl.h>
00253 #include <netinet/in.h>
00254 #include <netdb.h>
00255 #include <errno.h>
00256 #include <fcntl.h>
00257 #include <signal.h>
00258 #include <unistd.h>
00259 #include <stdio.h>
00260
00261 #include <netinet/tcp.h>
00262
00263 #include <netinet/in.h>
00264 #include <arpa/inet.h>
00265 #include <unistd.h>
00266
00267 typedef struct sockaddr_in AddressType;
00268
00269 typedef int SOCKET;
00270 const SOCKET BAD_SOCKET = -1;
00271 inline int DO_SELECT(SOCKET n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,struct timeval *timeout)
00272 {
00273 return select((int) n, readfds, writefds, exceptfds,timeout);
00274 }
00275
00276 inline int DO_CONNECT(const SOCKET a, const sockaddr_in *b)
00277 {
00278 return connect(a, reinterpret_cast<const struct ::sockaddr *>(b), sizeof(sockaddr));
00279 }
00280 inline int DO_SOCKET_READ(const SOCKET a, char * buf, const int size)
00281 {
00282 return recv(a, buf, size, 0);
00283 }
00284 inline int DO_SOCKET_WRITE(const SOCKET a, const char * buff, const int len)
00285 {
00286 return send(a, buff, len, 0);
00287 }
00288
00289 inline int DO_SOCKET_WRITE_TO(const SOCKET a, const char * buffer, const int buf_len, const sockaddr_in * addr)
00290 {
00291 return sendto(a, buffer, buf_len, 0, reinterpret_cast<const struct ::sockaddr *>(addr), sizeof(sockaddr));
00292 }
00293 inline SOCKET DO_NEWUDP()
00294 {
00295 return socket(AF_INET, SOCK_DGRAM, 0);
00296 }
00297 inline SOCKET DO_NEWTCP()
00298 {
00299 return socket(AF_INET, SOCK_STREAM, 0);
00300 }
00301 inline int DO_BIND(const SOCKET a, const sockaddr_in *b)
00302 {
00303 return bind(a, reinterpret_cast<const struct ::sockaddr *>(b), sizeof(sockaddr));
00304 }
00305 inline int DO_CLOSE(const SOCKET a)
00306 {
00307 return close(a);
00308 }
00309
00310 inline int DO_ACCEPT(SOCKET sck, sockaddr_in * adr)
00311 {
00312 unsigned int adrlen = sizeof(sockaddr);
00313 return accept(sck, ( sockaddr *)adr, &adrlen);
00314 };
00315
00316 inline int DO_RECV_FROM(SOCKET sck, char * data, int len, sockaddr_in * addr)
00317 {
00318 unsigned int plen = sizeof(sockaddr);
00319 return recvfrom(sck, data, len, 0, (sockaddr *)addr, &plen);
00320 }
00321
00322
00323 inline int init_network()
00324 {
00325 signal(SIGPIPE, SIG_IGN);
00326 return ALL_OK;
00327 }
00328
00329 inline int DO_LISTEN(const SOCKET a, const int size)
00330 {
00331 return listen(a, size);
00332 }
00333
00334 inline int GETERROR()
00335 {
00336 return errno;
00337 }
00338
00339 inline int SOCKIOCTL(const SOCKET s, const long flags, void * val)
00340 {
00341 return ioctl(s, flags, val);
00342 }
00343
00344 inline bool do_shutdown_send(SOCKET s)
00345 {
00346 return (shutdown(s,SHUT_WR) == 0);
00347 };
00348
00349
00350 #define BSDBLOCK
00351
00352
00353 const long LOCAL_NONBLOCK = 1;
00354
00355
00356 const int LOCAL_BLOCKING_ERROR = EAGAIN;
00357 const int LOCAL_CONNECT_BLOCKING = EINPROGRESS;
00358
00359 #else
00360
00361
00362
00363 No Host Type defined !!
00364 #error Fatal
00365 #endif
00366
00367
00368
00369 #endif //__SOCKET_PORTABLE_H__