UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
net.cpp File Reference
#include "common.h"
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <SDL_thread.h>
#include "../shared/scopedmutex.h"
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>

Go to the source code of this file.

Data Structures

struct  net_stream
 
struct  datagram
 
struct  datagram_socket
 

Macros

#define MAX_STREAMS   56
 
#define MAX_DATAGRAM_SOCKETS   7
 
#define INVALID_SOCKET   (-1)
 
#define netError   errno
 
#define netStringError   strerror
 
#define netCloseSocket   close
 
#define ioctlsocket   ioctl
 
#define AI_NUMERICSERV   0
 
#define AI_ADDRCONFIG   0
 
#define NET_MULTICAST_IP6   "ff04::696f:7175:616b:6533"
 use an admin local address per default so that network admins can decide on how to handle traffic. More...
 
#define dbuffer_len(dbuf)   (dbuf ? (dbuf)->length() : 0)
 

Typedefs

typedef int SOCKET
 

Functions

static int NET_StreamGetLength (struct net_stream *s)
 
static int NET_StreamGetFree (void)
 
static int NET_DatagramFindFreeSocket (void)
 
static struct net_streamNET_StreamNew (int index)
 
static void NET_ShowStreams_f (void)
 
void NET_Init (void)
 
void NET_Shutdown (void)
 
static void NET_StreamClose (struct net_stream *s)
 
static void do_accept (SOCKET sock)
 
void NET_Wait (int timeout)
 
static bool NET_SocketSetNonBlocking (SOCKET socketNum)
 
static struct net_streamNET_DoConnect (const char *node, const char *service, const struct addrinfo *addr, int i, stream_onclose_func *onclose)
 
struct net_streamNET_Connect (const char *node, const char *service, stream_onclose_func *onclose)
 Try to connect to a given host on a given port. More...
 
struct net_streamNET_ConnectToLoopBack (stream_onclose_func *onclose)
 
void NET_StreamEnqueue (struct net_stream *s, const char *data, int len)
 Enqueue a network message into a stream. More...
 
static int NET_StreamPeek (struct net_stream *s, char *data, int len)
 Returns the length of the waiting inbound buffer. More...
 
int NET_StreamDequeue (struct net_stream *s, char *data, int len)
 
dbufferNET_ReadMsg (struct net_stream *s)
 Reads messages from the network channel and adds them to the dbuffer where you can use the NET_Read* functions to get the values in the correct order. More...
 
voidNET_StreamGetData (struct net_stream *s)
 
void NET_StreamSetData (struct net_stream *s, void *data)
 
void NET_StreamFree (struct net_stream *s)
 Call NET_StreamFree to dump the whole thing right now. More...
 
void NET_StreamFinished (struct net_stream *s)
 Call NET_StreamFinished to mark the stream as uninteresting, but to finish sending any data in the buffer. The stream will appear closed after this call, and at some unspecified point in the future s will become an invalid pointer, so it should not be further referenced. More...
 
const char * NET_StreamToString (struct net_stream *s)
 Returns the numerical representation of a net_stream. More...
 
const char * NET_StreamPeerToName (struct net_stream *s, char *dst, int len, bool appendPort)
 
void NET_StreamSetCallback (struct net_stream *s, stream_callback_func *func)
 
bool NET_StreamIsLoopback (struct net_stream *s)
 
static int NET_DoStartServer (const struct addrinfo *addr)
 
static struct addrinfo * NET_GetAddrinfoForNode (const char *node, const char *service)
 
bool SV_Start (const char *node, const char *service, stream_callback_func *func)
 
void SV_Stop (void)
 
static struct datagram_socketNET_DatagramSocketDoNew (const struct addrinfo *addr)
 
struct datagram_socketNET_DatagramSocketNew (const char *node, const char *service, datagram_callback_func *func)
 Opens a datagram socket (UDP) More...
 
void NET_DatagramSend (struct datagram_socket *s, const char *buf, int len, struct sockaddr *to)
 
void NET_DatagramBroadcast (struct datagram_socket *s, const char *buf, int len, int port)
 
void NET_DatagramSocketClose (struct datagram_socket *s)
 
void NET_SockaddrToStrings (struct datagram_socket *s, struct sockaddr *addr, char *node, size_t nodelen, char *service, size_t servicelen)
 Convert sockaddr to string. More...
 
static void NET_AddrinfoToString (const struct addrinfo *addr, char *buf, size_t bufLength)
 
bool NET_ResolvNode (const char *node, char *buf, size_t bufLength)
 

Variables

static cvar_tnet_ipv4
 
static SDL_mutex * netMutex
 
static fd_set read_fds
 
static fd_set write_fds
 
