Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

unix_net.c File Reference

#include "../game/q_shared.h"
#include "../qcommon/qcommon.h"
#include <unistd.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <errno.h>

Include dependency graph for unix_net.c:

Include dependency graph

Go to the source code of this file.

Defines

#define MAX_IPS   16

Functions

char * NET_BaseAdrToString (netadr_t a)
char * NET_ErrorString (void)
void NET_GetLocalAddress (void)
void NET_Init (void)
int NET_IPSocket (char *net_interface, int port)
void NET_OpenIP (void)
void NET_Shutdown (void)
void NET_Sleep (int msec)
int NET_Socket (char *net_interface, int port)
void NetadrToSockadr (netadr_t *a, struct sockaddr_in *s)
void SockadrToNetadr (struct sockaddr_in *s, netadr_t *a)
qboolean Sys_GetPacket (netadr_t *net_from, msg_t *net_message)
qboolean Sys_IsLANAddress (netadr_t adr)
void Sys_SendPacket (int length, const void *data, netadr_t to)
void Sys_ShowIP (void)
qboolean Sys_StringToAdr (const char *s, netadr_t *a)
qboolean Sys_StringToSockaddr (const char *s, struct sockaddr *sadr)

Variables

int ip_socket
int ipx_socket
byte localIP [MAX_IPS][4]
netadr_t net_local_adr
cvar_tnoudp
int numIP


Define Documentation

#define MAX_IPS   16
 

Definition at line 55 of file unix_net.c.


Function Documentation

char* NET_BaseAdrToString netadr_t  a  ) 
 

Definition at line 91 of file unix_net.c.

References a, Com_sprintf(), netadr_t::ip, and s.

00092 {
00093     static  char    s[64];
00094     
00095     Com_sprintf (s, sizeof(s), "%i.%i.%i.%i", a.ip[0], a.ip[1], a.ip[2], a.ip[3]);
00096 
00097     return s;
00098 }

Here is the call graph for this function:

char * NET_ErrorString void   ) 
 

Definition at line 590 of file unix_net.c.

References code, and strerror().

Referenced by NET_IPSocket(), NET_IPXSocket(), NET_OpenSocks(), Sys_GetPacket(), and Sys_SendPacket().

00591 {
00592     int     code;
00593 
00594     code = errno;
00595     return strerror (code);
00596 }

Here is the call graph for this function:

void NET_GetLocalAddress void   ) 
 

Definition at line 428 of file unix_net.c.

References Com_Printf(), localIP, n, NULL, numIP, and p.

Referenced by NET_OpenIP().

00428                                  {
00429     char                hostname[256];
00430     struct hostent      *hostInfo;
00431     // int                  error; // bk001204 - unused
00432     char                *p;
00433     int                 ip;
00434     int                 n;
00435 
00436     if ( gethostname( hostname, 256 ) == -1 ) {
00437         return;
00438     }
00439 
00440     hostInfo = gethostbyname( hostname );
00441     if ( !hostInfo ) {
00442         return;
00443     }
00444 
00445     Com_Printf( "Hostname: %s\n", hostInfo->h_name );
00446     n = 0;
00447     while( ( p = hostInfo->h_aliases[n++] ) != NULL ) {
00448         Com_Printf( "Alias: %s\n", p );
00449     }
00450 
00451     if ( hostInfo->h_addrtype != AF_INET ) {
00452         return;
00453     }
00454 
00455     numIP = 0;
00456     while( ( p = hostInfo->h_addr_list[numIP++] ) != NULL && numIP < MAX_IPS ) {
00457         ip = ntohl( *(int *)p );
00458         localIP[ numIP ][0] = p[0];
00459         localIP[ numIP ][1] = p[1];
00460         localIP[ numIP ][2] = p[2];
00461         localIP[ numIP ][3] = p[3];
00462         Com_Printf( "IP: %i.%i.%i.%i\n", ( ip >> 24 ) & 0xff, ( ip >> 16 ) & 0xff, ( ip >> 8 ) & 0xff, ip & 0xff );
00463     }
00464 }

Here is the call graph for this function:

void NET_Init void   ) 
 

Definition at line 501 of file unix_net.c.

References Cvar_Get(), NET_OpenIP(), noudp, and cvar_s::value.

00502 {
00503     noudp = Cvar_Get ("net_noudp", "0", 0);
00504     // open sockets
00505     if (! noudp->value) {
00506         NET_OpenIP ();
00507     }
00508 }

Here is the call graph for this function:

int NET_IPSocket char *  net_interface,
int  port
 

Definition at line 516 of file unix_net.c.

References close(), Com_Printf(), err(), i, NET_ErrorString(), Q_stricmp(), qboolean, and Sys_StringToSockaddr().

