net.c File Reference

#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>
Include dependency graph for net.c:

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_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 (int sock)
void NET_Wait (int timeout)
static qboolean NET_SocketSetNonBlocking (int socket)
static struct net_streamNET_DoConnect (const char *node, const char *service, const struct addrinfo *addr, int i)
struct net_streamNET_Connect (const char *node, const char *service)
 Try to connect to a given host on a given port.
struct net_streamNET_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_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.

Variables

struct memPool_scom_networkPool
static cvar_tnet_ipv4
static fd_set read_fds
static fd_set write_fds
static int maxfd
static struct net_streamstreams [MAX_STREAMS]
static struct datagram_socketdatagram_sockets [MAX_DATAGRAM_SOCKETS]
static qboolean loopback_ready = qfalse
static qboolean server_running = qfalse
static stream_callback_funcserver_func = NULL
static int 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.c.


Define Documentation

#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
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 82 of file net.c.

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

#define INVALID_SOCKET   (-1)
#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"

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

Definition at line 92 of file net.c.

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


Typedef Documentation

typedef int SOCKET

Definition at line 68 of file net.c.


Function Documentation

static void do_accept ( int  sock  )  [static]
struct net_stream* NET_Connect ( const char *  node,
const char *  service 
) [read]

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

Parameters:
[in] node The host to connect to
[in] service The port to connect to
See also:
NET_DoConnect
NET_ConnectToLoopBack
Todo:
What about a timeout

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]
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 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]
See also:
NET_StreamNew

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 
)
void NET_DatagramSocketClose ( struct datagram_socket s  ) 
static struct datagram_socket* NET_DatagramSocketDoNew ( const struct addrinfo *  addr  )  [static, read]
struct datagram_socket* NET_DatagramSocketNew ( const char *  node,
const char *  service,
datagram_callback_func func 
) [read]

Opens a datagram socket (UDP).

See also:
NET_DatagramSocketDoNew
Parameters:
[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]
static int NET_DoStartServer ( const struct addrinfo *  addr  )  [static]
void NET_Init ( void   ) 
static void NET_ShowStreams_f ( void   )  [static]
void NET_Shutdown ( void   ) 
See also:
NET_Init

Definition at line 323 of file net.c.

References dbuffer_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.

Parameters:
[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]
int NET_StreamDequeue ( struct net_stream s,
char *  data,
int  len 
)
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 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.

See also:
NET_StreamClose
NET_StreamFinished

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]
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]
int NET_StreamPeek ( struct net_stream s,
char *  data,
int  len 
)

Returns the length of the waiting inbound buffer.

See also:
dbuffer_get

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 
)
Parameters:
[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  ) 
qboolean SV_Start ( const char *  node,
const char *  service,
stream_callback_func func 
)
See also:
NET_DoStartServer
Parameters:
[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
See also:
SV_ReadPacket
server_func
SV_Stop

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   ) 
See also:
SV_Start

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


Variable Documentation

Definition at line 42 of file net.c.

struct datagram_socket* datagram_sockets[MAX_DATAGRAM_SOCKETS] [static]

Definition at line 136 of file net.c.

qboolean loopback_ready = qfalse [static]

Definition at line 138 of file net.c.

Referenced by NET_StreamEnqueue(), and NET_Wait().

int maxfd [static]
cvar_t* net_ipv4 [static]

Definition at line 94 of file net.c.

fd_set read_fds [static]
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().

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]

Definition at line 135 of file net.c.

fd_set write_fds [static]

Generated by  doxygen 1.6.2