static SOCKET maxfd
 
static struct net_streamstreams [MAX_STREAMS]
 
static struct datagram_socketdatagram_sockets [MAX_DATAGRAM_SOCKETS]
 
static bool loopback_ready = false
 
static bool server_running = false
 
static stream_callback_funcserver_func = nullptr
 
static SOCKET server_socket = INVALID_SOCKET
 
static int server_family
 
static int server_addrlen
 

Detailed Description

Note
This file should fully support ipv6 and any other protocol that is compatible with the getaddrinfo interface, with the exception of NET_DatagramBroadcast() which must be amended for each protocol (and currently supports only ipv4)

Definition in file net.cpp.

Macro Definition Documentation

#define AI_ADDRCONFIG   0

Definition at line 95 of file net.cpp.

Referenced by NET_Connect(), NET_DatagramSocketNew(), and NET_GetAddrinfoForNode().

#define AI_NUMERICSERV   0
Todo:
Move this into the configure script AI_ADDRCONFIG, AI_ALL, and AI_V4MAPPED are available since glibc 2.3.3. AI_NUMERICSERV is available since glibc 2.3.4.

Definition at line 92 of file net.cpp.

Referenced by NET_Connect(), and NET_DatagramSocketNew().

#define dbuffer_len (   dbuf)    (dbuf ? (dbuf)->length() : 0)
#define ioctlsocket   ioctl

Definition at line 74 of file net.cpp.

Referenced by NET_SocketSetNonBlocking().

#define MAX_DATAGRAM_SOCKETS   7

Definition at line 43 of file net.cpp.

Referenced by NET_DatagramFindFreeSocket(), NET_Init(), and NET_Wait().

#define MAX_STREAMS   56

Definition at line 42 of file net.cpp.

Referenced by NET_Init(), NET_ShowStreams_f(), NET_StreamGetFree(), and NET_Wait().

#define NET_MULTICAST_IP6   "ff04::696f:7175:616b:6533"

use an admin local address per default so that network admins can decide on how to handle traffic.

Definition at line 102 of file net.cpp.

#define netCloseSocket   close
#define netError   errno

Definition at line 71 of file net.cpp.

Referenced by NET_DatagramSocketDoNew(), NET_DoConnect(), NET_DoStartServer(), and NET_Wait().

#define netStringError   strerror

Definition at line 72 of file net.cpp.

Referenced by NET_DatagramSocketDoNew(), NET_DoConnect(), NET_DoStartServer(), and NET_Wait().

Typedef Documentation

typedef int SOCKET

Definition at line 69 of file net.cpp.

Function Documentation

static void NET_AddrinfoToString ( const struct addrinfo *  addr,
char *  buf,
size_t  bufLength 
)
static

Definition at line 1223 of file net.cpp.

References Q_strncpyz().

Referenced by NET_ResolvNode().

struct net_stream* NET_Connect ( const char *  node,
const char *  service,
stream_onclose_func onclose 
)

Try to connect to a given host on a given port.

Parameters
[in]nodeThe host to connect to
[in]serviceThe port to connect to
[in]oncloseThe callback that is called on closing the returned stream. This is useful if you hold the pointer for the returned stream anywhere else and would like to get notified once this pointer is invalid.
See also
NET_DoConnect
NET_ConnectToLoopBack
Todo:
What about a timeout

Definition at line 644 of file net.cpp.

References AI_ADDRCONFIG, AI_NUMERICSERV, Com_Printf(), index, cvar_s::integer, NET_DoConnect(), NET_StreamGetFree(), and OBJZERO.

Referenced by CL_Connect(), GAME_GetImportData(), and Irc_Net_Connect().

struct net_stream* NET_ConnectToLoopBack ( stream_onclose_func onclose)
Parameters
[in]oncloseThe callback that is called on closing the returned stream. This is useful if you hold the pointer for the returned stream anywhere else and would like to get notified once this pointer is invalid.
See also
NET_Connect

Definition at line 681 of file net.cpp.

References Com_Printf(), net_stream::func, net_stream::inbound, net_stream::loopback, net_stream::loopback_peer, NET_StreamGetFree(), NET_StreamNew(), net_stream::onclose, net_stream::outbound, server_func, and server_running.

Referenced by CL_Connect().

void NET_DatagramBroadcast ( struct datagram_socket s,
const char *  buf,
int  len,
int  port 
)
See also
NET_DatagramSend
NET_DatagramSocketNew
Todo:
This is only sending on the first available device, what if we have several devices?

Definition at line 1159 of file net.cpp.

References Com_Error(), ERR_DROP, datagram_socket::family, and NET_DatagramSend().

Referenced by GAME_GetImportData().

