UFO: Alien Invasion
Loading...
Searching...
No Matches
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>
Include dependency graph for net.cpp:

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.
#define dbuffer_len(dbuf)

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.
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.
static int NET_StreamPeek (struct net_stream *s, char *data, int len)
 Returns the length of the waiting inbound buffer.
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.
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.
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.
const char * NET_StreamToString (struct net_stream *s)
 Returns the numerical representation of a net_stream.
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).
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.
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

◆ AI_ADDRCONFIG

#define AI_ADDRCONFIG   0

Definition at line 95 of file net.cpp.

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

◆ AI_NUMERICSERV

#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().

◆ dbuffer_len

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

Definition at line 104 of file net.cpp.

Referenced by NET_ShowStreams_f(), NET_StreamClose(), NET_StreamFinished(), NET_StreamGetLength(), NET_StreamPeek(), and NET_Wait().

◆ INVALID_SOCKET

◆ ioctlsocket

#define ioctlsocket   ioctl

Definition at line 74 of file net.cpp.

Referenced by NET_SocketSetNonBlocking().

◆ MAX_DATAGRAM_SOCKETS

#define MAX_DATAGRAM_SOCKETS   7

Definition at line 43 of file net.cpp.

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

◆ MAX_STREAMS

#define MAX_STREAMS   56

Definition at line 42 of file net.cpp.

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

◆ NET_MULTICAST_IP6

#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.

◆ netCloseSocket

#define netCloseSocket   close

◆ netError

#define netError   errno

Definition at line 71 of file net.cpp.

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

◆ netStringError

#define netStringError   strerror

Definition at line 72 of file net.cpp.

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

Typedef Documentation

◆ SOCKET

typedef int SOCKET

Definition at line 69 of file net.cpp.

Function Documentation

◆ do_accept()

◆ NET_AddrinfoToString()

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

Definition at line 1223 of file net.cpp.

References datagram::addr, and Q_strncpyz().

Referenced by NET_ResolvNode().

◆ NET_Connect()

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, NET_DoConnect(), net_ipv4, NET_StreamGetFree(), OBJZERO, and net_stream::onclose.

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

◆ NET_ConnectToLoopBack()

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().

◆ NET_DatagramBroadcast()

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, len, NET_DatagramSend(), and port.

Referenced by GAME_GetImportData().

◆ NET_DatagramFindFreeSocket()

int NET_DatagramFindFreeSocket ( void )
static
See also
NET_StreamNew

Definition at line 243 of file net.cpp.

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

Referenced by NET_DatagramSocketDoNew().

◆ NET_DatagramSend()

◆ NET_DatagramSocketClose()

◆ NET_DatagramSocketDoNew()

◆ NET_DatagramSocketNew()

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, NET_DatagramSocketDoNew(), net_ipv4, and OBJZERO.

Referenced by GAME_GetImportData(), and SV_InitGame().

◆ NET_DoConnect()

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

◆ NET_DoStartServer()

int NET_DoStartServer ( const struct addrinfo * addr)
static

◆ NET_GetAddrinfoForNode()

struct addrinfo * NET_GetAddrinfoForNode ( const char * node,
const char * service )
static

Definition at line 960 of file net.cpp.

References AI_ADDRCONFIG, Com_Printf(), net_ipv4, and OBJZERO.

Referenced by NET_ResolvNode(), and SV_Start().

◆ NET_Init()

◆ NET_ReadMsg()

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 len, LittleLong, NET_StreamDequeue(), NET_StreamGetLength(), NET_StreamPeek(), netMutex, and v.

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

◆ NET_ResolvNode()

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().

◆ NET_ShowStreams_f()

◆ NET_Shutdown()

◆ NET_SockaddrToStrings()

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::addr, datagram_socket::addrlen, Com_Printf(), and Q_strncpyz().

Referenced by GAME_GetImportData().

◆ NET_SocketSetNonBlocking()

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().

◆ NET_StreamClose()

◆ NET_StreamDequeue()

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

Definition at line 760 of file net.cpp.

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

Referenced by Irc_Proto_PollServerMsg(), and NET_ReadMsg().

◆ NET_StreamEnqueue()

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

◆ NET_StreamFinished()

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().

◆ NET_StreamFree()

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(), SV_ReadPacket(), and cgame_import_t::void().

◆ NET_StreamGetData()

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().

◆ NET_StreamGetFree()

int NET_StreamGetFree ( void )
static

◆ NET_StreamGetLength()

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().

◆ NET_StreamIsLoopback()

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().

◆ NET_StreamNew()

◆ NET_StreamPeek()

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, data, dbuffer_len, net_stream::finished, dbuffer::get(), net_stream::inbound, and len.

Referenced by NET_ReadMsg().

◆ NET_StreamPeerToName()

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(), len, 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().

◆ NET_StreamSetCallback()

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().

◆ NET_StreamSetData()

void NET_StreamSetData ( struct net_stream * s,
void * data )

Definition at line 805 of file net.cpp.

References data, and net_stream::data.

Referenced by GAME_GetImportData(), SVC_DirectConnect(), and cgame_import_t::void().

◆ NET_StreamToString()

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().

◆ NET_Wait()

◆ SV_Start()

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().

◆ SV_Stop()

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

◆ datagram_sockets

◆ loopback_ready

bool loopback_ready = false
static

Definition at line 154 of file net.cpp.

Referenced by NET_StreamEnqueue(), and NET_Wait().

◆ maxfd

SOCKET maxfd
static

◆ net_ipv4

cvar_t* net_ipv4
static

Definition at line 106 of file net.cpp.

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

◆ netMutex

SDL_mutex* netMutex
static

◆ read_fds

◆ server_addrlen

int server_addrlen
static

Definition at line 158 of file net.cpp.

Referenced by do_accept(), and NET_DoStartServer().

◆ server_family

int server_family
static

Definition at line 158 of file net.cpp.

Referenced by do_accept(), and NET_DoStartServer().

◆ server_func

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().

◆ server_running

bool server_running = false
static

Definition at line 155 of file net.cpp.

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

◆ server_socket

SOCKET server_socket = INVALID_SOCKET
static

Definition at line 157 of file net.cpp.

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

◆ streams

struct net_stream* streams[MAX_STREAMS]
static

◆ write_fds

fd_set write_fds
static