X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=lhnet.c;h=d9597f215469ef25fa17792299dd50dfe5d90fe4;hb=2d4296949ff91a36f212e375fffac6a5d18d80dc;hp=b728fa2c48bbe80ed150c9b96fc5cc747dc91351;hpb=0a2edb85ad96a5fe17d5c3d694e70ab8aaf65def;p=xonotic%2Fdarkplaces.git diff --git a/lhnet.c b/lhnet.c index b728fa2c..d9597f21 100644 --- a/lhnet.c +++ b/lhnet.c @@ -18,7 +18,7 @@ #endif // for Z_Malloc/Z_Free in quake -#if 1 +#ifndef STANDALONETEST #include "zone.h" #else #define Z_Malloc malloc @@ -29,6 +29,8 @@ int LHNETADDRESS_FromPort(lhnetaddress_t *address, int addresstype, int port) { + if (!address) + return 0; switch(addresstype) { case LHNETADDRESSTYPE_LOOP: @@ -42,14 +44,14 @@ int LHNETADDRESS_FromPort(lhnetaddress_t *address, int addresstype, int port) memset(address, 0, sizeof(*address)); address->addresstype = LHNETADDRESSTYPE_INET4; address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY; - address->addressdata.inet4.port = htons(port); + address->addressdata.inet4.port = htons((unsigned short)port); return 1; case LHNETADDRESSTYPE_INET6: // [0:0:0:0:0:0:0:0]:port (IN6ADDR_ANY, binds to all interfaces) memset(address, 0, sizeof(*address)); address->addresstype = LHNETADDRESSTYPE_INET6; address->addressdata.inet6.family = LHNETADDRESSTYPE_INET6_FAMILY; - address->addressdata.inet6.port = htons(port); + address->addressdata.inet6.port = htons((unsigned short)port); return 1; } return 0; @@ -57,10 +59,12 @@ int LHNETADDRESS_FromPort(lhnetaddress_t *address, int addresstype, int port) int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int defaultport) { - int i, port, namelen, number; + int port, namelen, d1, d2, d3, d4; struct hostent *hostentry; const char *colon; char name[128]; + if (!address || !string) + return 0; memset(address, 0, sizeof(*address)); address->addresstype = LHNETADDRESSTYPE_NONE; port = 0; @@ -90,66 +94,48 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def } // try to parse with gethostbyname first, because it can handle ipv4 and // ipv6 (in various address formats), as well as dns names - for (i = 0;i < 3;i++) + hostentry = gethostbyname(name); + if (hostentry) { - if (i == 0) - hostentry = gethostbyaddr(name, namelen, LHNETADDRESSTYPE_INET6_FAMILY); - else if (i == 1) - hostentry = gethostbyaddr(name, namelen, LHNETADDRESSTYPE_INET4_FAMILY); - else - hostentry = gethostbyname(name); - if (hostentry) + if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET6_FAMILY) { - if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET6_FAMILY) - { - // great it worked - address->addresstype = LHNETADDRESSTYPE_INET6; - address->addressdata.inet6.family = hostentry->h_addrtype; - address->addressdata.inet6.port = htons(port); - memcpy(address->addressdata.inet6.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet6.address)); + // great it worked + address->addresstype = LHNETADDRESSTYPE_INET6; + address->addressdata.inet6.family = hostentry->h_addrtype; + address->addressdata.inet6.port = htons((unsigned short)port); + memcpy(address->addressdata.inet6.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet6.address)); #ifdef STANDALONETEST - printf("gethostbyname(\"%s\") returned ipv6 address [%x:%x:%x:%x:%x:%x:%x:%x]:%d\n", name, (int)address->addressdata.inet6.address[0], (int)address->addressdata.inet6.address[1], (int)address->addressdata.inet6.address[2], (int)address->addressdata.inet6.address[3], (int)address->addressdata.inet6.address[4], (int)address->addressdata.inet6.address[5], (int)address->addressdata.inet6.address[6], (int)address->addressdata.inet6.address[7], (int)ntohs(address->addressdata.inet6.port)); + printf("gethostbyname(\"%s\") returned ipv6 address [%x:%x:%x:%x:%x:%x:%x:%x]:%d\n", name, (int)address->addressdata.inet6.address[0], (int)address->addressdata.inet6.address[1], (int)address->addressdata.inet6.address[2], (int)address->addressdata.inet6.address[3], (int)address->addressdata.inet6.address[4], (int)address->addressdata.inet6.address[5], (int)address->addressdata.inet6.address[6], (int)address->addressdata.inet6.address[7], (int)ntohs(address->addressdata.inet6.port)); #endif - return 1; - } - else if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET4_FAMILY) - { - // great it worked - address->addresstype = LHNETADDRESSTYPE_INET4; - address->addressdata.inet4.family = hostentry->h_addrtype; - address->addressdata.inet4.port = htons(port); - memcpy(address->addressdata.inet4.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet4.address)); + return 1; + } + else if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET4_FAMILY) + { + // great it worked + address->addresstype = LHNETADDRESSTYPE_INET4; + address->addressdata.inet4.family = hostentry->h_addrtype; + address->addressdata.inet4.port = htons((unsigned short)port); + memcpy(address->addressdata.inet4.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet4.address)); #ifdef STANDALONETEST - printf("gethostbyname(\"%s\") returned ipv4 address %d.%d.%d.%d:%d\n", name, (int)address->addressdata.inet4.address[0], (int)address->addressdata.inet4.address[1], (int)address->addressdata.inet4.address[2], (int)address->addressdata.inet4.address[3], (int)ntohs(address->addressdata.inet4.port)); + printf("gethostbyname(\"%s\") returned ipv4 address %d.%d.%d.%d:%d\n", name, (int)address->addressdata.inet4.address[0], (int)address->addressdata.inet4.address[1], (int)address->addressdata.inet4.address[2], (int)address->addressdata.inet4.address[3], (int)ntohs(address->addressdata.inet4.port)); #endif - return 1; - } + return 1; } } // failed, try to parse as an ipv4 address as a fallback (is this needed?) #ifdef STANDALONETEST printf("gethostbyname and gethostbyaddr failed on address \"%s\"\n", name); #endif - for (i = 0, number = 0;i < 4;string++) - { - if (*string >= '0' && *string <= '9') - number = number * 10 + (*string - '0'); - else if (number < 256 && (*string == '.' || *string == ':')) - { - address->addressdata.inet4.address[i++] = number; - number = 0; - } - else - break; - if (*string == 0 || *string == ':') - break; - } - if (i == 4) + if (sscanf(name, "%d.%d.%d.%d", &d1, &d2, &d3, &d4) == 4 && (unsigned int)d1 < 256 && (unsigned int)d2 < 256 && (unsigned int)d3 < 256 && (unsigned int)d4 < 256) { // parsed a valid ipv4 address address->addresstype = LHNETADDRESSTYPE_INET4; address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY; - address->addressdata.inet4.port = htons(port); + address->addressdata.inet4.port = htons((unsigned short)port); + address->addressdata.inet4.address[0] = (unsigned char)d1; + address->addressdata.inet4.address[1] = (unsigned char)d2; + address->addressdata.inet4.address[2] = (unsigned char)d3; + address->addressdata.inet4.address[3] = (unsigned char)d4; #ifdef STANDALONETEST printf("manual parsing of ipv4 dotted decimal address \"%s\" successful: %d.%d.%d.%d:%d\n", string, (int)address->addressdata.inet4.address[0], (int)address->addressdata.inet4.address[1], (int)address->addressdata.inet4.address[2], (int)address->addressdata.inet4.address[3], (int)ntohs(address->addressdata.inet4.port)); #endif @@ -161,6 +147,8 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int stringbuffersize, int includeport) { *string = 0; + if (!address || !string || stringbuffersize < 1) + return 0; switch(address->addresstype) { default: @@ -225,11 +213,16 @@ int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int strin int LHNETADDRESS_GetAddressType(const lhnetaddress_t *address) { - return address->addresstype; + if (address) + return address->addresstype; + else + return LHNETADDRESSTYPE_NONE; } int LHNETADDRESS_GetPort(const lhnetaddress_t *address) { + if (!address) + return -1; switch(address->addresstype) { case LHNETADDRESSTYPE_LOOP: @@ -245,16 +238,18 @@ int LHNETADDRESS_GetPort(const lhnetaddress_t *address) int LHNETADDRESS_SetPort(lhnetaddress_t *address, int port) { + if (!address) + return 0; switch(address->addresstype) { case LHNETADDRESSTYPE_LOOP: address->addressdata.loop.port = port; return 1; case LHNETADDRESSTYPE_INET4: - address->addressdata.inet4.port = htons(port); + address->addressdata.inet4.port = htons((unsigned short)port); return 1; case LHNETADDRESSTYPE_INET6: - address->addressdata.inet6.port = htons(port); + address->addressdata.inet6.port = htons((unsigned short)port); return 1; default: return 0; @@ -263,6 +258,8 @@ int LHNETADDRESS_SetPort(lhnetaddress_t *address, int port) int LHNETADDRESS_Compare(const lhnetaddress_t *address1, const lhnetaddress_t *address2) { + if (!address1 || !address2) + return 1; if (address1->addresstype != address2->addresstype) return 1; switch(address1->addresstype) @@ -340,6 +337,8 @@ void LHNET_Shutdown(void) lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address) { lhnetsocket_t *lhnetsocket, *s; + if (!address) + return NULL; lhnetsocket = Z_Malloc(sizeof(*lhnetsocket)); if (lhnetsocket) { @@ -461,12 +460,17 @@ void LHNET_CloseSocket(lhnetsocket_t *lhnetsocket) lhnetaddress_t *LHNET_AddressFromSocket(lhnetsocket_t *sock) { - return &sock->address; + if (sock) + return &sock->address; + else + return NULL; } int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, lhnetaddress_t *address) { int value = 0; + if (!lhnetsocket || !address || !content || maxcontentlength < 1) + return -1; if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP) { time_t currenttime; @@ -554,6 +558,8 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentlength, const lhnetaddress_t *address) { int value = -1; + if (!lhnetsocket || !address || !content || contentlength < 1) + return -1; if (lhnetsocket->address.addresstype != address->addresstype) return -1; if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)