static int NET_DatagramFindFreeSocket ( void  )
static
See also
NET_StreamNew

Definition at line 243 of file net.cpp.

References Com_DPrintf(), DEBUG_SERVER, i, and MAX_DATAGRAM_SOCKETS.

Referenced by NET_DatagramSocketDoNew().

struct datagram_socket* NET_DatagramSocketNew ( const char *  node,
const char *  service,
datagram_callback_func func 
)

Opens a datagram socket (UDP)

See also
NET_DatagramSocketDoNew
Parameters
[in]nodeThe numeric address to resolv (might be nullptr)
[in]serviceThe port number
[in]funcCallback function for data handling

Definition at line 1102 of file net.cpp.

References AI_ADDRCONFIG, AI_NUMERICSERV, Com_Printf(), datagram_socket::func, cvar_s::integer, NET_DatagramSocketDoNew(), and OBJZERO.

Referenced by GAME_GetImportData(), and SV_InitGame().

static struct net_stream* NET_DoConnect ( const char *  node,
const char *  service,
const struct addrinfo *  addr,
int  i,
stream_onclose_func onclose 
)
static
static int NET_DoStartServer ( const struct addrinfo *  addr)
static
static struct addrinfo* NET_GetAddrinfoForNode ( const char *  node,
const char *  service 
)
static

Definition at line 960 of file net.cpp.

References AI_ADDRCONFIG, Com_Printf(), cvar_s::integer, and OBJZERO.

Referenced by NET_ResolvNode(), and SV_Start().

dbuffer* NET_ReadMsg ( struct net_stream s)

Reads messages from the network channel and adds them to the dbuffer where you can use the NET_Read* functions to get the values in the correct order.

See also
NET_StreamDequeue

Definition at line 774 of file net.cpp.

References dbuffer::add(), len, LittleLong, NET_StreamDequeue(), NET_StreamGetLength(), NET_StreamPeek(), netMutex, and v.

Referenced by CL_ReadPackets(), GAME_GetImportData(), and SV_ReadPacket().

bool NET_ResolvNode ( const char *  node,
char *  buf,
size_t  bufLength 
)

Definition at line 1229 of file net.cpp.

References NET_AddrinfoToString(), and NET_GetAddrinfoForNode().

Referenced by HTTP_ResolvURL(), and TEST_F().

void NET_SockaddrToStrings ( struct datagram_socket s,
struct sockaddr *  addr,
char *  node,
size_t  nodelen,
char *  service,
size_t  servicelen 
)

Convert sockaddr to string.

Parameters
[in]sThe datagram socket type to get the addrlen from
[in]addrThe socket address to convert into a string
[out]nodeThe target node name buffer
[in]nodelenThe length of the node name buffer
[out]serviceThe target service name buffer
[in]servicelenThe length of the service name buffer

Definition at line 1212 of file net.cpp.

References datagram_socket::addrlen, Com_Printf(), and Q_strncpyz().

Referenced by GAME_GetImportData().

static bool NET_SocketSetNonBlocking ( SOCKET  socketNum)
static

Definition at line 583 of file net.cpp.

References Com_Printf(), and ioctlsocket.

Referenced by NET_DatagramSocketDoNew(), NET_DoConnect(), and NET_DoStartServer().

int NET_StreamDequeue ( struct net_stream s,
char *  data,
int  len 
)
See also
NET_StreamEnqueue

Definition at line 760 of file net.cpp.

References dbuffer::extract(), net_stream::finished, and net_stream::inbound.

Referenced by Irc_Proto_PollServerMsg(), and NET_ReadMsg().

void NET_StreamEnqueue ( struct net_stream s,
const char *  data,
int  len 
)
void NET_StreamFinished ( struct net_stream s)

Call NET_StreamFinished to mark the stream as uninteresting, but to finish sending any data in the buffer. The stream will appear closed after this call, and at some unspecified point in the future s will become an invalid pointer, so it should not be further referenced.

Definition at line 832 of file net.cpp.

References dbuffer_len, net_stream::finished, net_stream::inbound, INVALID_SOCKET, net_stream::loopback_peer, NET_StreamClose(), netMutex, net_stream::outbound, read_fds, and net_stream::socket.

Referenced by CL_Disconnect(), SV_DropClient(), and SV_FinalMessage().

void NET_StreamFree ( struct net_stream s)

Call NET_StreamFree to dump the whole thing right now.

See also
NET_StreamClose
NET_StreamFinished

Definition at line 817 of file net.cpp.

References net_stream::finished, and NET_StreamClose().

Referenced by GAME_GetImportData(), Irc_Net_Connect(), Irc_Net_Disconnect(), NET_StreamNew(), and SV_ReadPacket().