Referenced by NET_OpenIP().

00517 {
00518     int newsocket;
00519     struct sockaddr_in address;
00520     qboolean _qtrue = qtrue;
00521     int i = 1;
00522 
00523     if ( net_interface ) {
00524         Com_Printf("Opening IP socket: %s:%i\n", net_interface, port );
00525     } else {
00526         Com_Printf("Opening IP socket: localhost:%i\n", port );
00527     }
00528 
00529     if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
00530     {
00531         Com_Printf ("ERROR: UDP_OpenSocket: socket: %s", NET_ErrorString());
00532         return 0;
00533     }
00534 
00535     // make it non-blocking
00536     if (ioctl (newsocket, FIONBIO, &_qtrue) == -1)
00537     {
00538         Com_Printf ("ERROR: UDP_OpenSocket: ioctl FIONBIO:%s\n", NET_ErrorString());
00539         return 0;
00540     }
00541 
00542     // make it broadcast capable
00543     if (setsockopt(newsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) == -1)
00544     {
00545         Com_Printf ("ERROR: UDP_OpenSocket: setsockopt SO_BROADCAST:%s\n", NET_ErrorString());
00546         return 0;
00547     }
00548 
00549     if (!net_interface || !net_interface[0] || !Q_stricmp(net_interface, "localhost"))
00550         address.sin_addr.s_addr = INADDR_ANY;
00551     else
00552         Sys_StringToSockaddr (net_interface, (struct sockaddr *)&address);
00553 
00554     if (port == PORT_ANY)
00555         address.sin_port = 0;
00556     else
00557         address.sin_port = htons((short)port);
00558 
00559     address.sin_family = AF_INET;
00560 
00561     if( bind (newsocket, (void *)&address, sizeof(address)) == -1)
00562     {
00563         Com_Printf ("ERROR: UDP_OpenSocket: bind: %s\n", NET_ErrorString());
00564         close (newsocket);
00565         return 0;
00566     }
00567 
00568     return newsocket;
00569 }

Here is the call graph for this function:

void NET_OpenIP void   ) 
 

Definition at line 474 of file unix_net.c.

References Com_Error(), Cvar_Get(), Cvar_SetValue(), cvar_t, ERR_FATAL, i, ip_socket, NET_GetLocalAddress(), NET_IPSocket(), PORT_SERVER, cvar_s::string, va(), and cvar_s::value.

Referenced by NET_Config(), and NET_Init().

00475 {
00476     cvar_t  *ip;
00477     int     port;
00478     int     i;
00479 
00480     ip = Cvar_Get ("net_ip", "localhost", 0);
00481 
00482     port = Cvar_Get("net_port", va("%i", PORT_SERVER), 0)->value;
00483 
00484     for ( i = 0 ; i < 10 ; i++ ) {
00485         ip_socket = NET_IPSocket (ip->string, port + i);
00486         if ( ip_socket ) {
00487             Cvar_SetValue( "net_port", port + i );
00488             NET_GetLocalAddress();
00489             return;
00490         }
00491     }
00492     Com_Error (ERR_FATAL, "Couldn't allocate IP port");
00493 }

Here is the call graph for this function:

void NET_Shutdown void   ) 
 

Definition at line 576 of file unix_net.c.

References close(), and ip_socket.

00577 {
00578     if (ip_socket) {
00579         close(ip_socket);
00580         ip_socket = 0;
00581     }
00582 }

Here is the call graph for this function:

void NET_Sleep int  msec  ) 
 

Definition at line 599 of file unix_net.c.

References com_dedicated, cvar_s::integer, ip_socket, NULL, and qboolean.

00600 {
00601     struct timeval timeout;
00602     fd_set  fdset;
00603     extern qboolean stdin_active;
00604 
00605     if (!ip_socket || !com_dedicated->integer)
00606         return; // we're not a server, just run full speed
00607 
00608     FD_ZERO(&fdset);
00609     if (stdin_active)
00610         FD_SET(0, &fdset); // stdin is processed too
00611     FD_SET(ip_socket, &fdset); // network socket
00612     timeout.tv_sec = msec/1000;
00613     timeout.tv_usec = (msec%1000)*1000;
00614     select(ip_socket+1, &fdset, NULL, NULL, &timeout);
00615 }

int NET_Socket char *  net_interface,
int  port
 

void NetadrToSockadr netadr_t a,
struct sockaddr_in *  s
 

Definition at line 64 of file unix_net.c.

References a, netadr_t::ip, memset(), netadr_t::port, s, and netadr_t::type.

