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