UFO: Alien Invasion
|
#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_stream * | NET_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_stream * | NET_DoConnect (const char *node, const char *service, const struct addrinfo *addr, int i, stream_onclose_func *onclose) |
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. More... | |
struct net_stream * | NET_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) |
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. More... | |
void * | NET_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_socket * | NET_DatagramSocketDoNew (const struct addrinfo *addr) |
struct datagram_socket * | NET_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_t * | net_ipv4 |
static SDL_mutex * | netMutex |
static fd_set | read_fds |
static fd_set | write_fds |
static SOCKET | maxfd |
static struct net_stream * | streams [MAX_STREAMS] |
static struct datagram_socket * | datagram_sockets [MAX_DATAGRAM_SOCKETS] |
static bool | loopback_ready = false |
static bool | server_running = false |
static stream_callback_func * | server_func = nullptr |
static SOCKET | server_socket = INVALID_SOCKET |
static int | server_family |
static int | server_addrlen |
Definition in file net.cpp.
#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 |
Definition at line 92 of file net.cpp.
Referenced by NET_Connect(), and NET_DatagramSocketNew().
#define dbuffer_len | ( | dbuf | ) | (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().
#define INVALID_SOCKET (-1) |
Definition at line 70 of file net.cpp.
Referenced by NET_DatagramSocketDoNew(), NET_DoConnect(), NET_DoStartServer(), NET_StreamClose(), NET_StreamEnqueue(), NET_StreamFinished(), NET_StreamNew(), NET_Wait(), SV_Start(), and SV_Stop().
#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" |
#define netCloseSocket close |
Definition at line 73 of file net.cpp.
Referenced by do_accept(), NET_DatagramSocketClose(), NET_DatagramSocketDoNew(), NET_DoConnect(), NET_DoStartServer(), NET_StreamClose(), and SV_Stop().
#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().
Definition at line 396 of file net.cpp.
References net_stream::addrlen, Com_Printf(), net_stream::family, net_stream::func, net_stream::inbound, index, maxfd, NET_StreamGetFree(), NET_StreamNew(), netCloseSocket, net_stream::outbound, read_fds, server_addrlen, server_family, server_func, and net_stream::socket.
Referenced by NET_Wait().
|
static |
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.
[in] | node | The host to connect to |
[in] | service | The port to connect to |
[in] | onclose | The 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. |
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 | ) |
[in] | onclose | The 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. |
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 | ||
) |
Definition at line 1159 of file net.cpp.
References Com_Error(), ERR_DROP, datagram_socket::family, and NET_DatagramSend().
Referenced by GAME_GetImportData().
Definition at line 243 of file net.cpp.
References Com_DPrintf(), DEBUG_SERVER, i, and MAX_DATAGRAM_SOCKETS.
Referenced by NET_DatagramSocketDoNew().
void NET_DatagramSend | ( | struct datagram_socket * | s, |
const char * | buf, | ||
int | len, | ||
struct sockaddr * | to | ||
) |
Definition at line 1135 of file net.cpp.
References datagram::addr, datagram_socket::addrlen, com_networkPool, len, datagram::len, Mem_PoolAllocType, Mem_PoolAllocTypeN, datagram::msg, datagram::next, datagram_socket::queue_tail, datagram_socket::socket, and write_fds.
Referenced by NET_DatagramBroadcast(), and SV_DiscoveryCallback().
void NET_DatagramSocketClose | ( | struct datagram_socket * | s | ) |
Definition at line 1182 of file net.cpp.
References datagram::addr, datagram_socket::index, Mem_Free, datagram::msg, netCloseSocket, datagram::next, datagram_socket::queue, read_fds, datagram_socket::socket, and write_fds.
Referenced by GAME_GetImportData(), and SV_Shutdown().
|
static |
Definition at line 1040 of file net.cpp.
References datagram_socket::addrlen, com_networkPool, Com_Printf(), datagram_socket::family, datagram_socket::func, index, datagram_socket::index, INVALID_SOCKET, maxfd, Mem_PoolAllocType, NET_DatagramFindFreeSocket(), NET_SocketSetNonBlocking(), netCloseSocket, netError, netStringError, datagram_socket::queue, datagram_socket::queue_tail, read_fds, and datagram_socket::socket.
Referenced by NET_DatagramSocketNew().
struct datagram_socket* NET_DatagramSocketNew | ( | const char * | node, |
const char * | service, | ||
datagram_callback_func * | func | ||
) |
Opens a datagram socket (UDP)
[in] | node | The numeric address to resolv (might be nullptr) |
[in] | service | The port number |
[in] | func | Callback 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 |
Definition at line 593 of file net.cpp.
References net_stream::addrlen, Com_Printf(), net_stream::family, net_stream::inbound, INVALID_SOCKET, maxfd, NET_SocketSetNonBlocking(), NET_StreamNew(), netCloseSocket, netError, netStringError, net_stream::onclose, net_stream::outbound, read_fds, and net_stream::socket.
Referenced by NET_Connect().
|
static |
Definition at line 915 of file net.cpp.
References Com_Printf(), INVALID_SOCKET, maxfd, NET_SocketSetNonBlocking(), netCloseSocket, netError, netStringError, read_fds, server_addrlen, server_family, and net_stream::socket.
Referenced by SV_Start().
|
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().
Definition at line 305 of file net.cpp.
References Cmd_AddCommand(), Com_Error(), Com_Printf(), CVAR_ARCHIVE, Cvar_Get(), ERR_FATAL, i, MAX_DATAGRAM_SOCKETS, MAX_STREAMS, maxfd, NET_ShowStreams_f(), netMutex, read_fds, and write_fds.
Referenced by Qcommon_Init(), and TEST_Init().
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.
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().
Definition at line 284 of file net.cpp.
References net_stream::closed, Com_Printf(), dbuffer_len, net_stream::finished, i, net_stream::inbound, MAX_STREAMS, NET_StreamPeerToName(), net_stream::outbound, net_stream::socket, and UFO_SIZE_T.
Referenced by NET_Init().
Definition at line 337 of file net.cpp.
References netMutex.
Referenced by Qcommon_Shutdown(), GenericTest::TearDownTestCase(), FootStepTest::TearDownTestCase(), MapDefStatsTest::TearDownTestCase(), MapDefTest::TearDownTestCase(), MapDefMassRMATest::TearDownTestCase(), InventoryTest::TearDownTestCase(), and TEST_Shutdown().
void NET_SockaddrToStrings | ( | struct datagram_socket * | s, |
struct sockaddr * | addr, | ||
char * | node, | ||
size_t | nodelen, | ||
char * | service, | ||
size_t | servicelen | ||
) |
Convert sockaddr to string.
[in] | s | The datagram socket type to get the addrlen from |
[in] | addr | The socket address to convert into a string |
[out] | node | The target node name buffer |
[in] | nodelen | The length of the node name buffer |
[out] | service | The target service name buffer |
[in] | servicelen | The 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 |
Definition at line 583 of file net.cpp.
References Com_Printf(), and ioctlsocket.
Referenced by NET_DatagramSocketDoNew(), NET_DoConnect(), and NET_DoStartServer().
|
static |
Definition at line 350 of file net.cpp.
References net_stream::closed, Com_DPrintf(), Com_Printf(), dbuffer_len, DEBUG_SERVER, net_stream::finished, net_stream::func, net_stream::inbound, net_stream::index, INVALID_SOCKET, net_stream::loopback_peer, Mem_Free, netCloseSocket, net_stream::onclose, net_stream::outbound, read_fds, net_stream::socket, and write_fds.
Referenced by NET_StreamFinished(), NET_StreamFree(), and NET_Wait().
int NET_StreamDequeue | ( | struct net_stream * | s, |
char * | data, | ||
int | len | ||
) |
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 | ||
) |
Enqueue a network message into a stream.
Definition at line 719 of file net.cpp.
References dbuffer::add(), net_stream::closed, net_stream::finished, INVALID_SOCKET, net_stream::loopback_peer, loopback_ready, netMutex, net_stream::outbound, net_stream::ready, net_stream::socket, and write_fds.
Referenced by Irc_Net_Send(), NET_OOB_Printf(), NET_WriteConstMsg(), and NET_WriteMsg().
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.
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().
Definition at line 225 of file net.cpp.
References Com_DPrintf(), DEBUG_SERVER, i, and MAX_STREAMS.
Referenced by do_accept(), NET_Connect(), and NET_ConnectToLoopBack().
|
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 |
Definition at line 262 of file net.cpp.
References net_stream::addrlen, net_stream::closed, com_networkPool, net_stream::data, net_stream::family, net_stream::finished, net_stream::func, net_stream::inbound, index, net_stream::index, INVALID_SOCKET, net_stream::loopback, net_stream::loopback_peer, Mem_PoolAllocType, NET_StreamFree(), net_stream::outbound, net_stream::ready, and net_stream::socket.
Referenced by do_accept(), NET_ConnectToLoopBack(), and NET_DoConnect().
|
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 | ||
) |
[in] | s | The network stream to get the name for |
[out] | dst | The target buffer to store the ip and port in |
[in] | len | The length of the target buffer |
[in] | appendPort | Also 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
.
Definition at line 859 of file net.cpp.
References NET_StreamPeerToName().
Referenced by SVC_Info(), SVC_Status(), and SVC_TeamInfo().
Definition at line 423 of file net.cpp.
References dbuffer::add(), datagram::addr, datagram_socket::addrlen, Com_DPrintf(), Com_Printf(), dbuffer_len, DEBUG_SERVER, do_accept(), net_stream::finished, net_stream::func, datagram_socket::func, dbuffer::get(), i, net_stream::inbound, INVALID_SOCKET, len, datagram::len, net_stream::loopback, net_stream::loopback_peer, loopback_ready, MAX_DATAGRAM_SOCKETS, MAX_STREAMS, maxfd, Mem_Free, datagram::msg, NET_StreamClose(), NET_StreamGetLength(), NET_StreamPeerToName(), netError, netMutex, netStringError, datagram::next, net_stream::outbound, datagram_socket::queue, datagram_socket::queue_tail, read_fds, net_stream::ready, dbuffer::remove(), server_socket, net_stream::socket, datagram_socket::socket, Sys_Sleep(), and write_fds.
Referenced by CL_Disconnect(), Com_Error(), Com_Quit(), Qcommon_Frame(), and SV_FinalMessage().
bool SV_Start | ( | const char * | node, |
const char * | service, | ||
stream_callback_func * | func | ||
) |
[in] | node | The node to start the server with |
[in] | service | If this is nullptr we are in single player mode |
[in] | func | The server callback function to read the packets |
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().
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().
|
static |
|
static |
Definition at line 154 of file net.cpp.
Referenced by NET_StreamEnqueue(), and NET_Wait().
|
static |
Definition at line 150 of file net.cpp.
Referenced by do_accept(), NET_DatagramSocketDoNew(), NET_DoConnect(), NET_DoStartServer(), NET_Init(), and NET_Wait().
|
static |
Definition at line 107 of file net.cpp.
Referenced by NET_Init(), NET_ReadMsg(), NET_Shutdown(), NET_StreamEnqueue(), NET_StreamFinished(), and NET_Wait().
|
static |
Definition at line 148 of file net.cpp.
Referenced by do_accept(), NET_DatagramSocketClose(), NET_DatagramSocketDoNew(), NET_DoConnect(), NET_DoStartServer(), NET_Init(), NET_StreamClose(), NET_StreamFinished(), NET_Wait(), and SV_Stop().
|
static |
Definition at line 158 of file net.cpp.
Referenced by do_accept(), and NET_DoStartServer().
|
static |
Definition at line 158 of file net.cpp.
Referenced by do_accept(), and NET_DoStartServer().
|
static |
Definition at line 156 of file net.cpp.
Referenced by do_accept(), NET_ConnectToLoopBack(), SV_Start(), and SV_Stop().
|
static |
Definition at line 155 of file net.cpp.
Referenced by NET_ConnectToLoopBack(), SV_Start(), and SV_Stop().
|
static |
Definition at line 157 of file net.cpp.
Referenced by NET_Wait(), SV_Start(), and SV_Stop().
|
static |
|
static |
Definition at line 149 of file net.cpp.
Referenced by NET_DatagramSend(), NET_DatagramSocketClose(), NET_Init(), NET_StreamClose(), NET_StreamEnqueue(), and NET_Wait().