00065 {
00066     memset (s, 0, sizeof(*s));
00067 
00068     if (a->type == NA_BROADCAST)
00069     {
00070         s->sin_family = AF_INET;
00071 
00072         s->sin_port = a->port;
00073         *(int *)&s->sin_addr = -1;
00074     }
00075     else if (a->type == NA_IP)
00076     {
00077         s->sin_family = AF_INET;
00078 
00079         *(int *)&s->sin_addr = *(int *)&a->ip;
00080         s->sin_port = a->port;
00081     }
00082 }

Here is the call graph for this function:

void SockadrToNetadr struct sockaddr_in *  s,
netadr_t a
 

Definition at line 84 of file unix_net.c.

References a, netadr_t::ip, netadr_t::port, s, and netadr_t::type.

00085 {
00086     *(int *)&a->ip = *(int *)&s->sin_addr;
00087     a->port = s->sin_port;
00088     a->type = NA_IP;
00089 }

qboolean Sys_GetPacket netadr_t net_from,
msg_t net_message
 

Definition at line 158 of file unix_net.c.

References Com_Printf(), msg_t::cursize, msg_t::data, err(), msg_t::maxsize, NET_AdrToString(), NET_ErrorString(), qboolean, msg_t::readcount, and SockadrToNetadr().

00159 {
00160     int     ret;
00161     struct sockaddr_in  from;
00162     int     fromlen;
00163     int     net_socket;
00164     int     protocol;
00165     int     err;
00166 
00167     for (protocol = 0 ; protocol < 2 ; protocol++)
00168     {
00169         if (protocol == 0)
00170             net_socket = ip_socket;
00171         else
00172             net_socket = ipx_socket;
00173 
00174         if (!net_socket)
00175             continue;
00176 
00177         fromlen = sizeof(from);
00178         ret = recvfrom (net_socket, net_message->data, net_message->maxsize
00179             , 0, (struct sockaddr *)&from, &fromlen);
00180 
00181         SockadrToNetadr (&from, net_from);
00182         // bk000305: was missing
00183         net_message->readcount = 0;
00184 
00185         if (ret == -1)
00186         {
00187             err = errno;
00188 
00189             if (err == EWOULDBLOCK || err == ECONNREFUSED)
00190                 continue;
00191             Com_Printf ("NET_GetPacket: %s from %s\n", NET_ErrorString(),
00192                         NET_AdrToString(*net_from));
00193             continue;
00194         }
00195 
00196         if (ret == net_message->maxsize)
00197         {
00198             Com_Printf ("Oversize packet from %s\n", NET_AdrToString (*net_from));
00199             continue;
00200         }
00201 
00202         net_message->cursize = ret;
00203         return qtrue;
00204     }
00205 
00206     return qfalse;
00207 }

Here is the call graph for this function:

qboolean Sys_IsLANAddress netadr_t  adr  ) 
 

Definition at line 261 of file unix_net.c.

References i, netadr_t::ip, localIP, qboolean, and netadr_t::type.

00261                                             {
00262     int     i;
00263 
00264     if( adr.type == NA_LOOPBACK ) {
00265         return qtrue;
00266     }
00267 
00268     if( adr.type == NA_IPX ) {
00269         return qtrue;
00270     }
00271 
00272     if( adr.type != NA_IP ) {
00273         return qfalse;
00274     }
00275 
00276     // choose which comparison to use based on the class of the address being tested
00277     // any local adresses of a different class than the address being tested will fail based on the first byte
00278 
00279     // Class A
00280     if( (adr.ip[0] & 0x80) == 0x00 ) {
00281         for ( i = 0 ; i < numIP ; i++ ) {
00282             if( adr.ip[0] == localIP[i][0] ) {
00283                 return qtrue;
00284             }
00285         }
00286         // the RFC1918 class a block will pass the above test
00287         return qfalse;
00288     }
00289 
00290     // Class B
00291     if( (adr.ip[0] & 0xc0) == 0x80 ) {
00292         for ( i = 0 ; i < numIP ; i++ ) {
00293             if( adr.ip[0] == localIP[i][0] && adr.ip[1] == localIP[i][1] ) {
00294                 return qtrue;
00295             }
00296             // also check against the RFC1918 class b blocks
00297             if( adr.ip[0] == 172 && localIP[i][0] == 172 && (adr.ip[1] & 0xf0) == 16 && (localIP[i][1] & 0xf0) == 16 ) {
00298                 return qtrue;
00299             }
00300         }
00301         return qfalse;
00302     }
00303 
00304     // Class C
00305     for ( i = 0 ; i < numIP ; i++ ) {
00306         if( adr.ip[0] == localIP[i][0] && adr.ip[1] == localIP[i][1] && adr.ip[2] == localIP[i][2] ) {
00307             return qtrue;
00308         }
00309         // also check against the RFC1918 class c blocks
00310         if( adr.ip[0] == 192 && localIP[i][0] == 192 && adr.ip[1] == 168 && localIP[i][1] == 168 ) {
00311             return qtrue;
00312         }
00313     }
00314     return qfalse;
00315 }

