X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;f=lhnet.c;h=b97be361e24cb6a4629ab478fe4bd814c45def4f;hb=bc237466abd40659a8507ebaca3683543c4a4a21;hp=e244a6498d273cad15606e8d64ed7525974101a4;hpb=0c54c2b5a3703acc26568605966a324e11c01d5e;p=xonotic%2Fdarkplaces.git diff --git a/lhnet.c b/lhnet.c index e244a649..b97be361 100644 --- a/lhnet.c +++ b/lhnet.c @@ -42,6 +42,10 @@ #define SOCKETERRNO WSAGetLastError() +#define IOC_VENDOR 0x18000000 +#define _WSAIOW(x,y) (IOC_IN|(x)|(y)) +#define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12) + #define SOCKLEN_T int #elif defined(__MORPHOS__) #define ioctlsocket IoctlSocket @@ -104,7 +108,7 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def struct hostent *hostentry; const char *colon; char name[128]; - if (!address || !string) + if (!address || !string || !*string) return 0; memset(address, 0, sizeof(*address)); address->addresstype = LHNETADDRESSTYPE_NONE; @@ -134,7 +138,9 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def return 1; } // try to parse as dotted decimal ipv4 address first - 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) + // note this supports partial ip addresses + d1 = d2 = d3 = d4 = 0; + if (sscanf(name, "%d.%d.%d.%d", &d1, &d2, &d3, &d4) >= 1 && (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; @@ -155,7 +161,7 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def #ifdef STANDALONETEST if (i < MAX_NAMECACHE) #else - if (i < MAX_NAMECACHE && Sys_DoubleTime() < namecache[i].expirationtime) + if (i < MAX_NAMECACHE && realtime < namecache[i].expirationtime) #endif { *address = namecache[i].address; @@ -186,7 +192,7 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def namecache[namecacheposition].name[i] = name[i]; namecache[namecacheposition].name[i] = 0; #ifndef STANDALONETEST - namecache[namecacheposition].expirationtime = Sys_DoubleTime() + 12 * 3600; // 12 hours + namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours #endif namecache[namecacheposition].address = *address; namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE; @@ -206,7 +212,7 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def namecache[namecacheposition].name[i] = name[i]; namecache[namecacheposition].name[i] = 0; #ifndef STANDALONETEST - namecache[namecacheposition].expirationtime = Sys_DoubleTime() + 12 * 3600; // 12 hours + namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours #endif namecache[namecacheposition].address = *address; namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE; @@ -223,7 +229,7 @@ int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int def namecache[namecacheposition].name[i] = name[i]; namecache[namecacheposition].name[i] = 0; #ifndef STANDALONETEST - namecache[namecacheposition].expirationtime = Sys_DoubleTime() + 12 * 3600; // 12 hours + namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours #endif namecache[namecacheposition].address.addresstype = LHNETADDRESSTYPE_NONE; namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE; @@ -252,7 +258,7 @@ int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int strin { if (stringbuffersize >= 6) { - strcpy(string, "local"); + memcpy(string, "local", 6); return 1; } } @@ -493,6 +499,28 @@ static const char *LHNETPRIVATE_StrError(void) #endif } +void LHNET_SleepUntilPacket_Microseconds(int microseconds) +{ + fd_set fdreadset; + struct timeval tv; + int lastfd; + lhnetsocket_t *s; + FD_ZERO(&fdreadset); + lastfd = 0; + for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next) + { + if (s->address.addresstype == LHNETADDRESSTYPE_INET4 || s->address.addresstype == LHNETADDRESSTYPE_INET6) + { + if (lastfd < s->inetsocket) + lastfd = s->inetsocket; + FD_SET(s->inetsocket, &fdreadset); + } + } + tv.tv_sec = microseconds / 1000000; + tv.tv_usec = microseconds % 1000000; + select(lastfd + 1, &fdreadset, NULL, NULL, &tv); +} + lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address) { lhnetsocket_t *lhnetsocket, *s; @@ -547,6 +575,7 @@ lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address) { #ifdef WIN32 u_long _true = 1; + u_long _false = 0; #else char _true = 1; #endif @@ -564,6 +593,10 @@ lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address) lhnetsocket->prev = lhnetsocket->next->prev; lhnetsocket->next->prev = lhnetsocket; lhnetsocket->prev->next = lhnetsocket; +#ifdef WIN32 + if (ioctlsocket(lhnetsocket->inetsocket, SIO_UDP_CONNRESET, &_false) == -1) + Con_DPrintf("LHNET_OpenSocket_Connectionless: ioctlsocket SIO_UDP_CONNRESET returned error: %s\n", LHNETPRIVATE_StrError()); +#endif return lhnetsocket; } else @@ -642,7 +675,7 @@ int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, continue; } #ifndef STANDALONETEST - if (cl_netlocalping.value && (Sys_DoubleTime() - cl_netlocalping.value * (1.0 / 2000.0)) < p->sentdoubletime) + if (cl_netlocalping.value && (realtime - cl_netlocalping.value * (1.0 / 2000.0)) < p->sentdoubletime) continue; #endif if (value == 0 && p->destinationport == lhnetsocket->address.addressdata.loop.port) @@ -738,7 +771,7 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng p->next->prev = p; p->prev->next = p; #ifndef STANDALONETEST - p->sentdoubletime = Sys_DoubleTime(); + p->sentdoubletime = realtime; #endif value = contentlength; } @@ -749,7 +782,7 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng { if (SOCKETERRNO == EWOULDBLOCK) return 0; - Con_Printf("LHNET_Read: sendto returned error: %s\n", LHNETPRIVATE_StrError()); + Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError()); } } else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6) @@ -759,7 +792,7 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng { if (SOCKETERRNO == EWOULDBLOCK) return 0; - Con_Printf("LHNET_Read: sendto returned error: %s\n", LHNETPRIVATE_StrError()); + Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError()); } } return value; @@ -769,7 +802,7 @@ int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentleng int main(int argc, char **argv) { #if 1 - char *buffer = "socket to socket test successful", buffer2[1024]; + char *buffer = "test", buffer2[1024]; int blen = strlen(buffer); int b2len = 1024; lhnetsocket_t *sock1; @@ -782,8 +815,6 @@ int main(int argc, char **argv) int test1; int test2; - strcpy(buffer2, "socket to socket test failed"); - printf("calling LHNET_Init\n"); LHNET_Init(); @@ -806,8 +837,12 @@ int main(int argc, char **argv) usleep (100000); #endif printf("calling LHNET_Read on the second socket to read the packet sent from the first socket\n"); - test2 = LHNET_Read(sock2, buffer2, b2len, &myaddy3); - Con_Printf("%s\n", buffer2); + test2 = LHNET_Read(sock2, buffer2, b2len - 1, &myaddy3); + if (test2 > 0) + Con_Printf("socket to socket test succeeded\n"); + else + Con_Printf("socket to socket test failed\n"); + #ifdef WIN32 printf("press any key to exit\n"); getchar();