]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - net_udp.c
sv.edicts (and related things) are now dynamically reallocated as more edicts are...
[xonotic/darkplaces.git] / net_udp.c
index 01a1d30192ad383a4ae913c01de71ab94b5a2a38..46f638c5477bf8d62b4ea4fc1a651160e8bc3192 100644 (file)
--- a/net_udp.c
+++ b/net_udp.c
@@ -57,8 +57,8 @@ WSADATA               winsockdata;
 
 int UDP_Init (void)
 {
-       int i;
-       struct hostent *local = NULL;
+       int i, j;
+       struct hostent *local;
        char buff[MAXHOSTNAMELEN];
 
        if (COM_CheckParm ("-noudp"))
@@ -73,30 +73,55 @@ int UDP_Init (void)
 #endif
 
        // loopback as a worst case fallback
-       myAddr.d[0] = 127;myAddr.d[1] = 0;myAddr.d[2] = 0;myAddr.d[3] = 1;
+       myAddr.i = htonl(INADDR_ANY);
 
-       if ((i = COM_CheckParm("-ip")) != 0 && i < com_argc)
+       net_controlsocket = -1;
+       for (j = 0;net_controlsocket == -1;j++)
        {
-               myAddr.i = inet_addr(com_argv[i+1]);
-               Con_Printf("Binding to IP Interface Address of %i.%i.%i.%i\n", myAddr.d[0], myAddr.d[1], myAddr.d[2], myAddr.d[3]);
-       }
-       else if (gethostname(buff, MAXHOSTNAMELEN) != -1)
-       {
-               buff[MAXHOSTNAMELEN - 1] = 0;
-               local = gethostbyname(buff);
-               if (local != NULL)
-                       myAddr.i = *((int *)local->h_addr_list[0]);
-       }
-
-       sprintf(my_tcpip_address, "%d.%d.%d.%d", myAddr.d[0], myAddr.d[1], myAddr.d[2], myAddr.d[3]);
-
-       if ((net_controlsocket = UDP_OpenSocket (0)) == -1)
-       {
-               Con_Printf("UDP_Init: Unable to open control socket\n");
+               myAddr.d[0] = 127;
+               myAddr.d[1] = 0;
+               myAddr.d[2] = 0;
+               myAddr.d[3] = 1;
+               switch(j)
+               {
+               case 0:
+                       if ((i = COM_CheckParm("-ip")) != 0 && i < com_argc)
+                               myAddr.i = inet_addr(com_argv[i+1]);
+                       break;
+               case 1:
+                       myAddr.i = htonl(INADDR_ANY);
+                       break;
+               case 2:
+                       if (gethostname(buff, MAXHOSTNAMELEN) != -1)
+                       {
+                               buff[MAXHOSTNAMELEN - 1] = 0;
+                               local = gethostbyname(buff);
+                               if (local != NULL)
+                                       myAddr.i = *((int *)local->h_addr_list[0]);
+                       }
+                       break;
+               default:
+                       Con_Printf("UDP_Init: Giving up, UDP networking support disabled.\n");
 #ifdef WIN32
-               WSACleanup ();
+                       WSACleanup ();
 #endif
-               return -1;
+                       return -1;
+               }
+
+               if (myAddr.i != htonl(INADDR_LOOPBACK))
+               {
+                       if (myAddr.i == htonl(INADDR_LOOPBACK))
+                               sprintf(my_tcpip_address, "INADDR_LOOPBACK");
+                       else if (myAddr.i == htonl(INADDR_ANY))
+                               sprintf(my_tcpip_address, "INADDR_ANY");
+                       else
+                               sprintf(my_tcpip_address, "%d.%d.%d.%d", myAddr.d[0], myAddr.d[1], myAddr.d[2], myAddr.d[3]);
+                       Con_Printf("UDP_Init: Binding to IP Interface Address of %s...  ", my_tcpip_address);
+                       if ((net_controlsocket = UDP_OpenSocket (0)) == -1)
+                               Con_Printf("failed\n");
+                       else
+                               Con_Printf("succeeded\n");
+               }
        }
 
        ((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET;
@@ -258,13 +283,19 @@ int UDP_Read (int socket, qbyte *buf, int len, struct qsockaddr *addr)
                int e = WSAGetLastError();
                if (e == WSAEWOULDBLOCK || e == WSAECONNREFUSED)
                        return 0;
-               Con_Printf("UDP_Read: WASGetLastError == %i\n", e);
+               Con_Printf("UDP_Read(%i, %p, %i, <%s>): WSAGetLastError == %i\n", socket, buf, len, UDP_AddrToString(addr), e);
 #else
                if (errno == EWOULDBLOCK || errno == ECONNREFUSED)
                        return 0;
-               Con_Printf("UDP_Read: errno == %i (%s)\n", errno, strerror(errno));
+               Con_Printf("UDP_Read(%i, %p, %i, <%s>): errno == %i (%s)\n", socket, buf, len, UDP_AddrToString(addr), errno, strerror(errno));
 #endif
        }
+       else if (developer_networking.integer)
+       {
+               Con_Printf("UDP_Read(%i, %p, %i, <%s>) = %i\n", socket, buf, len, UDP_AddrToString(addr), ret);
+               Com_HexDumpToConsole(buf, ret);
+       }
+
        return ret;
 }
 
@@ -309,6 +340,12 @@ int UDP_Write (int socket, qbyte *buf, int len, struct qsockaddr *addr)
 {
        int ret;
 
+       if (developer_networking.integer)
+       {
+               Con_Printf("UDP_Write(%i, %p, %i, <%s>)\n", socket, buf, len, UDP_AddrToString(addr));
+               Com_HexDumpToConsole(buf, len);
+       }
+
        ret = sendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
        if (ret == -1)
        {
@@ -316,11 +353,11 @@ int UDP_Write (int socket, qbyte *buf, int len, struct qsockaddr *addr)
                int e = WSAGetLastError();
                if (e == WSAEWOULDBLOCK)
                        return 0;
-               Con_Printf("UDP_Write: WASGetLastError == %i\n", e);
+               Con_Printf("UDP_Write(%i, %p, %i, <%s>): WSAGetLastError == %i\n", socket, buf, len, UDP_AddrToString(addr), e);
 #else
                if (errno == EWOULDBLOCK)
                        return 0;
-               Con_Printf("UDP_Write: errno == %i (%s)\n", errno, strerror(errno));
+               Con_Printf("UDP_Write(%i, %p, %i, <%s>): errno == %i (%s)\n", socket, buf, len, UDP_AddrToString(addr), errno, strerror(errno));
 #endif
        }
        return ret;
@@ -340,16 +377,31 @@ char *UDP_AddrToString (const struct qsockaddr *addr)
 
 int UDP_StringToAddr (const char *string, struct qsockaddr *addr)
 {
-       int ha1, ha2, ha3, ha4, hp;
-       int ipaddr;
+       int ha[4], hp, ipaddr, j, numbers;
+       const char *colon;
 
-       sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
-       ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
+       hp = net_hostport;
+       colon = strrchr(string, ':');
+       if (colon)
+       {
+               hp = atoi(colon + 1);
+               if (hp == 0)
+                       hp = net_hostport;
+       }
+       numbers = sscanf(string, "%d.%d.%d.%d", &ha[0], &ha[1], &ha[2], &ha[3]);
+       for (ipaddr = 0, j = 0;j < numbers;j++)
+               ipaddr = (ipaddr << 8) | ha[j];
+       // if the address is incomplete take most important numbers from myAddr
+       if (numbers < 4)
+               ipaddr |= ntohl(myAddr.i) & (-1 << (numbers * 8));
 
        addr->sa_family = AF_INET;
        ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
        ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)hp);
-       return 0;
+       if (ipaddr == INADDR_ANY)
+               return -1;
+       else
+               return 0;
 }
 
 //=============================================================================