/* * SV_SendClientsFragments */ bool SV_SendClientsFragments( void ) { client_t *client; int i; bool sent = false; // send a message to each connected client for( i = 0, client = svs.clients; i < sv_maxclients->integer; i++, client++ ) { if( client->state == CS_FREE || client->state == CS_ZOMBIE ) continue; if( client->edict && ( client->edict->r.svflags & SVF_FAKECLIENT ) ) continue; if( !client->netchan.unsentFragments ) continue; if( !Netchan_TransmitNextFragment( &client->netchan ) ) { Com_Printf( "Error sending fragment to %s: %s\n", NET_AddressToString( &client->netchan.remoteAddress ), NET_ErrorString() ); if( client->reliable ) SV_DropClient( client, DROP_TYPE_GENERAL, "Error sending fragment: %s\n", NET_ErrorString() ); continue; } sent = true; } return sent; }
/* * TV_Relay_SendClientMessages */ void TV_Relay_SendClientMessages( relay_t *relay ) { int i; client_t *client; assert( relay ); // send a message to each connected client for( i = 0, client = tvs.clients; i < tv_maxclients->integer; i++, client++ ) { if( client->state != CS_SPAWNED ) continue; if( client->relay != relay ) continue; if( !TV_Relay_SendClientDatagram( relay, client ) ) { Com_Printf( "%s" S_COLOR_WHITE ": Error sending message: %s\n", client->name, NET_ErrorString() ); if( client->reliable ) { TV_Downstream_DropClient( client, DROP_TYPE_GENERAL, "Error sending message: %s\n", NET_ErrorString() ); } } } }
/* * TV_Downstream_SendClientsFragments */ qboolean TV_Downstream_SendClientsFragments( void ) { client_t *client; int i; qboolean remaining = qfalse; // send a message to each connected client for( i = 0, client = tvs.clients; i < tv_maxclients->integer; i++, client++ ) { if( client->state == CS_FREE || client->state == CS_ZOMBIE ) continue; if( !client->netchan.unsentFragments ) continue; if( !Netchan_TransmitNextFragment( &client->netchan ) ) { Com_Printf( "%s" S_COLOR_WHITE ": Error sending fragment: %s\n", client->name, NET_ErrorString() ); if( client->reliable ) { TV_Downstream_DropClient( client, DROP_TYPE_GENERAL, "Error sending fragment: %s\n", NET_ErrorString() ); } continue; } if( client->netchan.unsentFragments ) remaining = qtrue; } return remaining; }
/* ==================== NET_Socket ==================== */ int NET_IPSocket (char *net_interface, int port) { int newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); if (newsocket == -1) { Com_Printf ("UDP_OpenSocket: Couldn't make socket: %s\n", LOG_NET, NET_ErrorString()); return 0; } if (newsocket >= FD_SETSIZE) Com_Error (ERR_FATAL, "NET_IPSocket: socket is higher than FD_SETSIZE"); // make it non-blocking qboolean _true = true; if (ioctl (newsocket, FIONBIO, &_true) == -1) { Com_Printf ("UDP_OpenSocket: Couldn't make non-blocking: %s\n", LOG_NET, NET_ErrorString()); return 0; } // make it broadcast capable int i = 1; if (setsockopt(newsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) == -1) { Com_Printf ("UDP_OpenSocket: Couldn't set SO_BROADCAST: %s\n", LOG_NET, NET_ErrorString()); return 0; } // r1: accept icmp unreachables for quick disconnects if (!net_no_recverr->intvalue) { if (setsockopt (newsocket, IPPROTO_IP, IP_RECVERR, (char *)&i, sizeof(i)) == -1) { Com_Printf ("UDP_OpenSocket: Couldn't set IP_RECVERR: %s\n", LOG_NET, NET_ErrorString()); } } struct sockaddr_in address; if (!net_interface || !net_interface[0] || !Q_stricmp(net_interface, "localhost")) address.sin_addr.s_addr = INADDR_ANY; else NET_StringToSockaddr (net_interface, (struct sockaddr *)&address); if (port == PORT_ANY) address.sin_port = 0; else address.sin_port = htons((uint16)port); address.sin_family = AF_INET; if( bind (newsocket, (struct sockaddr *) &address, sizeof(address)) == -1) { close (newsocket); Com_Printf ("UDP_OpenSocket: Couldn't bind to UDP port %d: %s\n", LOG_NET, port, NET_ErrorString()); return 0; } return newsocket; }
/* ==================== NET_IPSocket ==================== */ int NET_IPSocket( char *net_interface, int port ) { SOCKET newsocket; struct sockaddr_in address; //bani - was improper parameter unsigned long _true = qtrue; int i = 1; int err; if ( net_interface ) { Com_Printf( "Opening IP socket: %s:%i\n", net_interface, port ); } else { Com_Printf( "Opening IP socket: localhost:%i\n", port ); } if ( ( newsocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) == INVALID_SOCKET ) { err = WSAGetLastError(); if ( err != WSAEAFNOSUPPORT ) { Com_Printf( "WARNING: UDP_OpenSocket: socket: %s\n", NET_ErrorString() ); } return 0; } // make it non-blocking if ( ioctlsocket( newsocket, FIONBIO, &_true ) == SOCKET_ERROR ) { Com_Printf( "WARNING: UDP_OpenSocket: ioctl FIONBIO: %s\n", NET_ErrorString() ); return 0; } // make it broadcast capable if ( setsockopt( newsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof( i ) ) == SOCKET_ERROR ) { Com_Printf( "WARNING: UDP_OpenSocket: setsockopt SO_BROADCAST: %s\n", NET_ErrorString() ); return 0; } if ( !net_interface || !net_interface[0] || !Q_stricmp( net_interface, "localhost" ) ) { address.sin_addr.s_addr = INADDR_ANY; } else { Sys_StringToSockaddr( net_interface, (struct sockaddr *)&address ); } if ( port == PORT_ANY ) { address.sin_port = 0; } else { address.sin_port = htons( (short)port ); } address.sin_family = AF_INET; if ( bind( newsocket, (void *)&address, sizeof( address ) ) == SOCKET_ERROR ) { Com_Printf( "WARNING: UDP_OpenSocket: bind: %s\n", NET_ErrorString() ); closesocket( newsocket ); return 0; } //bani NET_GetInterfaces(); return newsocket; }
/* ================== NET_UDPSocket ================== */ static bool NET_UDPSocket (netSrc_t sock, const char *ip, int port){ struct sockaddr_in adr; ulong value = 1; Com_DPrintf("NET_UDPSocket( %s, %i )\n", ip, port); // Open an UDP socket if ((net.sockets[sock] = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET){ Com_DPrintf(S_COLOR_YELLOW "NET_OpenSocket: socket() = %s\n", NET_ErrorString()); return false; } // Make it non-blocking if (ioctlsocket(net.sockets[sock], FIONBIO, &value) == SOCKET_ERROR){ Com_DPrintf(S_COLOR_YELLOW "NET_OpenSocket: ioctlsocket() = %s\n", NET_ErrorString()); closesocket(net.sockets[sock]); net.sockets[sock] = INVALID_SOCKET; return false; } // Make it broadcast capable if (setsockopt(net.sockets[sock], SOL_SOCKET, SO_BROADCAST, (const char *)&value, sizeof(value)) == SOCKET_ERROR){ Com_DPrintf(S_COLOR_YELLOW "NET_OpenSocket: setsockopt() = %s\n", NET_ErrorString()); closesocket(net.sockets[sock]); net.sockets[sock] = INVALID_SOCKET; return false; } // Bind it adr.sin_family = AF_INET; if (!Str_ICompare(ip, "localhost")) adr.sin_addr.s_addr = INADDR_ANY; else { if (!NET_StringToSockAdr(ip, (struct sockaddr *)&adr)) adr.sin_addr.s_addr = INADDR_ANY; } if (port == 0) adr.sin_port = 0; else adr.sin_port = htons((ushort)port); if (bind(net.sockets[sock], (void *)&adr, sizeof(adr)) == SOCKET_ERROR){ Com_DPrintf(S_COLOR_YELLOW "NET_OpenSocket: bind() = %s\n", NET_ErrorString()); closesocket(net.sockets[sock]); net.sockets[sock] = INVALID_SOCKET; return false; } return true; }
int NET_IPXSocket(int port) { int newsocket; struct sockaddr_ipx address; unsigned long t = 1; if ((newsocket = socket(PF_IPX, SOCK_DGRAM, NSPROTO_IPX)) == -1) { int err; err = WSAGetLastError(); if (err != WSAEAFNOSUPPORT) { Com_Printf("WARNING: IPX_Socket: socket: %s\n", NET_ErrorString()); } return 0; } /* make it non-blocking */ if (ioctlsocket(newsocket, FIONBIO, &t) == -1) { Com_Printf("WARNING: IPX_Socket: ioctl FIONBIO: %s\n", NET_ErrorString()); return 0; } /* make it broadcast capable */ if (setsockopt(newsocket, SOL_SOCKET, SO_BROADCAST, (char *)&t, sizeof(t)) == -1) { Com_Printf("WARNING: IPX_Socket: setsockopt SO_BROADCAST: %s\n", NET_ErrorString()); return 0; } address.sa_family = AF_IPX; memset(address.sa_netnum, 0, 4); memset(address.sa_nodenum, 0, 6); if (port == PORT_ANY) { address.sa_socket = 0; } else { address.sa_socket = htons((short)port); } if (bind(newsocket, (void *)&address, sizeof(address)) == -1) { Com_Printf("WARNING: IPX_Socket: bind: %s\n", NET_ErrorString()); closesocket(newsocket); return 0; } return newsocket; }
/* ==================== NET_IPSocket ==================== */ int NET_IPSocket(char *net_interface, int port, int *err) { SOCKET newsocket; struct sockaddr_in address; u_long _true = 1; int i = 1; *err = 0; if (net_interface) { Com_Printf("Opening IP socket: %s:%i\n", net_interface, port); } else { Com_Printf("Opening IP socket: 0.0.0.0:%i\n", port); } if ((newsocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) { *err = socketError; Com_Printf("WARNING: NET_IPSocket: socket: %s\n", NET_ErrorString()); return newsocket; } // make it non-blocking if (ioctlsocket(newsocket, FIONBIO, &_true) == SOCKET_ERROR) { Com_Printf("WARNING: NET_IPSocket: ioctl FIONBIO: %s\n", NET_ErrorString()); *err = socketError; closesocket(newsocket); return INVALID_SOCKET; } // make it broadcast capable if (setsockopt(newsocket, SOL_SOCKET, SO_BROADCAST, (char *) & i, sizeof (i)) == SOCKET_ERROR) { Com_Printf("WARNING: NET_IPSocket: setsockopt SO_BROADCAST: %s\n", NET_ErrorString()); } if (!net_interface || !net_interface[0]) { address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; } else { if (!Sys_StringToSockaddr(net_interface, (struct sockaddr *) & address, sizeof (address), AF_INET)) { closesocket(newsocket); return INVALID_SOCKET; } } if (port == PORT_ANY) { address.sin_port = 0; } else { address.sin_port = htons((short) port); } if (bind(newsocket, (void *) & address, sizeof (address)) == SOCKET_ERROR) { Com_Printf("WARNING: NET_IPSocket: bind: %s\n", NET_ErrorString()); *err = socketError; closesocket(newsocket); return INVALID_SOCKET; } return newsocket; }
SOCKET NET_IPSocket( const char *net_interface, int port ) { SOCKET newsocket; struct sockaddr_in address; u_long _true = 1; int i = 1; if( !net_interface || !net_interface[0] ) { net_interface = "localhost"; } Com_Printf( "Opening IP socket: %s:%i\n", net_interface, port ); if( ( newsocket = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) == INVALID_SOCKET ) { #ifdef _WIN32 if( WSAGetLastError() != WSAEAFNOSUPPORT ) #endif Com_Printf ("WARNING: UDP_OpenSocket: socket: %s", NET_ErrorString() ); return INVALID_SOCKET; } // make it non-blocking if( ioctlsocket( newsocket, FIONBIO, &_true ) == SOCKET_ERROR ) { Com_Printf ("WARNING: UDP_OpenSocket: ioctl FIONBIO: %s\n", NET_ErrorString() ); closesocket( newsocket ); return INVALID_SOCKET; } // make it broadcast capable if( setsockopt( newsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof( i ) ) == SOCKET_ERROR ) { Com_Printf ("WARNING: UDP_OpenSocket: setsockopt SO_BROADCAST: %s\n", NET_ErrorString() ); closesocket( newsocket ); return INVALID_SOCKET; } if( !Q_stricmp( net_interface, "localhost" ) ) { address.sin_addr.s_addr = INADDR_ANY; } else { NET_StringToSockaddr( net_interface, (struct sockaddr *)&address ); } if( port == PORT_ANY ) { address.sin_port = 0; } else { address.sin_port = htons( (u_short)port ); } address.sin_family = AF_INET; if( bind( newsocket, (void *)&address, sizeof( address ) ) == SOCKET_ERROR ) { Com_Printf ("WARNING: UDP_OpenSocket: bind: %s\n", NET_ErrorString() ); closesocket( newsocket ); return INVALID_SOCKET; } return newsocket; }
/* ==================== NET_IPXSocket ==================== */ int NET_IPXSocket(int port) { SOCKET newsocket; struct sockaddr_ipx address; //bani - was improper parameter unsigned long _true = qtrue; int err; if((newsocket = socket(AF_IPX, SOCK_DGRAM, NSPROTO_IPX)) == INVALID_SOCKET) { err = WSAGetLastError(); if(err != WSAEAFNOSUPPORT) { Com_Printf("WARNING: IPX_Socket: socket: %s\n", NET_ErrorString()); } return 0; } // make it non-blocking if(ioctlsocket(newsocket, FIONBIO, &_true) == SOCKET_ERROR) { Com_Printf("WARNING: IPX_Socket: ioctl FIONBIO: %s\n", NET_ErrorString()); return 0; } // make it broadcast capable if(setsockopt(newsocket, SOL_SOCKET, SO_BROADCAST, (char *)&_true, sizeof(_true)) == SOCKET_ERROR) { Com_Printf("WARNING: IPX_Socket: setsockopt SO_BROADCAST: %s\n", NET_ErrorString()); return 0; } address.sa_family = AF_IPX; memset(address.sa_netnum, 0, 4); memset(address.sa_nodenum, 0, 6); if(port == PORT_ANY) { address.sa_socket = 0; } else { address.sa_socket = htons((short)port); } if(bind(newsocket, (void *)&address, sizeof(address)) == SOCKET_ERROR) { Com_Printf("WARNING: IPX_Socket: bind: %s\n", NET_ErrorString()); closesocket(newsocket); return 0; } return newsocket; }
/* * SV_SendClientMessages */ void SV_SendClientMessages( void ) { int i; client_t *client; // send a message to each connected client for( i = 0, client = svs.clients; i < sv_maxclients->integer; i++, client++ ) { if( client->state == CS_FREE || client->state == CS_ZOMBIE ) continue; if( client->edict && ( client->edict->r.svflags & SVF_FAKECLIENT ) ) { client->lastSentFrameNum = sv.framenum; continue; } if( !client->tvclient ) { SV_UpdateActivity(); } if( client->state == CS_SPAWNED ) { if( !SV_SendClientDatagram( client ) ) { Com_Printf( "Error sending message to %s: %s\n", client->name, NET_ErrorString() ); if( client->reliable ) { SV_DropClient( client, DROP_TYPE_GENERAL, "Error sending message: %s\n", NET_ErrorString() ); } } } else { // send pending reliable commands, or send heartbeats for not timing out if( client->reliableSequence > client->reliableAcknowledge || svs.realtime - client->lastPacketSentTime > 1000 ) { SV_InitClientMessage( client, &tmpMessage, NULL, 0 ); SV_AddReliableCommandsToMessage( client, &tmpMessage ); if( !SV_SendMessageToClient( client, &tmpMessage ) ) { Com_Printf( "Error sending message to %s: %s\n", client->name, NET_ErrorString() ); if( client->reliable ) { SV_DropClient( client, DROP_TYPE_GENERAL, "Error sending message: %s\n", NET_ErrorString() ); } } } } } }
/* ================== idTCP::Init ================== */ bool idTCP::Init( const char *host, short port ) { unsigned long _true = 1; struct sockaddr sadr; if ( !Sys_StringToNetAdr( host, &address, true ) ) { common->Printf( "Couldn't resolve server name \"%s\"\n", host ); return false; } address.type = NA_IP; if ( !address.port ) { address.port = port; } common->Printf( "\"%s\" resolved to %i.%i.%i.%i:%i\n", host, address.ip[0], address.ip[1], address.ip[2], address.ip[3], address.port ); Net_NetadrToSockadr( &address, &sadr ); if ( fd ) { common->Warning( "idTCP::Init: already initialized?" ); } if ( ( fd = socket( AF_INET, SOCK_STREAM, 0 ) ) == INVALID_SOCKET ) { fd = 0; common->Printf( "ERROR: idTCP::Init: socket: %s\n", NET_ErrorString() ); return false; } if ( connect( fd, &sadr, sizeof(sadr)) == SOCKET_ERROR ) { common->Printf( "ERROR: idTCP::Init: connect: %s\n", NET_ErrorString() ); closesocket( fd ); fd = 0; return false; } // make it non-blocking if( ioctlsocket( fd, FIONBIO, &_true ) == SOCKET_ERROR ) { common->Printf( "ERROR: idTCP::Init: ioctl FIONBIO: %s\n", NET_ErrorString() ); closesocket( fd ); fd = 0; return false; } common->DPrintf( "Opened TCP connection\n" ); return true; }
/* ==================== NET_JoinMulticast Join an ipv6 multicast group ==================== */ void NET_JoinMulticast6(void) { int err; if (ip6_socket == INVALID_SOCKET || multicast6_socket != INVALID_SOCKET || (net_enabled->integer & NET_DISABLEMCAST)) { return; } if (IN6_IS_ADDR_MULTICAST(&boundto.sin6_addr) || IN6_IS_ADDR_UNSPECIFIED(&boundto.sin6_addr)) { // The way the socket was bound does not prohibit receiving multi-cast packets. So we don't need to open a new one. multicast6_socket = ip6_socket; } else { if ((multicast6_socket = NET_IP6Socket(net_mcast6addr->string, ntohs(boundto.sin6_port), NULL, &err)) == INVALID_SOCKET) { // If the OS does not support binding to multicast addresses, like WinXP, at least try with the normal file descriptor. multicast6_socket = ip6_socket; } } if (curgroup.ipv6mr_interface) { if (setsockopt(multicast6_socket, IPPROTO_IPV6, IPV6_MULTICAST_IF, (char *) &curgroup.ipv6mr_interface, sizeof(curgroup.ipv6mr_interface)) < 0) { Com_Printf("NET_JoinMulticast6: Couldn't set scope on multicast socket: %s\n", NET_ErrorString()); if (multicast6_socket != ip6_socket) { closesocket(multicast6_socket); multicast6_socket = INVALID_SOCKET; return; } } } if (setsockopt(multicast6_socket, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *) &curgroup, sizeof(curgroup))) { Com_Printf("NET_JoinMulticast6: Couldn't join multicast group: %s\n", NET_ErrorString()); if (multicast6_socket != ip6_socket) { closesocket(multicast6_socket); multicast6_socket = INVALID_SOCKET; return; } } }
/* ==================== NET_Socket ==================== */ int NET_IPSocket (char *net_interface, int port) { int newsocket; struct sockaddr_in address; u_long _true = true; int i = 1; int err; if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { err = WSAGetLastError(); if (err != WSAEAFNOSUPPORT) Com_Printf ("WARNING: UDP_OpenSocket: socket: %s", NET_ErrorString()); return 0; } // make it non-blocking if (ioctlsocket (newsocket, FIONBIO, &_true) == -1) { Com_Printf ("WARNING: UDP_OpenSocket: ioctl FIONBIO: %s\n", NET_ErrorString()); return 0; } // make it broadcast capable if (setsockopt(newsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) == -1) { Com_Printf ("WARNING: UDP_OpenSocket: setsockopt SO_BROADCAST: %s\n", NET_ErrorString()); return 0; } if (!net_interface || !net_interface[0] || !_stricmp(net_interface, "localhost")) address.sin_addr.s_addr = INADDR_ANY; else NET_StringToSockaddr (net_interface, (struct sockaddr *)&address); if (port == PORT_ANY) address.sin_port = 0; else address.sin_port = htons((short)port); address.sin_family = AF_INET; if( bind (newsocket, (void *)&address, sizeof(address)) == -1) { Com_Printf ("WARNING: UDP_OpenSocket: bind: %s\n", NET_ErrorString()); closesocket (newsocket); return 0; } return newsocket; }
/* ==================== NET_IPXSocket ==================== */ int NET_IPXSocket( int port ) { #ifdef _XBOX assert(!"NET_IPXSocket() - Not supported"); return INVALID_SOCKET; #else SOCKET newsocket; struct sockaddr_ipx address; int _true = 1; int err; if( ( newsocket = socket( AF_IPX, SOCK_DGRAM, NSPROTO_IPX ) ) == INVALID_SOCKET ) { err = WSAGetLastError(); if (err != WSAEAFNOSUPPORT) { Com_Printf( "WARNING: IPX_Socket: socket: %s\n", NET_ErrorString() ); } return INVALID_SOCKET; } // make it non-blocking if( ioctlsocket( newsocket, FIONBIO, (unsigned long *)&_true ) == SOCKET_ERROR ) { Com_Printf( "WARNING: IPX_Socket: ioctl FIONBIO: %s\n", NET_ErrorString() ); return INVALID_SOCKET; } // make it broadcast capable if( setsockopt( newsocket, SOL_SOCKET, SO_BROADCAST, (char *)&_true, sizeof( _true ) ) == SOCKET_ERROR ) { Com_Printf( "WARNING: IPX_Socket: setsockopt SO_BROADCAST: %s\n", NET_ErrorString() ); return INVALID_SOCKET; } address.sa_family = AF_IPX; memset( address.sa_netnum, 0, 4 ); memset( address.sa_nodenum, 0, 6 ); if( port == PORT_ANY ) { address.sa_socket = 0; } else { address.sa_socket = htons( (short)port ); } if( bind( newsocket, (const struct sockaddr *)&address, sizeof(address) ) == SOCKET_ERROR ) { Com_Printf( "WARNING: IPX_Socket: bind: %s\n", NET_ErrorString() ); closesocket( newsocket ); return INVALID_SOCKET; } return newsocket; #endif }
/* <???> ../engine/net_ws.c */ int NET_IPXSocket(int hostshort) { int err; u_long optval = 1; SOCKET newsocket; SOCKADDR_IPX address; if((newsocket = CRehldsPlatformHolder::get()->socket(PF_IPX, SOCK_DGRAM, NSPROTO_IPX)) == INVALID_SOCKET) { err = CRehldsPlatformHolder::get()->WSAGetLastError(); if (err != WSAEAFNOSUPPORT) Con_Printf("WARNING: IPX_Socket: port: %d socket: %s\n", hostshort, NET_ErrorString(err)); return 0; } if (CRehldsPlatformHolder::get()->ioctlsocket(newsocket, FIONBIO, &optval) == SOCKET_ERROR) { err = CRehldsPlatformHolder::get()->WSAGetLastError(); Con_Printf("WARNING: IPX_Socket: port: %d ioctl FIONBIO: %s\n", hostshort, NET_ErrorString(err)); return 0; } if (CRehldsPlatformHolder::get()->setsockopt(newsocket, SOL_SOCKET, SO_BROADCAST, (const char *)&optval, sizeof(optval)) == SOCKET_ERROR) { err = CRehldsPlatformHolder::get()->WSAGetLastError(); Con_Printf("WARNING: IPX_Socket: port: %d setsockopt SO_BROADCAST: %s\n", hostshort, NET_ErrorString(err)); return 0; } if (CRehldsPlatformHolder::get()->setsockopt(newsocket, SOL_SOCKET, 4, (const char *)&optval, sizeof(optval)) == SOCKET_ERROR) { err = CRehldsPlatformHolder::get()->WSAGetLastError(); return 0; } address.sa_family = AF_IPX; Q_memset(address.sa_netnum, 0, 4); Q_memset(address.sa_nodenum, 0, 6); if (hostshort == -1) address.sa_socket = 0; else address.sa_socket = htons((u_short)hostshort); if (CRehldsPlatformHolder::get()->bind(newsocket, (struct sockaddr *)&address, sizeof(SOCKADDR_IPX)) == SOCKET_ERROR) { err = CRehldsPlatformHolder::get()->WSAGetLastError(); Con_Printf("WARNING: IPX_Socket: port: %d bind: %s\n", hostshort, NET_ErrorString(err)); CRehldsPlatformHolder::get()->closesocket(newsocket); return 0; } return newsocket; }
/* ================== idTCP::Read ================== */ int idTCP::Read( void *data, int size ) { int nbytes; if ( !fd ) { common->Printf("idTCP::Read: not initialized\n"); return -1; } if ( ( nbytes = recv( fd, (char *)data, size, 0 ) ) == SOCKET_ERROR ) { if ( WSAGetLastError() == WSAEWOULDBLOCK ) { return 0; } common->Printf( "ERROR: idTCP::Read: %s\n", NET_ErrorString() ); Close(); return -1; } // a successful read of 0 bytes indicates remote has closed the connection if ( nbytes == 0 ) { common->DPrintf( "idTCP::Read: read 0 bytes - assume connection closed\n" ); return -1; } return nbytes; }
/* ================== NET_SendPacket ================== */ void NET_SendPacket (netSrc_t sock, const netAdr_t to, const void *data, int length){ struct sockaddr adr; int result, error; if (NET_SendLoopPacket(sock, to, data, length)) return; if (to.type != NA_BROADCAST && to.type != NA_IP) Com_Error(ERR_FATAL, "NET_SendPacket: bad address type"); if (net.sockets[sock] == INVALID_SOCKET) return; NET_NetAdrToSockAdr(&to, &adr); result = sendto(net.sockets[sock], (const char *)data, length, 0, &adr, sizeof(adr)); if (result == SOCKET_ERROR){ error = WSAGetLastError(); // WSAEWOULDBLOCK is silent if (error == WSAEWOULDBLOCK) return; // Some PPP links don't allow broadcasts if (error == WSAEADDRNOTAVAIL && to.type == NA_BROADCAST) return; Com_Printf(S_COLOR_RED "NET_SendPacket: %s to %s\n", NET_ErrorString(), NET_AddressToString(to)); } net.packetsSent[sock]++; net.bytesSent[sock] += result; }
/* ================== Sys_SendPacket ================== */ void Sys_SendPacket( int length, const void* data, netadr_t to ) { int ret = SOCKET_ERROR; struct sockaddr_storage addr; if ( to.type != NA_BROADCAST && to.type != NA_IP && to.type != NA_IP6 && to.type != NA_MULTICAST6 ) { Com_Error( ERR_FATAL, "Sys_SendPacket: bad address type" ); return; } if ( ( ip_socket == INVALID_SOCKET && to.type == NA_IP ) || ( ip_socket == INVALID_SOCKET && to.type == NA_BROADCAST ) || ( ip6_socket == INVALID_SOCKET && to.type == NA_IP6 ) || ( ip6_socket == INVALID_SOCKET && to.type == NA_MULTICAST6 ) ) return; if ( to.type == NA_MULTICAST6 && ( net_enabled->integer & NET_DISABLEMCAST ) ) return; memset( &addr, 0, sizeof( addr ) ); NetadrToSockadr( &to, ( struct sockaddr* ) &addr ); if ( usingSocks && to.type == NA_IP ) { socksBuf[0] = 0; // reserved socksBuf[1] = 0; socksBuf[2] = 0; // fragment (not fragmented) socksBuf[3] = 1; // address type: IPV4 *( int* )&socksBuf[4] = ( ( struct sockaddr_in* )&addr )->sin_addr.s_addr; *( short* )&socksBuf[8] = ( ( struct sockaddr_in* )&addr )->sin_port; memcpy( &socksBuf[10], data, length ); ret = sendto( ip_socket, socksBuf, length + 10, 0, &socksRelayAddr, sizeof( socksRelayAddr ) ); } else { if ( addr.ss_family == AF_INET ) ret = sendto( ip_socket, ( const char* )data, length, 0, ( struct sockaddr* ) &addr, sizeof( struct sockaddr_in ) ); else if ( addr.ss_family == AF_INET6 ) ret = sendto( ip6_socket, ( const char* )data, length, 0, ( struct sockaddr* ) &addr, sizeof( struct sockaddr_in6 ) ); } if ( ret == SOCKET_ERROR ) { int err = socketError; // wouldblock is silent if ( err == EAGAIN ) { return; } // some PPP links do not allow broadcasts and return an error if ( ( err == EADDRNOTAVAIL ) && ( ( to.type == NA_BROADCAST ) ) ) { return; } Com_Printf( "Sys_SendPacket: %s\n", NET_ErrorString() ); } }
static void NET_GetLocalAddress( void ) { struct ifaddrs *ifap, *search; numIP = 0; if ( getifaddrs( &ifap ) ) { Com_Printf( "NET_GetLocalAddress: Unable to get list of network interfaces: %s\n", NET_ErrorString() ); } else { for ( search = ifap; search; search = search->ifa_next ) { // Only add interfaces that are up. if ( ifap->ifa_flags & IFF_UP ) { NET_AddLocalAddress( search->ifa_name, search->ifa_addr, search->ifa_netmask ); } } freeifaddrs( ifap ); Sys_ShowIP(); } }
/* ================== Sys_SendPacket ================== */ void Sys_SendPacket( int length, const void *data, netadr_t to ) { int ret; struct sockaddr addr; SOCKET net_socket; if( to.type == NA_BROADCAST ) { net_socket = ip_socket; } else if( to.type == NA_IP ) { net_socket = ip_socket; } else if( to.type == NA_IPX ) { net_socket = ipx_socket; } else if( to.type == NA_BROADCAST_IPX ) { net_socket = ipx_socket; } else { Com_Error( ERR_FATAL, "Sys_SendPacket: bad address type" ); return; } if( !net_socket ) { return; } NetadrToSockadr( &to, &addr ); if( usingSocks && to.type == NA_IP ) { socksBuf[0] = 0; // reserved socksBuf[1] = 0; socksBuf[2] = 0; // fragment (not fragmented) socksBuf[3] = 1; // address type: IPV4 *(int *)&socksBuf[4] = ((struct sockaddr_in *)&addr)->sin_addr.s_addr; *(short *)&socksBuf[8] = ((struct sockaddr_in *)&addr)->sin_port; memcpy( &socksBuf[10], data, length ); ret = sendto( net_socket, socksBuf, length+10, 0, &socksRelayAddr, sizeof(socksRelayAddr) ); } else { ret = sendto( net_socket, (const char *)data, length, 0, &addr, sizeof(addr) ); } if( ret == SOCKET_ERROR ) { int err = WSAGetLastError(); // wouldblock is silent if( err == WSAEWOULDBLOCK ) { return; } // some PPP links do not allow broadcasts and return an error if( ( err == WSAEADDRNOTAVAIL ) && ( ( to.type == NA_BROADCAST ) || ( to.type == NA_BROADCAST_IPX ) ) ) { return; } Com_Printf( "NET_SendPacket: %s\n", NET_ErrorString() ); } }
/* ================== NET_CloseSocket ================== */ static void NET_CloseSocket (netSrc_t sock){ if (net.sockets[sock] == INVALID_SOCKET) return; if (closesocket(net.sockets[sock]) == SOCKET_ERROR) Com_DPrintf(S_COLOR_YELLOW "NET_CloseSocket: closesocket() = %s\n", NET_ErrorString()); net.sockets[sock] = INVALID_SOCKET; }
/* ================== Net_GetUDPPacket ================== */ bool Net_GetUDPPacket( int netSocket, netadr_t &net_from, char *data, int &size, int maxSize ) { int ret; struct sockaddr from; int fromlen; int err; if( !netSocket ) { return false; } fromlen = sizeof(from); ret = recvfrom( netSocket, data, maxSize, 0, (struct sockaddr *)&from, &fromlen ); if ( ret == SOCKET_ERROR ) { err = WSAGetLastError(); if( err == WSAEWOULDBLOCK || err == WSAECONNRESET ) { return false; } char buf[1024]; sprintf( buf, "Net_GetUDPPacket: %s\n", NET_ErrorString() ); OutputDebugString( buf ); return false; } if ( netSocket == ip_socket ) { memset( ((struct sockaddr_in *)&from)->sin_zero, 0, 8 ); } if ( usingSocks && netSocket == ip_socket && memcmp( &from, &socksRelayAddr, fromlen ) == 0 ) { if ( ret < 10 || data[0] != 0 || data[1] != 0 || data[2] != 0 || data[3] != 1 ) { return false; } net_from.type = NA_IP; net_from.ip[0] = data[4]; net_from.ip[1] = data[5]; net_from.ip[2] = data[6]; net_from.ip[3] = data[7]; net_from.port = *(short *)&data[8]; memmove( data, &data[10], ret - 10 ); } else { Net_SockadrToNetadr( &from, &net_from ); } if( ret == maxSize ) { char buf[1024]; sprintf( buf, "Net_GetUDPPacket: oversize packet from %s\n", Sys_NetAdrToString( net_from ) ); OutputDebugString( buf ); return false; } size = ret; return true; }
void NET_SendPacket (netsrc_t sock, int length, void *data, netadr_t to) { int ret; struct sockaddr_in addr; int net_socket; if ( to.type == NA_LOOPBACK ) { NET_SendLoopPacket (sock, length, data, to); return; } if (to.type == NA_BROADCAST) { net_socket = ip_sockets[sock]; if (!net_socket) return; } else if (to.type == NA_IP) { net_socket = ip_sockets[sock]; if (!net_socket) return; } else if (to.type == NA_IPX) { net_socket = ipx_sockets[sock]; if (!net_socket) return; } else if (to.type == NA_BROADCAST_IPX) { net_socket = ipx_sockets[sock]; if (!net_socket) return; } else Com_Error (ERR_FATAL, "NET_SendPacket: bad address type"); NetadrToSockadr (&to, &addr); // >>> FIX: For Nintendo Wii using devkitPPC / libogc // Switching to the equivalent function in the platform: //ret = sendto (net_socket, data, length, 0, (struct sockaddr *)&addr, sizeof(addr) ); //if (ret == -1) ret = net_sendto (net_socket, data, length, 0, (struct sockaddr *)&addr, 8); if (ret < 0) // <<< FIX { Com_Printf ("NET_SendPacket ERROR: %s to %s\n", NET_ErrorString(), NET_AdrToString (to)); } }
qboolean NET_GetPacket (netsrc_t sock, netadr_t *net_from, sizebuf_t *net_message) { int ret; struct sockaddr_storage from; socklen_t fromlen; int net_socket; int protocol; int err; if (NET_GetLoopPacket (sock, net_from, net_message)) return true; for (protocol = 0 ; protocol < 3 ; protocol++) { if (protocol == 0) net_socket = ip_sockets[sock]; else if (protocol == 1) net_socket = ip6_sockets[sock]; else net_socket = ipx_sockets[sock]; if (!net_socket) continue; fromlen = sizeof(from); ret = recvfrom (net_socket, net_message->data, net_message->maxsize, 0, (struct sockaddr *)&from, &fromlen); SockadrToNetadr (&from, net_from); if (ret == -1) { err = errno; if (err == EWOULDBLOCK || err == ECONNREFUSED) continue; Com_Printf ("NET_GetPacket: %s from %s\n", NET_ErrorString(), NET_AdrToString(*net_from)); continue; } if (ret == net_message->maxsize) { Com_Printf ("Oversize packet from %s\n", NET_AdrToString (*net_from)); continue; } net_message->cursize = ret; return true; } return false; }
/* ================== Sys_SendVoicePacket ================== */ void Sys_SendVoicePacket( int length, const void *data, netadr_t to ) { int ret; struct sockaddr addr; // check for valid packet intentions (direct send or broadcast) // if( to.type != NA_IP && to.type != NA_BROADCAST ) { Com_Error( ERR_FATAL, "Sys_SendVoicePacket: bad address type" ); return; } // check we have our voice socket set up // if( v_socket == INVALID_SOCKET) { return; } NetadrToSockadr( &to, &addr ); #ifdef SOF2_METRICS gXBL_NumberVoicePacketsSent++; gXBL_SizeVoicePacketsSent += length; #endif /*if( usingSocks && to.type == NA_IP ) { vsocksBuf[0] = 0; // reserved vsocksBuf[1] = 0; vsocksBuf[2] = 0; // fragment (not fragmented) vsocksBuf[3] = 1; // address type: IPV4 *(int *)&vsocksBuf[4] = ((struct sockaddr_in *)&addr)->sin_addr.s_addr; *(short *)&vsocksBuf[8] = ((struct sockaddr_in *)&addr)->sin_port; memcpy( &vsocksBuf[10], data, length ); ret = sendto( v_socket, vsocksBuf, length+10, 0, &socksRelayAddr, sizeof(socksRelayAddr) ); } else {*/ ret = sendto( v_socket, (const char *)data, length, 0, &addr, sizeof(addr) ); //} if( ret == SOCKET_ERROR ) { int err = WSAGetLastError(); // wouldblock is silent if( err == WSAEWOULDBLOCK ) { return; } // some PPP links do not allow broadcasts and return an error if( ( err == WSAEADDRNOTAVAIL ) && ( ( to.type == NA_BROADCAST ) || ( to.type == NA_BROADCAST_IPX ) ) ) { return; } Com_DPrintf( "NET_SendVoicePacket: %s\n", NET_ErrorString() ); } }
/* * TV_Downstream_SendClientMessages */ void TV_Downstream_SendClientMessages( void ) { int i; client_t *client; msg_t message; qbyte messageData[MAX_MSGLEN]; // send a message to each connected client for( i = 0, client = tvs.clients; i < tv_maxclients->integer; i++, client++ ) { if( client->state == CS_FREE || client->state == CS_ZOMBIE ) continue; if( client->state < CS_SPAWNED ) { // send pending reliable commands, or send heartbeats for not timing out /* if( client->reliableSequence > client->reliableSent || (client->reliableSequence > client->reliableAcknowledge && tvs.realtime - client->lastPacketSentTime > 50) || tvs.realtime - client->lastPacketSentTime > 500 ) */ if( client->reliableSequence > client->reliableAcknowledge || tvs.realtime - client->lastPacketSentTime > 1000 ) { TV_Downstream_InitClientMessage( client, &message, messageData, sizeof( messageData ) ); TV_Downstream_AddReliableCommandsToMessage( client, &message ); if( !TV_Downstream_SendMessageToClient( client, &message ) ) { Com_Printf( "%s" S_COLOR_WHITE ": Error sending message: %s\n", client->name, NET_ErrorString() ); if( client->reliable ) { TV_Downstream_DropClient( client, DROP_TYPE_GENERAL, "Error sending message: %s\n", NET_ErrorString() ); } } } } } }
void NET_SendPacket (netsrc_t sock, int length, void *data, netadr_t to) { int ret; struct sockaddr_in addr; int net_socket; if ( to.type == NA_LOOPBACK ) { NET_SendLoopPacket (sock, length, data, to); return; } if (to.type == NA_BROADCAST) { net_socket = ip_sockets[sock]; if (!net_socket) return; } else if (to.type == NA_IP) { net_socket = ip_sockets[sock]; if (!net_socket) return; } else if (to.type == NA_IPX) { net_socket = ipx_sockets[sock]; if (!net_socket) return; } else if (to.type == NA_BROADCAST_IPX) { net_socket = ipx_sockets[sock]; if (!net_socket) return; } else { Com_Error (ERR_FATAL, "NET_SendPacket: bad address type"); return; } NetadrToSockadr (&to, &addr); ret = sendto (net_socket, data, length, 0, (struct sockaddr *)&addr, sizeof(addr) ); if (ret == -1) { Com_Printf ("NET_SendPacket ERROR: %s to %s\n", NET_ErrorString(), NET_AdrToString (to)); } }
/* ==================== NET_TCPSocket This socket WILL BLOCK! Use it quickly, and DUMP it. ==================== */ int NET_TCPSocket (int port) { int newsocket; int err; if ((newsocket = socket(PF_INET, SOCK_STREAM , IPPROTO_TCP)) == -1) { err = errno; Com_Printf("WARNING: TCP_OpenSocket: socket: %s", NET_ErrorString()); return 0; } return newsocket; }
/* * Netchan_OutOfBand * * Sends an out-of-band datagram */ void Netchan_OutOfBand( const socket_t *socket, const netadr_t *address, size_t length, const uint8_t *data ) { msg_t send; uint8_t send_buf[MAX_PACKETLEN]; // write the packet header MSG_Init( &send, send_buf, sizeof( send_buf ) ); MSG_WriteLong( &send, -1 ); // -1 sequence means out of band MSG_WriteData( &send, data, length ); // send the datagram if( !NET_SendPacket( socket, send.data, send.cursize, address ) ) Com_Printf( "NET_SendPacket: Error: %s\n", NET_ErrorString() ); }