| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name net_lowlevel.h - The network low level header file. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2007 by Lutz Sammer and Jimmy Salmon |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
|
| 28 |
#ifndef __NET_LOWLEVEL_H |
| 29 |
#define __NET_LOWLEVEL_H |
| 30 |
|
| 31 |
//@{ |
| 32 |
|
| 33 |
/*---------------------------------------------------------------------------- |
| 34 |
-- Includes |
| 35 |
----------------------------------------------------------------------------*/ |
| 36 |
|
| 37 |
#include <vector> |
| 38 |
|
| 39 |
#ifndef _MSC_VER |
| 40 |
#include <errno.h> |
| 41 |
#include <time.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
// Include system network headers |
| 45 |
#if defined(__WIN32__) || defined(WIN32) || defined(_WIN32) |
| 46 |
|
| 47 |
#define USE_WINSOCK |
| 48 |
|
| 49 |
#include <winsock2.h> |
| 50 |
|
| 51 |
#include <windows.h> |
| 52 |
#include <winsock.h> |
| 53 |
//#include <ws2tcpip.h> |
| 54 |
|
| 55 |
// MS Knowledge base fix for SIO_GET_INTERFACE_LIST with NT4.0 ++ |
| 56 |
#define SIO_GET_INTERFACE_LIST 0x4004747F |
| 57 |
#define IFF_UP 1 |
| 58 |
#define IFF_LOOPBACK 4 |
| 59 |
typedef struct _OLD_INTERFACE_INFO |
| 60 |
{ |
| 61 |
unsigned long iiFlags; /* Interface flags */ |
| 62 |
SOCKADDR iiAddress; /* Interface address */ |
| 63 |
SOCKADDR iiBroadcastAddress; /* Broadcast address */ |
| 64 |
SOCKADDR iiNetmask; /* Network mask */ |
| 65 |
} OLD_INTERFACE_INFO; |
| 66 |
#define INTERFACE_INFO OLD_INTERFACE_INFO |
| 67 |
|
| 68 |
#else // UNIX |
| 69 |
#include <sys/time.h> |
| 70 |
#include <unistd.h> |
| 71 |
#include <netinet/in.h> |
| 72 |
#include <netdb.h> |
| 73 |
#include <sys/socket.h> |
| 74 |
#include <sys/ioctl.h> |
| 75 |
#ifndef __BEOS__ |
| 76 |
#include <net/if.h> |
| 77 |
#include <arpa/inet.h> |
| 78 |
#endif |
| 79 |
#define INVALID_SOCKET -1 |
| 80 |
#endif // !WIN32 |
| 81 |
|
| 82 |
#ifndef INADDR_NONE |
| 83 |
#define INADDR_NONE -1 |
| 84 |
#endif |
| 85 |
|
| 86 |
/*---------------------------------------------------------------------------- |
| 87 |
-- Defines |
| 88 |
----------------------------------------------------------------------------*/ |
| 89 |
|
| 90 |
#define NIPQUAD(ad) \ |
| 91 |
(int)(((ad) >> 24) & 0xff), (int)(((ad) >> 16) & 0xff), \ |
| 92 |
(int)(((ad) >> 8) & 0xff), (int)((ad) & 0xff) |
| 93 |
|
| 94 |
#ifdef USE_WIN32 |
| 95 |
typedef SOCKET Socket; |
| 96 |
#else |
| 97 |
typedef int Socket; |
| 98 |
#endif |
| 99 |
|
| 100 |
/*---------------------------------------------------------------------------- |
| 101 |
-- Declarations |
| 102 |
----------------------------------------------------------------------------*/ |
| 103 |
|
| 104 |
struct SocketSet { |
| 105 |
SocketSet() : MaxSockFD(0) {} |
| 106 |
|
| 107 |
void AddSocket(Socket socket); |
| 108 |
void DelSocket(Socket socket); |
| 109 |
|
| 110 |
std::vector<Socket> Sockets; |
| 111 |
std::vector<int> SocketReady; |
| 112 |
Socket MaxSockFD; |
| 113 |
}; |
| 114 |
|
| 115 |
/*---------------------------------------------------------------------------- |
| 116 |
-- Variables |
| 117 |
----------------------------------------------------------------------------*/ |
| 118 |
|
| 119 |
extern int NetLastSocket; /// Last socket |
| 120 |
extern unsigned long NetLastHost; /// Last host number (net format) |
| 121 |
extern int NetLastPort; /// Last port number (net format) |
| 122 |
extern unsigned long NetLocalAddrs[]; /// Local IP-Addrs of this host (net format) |
| 123 |
|
| 124 |
/*---------------------------------------------------------------------------- |
| 125 |
-- Functions |
| 126 |
----------------------------------------------------------------------------*/ |
| 127 |
|
| 128 |
/// Hardware dependend network init. |
| 129 |
extern int NetInit(void); |
| 130 |
/// Hardware dependend network exit. |
| 131 |
extern void NetExit(void); |
| 132 |
/// Resolve host in name or or colon dot notation. |
| 133 |
extern unsigned long NetResolveHost(const std::string &host); |
| 134 |
/// Get local IP from network file descriptor |
| 135 |
extern int NetSocketAddr(const Socket sock); |
| 136 |
/// Open a UDP Socket port. |
| 137 |
extern Socket NetOpenUDP(int port); |
| 138 |
/// Open a TCP Socket port. |
| 139 |
extern Socket NetOpenTCP(int port); |
| 140 |
/// Close a UDP socket port. |
| 141 |
extern void NetCloseUDP(Socket sockfd); |
| 142 |
/// Close a TCP socket port. |
| 143 |
extern void NetCloseTCP(Socket sockfd); |
| 144 |
/// Set socket to non-blocking |
| 145 |
extern int NetSetNonBlocking(Socket sockfd); |
| 146 |
/// Open a TCP connection. |
| 147 |
extern int NetConnectTCP(Socket sockfd, unsigned long addr, int port); |
| 148 |
/// Send through a UPD socket to a host:port. |
| 149 |
extern int NetSendUDP(Socket sockfd, unsigned long host, int port, |
| 150 |
const void *buf, int len); |
| 151 |
/// Send through a TCP socket |
| 152 |
extern int NetSendTCP(Socket sockfd, const void *buf, int len); |
| 153 |
/// Wait for socket ready. |
| 154 |
extern int NetSocketReady(Socket sockfd, int timeout); |
| 155 |
/// Wait for socket set ready. |
| 156 |
extern int NetSocketSetReady(SocketSet *sockfd, int timeout); |
| 157 |
/// Check if a socket in a socket set is ready. |
| 158 |
extern int NetSocketSetSocketReady(SocketSet *set, Socket socket); |
| 159 |
/// Receive from a UDP socket. |
| 160 |
extern int NetRecvUDP(Socket sockfd, void *buf, int len); |
| 161 |
/// Receive from a TCP socket. |
| 162 |
extern int NetRecvTCP(Socket sockfd, void *buf, int len); |
| 163 |
/// Listen for connections on a TCP socket |
| 164 |
extern int NetListenTCP(Socket sockfd); |
| 165 |
/// Accept a connection on a TCP socket |
| 166 |
extern Socket NetAcceptTCP(Socket sockfd); |
| 167 |
|
| 168 |
/// Add a socket to a socket set |
| 169 |
extern void NetAddSocket(SocketSet *set, Socket socket); |
| 170 |
/// Delete a socket from a socket set |
| 171 |
extern void NetDelSocket(SocketSet *set, Socket socket); |
| 172 |
|
| 173 |
//@} |
| 174 |
|
| 175 |
#endif // !__NET_LOWLEVEL_H |