void Sys_SendPacket int  length,
const void *  data,
netadr_t  to
 

Definition at line 211 of file unix_net.c.

References Com_Error(), Com_Printf(), data, ERR_FATAL, length(), NET_AdrToString(), NET_ErrorString(), NetadrToSockadr(), and netadr_t::type.

00212 {
00213     int     ret;
00214     struct sockaddr_in  addr;
00215     int     net_socket;
00216 
00217     if (to.type == NA_BROADCAST)
00218     {
00219         net_socket = ip_socket;
00220     }
00221     else if (to.type == NA_IP)
00222     {
00223         net_socket = ip_socket;
00224     }
00225     else if (to.type == NA_IPX)
00226     {
00227         net_socket = ipx_socket;
00228     }
00229     else if (to.type == NA_BROADCAST_IPX)
00230     {
00231         net_socket = ipx_socket;
00232     }
00233     else {
00234         Com_Error (ERR_FATAL, "NET_SendPacket: bad address type");
00235         return;
00236     }
00237 
00238     if (!net_socket)
00239         return;
00240 
00241     NetadrToSockadr (&to, &addr);
00242 
00243     ret = sendto (net_socket, data, length, 0, (struct sockaddr *)&addr, sizeof(addr) );
00244     if (ret == -1)
00245     {
00246         Com_Printf ("NET_SendPacket ERROR: %s to %s\n", NET_ErrorString(),
00247                 NET_AdrToString (to));
00248     }
00249 }

Here is the call graph for this function:

void Sys_ShowIP void   ) 
 

Definition at line 322 of file unix_net.c.

References Com_Printf(), i, and localIP.

00322                       {
00323     int i;
00324 
00325     for (i = 0; i < numIP; i++) {
00326         Com_Printf( "IP: %i.%i.%i.%i\n", localIP[i][0], localIP[i][1], localIP[i][2], localIP[i][3] );
00327     }
00328 }

Here is the call graph for this function:

qboolean Sys_StringToAdr const char *  s,
netadr_t a
 

Definition at line 143 of file unix_net.c.

References a, qboolean, s, SockadrToNetadr(), and Sys_StringToSockaddr().

00144 {
00145     struct sockaddr_in sadr;
00146     
00147     if (!Sys_StringToSockaddr (s, (struct sockaddr *)&sadr))
00148         return qfalse;
00149     
00150     SockadrToNetadr (&sadr, a);
00151 
00152     return qtrue;
00153 }

Here is the call graph for this function:

qboolean Sys_StringToSockaddr const char *  s,
struct sockaddr *  sadr
 

Definition at line 108 of file unix_net.c.

References h(), memset(), qboolean, and s.

00109 {
00110     struct hostent  *h;
00111     //char  *colon; // bk001204 - unused
00112     
00113     memset (sadr, 0, sizeof(*sadr));
00114     ((struct sockaddr_in *)sadr)->sin_family = AF_INET;
00115     
00116     ((struct sockaddr_in *)sadr)->sin_port = 0;
00117     
00118     if ( s[0] >= '0' && s[0] <= '9')
00119     {
00120         *(int *)&((struct sockaddr_in *)sadr)->sin_addr = inet_addr(s);
00121     }
00122     else
00123     {
00124         if (! (h = gethostbyname(s)) )
00125             return qfalse;
00126         *(int *)&((struct sockaddr_in *)sadr)->sin_addr = *(int *)h->h_addr_list[0];
00127     }
00128     
00129     return qtrue;
00130 }

Here is the call graph for this function:


Variable Documentation

int ip_socket
 

Definition at line 52 of file unix_net.c.

Referenced by NET_Config(), NET_OpenIP(), NET_Shutdown(), NET_Sleep(), and Sys_GetPacket().

int ipx_socket
 

Definition at line 53 of file unix_net.c.

Referenced by NET_Config(), and NET_OpenIPX().

byte localIP[MAX_IPS][4] [static]
 

Definition at line 57 of file unix_net.c.

Referenced by NET_GetLocalAddress(), Sys_IsLANAddress(), and Sys_ShowIP().

netadr_t net_local_adr
 

Definition at line 50 of file unix_net.c.

cvar_t* noudp [static]
 

Definition at line 48 of file unix_net.c.

Referenced by NET_Init().

int numIP [static]
 

Definition at line 56 of file unix_net.c.

Referenced by NET_GetLocalAddress().


Generated on Thu Aug 25 15:39:12 2005 for Quake III Arena by  doxygen 1.3.9.1