/* ==================== NET_OpenIP ==================== */ void NET_OpenIP( void ) { int i; int err; int port; int port6; port = net_port->integer; port6 = net_port6->integer; NET_GetLocalAddress(); // automatically scan for a valid port, so multiple // dedicated servers can be started without requiring // a different net_port for each one if(net_enabled->integer & NET_ENABLEV6) { for( i = 0 ; i < 10 ; i++ ) { ip6_socket = NET_IP6Socket(net_ip6->string, port6 + i, &boundto, &err); if (ip6_socket != INVALID_SOCKET) { Cvar_SetValue( "net_port6", port6 + i ); break; } else { if(err == EAFNOSUPPORT) break; } } if(ip6_socket == INVALID_SOCKET) Com_Printf( "WARNING: Couldn't bind to a v6 ip address.\n"); } if(net_enabled->integer & NET_ENABLEV4) { for( i = 0 ; i < 10 ; i++ ) { ip_socket = NET_IPSocket( net_ip->string, port + i, &err ); if (ip_socket != INVALID_SOCKET) { Cvar_SetValue( "net_port", port + i ); if (net_socksEnabled->integer) NET_OpenSocks( port + i ); break; } else { if(err == EAFNOSUPPORT) break; } } if(ip_socket == INVALID_SOCKET) Com_Printf( "WARNING: Couldn't bind to a v4 ip address.\n"); } }
/* ==================== 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 Windows XP, at least try with a non-multicast socket. 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_OpenIP ==================== */ static void NET_OpenIP( void ) { int i; int err; int port = PORT_ANY; int port6 = PORT_ANY; struct sockaddr_in boundto4; if ( serverMode ) { port = NET_EnsureValidPortNo( net_port->integer ); port6 = NET_EnsureValidPortNo( net_port6->integer ); } NET_GetLocalAddress(); // automatically scan for a valid port, so multiple // dedicated servers can be started without requiring // a different net_port for each one if ( net_enabled->integer & NET_ENABLEV6 ) { for ( i = ( port6 == PORT_ANY ? 1 : MAX_TRY_PORTS ); i; i++ ) { ip6_socket = NET_IP6Socket( net_ip6->string, port6, &boundto, &err ); if ( ip6_socket != INVALID_SOCKET ) { port6 = ntohs( boundto.sin6_port ); break; } else if ( err == EAFNOSUPPORT ) { port6 = PORT_ANY; break; } port6 = ( port6 == PORT_ANY ) ? port6 : NET_EnsureValidPortNo( port6 + 1 ); } if ( ip6_socket == INVALID_SOCKET || err == EAFNOSUPPORT ) { Com_Printf( "WARNING: Couldn't bind to an IPv6 address.\n" ); port6 = PORT_ANY; } } if ( net_enabled->integer & NET_ENABLEV4 ) { for ( i = ( port6 == PORT_ANY ? 1 : MAX_TRY_PORTS ); i; i++ ) { ip_socket = NET_IPSocket( net_ip->string, port, &boundto4, &err ); if ( ip_socket != INVALID_SOCKET ) { port = ntohs( boundto4.sin_port ); break; } else if ( err == EAFNOSUPPORT ) { port = PORT_ANY; break; } port = ( port == PORT_ANY ) ? port : NET_EnsureValidPortNo( port + 1 ); } if ( ip_socket == INVALID_SOCKET ) { Com_Printf( "WARNING: Couldn't bind to an IPv4 address.\n" ); port = PORT_ANY; } } if ( port != PORT_ANY && net_socksEnabled->integer ) { NET_OpenSocks( port ); } Cvar_Set( "net_currentPort", va( "%i", port ) ); Cvar_Set( "net_currentPort6", va( "%i", port6 ) ); }