void* NET_StreamGetData ( struct net_stream s)

Definition at line 800 of file net.cpp.

References net_stream::data.

Referenced by GAME_GetImportData(), and SV_ReadPacket().

static int NET_StreamGetFree ( void  )
static
static int NET_StreamGetLength ( struct net_stream s)
inlinestatic

Definition at line 214 of file net.cpp.

References dbuffer_len, and net_stream::inbound.

Referenced by NET_ReadMsg(), and NET_Wait().

bool NET_StreamIsLoopback ( struct net_stream s)

Definition at line 910 of file net.cpp.

References net_stream::loopback.

Referenced by CL_ConnectionlessPacket(), and NET_StreamPeerToName().

static int NET_StreamPeek ( struct net_stream s,
char *  data,
int  len 
)
static

Returns the length of the waiting inbound buffer.

Definition at line 745 of file net.cpp.

References net_stream::closed, dbuffer_len, net_stream::finished, dbuffer::get(), and net_stream::inbound.

Referenced by NET_ReadMsg().

const char* NET_StreamPeerToName ( struct net_stream s,
char *  dst,
int  len,
bool  appendPort 
)
Parameters
[in]sThe network stream to get the name for
[out]dstThe target buffer to store the ip and port in
[in]lenThe length of the target buffer
[in]appendPortAlso append the port number to the target buffer

Definition at line 872 of file net.cpp.

References net_stream::addrlen, Com_Printf(), Com_sprintf(), NET_StreamIsLoopback(), Q_strncpyz(), and net_stream::socket.

Referenced by GAME_GetImportData(), NET_ShowStreams_f(), NET_StreamToString(), NET_Wait(), SV_ConnectionlessPacket(), SV_Status_f(), SVC_BucketForAddress(), SVC_DirectConnect(), and SVC_RemoteCommand().

void NET_StreamSetCallback ( struct net_stream s,
stream_callback_func func 
)

Definition at line 903 of file net.cpp.

References net_stream::func.

Referenced by GAME_GetImportData().

void NET_StreamSetData ( struct net_stream s,
void data 
)

Definition at line 805 of file net.cpp.

References net_stream::data.

Referenced by GAME_GetImportData(), and SVC_DirectConnect().

const char* NET_StreamToString ( struct net_stream s)

Returns the numerical representation of a net_stream.

Note
Not thread safe!

Definition at line 859 of file net.cpp.

References NET_StreamPeerToName().

Referenced by SVC_Info(), SVC_Status(), and SVC_TeamInfo().

bool SV_Start ( const char *  node,
const char *  service,
stream_callback_func func 
)
See also
NET_DoStartServer
Parameters
[in]nodeThe node to start the server with
[in]serviceIf this is nullptr we are in single player mode
[in]funcThe server callback function to read the packets
See also
SV_ReadPacket
server_func
SV_Stop

Definition at line 990 of file net.cpp.

References Com_Printf(), INVALID_SOCKET, NET_DoStartServer(), NET_GetAddrinfoForNode(), server_func, server_running, and server_socket.

Referenced by SV_InitGame().

void SV_Stop ( void  )
See also
SV_Start

Definition at line 1026 of file net.cpp.

References INVALID_SOCKET, netCloseSocket, read_fds, server_func, server_running, and server_socket.

Referenced by SV_Shutdown().

Variable Documentation

struct datagram_socket* datagram_sockets[MAX_DATAGRAM_SOCKETS]
static

Definition at line 152 of file net.cpp.

bool loopback_ready = false
static

Definition at line 154 of file net.cpp.

Referenced by NET_StreamEnqueue(), and NET_Wait().

SOCKET maxfd
static
cvar_t* net_ipv4
static

Definition at line 106 of file net.cpp.

SDL_mutex* netMutex
static
int server_addrlen
static

Definition at line 158 of file net.cpp.

Referenced by do_accept(), and NET_DoStartServer().

int server_family
static

Definition at line 158 of file net.cpp.

Referenced by do_accept(), and NET_DoStartServer().

stream_callback_func* server_func = nullptr
static

Definition at line 156 of file net.cpp.

Referenced by do_accept(), NET_ConnectToLoopBack(), SV_Start(), and SV_Stop().

bool server_running = false
static

Definition at line 155 of file net.cpp.

Referenced by NET_ConnectToLoopBack(), SV_Start(), and SV_Stop().

SOCKET server_socket = INVALID_SOCKET
static

Definition at line 157 of file net.cpp.

Referenced by NET_Wait(), SV_Start(), and SV_Stop().

struct net_stream* streams[MAX_STREAMS]
static

Definition at line 151 of file net.cpp.

fd_set write_fds
static