00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef __NET_LOWLEVEL_H
00030 #define __NET_LOWLEVEL_H
00031
00033
00034
00035
00036
00037
00038 #include <vector>
00039
00040 #ifndef _MSC_VER
00041 #include <errno.h>
00042 #include <time.h>
00043 #endif
00044
00045
00046 #if defined(__WIN32__) || defined(WIN32) || defined(_WIN32)
00047
00048 #define USE_WINSOCK
00049
00050 #include <winsock2.h>
00051
00052 #include <windows.h>
00053 #include <winsock.h>
00054
00055
00056
00057 #define SIO_GET_INTERFACE_LIST 0x4004747F
00058 #define IFF_UP 1
00059 #define IFF_LOOPBACK 4
00060 typedef struct _OLD_INTERFACE_INFO {
00061 unsigned long iiFlags;
00062 SOCKADDR iiAddress;
00063 SOCKADDR iiBroadcastAddress;
00064 SOCKADDR iiNetmask;
00065 } OLD_INTERFACE_INFO;
00066 #define INTERFACE_INFO OLD_INTERFACE_INFO
00067
00068 #else // UNIX
00069 #include <sys/time.h>
00070 #include <unistd.h>
00071 #include <netinet/in.h>
00072 #include <netdb.h>
00073 #include <sys/socket.h>
00074 #include <sys/ioctl.h>
00075 #ifndef __BEOS__
00076 #include <net/if.h>
00077 #include <arpa/inet.h>
00078 #endif
00079 #define INVALID_SOCKET -1
00080 #endif // !WIN32
00081
00082 #ifndef INADDR_NONE
00083 #define INADDR_NONE -1
00084 #endif
00085
00086
00087
00088
00089
00090 #define NIPQUAD(ad) \
00091 (int)(((ad) >> 24) & 0xff), (int)(((ad) >> 16) & 0xff), \
00092 (int)(((ad) >> 8) & 0xff), (int)((ad) & 0xff)
00093
00094 #ifdef USE_WINSOCK
00095 typedef SOCKET Socket;
00096 #else
00097 typedef int Socket;
00098 #endif
00099
00100
00101
00102
00103
00104 struct SocketSet {
00105 SocketSet() : MaxSockFD(0) {}
00106
00107 void AddSocket(Socket socket);
00108 void DelSocket(Socket socket);
00109
00110 std::vector<Socket> Sockets;
00111 std::vector<int> SocketReady;
00112 Socket MaxSockFD;
00113 };
00114
00115
00116
00117
00118
00119 extern int NetLastSocket;
00120 extern unsigned long NetLastHost;
00121 extern int NetLastPort;
00122 extern unsigned long NetLocalAddrs[];
00123
00124
00125
00126
00127
00129 extern int NetInit();
00131 extern void NetExit();
00133 extern unsigned long NetResolveHost(const std::string &host);
00135 extern int NetSocketAddr(const Socket sock);
00137 extern Socket NetOpenUDP(const char *addr, int port);
00139 extern Socket NetOpenTCP(const char *addr, int port);
00141 extern void NetCloseUDP(Socket sockfd);
00143 extern void NetCloseTCP(Socket sockfd);
00145 extern int NetSetNonBlocking(Socket sockfd);
00147 extern int NetConnectTCP(Socket sockfd, unsigned long addr, int port);
00149 extern int NetSendUDP(Socket sockfd, unsigned long host, int port,
00150 const void *buf, int len);
00152 extern int NetSendTCP(Socket sockfd, const void *buf, int len);
00154 extern int NetSocketReady(Socket sockfd, int timeout);
00156 extern int NetSocketSetReady(SocketSet *sockfd, int timeout);
00158 extern int NetSocketSetSocketReady(SocketSet *set, Socket socket);
00160 extern int NetRecvUDP(Socket sockfd, void *buf, int len);
00162 extern int NetRecvTCP(Socket sockfd, void *buf, int len);
00164 extern int NetListenTCP(Socket sockfd);
00166 extern Socket NetAcceptTCP(Socket sockfd);
00167
00169 extern void NetAddSocket(SocketSet *set, Socket socket);
00171 extern void NetDelSocket(SocketSet *set, Socket socket);
00172
00174
00175 #endif // !__NET_LOWLEVEL_H