Beispiel #1
0
int
WINS_OpenSocket(int port)
{
    int newsocket;
    struct sockaddr_in address;
    u_long _true = 1;

    if ((newsocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
	return -1;
    if (ioctlsocket(newsocket, FIONBIO, &_true) == -1)
	goto ErrorReturn;

    address.sin_family = AF_INET;
    if (bindAddr.s_addr != INADDR_NONE)
	address.sin_addr.s_addr = bindAddr.s_addr;
    else
	address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons((unsigned short)port);
    if (bind(newsocket, (struct sockaddr *)&address, sizeof(address)) == 0)
	return newsocket;

    if (tcpipAvailable)
	Sys_Error("Unable to bind to %s",
		   WINS_AddrToString((struct qsockaddr *)&address));
    else /* we are still in init phase, no need to error */
	Con_Printf("Unable to bind to %s\n",
		   WINS_AddrToString((struct qsockaddr *)&address));

  ErrorReturn:
    closesocket(newsocket);
    return -1;
}
Beispiel #2
0
int WINS_OpenSocket(int port)
{
    int newsocket;
    struct sockaddr_in address;
    u_long _true = 1;

    if ((newsocket = psocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
        return -1;
    }

    if (pioctlsocket(newsocket, FIONBIO, &_true) == -1) {
        goto ErrorReturn;
    }

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = myAddr;
    address.sin_port = htons((unsigned short)port);
    if (bind(newsocket, (void *)&address, sizeof(address)) == 0) {
        return newsocket;
    }

    Sys_Error("Unable to bind to %s", WINS_AddrToString((struct qsockaddr *)&address));
ErrorReturn:
    pclosesocket(newsocket);
    return -1;
}
Beispiel #3
0
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
socket_t *Net_ListenSocket(int port)
{
	int newsock;
	socket_t *sock;

	newsock = WINS_OpenReliableSocket(port);
	if (newsock == -1) return NULL;

	if (WINS_Listen(newsock) == -1)
	{
		WINS_CloseSocket(newsock);
		return NULL;
	} //end if
	sock = Net_AllocSocket();
	if (sock == NULL)
	{
		WINS_CloseSocket(newsock);
		return NULL;
	} //end if
	sock->socket = newsock;
	WINS_GetSocketAddr(newsock, &sock->addr);
	WinPrint("listen socket opened at %s\n", WINS_AddrToString(&sock->addr));
	//
	return sock;
} //end of the function Net_ListenSocket
Beispiel #4
0
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
void Net_SetAddressPort( address_t *address, int port ){
	sockaddr_t addr;

	WINS_StringToAddr( address->ip, &addr );
	WINS_SetSocketPort( &addr, port );
	strcpy( address->ip, WINS_AddrToString( &addr ) );
} //end of the function Net_SetAddressPort
Beispiel #5
0
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int WINS_GetNameFromAddr( struct sockaddr_s *addr, char *name ){
	struct hostent *hostentry;

	hostentry = gethostbyaddr( (char *)&( (struct sockaddr_in *)addr )->sin_addr, sizeof( struct in_addr ), AF_INET );
	if ( hostentry ) {
		strncpy( name, (char *)hostentry->h_name, NET_NAMELEN - 1 );
		return 0;
	}

	strcpy( name, WINS_AddrToString( addr ) );
	return 0;
} //end of the function WINS_GetNameFromAddr
Beispiel #6
0
int WINS_GetNameFromAddr (struct qsockaddr *addr, char *name)
{
#ifdef ID_PC
	struct hostent *hostentry;

	hostentry = pgethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
	if (hostentry)
	{
		Q_strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
		return 0;
	}

	Q_strcpy (name, WINS_AddrToString (addr));
#endif
	return 0;
}
Beispiel #7
0
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int WINS_Init( void ){
	int i;
	struct hostent *local;
	char buff[MAXHOSTNAMELEN];
	struct sockaddr_s addr;
	char    *p;
/*
   linux doesn't have anything to initialize for the net
   "Windows .. built for the internet .. the internet .. built with unix"
 */
#if 0
	WORD wVersionRequested;

	wVersionRequested = MAKEWORD( 2, 2 );

	r = WSAStartup( wVersionRequested, &winsockdata );

	if ( r ) {
		WinPrint( "Winsock initialization failed.\n" );
		return -1;
	}
#endif
	/*
	   i = COM_CheckParm ("-udpport");
	   if (i == 0)*/
	net_hostport = DEFAULTnet_hostport;
	/*
	   else if (i < com_argc-1)
	    net_hostport = Q_atoi (com_argv[i+1]);
	   else
	    Sys_Error ("WINS_Init: you must specify a number after -udpport");
	 */

	// determine my name & address
	gethostname( buff, MAXHOSTNAMELEN );
	local = gethostbyname( buff );
	if(local && local->h_addr_list && local->h_addr_list[0])
		myAddr = *(int *)local->h_addr_list[0];
	else
		myAddr = inet_addr("127.0.0.1");

	// if the quake hostname isn't set, set it to the machine name
//	if (Q_strcmp(hostname.string, "UNNAMED") == 0)
	{
		// see if it's a text IP address (well, close enough)
		for ( p = buff; *p; p++ )
			if ( ( *p < '0' || *p > '9' ) && *p != '.' ) {
				break;
			}

		// if it is a real name, strip off the domain; we only want the host
		if ( *p ) {
			for ( i = 0; i < 15; i++ )
				if ( buff[i] == '.' ) {
					break;
				}
			buff[i] = 0;
		}
//		Cvar_Set ("hostname", buff);
	}

	//++timo WTF is that net_controlsocket? it's sole purpose is to retrieve the local IP?
	if ( ( net_controlsocket = WINS_OpenSocket( 0 ) ) == SOCKET_ERROR ) {
		WinError( "WINS_Init: Unable to open control socket\n" );
	}

	( (struct sockaddr_in *)&broadcastaddr )->sin_family = AF_INET;
	( (struct sockaddr_in *)&broadcastaddr )->sin_addr.s_addr = INADDR_BROADCAST;
	( (struct sockaddr_in *)&broadcastaddr )->sin_port = htons( (u_short)net_hostport );

	WINS_GetSocketAddr( net_controlsocket, &addr );
	strcpy( my_tcpip_address,  WINS_AddrToString( &addr ) );
	p = strrchr( my_tcpip_address, ':' );
	if ( p ) {
		*p = 0;
	}
	WinPrint( "Winsock Initialized\n" );

	return net_controlsocket;
} //end of the function WINS_Init
Beispiel #8
0
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
void Net_SocketToAddress(socket_t *sock, address_t *address)
{
	strcpy(address->ip, WINS_AddrToString(&sock->addr));
} //end of the function Net_SocketToAddress
Beispiel #9
-1
int
WINS_Init(void)
{
    int i;
    int err;
    char buff[MAXHOSTNAMELEN];
    char *colon;
    struct hostent *local;
    struct qsockaddr addr;

    if (COM_CheckParm("-noudp"))
	return -1;

    if (!winsock_initialized) {
	err = WSAStartup(MAKEWORD(1,1), &winsockdata);
	if (err) {
	    Con_SafePrintf("Winsock initialization failed.\n");
	    return -1;
	}
    }
    winsock_initialized++;

    /* determine my name & address */
    myAddr.s_addr = htonl(INADDR_LOOPBACK);
    err = gethostname(buff, MAXHOSTNAMELEN);
    if (err) {
	Con_Printf("%s: WARNING: gethostname failed.\n", __func__);
    } else {
	buff[MAXHOSTNAMELEN - 1] = 0;
	blocktime = Sys_DoubleTime();
	WSASetBlockingHook(BlockingHook);
	local = gethostbyname(buff);
	WSAUnhookBlockingHook();
	if (!local) {
	    Con_Printf("%s: WARNING: gethostbyname timed out.\n", __func__);
	} else if (local->h_addrtype != AF_INET) {
	    Con_Printf("%s: address from gethostbyname not IPv4\n", __func__);
	} else {
	    myAddr = *(struct in_addr *)local->h_addr_list[0];
	}
    }
    Con_Printf ("UDP, Local address: %s\n", inet_ntoa(myAddr));

    i = COM_CheckParm("-ip");
    if (i && i < com_argc - 1) {
	bindAddr.s_addr = inet_addr(com_argv[i + 1]);
	if (bindAddr.s_addr == INADDR_NONE)
	    Sys_Error("%s: %s is not a valid IP address", __func__,
		      com_argv[i + 1]);
	Con_Printf("Binding to IP Interface Address of %s\n", com_argv[i + 1]);
    } else {
	bindAddr.s_addr = INADDR_NONE;
    }

    i = COM_CheckParm("-localip");
    if (i && i < com_argc - 1) {
	localAddr.s_addr = inet_addr(com_argv[i + 1]);
	if (localAddr.s_addr == INADDR_NONE)
	    Sys_Error("%s: %s is not a valid IP address", __func__,
		      com_argv[i + 1]);
	Con_Printf("Advertising %s as the local IP in response packets\n",
		   com_argv[i + 1]);
    } else {
	localAddr.s_addr = INADDR_NONE;
    }

    net_controlsocket = WINS_OpenSocket(0);
    if (net_controlsocket == -1) {
	Con_Printf("%s: Unable to open control socket\n", __func__);
	if (--winsock_initialized == 0)
	    WSACleanup();
	return -1;
    }

    broadcastaddr.sin_family = AF_INET;
    broadcastaddr.sin_addr.s_addr = INADDR_BROADCAST;
    broadcastaddr.sin_port = htons((unsigned short)net_hostport);

    WINS_GetSocketAddr(net_controlsocket, &addr);
    strcpy(my_tcpip_address, WINS_AddrToString(&addr));
    colon = strrchr(my_tcpip_address, ':');
    if (colon)
	*colon = 0;

    Con_Printf("Winsock TCP/IP Initialized (%s)\n", my_tcpip_address);
    tcpipAvailable = true;

    return net_controlsocket;
}
Beispiel #10
-1
int WINS_Init (void)
{
	int		i;
	struct hostent *local = NULL;
	char	buff[MAXHOSTNAMELEN];
	struct qsockaddr addr;
	char	*p;
	int		r;
	WORD	wVersionRequested;

// initialize the Winsock function vectors (we do this instead of statically linking
// so we can run on Win 3.1, where there isn't necessarily Winsock)
    hInst = LoadLibrary("wsock32.dll");
	
	if (hInst == NULL)
	{
		Con_SafePrintf ("Failed to load winsock.dll\n");
		winsock_lib_initialized = false;
		return -1;
	}

	winsock_lib_initialized = true;

    pWSAStartup = (void *)GetProcAddress(hInst, "WSAStartup");
    pWSACleanup = (void *)GetProcAddress(hInst, "WSACleanup");
    pWSAGetLastError = (void *)GetProcAddress(hInst, "WSAGetLastError");
    psocket = (void *)GetProcAddress(hInst, "socket");
    pioctlsocket = (void *)GetProcAddress(hInst, "ioctlsocket");
    psetsockopt = (void *)GetProcAddress(hInst, "setsockopt");
    precvfrom = (void *)GetProcAddress(hInst, "recvfrom");
    psendto = (void *)GetProcAddress(hInst, "sendto");
    pclosesocket = (void *)GetProcAddress(hInst, "closesocket");
    pgethostname = (void *)GetProcAddress(hInst, "gethostname");
    pgethostbyname = (void *)GetProcAddress(hInst, "gethostbyname");
    pgethostbyaddr = (void *)GetProcAddress(hInst, "gethostbyaddr");
    pgetsockname = (void *)GetProcAddress(hInst, "getsockname");

    if (!pWSAStartup || !pWSACleanup || !pWSAGetLastError ||
		!psocket || !pioctlsocket || !psetsockopt ||
		!precvfrom || !psendto || !pclosesocket ||
		!pgethostname || !pgethostbyname || !pgethostbyaddr ||
		!pgetsockname)
	{
		Con_SafePrintf ("Couldn't GetProcAddress from winsock.dll\n");
		return -1;
	}

	if (COM_CheckParm ("-noudp"))
		return -1;

	if (winsock_initialized == 0)
	{
		wVersionRequested = MAKEWORD(1, 1); 

		r = pWSAStartup (MAKEWORD(1, 1), &winsockdata);

		if (r)
		{
			Con_SafePrintf ("Winsock initialization failed.\n");
			return -1;
		}
	}
	winsock_initialized++;

	// determine my name & address
	if (pgethostname(buff, MAXHOSTNAMELEN) == 0)
	{
		blocktime = Sys_FloatTime();
		WSASetBlockingHook(BlockingHook);
		local = pgethostbyname(buff);
		WSAUnhookBlockingHook();
		if (local == NULL)
		{
			Con_DPrintf ("Winsock TCP/IP Initialization timed out.\n");
			if (--winsock_initialized == 0)
				pWSACleanup ();
			return -1;
		}
	}

	if (local)
	{
		myAddr = *(int *)local->h_addr_list[0];

		// if the quake hostname isn't set, set it to the machine name
		if (strcmp(hostname.string, "UNNAMED") == 0)
		{
			// see if it's a text IP address (well, close enough)
			for (p = buff; *p; p++)
				if ((*p < '0' || *p > '9') && *p != '.')
					break;

			// if it is a real name, strip off the domain; we only want the host
			if (*p)
			{
				for (i = 0; i < 15; i++)
					if (buff[i] == '.')
						break;
				buff[i] = 0;
			}
			Cvar_Set ("hostname", buff);
		}
	}

	if ((net_controlsocket = WINS_OpenSocket (0)) == -1)
	{
		Con_Printf("WINS_Init: Unable to open control socket\n");
		if (--winsock_initialized == 0)
			pWSACleanup ();
		return -1;
	}

	((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET;
	((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = INADDR_BROADCAST;
	((struct sockaddr_in *)&broadcastaddr)->sin_port = htons((unsigned short)net_hostport);

	WINS_GetSocketAddr (net_controlsocket, &addr);
	strcpy(my_tcpip_address,  WINS_AddrToString (&addr));
	p = strrchr (my_tcpip_address, ':');
	if (p)
		*p = 0;

	Con_Printf("Winsock TCP/IP Initialized\n");
	tcpipAvailable = true;

	return net_controlsocket;
}