#include "common.h"
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.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 |
Defines | |
#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. | |
Typedefs | |
typedef int | SOCKET |
Functions | |
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 (int sock) |
void | NET_Wait (int timeout) |
static qboolean | NET_SocketSetNonBlocking (int socket) |
static struct net_stream * | NET_DoConnect (const char *node, const char *service, const struct addrinfo *addr, int i) |
struct net_stream * | NET_Connect (const char *node, const char *service) |
Try to connect to a given host on a given port. | |
struct net_stream * | NET_ConnectToLoopBack (void) |
void | NET_StreamEnqueue (struct net_stream *s, const char *data, int len) |
Enqueue a network message into a stream. | |
qboolean | NET_StreamIsClosed (struct net_stream *s) |
int | NET_StreamGetLength (struct net_stream *s) |
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) |
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. | |
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_StreamPeerToName (struct net_stream *s, char *dst, int len, qboolean appendPort) |
void | NET_StreamSetCallback (struct net_stream *s, stream_callback_func *func) |
qboolean | NET_StreamIsLoopback (struct net_stream *s) |
static int | NET_DoStartServer (const struct addrinfo *addr) |
qboolean | 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). | |
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. | |
Variables | |
struct memPool_s * | com_networkPool |
static cvar_t * | net_ipv4 |
static fd_set | read_fds |
static fd_set | write_fds |
static int | maxfd |
static struct net_stream * | streams [MAX_STREAMS] |
static struct datagram_socket * | datagram_sockets [MAX_DATAGRAM_SOCKETS] |
static qboolean | loopback_ready = qfalse |
static qboolean | server_running = qfalse |
static stream_callback_func * | server_func = NULL |
static int | server_socket = INVALID_SOCKET |
static int | server_family |
static int | server_addrlen |
Definition in file net.c.
#define AI_ADDRCONFIG 0 |
Definition at line 85 of file net.c.
Referenced by NET_Connect(), NET_DatagramSocketNew(), and SV_Start().
#define AI_NUMERICSERV 0 |
Definition at line 82 of file net.c.
Referenced by NET_Connect(), NET_DatagramSocketNew(), and SV_Start().
#define INVALID_SOCKET (-1) |
Definition at line 69 of file net.c.
Referenced by NET_DatagramSocketDoNew(), NET_DoConnect(), NET_DoStartServer(), NET_StreamClose(), NET_StreamNew(), NET_Wait(), SV_Start(), and SV_Stop().
#define ioctlsocket ioctl |
Definition at line 73 of file net.c.
Referenced by NET_SocketSetNonBlocking().
#define MAX_DATAGRAM_SOCKETS 7 |
Definition at line 40 of file net.c.
Referenced by NET_DatagramFindFreeSocket(), NET_Init(), and NET_Wait().
#define MAX_STREAMS 56 |
Definition at line 39 of file net.c.
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 72 of file net.c.
Referenced by do_accept(), NET_DatagramSocketClose(), NET_DatagramSocketDoNew(), NET_DoConnect(), NET_DoStartServer(), NET_StreamClose(), and SV_Stop().
#define netError errno |
Definition at line 70 of file net.c.
Referenced by NET_DatagramSocketDoNew(), NET_DoConnect(), NET_DoStartServer(), and NET_Wait().
#define netStringError strerror |
Definition at line 71 of file net.c.
Referenced by NET_DatagramSocketDoNew(), NET_DoConnect(), NET_DoStartServer(), and NET_Wait().
static void do_accept | ( | int | sock | ) | [static] |
Definition at line 379 of file net.c.
References net_stream::addrlen, Com_Printf(), net_stream::family, net_stream::func, net_stream::inbound, net_stream::index, maxfd, NET_StreamGetFree(), NET_StreamNew(), netCloseSocket, new_dbuffer(), net_stream::outbound, read_fds, server_addrlen, server_family, server_func, and net_stream::socket.
Referenced by NET_Wait().
struct net_stream* NET_Connect | ( | const char * | node, | |
const char * | service | |||
) | [read] |
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 |
Definition at line 622 of file net.c.
References AI_ADDRCONFIG, AI_NUMERICSERV, Com_Printf(), net_stream::index, cvar_s::integer, NET_DoConnect(), and NET_StreamGetFree().
Referenced by CL_Connect(), CL_PingServer(), CL_Rcon_f(), CL_ServerInfo_f(), and Irc_Net_Connect().
struct net_stream* NET_ConnectToLoopBack | ( | void | ) | [read] |
Definition at line 659 of file net.c.
References Com_Printf(), net_stream::func, net_stream::inbound, net_stream::loopback, net_stream::loopback_peer, NET_StreamGetFree(), NET_StreamNew(), new_dbuffer(), net_stream::outbound, qtrue, 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 1098 of file net.c.
References Com_Error(), ERR_DROP, datagram_socket::family, and NET_DatagramSend().
Referenced by CL_PingServers_f().
static int NET_DatagramFindFreeSocket | ( | void | ) | [static] |
Definition at line 223 of file net.c.
References Com_DPrintf(), DEBUG_SERVER, i, MAX_DATAGRAM_SOCKETS, and pos.
Referenced by NET_DatagramSocketDoNew().
void NET_DatagramSend | ( | struct datagram_socket * | s, | |
const char * | buf, | |||
int | len, | |||
struct sockaddr * | to | |||
) |
Definition at line 1073 of file net.c.
References datagram::addr, datagram_socket::addrlen, datagram::len, Mem_PoolAlloc, 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 1121 of file net.c.
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 CL_Connect(), and SV_Shutdown().
static struct datagram_socket* NET_DatagramSocketDoNew | ( | const struct addrinfo * | addr | ) | [static, read] |
Definition at line 975 of file net.c.
References datagram_socket::addrlen, Com_Printf(), datagram_socket::family, datagram_socket::func, datagram_socket::index, INVALID_SOCKET, maxfd, Mem_PoolAlloc, 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 | |||
) | [read] |
Opens a datagram socket (UDP).
[in] | node | The numeric address to resolv (might be NULL) |
[in] | service | The port number |
[in] | func | Callback function for data handling |
Definition at line 1038 of file net.c.
References AI_ADDRCONFIG, AI_NUMERICSERV, Com_Printf(), datagram_socket::func, cvar_s::integer, NET_DatagramSocketDoNew(), and qfalse.
Referenced by CL_PingServers_f(), and SV_InitGame().
static struct net_stream* NET_DoConnect | ( | const char * | node, | |
const char * | service, | |||
const struct addrinfo * | addr, | |||
int | i | |||
) | [static, read] |
Definition at line 574 of file net.c.
References net_stream::addrlen, Com_Printf(), net_stream::family, net_stream::inbound, INVALID_SOCKET, maxfd, NET_SocketSetNonBlocking(), NET_StreamNew(), netCloseSocket, netError, netStringError, new_dbuffer(), net_stream::outbound, read_fds, and net_stream::socket.
Referenced by NET_Connect().
static int NET_DoStartServer | ( | const struct addrinfo * | addr | ) | [static] |
Definition at line 858 of file net.c.
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().
void NET_Init | ( | void | ) |
Definition at line 287 of file net.c.
References Cmd_AddCommand(), Com_Error(), Com_Printf(), CVAR_ARCHIVE, Cvar_Get(), dbuffer_init(), ERR_FATAL, i, MAX_DATAGRAM_SOCKETS, MAX_STREAMS, maxfd, NET_ShowStreams_f(), read_fds, and write_fds.
Referenced by Qcommon_Init().
static void NET_ShowStreams_f | ( | void | ) | [static] |
Definition at line 265 of file net.c.
References net_stream::closed, Com_Printf(), dbuffer_len, net_stream::finished, i, net_stream::inbound, MAX_STREAMS, NET_StreamPeerToName(), net_stream::outbound, qtrue, net_stream::socket, and UFO_SIZE_T.
Referenced by NET_Init().
void NET_Shutdown | ( | void | ) |
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 1151 of file net.c.
References datagram_socket::addrlen, Com_Printf(), and Q_strncpyz().
Referenced by CL_ServerListDiscoveryCallback().
static qboolean NET_SocketSetNonBlocking | ( | int | socket | ) | [static] |
Definition at line 564 of file net.c.
References Com_Printf(), errno, ioctlsocket, qfalse, and qtrue.
Referenced by NET_DatagramSocketDoNew(), NET_DoConnect(), and NET_DoStartServer().
static void NET_StreamClose | ( | struct net_stream * | s | ) | [static] |
Definition at line 335 of file net.c.
References net_stream::closed, Com_DPrintf(), Com_Printf(), dbuffer_len, DEBUG_SERVER, net_stream::finished, free_dbuffer(), net_stream::func, net_stream::inbound, net_stream::index, INVALID_SOCKET, net_stream::loopback_peer, Mem_Free, netCloseSocket, net_stream::outbound, qtrue, 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 745 of file net.c.
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 699 of file net.c.
References net_stream::closed, dbuffer_add(), net_stream::finished, net_stream::loopback_peer, loopback_ready, net_stream::outbound, qtrue, 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 785 of file net.c.
References dbuffer_len, net_stream::finished, free_dbuffer(), net_stream::inbound, net_stream::loopback_peer, NET_StreamClose(), net_stream::outbound, qtrue, 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 770 of file net.c.
References net_stream::finished, NET_StreamClose(), and qtrue.
Referenced by CL_PingServerCallback(), CL_RconCallback(), CL_ServerInfoCallback(), Irc_Net_Connect(), Irc_Net_Disconnect(), NET_StreamNew(), and SV_ReadPacket().
void* NET_StreamGetData | ( | struct net_stream * | s | ) |
Definition at line 753 of file net.c.
References net_stream::data.
Referenced by CL_PingServerCallback(), and SV_ReadPacket().
static int NET_StreamGetFree | ( | void | ) | [static] |
Definition at line 204 of file net.c.
References Com_DPrintf(), DEBUG_SERVER, i, MAX_STREAMS, and pos.
Referenced by do_accept(), NET_Connect(), and NET_ConnectToLoopBack().
int NET_StreamGetLength | ( | struct net_stream * | s | ) |
Definition at line 721 of file net.c.
References dbuffer_len, and net_stream::inbound.
Referenced by NET_ReadMsg(), and NET_Wait().
qboolean NET_StreamIsClosed | ( | struct net_stream * | s | ) |
Definition at line 716 of file net.c.
References net_stream::closed, net_stream::finished, and qtrue.
qboolean NET_StreamIsLoopback | ( | struct net_stream * | s | ) |
Definition at line 853 of file net.c.
References net_stream::loopback.
Referenced by CL_ConnectionlessPacket(), and NET_StreamPeerToName().
static struct net_stream* NET_StreamNew | ( | int | index | ) | [static, read] |
Definition at line 243 of file net.c.
References net_stream::addrlen, net_stream::closed, net_stream::data, net_stream::family, net_stream::finished, net_stream::func, net_stream::inbound, net_stream::index, INVALID_SOCKET, net_stream::loopback, net_stream::loopback_peer, Mem_PoolAlloc, NET_StreamFree(), net_stream::outbound, qfalse, net_stream::ready, and net_stream::socket.
Referenced by do_accept(), NET_ConnectToLoopBack(), and NET_DoConnect().
int NET_StreamPeek | ( | struct net_stream * | s, | |
char * | data, | |||
int | len | |||
) |
Returns the length of the waiting inbound buffer.
Definition at line 730 of file net.c.
References net_stream::closed, dbuffer_get(), dbuffer_len, net_stream::finished, and net_stream::inbound.
Referenced by NET_ReadMsg().
const char* NET_StreamPeerToName | ( | struct net_stream * | s, | |
char * | dst, | |||
int | len, | |||
qboolean | 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 814 of file net.c.
References net_stream::addrlen, Com_Printf(), Com_sprintf(), NET_StreamIsLoopback(), Q_strncpyz(), and net_stream::socket.
Referenced by CL_ServerInfoCallback(), NET_ShowStreams_f(), NET_Wait(), SV_ConnectionlessPacket(), SV_Status_f(), SVC_DirectConnect(), and SVC_RemoteCommand().
void NET_StreamSetCallback | ( | struct net_stream * | s, | |
stream_callback_func * | func | |||
) |
Definition at line 846 of file net.c.
References net_stream::func.
Referenced by CL_PingServer(), CL_Rcon_f(), and CL_ServerInfo_f().
void NET_StreamSetData | ( | struct net_stream * | s, | |
void * | data | |||
) |
Definition at line 758 of file net.c.
References net_stream::data.
Referenced by CL_PingServer(), and SVC_DirectConnect().
void NET_Wait | ( | int | timeout | ) |
Definition at line 407 of file net.c.
References datagram::addr, datagram_socket::addrlen, Com_DPrintf(), Com_Printf(), dbuffer_add(), dbuffer_get(), dbuffer_len, dbuffer_remove(), DEBUG_SERVER, do_accept(), errno, net_stream::finished, datagram_socket::func, net_stream::func, i, net_stream::inbound, INVALID_SOCKET, datagram::len, 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, netStringError, datagram::next, net_stream::outbound, qfalse, qtrue, datagram_socket::queue, datagram_socket::queue_tail, read_fds, net_stream::ready, server_socket, datagram_socket::socket, net_stream::socket, Sys_Sleep(), and write_fds.
Referenced by CL_Disconnect(), Com_Error(), Com_Quit(), Qcommon_Frame(), and SV_FinalMessage().
qboolean 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 NULL we are in single player mode |
[in] | func | The server callback function to read the packets |
Definition at line 912 of file net.c.
References AI_ADDRCONFIG, AI_NUMERICSERV, Com_Printf(), cvar_s::integer, INVALID_SOCKET, NET_DoStartServer(), qfalse, qtrue, server_func, server_running, and server_socket.
Referenced by SV_InitGame().
void SV_Stop | ( | void | ) |
Definition at line 961 of file net.c.
References INVALID_SOCKET, netCloseSocket, qfalse, read_fds, server_func, server_running, and server_socket.
Referenced by SV_Shutdown().
struct memPool_s* com_networkPool |
struct datagram_socket* datagram_sockets[MAX_DATAGRAM_SOCKETS] [static] |
qboolean loopback_ready = qfalse [static] |
Definition at line 138 of file net.c.
Referenced by NET_StreamEnqueue(), and NET_Wait().
int maxfd [static] |
Definition at line 134 of file net.c.
Referenced by do_accept(), NET_DatagramSocketDoNew(), NET_DoConnect(), NET_DoStartServer(), NET_Init(), and NET_Wait().
fd_set read_fds [static] |
Definition at line 132 of file net.c.
Referenced by do_accept(), NET_DatagramSocketClose(), NET_DatagramSocketDoNew(), NET_DoConnect(), NET_DoStartServer(), NET_Init(), NET_StreamClose(), NET_StreamFinished(), NET_Wait(), and SV_Stop().
int server_addrlen [static] |
Definition at line 142 of file net.c.
Referenced by do_accept(), and NET_DoStartServer().
int server_family [static] |
Definition at line 142 of file net.c.
Referenced by do_accept(), and NET_DoStartServer().
stream_callback_func* server_func = NULL [static] |
Definition at line 140 of file net.c.
Referenced by do_accept(), NET_ConnectToLoopBack(), SV_Start(), and SV_Stop().
qboolean server_running = qfalse [static] |
Definition at line 139 of file net.c.
Referenced by NET_ConnectToLoopBack(), SV_Start(), and SV_Stop().
int server_socket = INVALID_SOCKET [static] |
Definition at line 141 of file net.c.
Referenced by NET_Wait(), SV_Start(), and SV_Stop().
struct net_stream* streams[MAX_STREAMS] [static] |
fd_set write_fds [static] |
Definition at line 133 of file net.c.
Referenced by NET_DatagramSend(), NET_DatagramSocketClose(), NET_Init(), NET_StreamClose(), NET_StreamEnqueue(), and NET_Wait().