コード例 #1
0
int UDP_Init (void)
{
	struct hostent *local;
	char	buff[MAXHOSTNAMELEN];
	struct qsockaddr addr;
	char *colon;

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

#if 1 // Android
	AndroidGetAddr();
#else
	// determine my name & address
	gethostname(buff, MAXHOSTNAMELEN);
	local = gethostbyname(buff);

	if(!local)
	{
		Con_Printf("Could not gethostbyname(\"%s\")\n", buff);
		return -1;
	}

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

	// if the quake hostname isn't set, set it to the machine name
	if (Q_strcmp(hostname.string, "UNNAMED") == 0)
	{
		buff[15] = 0;
		Cvar_Set ("hostname", buff);
	}
#endif
	if ((net_controlsocket = UDP_OpenSocket (0)) == -1)
		Sys_Error("UDP_Init: Unable to open control socket\n");

	sockaddr_in temp;

	memcpy(&temp, &broadcastaddr, sizeof(temp));

	temp.sin_family = AF_INET;
	temp.sin_addr.s_addr = INADDR_BROADCAST;
	temp.sin_port = htons(net_hostport);

	memcpy(&broadcastaddr, &temp, sizeof(temp));

	UDP_GetSocketAddr (net_controlsocket, &addr);
	Q_strcpy(my_tcpip_address,  UDP_AddrToString (&addr));
	colon = Q_strrchr (my_tcpip_address, ':');
	if (colon)
		*colon = 0;

	Con_Printf("UDP Initialized\n");
	tcpipAvailable = true;

	return net_controlsocket;
}
コード例 #2
0
ファイル: w32_udpip.c プロジェクト: cr88192/bgbtech_engine
VADDR *udpsock_getaddr(VFILE *sock)
{
	VADDR *tmp;
	tmp=gcalloc(sizeof(VADDR));
	if(UDP_GetSocketAddr(*(int *)sock->buffer, tmp))
	{
		gcfree(tmp);
		tmp=NULL;
	}
	return(tmp);
}
コード例 #3
0
ファイル: net_udp.c プロジェクト: luaman/qforge-old
int
UDP_Init ( void )
{
	struct hostent *local;
	char	buff[MAXHOSTNAMELEN];
	struct qsockaddr addr;
	char *colon;

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

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

	// if the quake hostname isn't set, set it to the machine name
	if (Q_strcmp(hostname->string, "UNNAMED") == 0)
	{
		buff[15] = 0;
		Cvar_Set (hostname, buff);
	}

	if ((net_controlsocket = UDP_OpenSocket (0)) == -1)
		Sys_Error("UDP_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(net_hostport);

	UDP_GetSocketAddr (net_controlsocket, &addr);
	Q_strcpy(my_tcpip_address, UDP_AddrToString (&addr));
	colon = Q_strrchr (my_tcpip_address, ':');
	if (colon)
		*colon = 0;

	Con_Printf("UDP Initialized\n");
	tcpipAvailable = true;

	return net_controlsocket;
}
コード例 #4
0
ファイル: net_udp.c プロジェクト: Izhido/Quake_For_OSX
int UDP_Init (void)
{
	struct hostent *local;
	char*	buff;
	struct qsockaddr addr;
	char *colon;
	
	if (COM_CheckParm ("-noudp"))
		return -1;

    // determine my name & address

    struct ifaddrs *allInterfaces;
    
    if (getifaddrs(&allInterfaces) == 0)
    {
        struct ifaddrs *interface;
        
        for (interface = allInterfaces; interface != NULL; interface = interface->ifa_next)
        {
            unsigned int flags = interface->ifa_flags;
            struct sockaddr *addr = interface->ifa_addr;
            
            if ((flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING))
            {
                if (addr->sa_family == AF_INET)
                {
                    buff = malloc(MAXHOSTNAMELEN + 1);
                    getnameinfo(addr, addr->sa_len, buff, MAXHOSTNAMELEN, NULL, 0, NI_NUMERICHOST);

                    if (net_ipaddressescount >= net_ipaddressessize)
                    {
                        int newsize = net_ipaddressescount + 4;
                        char** newipaddresses = malloc(newsize * sizeof(char*));
                        
                        if (net_ipaddresses != NULL)
                        {
                            memcpy(newipaddresses, net_ipaddresses, net_ipaddressescount * sizeof(char*));
                            
                            free(net_ipaddresses);
                        }
                        
                        net_ipaddresses = newipaddresses;
                        net_ipaddressessize = newsize;
                    }
                    
                    net_ipaddresses[net_ipaddressescount] = buff;
                    net_ipaddressescount++;
                }
            }
        }
    
        freeifaddrs(allInterfaces);
        
        if (net_ipaddressescount == 0)
        {
            return -1;
        }
        
        if (net_ipaddress == NULL)
        {
            buff = net_ipaddresses[net_ipaddressescount - 1];
            local = gethostbyname(buff);
            if (local == NULL)
            {
                return -1;
            }
            myAddr = *(int *)local->h_addr_list[0];
        }
        else
        {
            qboolean found = false;
            
            for (int i = 0; i < net_ipaddressescount; i++)
            {
                buff = net_ipaddresses[i];
                
                if (strcmp(net_ipaddress, buff) == 0)
                {
                    local = gethostbyname(buff);
                    if (local == NULL)
                    {
                        return -1;
                    }
                    myAddr = *(int *)local->h_addr_list[0];
                    
                    found = true;
                }
            }
            
            if (!found)
            {
                return -1;
            }
        }
    }
    else
    {
        return -1;
    }

    // if the quake hostname isn't set, set it to the machine name
	if (Q_strcmp(hostname.string, "UNNAMED") == 0)
	{
		buff[15] = 0;
		Cvar_Set ("hostname", buff);
	}

	if ((net_controlsocket = UDP_OpenSocket (0)) == -1)
		Sys_Error("UDP_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(net_hostport);

	UDP_GetSocketAddr (net_controlsocket, &addr);
	Q_strcpy(my_tcpip_address,  UDP_AddrToString (&addr));
	colon = Q_strrchr (my_tcpip_address, ':');
	if (colon)
		*colon = 0;

	Con_Printf("UDP Initialized\n");
	tcpipAvailable = true;

	return net_controlsocket;
}
コード例 #5
0
ファイル: net_udp.c プロジェクト: CatalystG/tyrquake
int
UDP_Init(void)
{
    int i;
    int err;
    char buff[MAXHOSTNAMELEN];
    char *colon;
    struct hostent *local;
    netadr_t addr;

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

    /* determine my name & address, default to loopback */
    myAddr.ip.l = htonl(INADDR_LOOPBACK);
    myAddr.port = htons(DEFAULTnet_hostport);
    err = gethostname(buff, MAXHOSTNAMELEN);
    if (err) {
	Con_Printf("%s: WARNING: gethostname failed (%s)\n", __func__,
		   strerror(errno));
    } else {
	buff[MAXHOSTNAMELEN - 1] = 0;
	local = gethostbyname(buff);
	if (!local) {
	    Con_Printf("%s: WARNING: gethostbyname failed (%s)\n", __func__,
			hstrerror(h_errno));
	} else if (local->h_addrtype != AF_INET) {
	    Con_Printf("%s: address from gethostbyname not IPv4\n", __func__);
	} else {
	    struct in_addr *inaddr = (struct in_addr *)local->h_addr_list[0];
	    myAddr.ip.l = inaddr->s_addr;
	}
    }

    i = COM_CheckParm("-ip");
    if (i && i < com_argc - 1) {
	bindAddr.ip.l = inet_addr(com_argv[i + 1]);
	if (bindAddr.ip.l == 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.ip.l = INADDR_NONE;
    }

    i = COM_CheckParm("-localip");
    if (i && i < com_argc - 1) {
	localAddr.ip.l = inet_addr(com_argv[i + 1]);
	if (localAddr.ip.l == 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.ip.l = INADDR_NONE;
    }

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

    /* myAddr may resolve to 127.0.0.1, see if we can do any better */
    memset (ifname, 0, sizeof(ifname));
    if (myAddr.ip.l == htonl(INADDR_LOOPBACK)) {
	if (udp_scan_iface(net_controlsocket) == 0)
	    Con_Printf ("UDP, Local address: %s (%s)\n",
			NET_AdrToString(&myAddr), ifname);
    }
    if (ifname[0] == 0) {
	Con_Printf ("UDP, Local address: %s\n", NET_AdrToString(&myAddr));
    }

    broadcastaddr.ip.l = INADDR_BROADCAST;
    broadcastaddr.port = htons(net_hostport);

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

    Con_Printf("UDP Initialized (%s)\n", my_tcpip_address);
    tcpipAvailable = true;

    return net_controlsocket;
}