コード例 #1
0
/*
=============
NET_StringToAdr

Traps "localhost" for loopback, passes everything else to system
=============
*/
qboolean	NET_StringToAdr( const char *s, netadr_t *a ) {
	qboolean	r;
	char	base[MAX_STRING_CHARS];
	char	*port;

	if (!strcmp (s, "localhost")) {
		Com_Memset (a, 0, sizeof(*a));
		a->type = NA_LOOPBACK;
		return qtrue;
	}

	// look for a port number
	Q_strncpyz( base, s, sizeof( base ) );
	port = strstr( base, ":" );
	if ( port ) {
		*port = 0;
		port++;
	}

	r = Sys_StringToAdr( base, a );

	if ( !r ) {
		a->type = NA_BAD;
		return qfalse;
	}

	// inet_addr returns this if out of range
	if ( a->ip[0] == 255 && a->ip[1] == 255 && a->ip[2] == 255 && a->ip[3] == 255 ) {
		a->type = NA_BAD;
		return qfalse;
	}

	if ( port ) {
		a->port = BigShort( (short)atoi( port ) );
	} else {
		a->port = BigShort( PORT_SERVER );
	}

	return qtrue;
}
コード例 #2
0
/*
=============
NET_StringToAdr

Traps "localhost" for loopback, passes everything else to system
return 0 on address not found, 1 on address found with port, 2 on address found without port.
=============
*/
int NET_StringToAdr( const char *s, netadr_t *a, netadrtype_t family )
{
	char base[ MAX_STRING_CHARS ], *search;
	char *port = nullptr;

	if ( !strcmp( s, "localhost" ) )
	{
		Com_Memset( a, 0, sizeof( *a ) );
		a->type = NA_LOOPBACK;
// as NA_LOOPBACK doesn't require ports report port was given.
		return 1;
	}

	Q_strncpyz( base, s, sizeof( base ) );

	if ( *base == '[' || Q_CountChar( base, ':' ) > 1 )
	{
		// This is an IPv6 address, handle it specially.
		search = strchr( base, ']' );

		if ( search )
		{
			*search = '\0';
			search++;

			if ( *search == ':' )
			{
				port = search + 1;
			}
		}

		if ( *base == '[' )
		{
			search = base + 1;
		}
		else
		{
			search = base;
		}
	}
	else
	{
		// look for a port number
		port = strchr( base, ':' );

		if ( port )
		{
			*port = '\0';
			port++;
		}

		search = base;
	}

	if ( !Sys_StringToAdr( search, a, family ) )
	{
		a->type = NA_BAD;
		return 0;
	}

	if ( port )
	{
		a->port = BigShort( ( short ) atoi( port ) );
		return 1;
	}
	else
	{
		a->port = BigShort( PORT_SERVER );
		return 2;
	}
}