2 // Written by Forest Hale 2003-06-15 and placed into public domain.
6 // Windows XP or higher is required for getaddrinfo, but the inclusion of wspiapi provides fallbacks for older versions
7 # define _WIN32_WINNT 0x0501
10 # include <ws2tcpip.h>
16 #ifndef STANDALONETEST
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/ioctl.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
39 #include <proto/socket.h>
42 // for Z_Malloc/Z_Free in quake
43 #ifndef STANDALONETEST
48 #define Con_Print printf
49 #define Con_Printf printf
50 #define Z_Malloc malloc
57 #define EWOULDBLOCK WSAEWOULDBLOCK
58 #define ECONNREFUSED WSAECONNREFUSED
60 #define SOCKETERRNO WSAGetLastError()
62 #define IOC_VENDOR 0x18000000
63 #define _WSAIOW(x,y) (IOC_IN|(x)|(y))
64 #define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
67 #elif defined(__MORPHOS__)
68 #define ioctlsocket IoctlSocket
69 #define closesocket CloseSocket
70 #define SOCKETERRNO Errno()
74 #define ioctlsocket ioctl
75 #define closesocket close
76 #define SOCKETERRNO errno
78 #define SOCKLEN_T socklen_t
82 #define LHNET_RECVFROM_FLAGS MSG_DONTWAIT
83 #define LHNET_SENDTO_FLAGS 0
85 #define LHNET_RECVFROM_FLAGS 0
86 #define LHNET_SENDTO_FLAGS 0
89 typedef struct lhnetaddressnative_s
91 lhnetaddresstype_t addresstype;
96 struct sockaddr_in in;
98 struct sockaddr_in6 in6;
103 lhnetaddressnative_t;
105 // to make LHNETADDRESS_FromString resolve repeated hostnames faster, cache them
106 #define MAX_NAMECACHE 64
107 static struct namecache_s
109 lhnetaddressnative_t address;
110 double expirationtime;
113 namecache[MAX_NAMECACHE];
114 static int namecacheposition = 0;
116 int LHNETADDRESS_FromPort(lhnetaddress_t *vaddress, lhnetaddresstype_t addresstype, int port)
118 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
125 case LHNETADDRESSTYPE_LOOP:
126 // local:port (loopback)
127 memset(address, 0, sizeof(*address));
128 address->addresstype = LHNETADDRESSTYPE_LOOP;
129 address->port = port;
131 case LHNETADDRESSTYPE_INET4:
132 // 0.0.0.0:port (INADDR_ANY, binds to all interfaces)
133 memset(address, 0, sizeof(*address));
134 address->addresstype = LHNETADDRESSTYPE_INET4;
135 address->port = port;
136 address->addr.in.sin_family = AF_INET;
137 address->addr.in.sin_port = htons((unsigned short)port);
140 case LHNETADDRESSTYPE_INET6:
141 // [0:0:0:0:0:0:0:0]:port (IN6ADDR_ANY, binds to all interfaces)
142 memset(address, 0, sizeof(*address));
143 address->addresstype = LHNETADDRESSTYPE_INET6;
144 address->port = port;
145 address->addr.in6.sin6_family = AF_INET6;
146 address->addr.in6.sin6_port = htons((unsigned short)port);
154 int LHNETADDRESS_Resolve(lhnetaddressnative_t *address, const char *name, int port)
157 struct addrinfo hints;
158 struct addrinfo* addrinf;
161 dpsnprintf (port_buff, sizeof (port_buff), "%d", port);
162 port_buff[sizeof (port_buff) - 1] = '\0';
164 memset(&hints, 0, sizeof (hints));
165 hints.ai_family = AF_UNSPEC;
166 hints.ai_socktype = SOCK_DGRAM;
167 //hints.ai_flags = AI_PASSIVE;
169 err = getaddrinfo(name, port_buff, &hints, &addrinf);
170 if (err != 0 || addrinf == NULL)
172 if (addrinf->ai_addr->sa_family != AF_INET6 && addrinf->ai_addr->sa_family != AF_INET)
174 freeaddrinfo (addrinf);
179 if (addrinf->ai_addr->sa_family == AF_INET6)
181 address->addresstype = LHNETADDRESSTYPE_INET6;
182 memcpy(&address->addr.in6, addrinf->ai_addr, sizeof(address->addr.in6));
186 address->addresstype = LHNETADDRESSTYPE_INET4;
187 memcpy(&address->addr.in, addrinf->ai_addr, sizeof(address->addr.in));
189 address->port = port;
191 freeaddrinfo (addrinf);
195 int LHNETADDRESS_FromString(lhnetaddress_t *vaddress, const char *string, int defaultport)
197 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
198 int i, port, d1, d2, d3, d4, resolved;
202 #ifdef STANDALONETEST
205 const char* addr_start;
206 const char* addr_end = NULL;
207 const char* port_name = NULL;
208 int addr_family = AF_UNSPEC;
210 if (!address || !string || !*string)
212 memset(address, 0, sizeof(*address));
213 address->addresstype = LHNETADDRESSTYPE_NONE;
216 // If it's a bracketed IPv6 address
217 if (string[0] == '[')
219 const char* end_bracket = strchr(string, ']');
221 if (end_bracket == NULL)
224 if (end_bracket[1] == ':')
225 port_name = end_bracket + 2;
226 else if (end_bracket[1] != '\0')
229 addr_family = AF_INET6;
230 addr_start = &string[1];
231 addr_end = end_bracket;
235 const char* first_colon;
239 // If it's a numeric non-bracket IPv6 address (-> no port),
240 // or it's a numeric IPv4 address, or a name, with a port
241 first_colon = strchr(string, ':');
242 if (first_colon != NULL)
244 const char* last_colon = strrchr(first_colon + 1, ':');
246 // If it's an numeric IPv4 address, or a name, with a port
247 if (last_colon == NULL)
249 addr_end = first_colon;
250 port_name = first_colon + 1;
253 addr_family = AF_INET6;
257 if (addr_end != NULL)
258 namelen = addr_end - addr_start;
260 namelen = strlen (addr_start);
262 if (namelen >= sizeof(name))
263 namelen = sizeof(name) - 1;
264 memcpy (name, addr_start, namelen);
268 port = atoi(port_name);
274 if (!strcmp(name, "local"))
276 address->addresstype = LHNETADDRESSTYPE_LOOP;
277 address->port = port;
280 // try to parse as dotted decimal ipv4 address first
281 // note this supports partial ip addresses
282 d1 = d2 = d3 = d4 = 0;
284 #define sscanf sscanf_s
286 if (addr_family != AF_INET6 &&
287 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)
289 // parsed a valid ipv4 address
290 address->addresstype = LHNETADDRESSTYPE_INET4;
291 address->port = port;
292 address->addr.in.sin_family = AF_INET;
293 address->addr.in.sin_port = htons((unsigned short)port);
294 a = (unsigned char *)&address->addr.in.sin_addr;
299 #ifdef STANDALONETEST
300 LHNETADDRESS_ToString(address, string2, sizeof(string2), 1);
301 printf("manual parsing of ipv4 dotted decimal address \"%s\" successful: %s\n", string, string2);
305 for (i = 0;i < MAX_NAMECACHE;i++)
306 if (!strcmp(namecache[i].name, name))
308 #ifdef STANDALONETEST
309 if (i < MAX_NAMECACHE)
311 if (i < MAX_NAMECACHE && realtime < namecache[i].expirationtime)
314 *address = namecache[i].address;
315 address->port = port;
316 if (address->addresstype == LHNETADDRESSTYPE_INET6)
318 address->addr.in6.sin6_port = htons((unsigned short)port);
321 else if (address->addresstype == LHNETADDRESSTYPE_INET4)
323 address->addr.in.sin_port = htons((unsigned short)port);
329 for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
330 namecache[namecacheposition].name[i] = name[i];
331 namecache[namecacheposition].name[i] = 0;
332 #ifndef STANDALONETEST
333 namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
336 // try resolving the address (handles dns and other ip formats)
337 resolved = LHNETADDRESS_Resolve(address, name, port);
340 #ifdef STANDALONETEST
341 const char *protoname;
343 switch (address->addresstype)
345 case LHNETADDRESSTYPE_INET6:
348 case LHNETADDRESSTYPE_INET4:
352 protoname = "UNKNOWN";
355 LHNETADDRESS_ToString(vaddress, string2, sizeof(string2), 1);
356 Con_Printf("LHNETADDRESS_Resolve(\"%s\") returned %s address %s\n", string, protoname, string2);
358 namecache[namecacheposition].address = *address;
362 #ifdef STANDALONETEST
363 printf("name resolution failed on address \"%s\"\n", name);
365 namecache[namecacheposition].address.addresstype = LHNETADDRESSTYPE_NONE;
368 namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
372 int LHNETADDRESS_FromString(lhnetaddress_t *vaddress, const char *string, int defaultport)
374 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
375 int i, port, namelen, d1, d2, d3, d4;
376 struct hostent *hostentry;
380 #ifdef STANDALONETEST
383 if (!address || !string || !*string)
385 memset(address, 0, sizeof(*address));
386 address->addresstype = LHNETADDRESSTYPE_NONE;
388 colon = strrchr(string, ':');
389 if (colon && (colon == strchr(string, ':') || (string[0] == '[' && colon - string > 0 && colon[-1] == ']')))
390 // EITHER: colon is the ONLY colon OR: colon comes after [...] delimited IPv6 address
391 // fixes misparsing of IPv6 addresses without port
393 port = atoi(colon + 1);
396 colon = string + strlen(string);
399 namelen = colon - string;
402 if (string[0] == '[' && namelen > 0 && string[namelen-1] == ']') // ipv6
407 memcpy(name, string, namelen);
410 if (!strcmp(name, "local"))
412 address->addresstype = LHNETADDRESSTYPE_LOOP;
413 address->port = port;
416 // try to parse as dotted decimal ipv4 address first
417 // note this supports partial ip addresses
418 d1 = d2 = d3 = d4 = 0;
420 #define sscanf sscanf_s
422 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)
424 // parsed a valid ipv4 address
425 address->addresstype = LHNETADDRESSTYPE_INET4;
426 address->port = port;
427 address->addr.in.sin_family = AF_INET;
428 address->addr.in.sin_port = htons((unsigned short)port);
429 a = (unsigned char *)&address->addr.in.sin_addr;
434 #ifdef STANDALONETEST
435 LHNETADDRESS_ToString(address, string2, sizeof(string2), 1);
436 printf("manual parsing of ipv4 dotted decimal address \"%s\" successful: %s\n", string, string2);
440 for (i = 0;i < MAX_NAMECACHE;i++)
441 if (!strcmp(namecache[i].name, name))
443 #ifdef STANDALONETEST
444 if (i < MAX_NAMECACHE)
446 if (i < MAX_NAMECACHE && realtime < namecache[i].expirationtime)
449 *address = namecache[i].address;
450 address->port = port;
451 if (address->addresstype == LHNETADDRESSTYPE_INET6)
454 address->addr.in6.sin6_port = htons((unsigned short)port);
458 else if (address->addresstype == LHNETADDRESSTYPE_INET4)
460 address->addr.in.sin_port = htons((unsigned short)port);
465 // try gethostbyname (handles dns and other ip formats)
466 hostentry = gethostbyname(name);
469 if (hostentry->h_addrtype == AF_INET6)
473 address->addresstype = LHNETADDRESSTYPE_INET6;
474 address->port = port;
475 address->addr.in6.sin6_family = hostentry->h_addrtype;
476 address->addr.in6.sin6_port = htons((unsigned short)port);
477 memcpy(&address->addr.in6.sin6_addr, hostentry->h_addr_list[0], sizeof(address->addr.in6.sin6_addr));
478 for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
479 namecache[namecacheposition].name[i] = name[i];
480 namecache[namecacheposition].name[i] = 0;
481 #ifndef STANDALONETEST
482 namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
484 namecache[namecacheposition].address = *address;
485 namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
486 #ifdef STANDALONETEST
487 LHNETADDRESS_ToString(address, string2, sizeof(string2), 1);
488 printf("gethostbyname(\"%s\") returned ipv6 address %s\n", string, string2);
493 else if (hostentry->h_addrtype == AF_INET)
496 address->addresstype = LHNETADDRESSTYPE_INET4;
497 address->port = port;
498 address->addr.in.sin_family = hostentry->h_addrtype;
499 address->addr.in.sin_port = htons((unsigned short)port);
500 memcpy(&address->addr.in.sin_addr, hostentry->h_addr_list[0], sizeof(address->addr.in.sin_addr));
501 for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
502 namecache[namecacheposition].name[i] = name[i];
503 namecache[namecacheposition].name[i] = 0;
504 #ifndef STANDALONETEST
505 namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
507 namecache[namecacheposition].address = *address;
508 namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
509 #ifdef STANDALONETEST
510 LHNETADDRESS_ToString(address, string2, sizeof(string2), 1);
511 printf("gethostbyname(\"%s\") returned ipv4 address %s\n", string, string2);
516 #ifdef STANDALONETEST
517 printf("gethostbyname failed on address \"%s\"\n", name);
519 for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
520 namecache[namecacheposition].name[i] = name[i];
521 namecache[namecacheposition].name[i] = 0;
522 #ifndef STANDALONETEST
523 namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
525 namecache[namecacheposition].address.addresstype = LHNETADDRESSTYPE_NONE;
526 namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
531 int LHNETADDRESS_ToString(const lhnetaddress_t *vaddress, char *string, int stringbuffersize, int includeport)
533 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
534 const unsigned char *a;
536 if (!address || !string || stringbuffersize < 1)
538 switch(address->addresstype)
542 case LHNETADDRESSTYPE_LOOP:
545 if (stringbuffersize >= 12)
547 dpsnprintf(string, stringbuffersize, "local:%d", address->port);
553 if (stringbuffersize >= 6)
555 memcpy(string, "local", 6);
560 case LHNETADDRESSTYPE_INET4:
561 a = (const unsigned char *)(&address->addr.in.sin_addr);
564 if (stringbuffersize >= 22)
566 dpsnprintf(string, stringbuffersize, "%d.%d.%d.%d:%d", a[0], a[1], a[2], a[3], address->port);
572 if (stringbuffersize >= 16)
574 dpsnprintf(string, stringbuffersize, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
580 case LHNETADDRESSTYPE_INET6:
581 a = (const unsigned char *)(&address->addr.in6.sin6_addr);
584 if (stringbuffersize >= 88)
586 dpsnprintf(string, stringbuffersize, "[%x:%x:%x:%x:%x:%x:%x:%x]:%d", a[0] * 256 + a[1], a[2] * 256 + a[3], a[4] * 256 + a[5], a[6] * 256 + a[7], a[8] * 256 + a[9], a[10] * 256 + a[11], a[12] * 256 + a[13], a[14] * 256 + a[15], address->port);
592 if (stringbuffersize >= 80)
594 dpsnprintf(string, stringbuffersize, "%x:%x:%x:%x:%x:%x:%x:%x", a[0] * 256 + a[1], a[2] * 256 + a[3], a[4] * 256 + a[5], a[6] * 256 + a[7], a[8] * 256 + a[9], a[10] * 256 + a[11], a[12] * 256 + a[13], a[14] * 256 + a[15]);
604 int LHNETADDRESS_GetAddressType(const lhnetaddress_t *address)
607 return address->addresstype;
609 return LHNETADDRESSTYPE_NONE;
612 const char *LHNETADDRESS_GetInterfaceName(const lhnetaddress_t *vaddress)
615 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
617 if (address && address->addresstype == LHNETADDRESSTYPE_INET6)
621 static char ifname [IF_NAMESIZE];
623 if (if_indextoname(address->addr.in6.sin6_scope_id, ifname) == ifname)
628 // The Win32 API doesn't have if_indextoname() until Windows Vista,
629 // but luckily it just uses the interface ID as the interface name
631 static char ifname [16];
633 if (dpsnprintf(ifname, sizeof(ifname), "%lu", address->addr.in6.sin6_scope_id) > 0)
643 int LHNETADDRESS_GetPort(const lhnetaddress_t *address)
647 return address->port;
650 int LHNETADDRESS_SetPort(lhnetaddress_t *vaddress, int port)
652 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
655 address->port = port;
656 switch(address->addresstype)
658 case LHNETADDRESSTYPE_LOOP:
660 case LHNETADDRESSTYPE_INET4:
661 address->addr.in.sin_port = htons((unsigned short)port);
664 case LHNETADDRESSTYPE_INET6:
665 address->addr.in6.sin6_port = htons((unsigned short)port);
673 int LHNETADDRESS_Compare(const lhnetaddress_t *vaddress1, const lhnetaddress_t *vaddress2)
675 lhnetaddressnative_t *address1 = (lhnetaddressnative_t *)vaddress1;
676 lhnetaddressnative_t *address2 = (lhnetaddressnative_t *)vaddress2;
677 if (!address1 || !address2)
679 if (address1->addresstype != address2->addresstype)
681 switch(address1->addresstype)
683 case LHNETADDRESSTYPE_LOOP:
684 if (address1->port != address2->port)
687 case LHNETADDRESSTYPE_INET4:
688 if (address1->addr.in.sin_family != address2->addr.in.sin_family)
690 if (memcmp(&address1->addr.in.sin_addr, &address2->addr.in.sin_addr, sizeof(address1->addr.in.sin_addr)))
692 if (address1->port != address2->port)
696 case LHNETADDRESSTYPE_INET6:
697 if (address1->addr.in6.sin6_family != address2->addr.in6.sin6_family)
699 if (memcmp(&address1->addr.in6.sin6_addr, &address2->addr.in6.sin6_addr, sizeof(address1->addr.in6.sin6_addr)))
701 if (address1->port != address2->port)
710 typedef struct lhnetpacket_s
717 #ifndef STANDALONETEST
718 double sentdoubletime;
720 struct lhnetpacket_s *next, *prev;
724 static int lhnet_active;
725 static lhnetsocket_t lhnet_socketlist;
726 static lhnetpacket_t lhnet_packetlist;
728 static int lhnet_didWSAStartup = 0;
729 static WSADATA lhnet_winsockdata;
732 void LHNET_Init(void)
736 lhnet_socketlist.next = lhnet_socketlist.prev = &lhnet_socketlist;
737 lhnet_packetlist.next = lhnet_packetlist.prev = &lhnet_packetlist;
740 lhnet_didWSAStartup = !WSAStartup(MAKEWORD(1, 1), &lhnet_winsockdata);
741 if (!lhnet_didWSAStartup)
742 Con_Print("LHNET_Init: WSAStartup failed, networking disabled\n");
746 void LHNET_Shutdown(void)
751 while (lhnet_socketlist.next != &lhnet_socketlist)
752 LHNET_CloseSocket(lhnet_socketlist.next);
753 while (lhnet_packetlist.next != &lhnet_packetlist)
755 p = lhnet_packetlist.next;
756 p->prev->next = p->next;
757 p->next->prev = p->prev;
761 if (lhnet_didWSAStartup)
763 lhnet_didWSAStartup = 0;
770 static const char *LHNETPRIVATE_StrError(void)
773 int i = WSAGetLastError();
776 case WSAEINTR: return "WSAEINTR";
777 case WSAEBADF: return "WSAEBADF";
778 case WSAEACCES: return "WSAEACCES";
779 case WSAEFAULT: return "WSAEFAULT";
780 case WSAEINVAL: return "WSAEINVAL";
781 case WSAEMFILE: return "WSAEMFILE";
782 case WSAEWOULDBLOCK: return "WSAEWOULDBLOCK";
783 case WSAEINPROGRESS: return "WSAEINPROGRESS";
784 case WSAEALREADY: return "WSAEALREADY";
785 case WSAENOTSOCK: return "WSAENOTSOCK";
786 case WSAEDESTADDRREQ: return "WSAEDESTADDRREQ";
787 case WSAEMSGSIZE: return "WSAEMSGSIZE";
788 case WSAEPROTOTYPE: return "WSAEPROTOTYPE";
789 case WSAENOPROTOOPT: return "WSAENOPROTOOPT";
790 case WSAEPROTONOSUPPORT: return "WSAEPROTONOSUPPORT";
791 case WSAESOCKTNOSUPPORT: return "WSAESOCKTNOSUPPORT";
792 case WSAEOPNOTSUPP: return "WSAEOPNOTSUPP";
793 case WSAEPFNOSUPPORT: return "WSAEPFNOSUPPORT";
794 case WSAEAFNOSUPPORT: return "WSAEAFNOSUPPORT";
795 case WSAEADDRINUSE: return "WSAEADDRINUSE";
796 case WSAEADDRNOTAVAIL: return "WSAEADDRNOTAVAIL";
797 case WSAENETDOWN: return "WSAENETDOWN";
798 case WSAENETUNREACH: return "WSAENETUNREACH";
799 case WSAENETRESET: return "WSAENETRESET";
800 case WSAECONNABORTED: return "WSAECONNABORTED";
801 case WSAECONNRESET: return "WSAECONNRESET";
802 case WSAENOBUFS: return "WSAENOBUFS";
803 case WSAEISCONN: return "WSAEISCONN";
804 case WSAENOTCONN: return "WSAENOTCONN";
805 case WSAESHUTDOWN: return "WSAESHUTDOWN";
806 case WSAETOOMANYREFS: return "WSAETOOMANYREFS";
807 case WSAETIMEDOUT: return "WSAETIMEDOUT";
808 case WSAECONNREFUSED: return "WSAECONNREFUSED";
809 case WSAELOOP: return "WSAELOOP";
810 case WSAENAMETOOLONG: return "WSAENAMETOOLONG";
811 case WSAEHOSTDOWN: return "WSAEHOSTDOWN";
812 case WSAEHOSTUNREACH: return "WSAEHOSTUNREACH";
813 case WSAENOTEMPTY: return "WSAENOTEMPTY";
814 case WSAEPROCLIM: return "WSAEPROCLIM";
815 case WSAEUSERS: return "WSAEUSERS";
816 case WSAEDQUOT: return "WSAEDQUOT";
817 case WSAESTALE: return "WSAESTALE";
818 case WSAEREMOTE: return "WSAEREMOTE";
819 case WSAEDISCON: return "WSAEDISCON";
820 case 0: return "no error";
821 default: return "unknown WSAE error";
824 return strerror(errno);
828 void LHNET_SleepUntilPacket_Microseconds(int microseconds)
837 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
839 if (s->address.addresstype == LHNETADDRESSTYPE_INET4 || s->address.addresstype == LHNETADDRESSTYPE_INET6)
841 if (lastfd < s->inetsocket)
842 lastfd = s->inetsocket;
843 FD_SET((unsigned int)s->inetsocket, &fdreadset);
846 tv.tv_sec = microseconds / 1000000;
847 tv.tv_usec = microseconds % 1000000;
848 select(lastfd + 1, &fdreadset, NULL, NULL, &tv);
850 Sys_Sleep(microseconds);
854 lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
856 lhnetsocket_t *lhnetsocket, *s;
859 lhnetsocket = (lhnetsocket_t *)Z_Malloc(sizeof(*lhnetsocket));
862 memset(lhnetsocket, 0, sizeof(*lhnetsocket));
863 lhnetsocket->address = *address;
864 switch(lhnetsocket->address.addresstype)
866 case LHNETADDRESSTYPE_LOOP:
867 if (lhnetsocket->address.port == 0)
869 // allocate a port dynamically
870 // this search will always terminate because there is never
871 // an allocated socket with port 0, so if the number wraps it
872 // will find the port is unused, and then refuse to use port
873 // 0, causing an intentional failure condition
874 lhnetsocket->address.port = 1024;
877 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
878 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.port == lhnetsocket->address.port)
880 if (s == &lhnet_socketlist)
882 lhnetsocket->address.port++;
885 // check if the port is available
886 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
887 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.port == lhnetsocket->address.port)
889 if (s == &lhnet_socketlist && lhnetsocket->address.port != 0)
891 lhnetsocket->next = &lhnet_socketlist;
892 lhnetsocket->prev = lhnetsocket->next->prev;
893 lhnetsocket->next->prev = lhnetsocket;
894 lhnetsocket->prev->next = lhnetsocket;
898 case LHNETADDRESSTYPE_INET4:
900 case LHNETADDRESSTYPE_INET6:
903 if (lhnet_didWSAStartup)
907 if ((lhnetsocket->inetsocket = socket(address->addresstype == LHNETADDRESSTYPE_INET6 ? PF_INET6 : PF_INET, SOCK_DGRAM, IPPROTO_UDP)) != -1)
909 if ((lhnetsocket->inetsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) != -1)
923 if (ioctlsocket(lhnetsocket->inetsocket, FIONBIO, &_true) != -1)
927 // We need to set this flag to tell the OS that we only listen on IPv6. If we don't
928 // most OSes will create a dual-protocol socket that also listens on IPv4. In this case
929 // if an IPv4 socket is already bound to the port we want, our bind() call will fail.
931 if (address->addresstype != LHNETADDRESSTYPE_INET6
932 || setsockopt (lhnetsocket->inetsocket, IPPROTO_IPV6, IPV6_V6ONLY,
933 (const char *)&ipv6_only, sizeof(ipv6_only)) == 0
935 // The Win32 API only supports IPV6_V6ONLY since Windows Vista, but fortunately
936 // the default value is what we want on Win32 anyway (IPV6_V6ONLY = true)
937 || SOCKETERRNO == WSAENOPROTOOPT
942 lhnetaddressnative_t *localaddress = (lhnetaddressnative_t *)&lhnetsocket->address;
946 if (address->addresstype == LHNETADDRESSTYPE_INET6)
948 namelen = sizeof(localaddress->addr.in6);
949 bindresult = bind(lhnetsocket->inetsocket, &localaddress->addr.sock, namelen);
950 if (bindresult != -1)
951 getsockname(lhnetsocket->inetsocket, &localaddress->addr.sock, &namelen);
956 namelen = sizeof(localaddress->addr.in);
957 bindresult = bind(lhnetsocket->inetsocket, &localaddress->addr.sock, namelen);
958 if (bindresult != -1)
959 getsockname(lhnetsocket->inetsocket, &localaddress->addr.sock, &namelen);
961 if (bindresult != -1)
964 // enable broadcast on this socket
965 setsockopt(lhnetsocket->inetsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i));
966 lhnetsocket->next = &lhnet_socketlist;
967 lhnetsocket->prev = lhnetsocket->next->prev;
968 lhnetsocket->next->prev = lhnetsocket;
969 lhnetsocket->prev->next = lhnetsocket;
971 if (ioctlsocket(lhnetsocket->inetsocket, SIO_UDP_CONNRESET, &_false) == -1)
972 Con_DPrintf("LHNET_OpenSocket_Connectionless: ioctlsocket SIO_UDP_CONNRESET returned error: %s\n", LHNETPRIVATE_StrError());
977 Con_Printf("LHNET_OpenSocket_Connectionless: bind returned error: %s\n", LHNETPRIVATE_StrError());
981 Con_Printf("LHNET_OpenSocket_Connectionless: setsockopt(IPV6_V6ONLY) returned error: %s\n", LHNETPRIVATE_StrError());
985 Con_Printf("LHNET_OpenSocket_Connectionless: ioctlsocket returned error: %s\n", LHNETPRIVATE_StrError());
986 closesocket(lhnetsocket->inetsocket);
989 Con_Printf("LHNET_OpenSocket_Connectionless: socket returned error: %s\n", LHNETPRIVATE_StrError());
993 Con_Print("LHNET_OpenSocket_Connectionless: can't open a socket (WSAStartup failed during LHNET_Init)\n");
1004 void LHNET_CloseSocket(lhnetsocket_t *lhnetsocket)
1008 // unlink from socket list
1009 if (lhnetsocket->next == NULL)
1011 lhnetsocket->next->prev = lhnetsocket->prev;
1012 lhnetsocket->prev->next = lhnetsocket->next;
1013 lhnetsocket->next = NULL;
1014 lhnetsocket->prev = NULL;
1016 // no special close code for loopback, just inet
1017 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4 || lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
1019 closesocket(lhnetsocket->inetsocket);
1021 Z_Free(lhnetsocket);
1025 lhnetaddress_t *LHNET_AddressFromSocket(lhnetsocket_t *sock)
1028 return &sock->address;
1033 int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, lhnetaddress_t *vaddress)
1035 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
1037 if (!lhnetsocket || !address || !content || maxcontentlength < 1)
1039 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
1042 lhnetpacket_t *p, *pnext;
1043 // scan for any old packets to timeout while searching for a packet
1044 // that is waiting to be delivered to this socket
1045 currenttime = time(NULL);
1046 for (p = lhnet_packetlist.next;p != &lhnet_packetlist;p = pnext)
1049 if (p->timeout < currenttime)
1052 p->next->prev = p->prev;
1053 p->prev->next = p->next;
1057 #ifndef STANDALONETEST
1058 if (cl_netlocalping.value && (realtime - cl_netlocalping.value * (1.0 / 2000.0)) < p->sentdoubletime)
1061 if (value == 0 && p->destinationport == lhnetsocket->address.port)
1063 if (p->length <= maxcontentlength)
1065 lhnetaddressnative_t *localaddress = (lhnetaddressnative_t *)&lhnetsocket->address;
1066 *address = *localaddress;
1067 address->port = p->sourceport;
1068 memcpy(content, p->data, p->length);
1074 p->next->prev = p->prev;
1075 p->prev->next = p->next;
1080 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
1082 SOCKLEN_T inetaddresslength;
1083 address->addresstype = LHNETADDRESSTYPE_NONE;
1084 inetaddresslength = sizeof(address->addr.in);
1085 value = recvfrom(lhnetsocket->inetsocket, (char *)content, maxcontentlength, LHNET_RECVFROM_FLAGS, &address->addr.sock, &inetaddresslength);
1088 address->addresstype = LHNETADDRESSTYPE_INET4;
1089 address->port = ntohs(address->addr.in.sin_port);
1094 int e = SOCKETERRNO;
1095 if (e == EWOULDBLOCK)
1100 Con_Print("Connection refused\n");
1103 Con_DPrintf("LHNET_Read: recvfrom returned error: %s\n", LHNETPRIVATE_StrError());
1107 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
1109 SOCKLEN_T inetaddresslength;
1110 address->addresstype = LHNETADDRESSTYPE_NONE;
1111 inetaddresslength = sizeof(address->addr.in6);
1112 value = recvfrom(lhnetsocket->inetsocket, (char *)content, maxcontentlength, LHNET_RECVFROM_FLAGS, &address->addr.sock, &inetaddresslength);
1115 address->addresstype = LHNETADDRESSTYPE_INET6;
1116 address->port = ntohs(address->addr.in6.sin6_port);
1119 else if (value == -1)
1121 int e = SOCKETERRNO;
1122 if (e == EWOULDBLOCK)
1127 Con_Print("Connection refused\n");
1130 Con_DPrintf("LHNET_Read: recvfrom returned error: %s\n", LHNETPRIVATE_StrError());
1137 int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentlength, const lhnetaddress_t *vaddress)
1139 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
1141 if (!lhnetsocket || !address || !content || contentlength < 1)
1143 if (lhnetsocket->address.addresstype != address->addresstype)
1145 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
1148 p = (lhnetpacket_t *)Z_Malloc(sizeof(*p) + contentlength);
1149 p->data = (void *)(p + 1);
1150 memcpy(p->data, content, contentlength);
1151 p->length = contentlength;
1152 p->sourceport = lhnetsocket->address.port;
1153 p->destinationport = address->port;
1154 p->timeout = time(NULL) + 10;
1155 p->next = &lhnet_packetlist;
1156 p->prev = p->next->prev;
1159 #ifndef STANDALONETEST
1160 p->sentdoubletime = realtime;
1162 value = contentlength;
1164 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
1166 value = sendto(lhnetsocket->inetsocket, (char *)content, contentlength, LHNET_SENDTO_FLAGS, (struct sockaddr *)&address->addr.in, sizeof(struct sockaddr_in));
1169 if (SOCKETERRNO == EWOULDBLOCK)
1171 Con_DPrintf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError());
1175 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
1177 value = sendto(lhnetsocket->inetsocket, (char *)content, contentlength, 0, (struct sockaddr *)&address->addr.in6, sizeof(struct sockaddr_in6));
1180 if (SOCKETERRNO == EWOULDBLOCK)
1182 Con_DPrintf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError());
1189 #ifdef STANDALONETEST
1190 int main(int argc, char **argv)
1193 char *buffer = "test", buffer2[1024];
1194 int blen = strlen(buffer);
1196 lhnetsocket_t *sock1;
1197 lhnetsocket_t *sock2;
1198 lhnetaddress_t myaddy1;
1199 lhnetaddress_t myaddy2;
1200 lhnetaddress_t myaddy3;
1201 lhnetaddress_t localhostaddy1;
1202 lhnetaddress_t localhostaddy2;
1206 printf("calling LHNET_Init\n");
1209 printf("calling LHNET_FromPort twice to create two local addresses\n");
1210 LHNETADDRESS_FromPort(&myaddy1, LHNETADDRESSTYPE_INET4, 4000);
1211 LHNETADDRESS_FromPort(&myaddy2, LHNETADDRESSTYPE_INET4, 4001);
1212 LHNETADDRESS_FromString(&localhostaddy1, "127.0.0.1", 4000);
1213 LHNETADDRESS_FromString(&localhostaddy2, "127.0.0.1", 4001);
1215 printf("calling LHNET_OpenSocket_Connectionless twice to create two local sockets\n");
1216 sock1 = LHNET_OpenSocket_Connectionless(&myaddy1);
1217 sock2 = LHNET_OpenSocket_Connectionless(&myaddy2);
1219 printf("calling LHNET_Write to send a packet from the first socket to the second socket\n");
1220 test1 = LHNET_Write(sock1, buffer, blen, &localhostaddy2);
1221 printf("sleeping briefly\n");
1227 printf("calling LHNET_Read on the second socket to read the packet sent from the first socket\n");
1228 test2 = LHNET_Read(sock2, buffer2, b2len - 1, &myaddy3);
1230 Con_Printf("socket to socket test succeeded\n");
1232 Con_Printf("socket to socket test failed\n");
1235 printf("press any key to exit\n");
1239 printf("calling LHNET_Shutdown\n");
1241 printf("exiting\n");
1244 lhnetsocket_t *sock[16], *sendsock;
1253 int sendmessagelength;
1254 lhnetaddress_t destaddress;
1255 lhnetaddress_t receiveaddress;
1256 lhnetaddress_t sockaddress[16];
1257 char buffer[1536], addressstring[128], addressstring2[128];
1258 if ((argc == 2 || argc == 5) && (port = atoi(argv[1])) >= 1 && port < 65535)
1260 printf("calling LHNET_Init()\n");
1264 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_LOOP, port);
1265 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET4, port);
1266 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET6, port+1);
1270 sendmessagelength = 0;
1272 for (i = 0;i < numsockets;i++)
1274 LHNETADDRESS_ToString(&sockaddress[i], addressstring, sizeof(addressstring), 1);
1275 printf("calling LHNET_OpenSocket_Connectionless(<%s>)\n", addressstring);
1276 if ((sock[i] = LHNET_OpenSocket_Connectionless(&sockaddress[i])))
1278 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
1279 printf("opened socket successfully (address \"%s\")\n", addressstring2);
1283 printf("failed to open socket\n");
1294 count = atoi(argv[2]);
1295 if (LHNETADDRESS_FromString(&destaddress, argv[3], -1))
1297 sendmessage = argv[4];
1298 sendmessagelength = strlen(sendmessage);
1300 for (i = 0;i < numsockets;i++)
1301 if (sock[i] && LHNETADDRESS_GetAddressType(&destaddress) == LHNETADDRESS_GetAddressType(&sockaddress[i]))
1303 if (sendsock == NULL)
1305 printf("Could not find an open socket matching the addresstype (%i) of destination address, switching to listen only mode\n", LHNETADDRESS_GetAddressType(&destaddress));
1311 printf("LHNETADDRESS_FromString did not like the address \"%s\", switching to listen only mode\n", argv[3]);
1315 printf("started, now listening for \"exit\" on the opened sockets\n");
1316 oldtime = time(NULL);
1324 for (i = 0;i < numsockets;i++)
1328 length = LHNET_Read(sock[i], buffer, sizeof(buffer), &receiveaddress);
1330 printf("localsock read error: length < 0");
1331 else if (length > 0 && length < (int)sizeof(buffer))
1334 LHNETADDRESS_ToString(&receiveaddress, addressstring, sizeof(addressstring), 1);
1335 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
1336 printf("received message \"%s\" from \"%s\" on socket \"%s\"\n", buffer, addressstring, addressstring2);
1337 if (!strcmp(buffer, "exit"))
1344 if (argc == 5 && count > 0)
1346 newtime = time(NULL);
1347 if (newtime != oldtime)
1349 LHNETADDRESS_ToString(&destaddress, addressstring, sizeof(addressstring), 1);
1350 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sendsock), addressstring2, sizeof(addressstring2), 1);
1351 printf("calling LHNET_Write(<%s>, \"%s\", %i, <%s>)\n", addressstring2, sendmessage, sendmessagelength, addressstring);
1352 length = LHNET_Write(sendsock, sendmessage, sendmessagelength, &destaddress);
1353 if (length == sendmessagelength)
1354 printf("sent successfully\n");
1356 printf("LH_Write failed, returned %i (length of message was %i)\n", length, strlen(argv[4]));
1360 printf("Done sending, still listening for \"exit\"\n");
1364 for (i = 0;i < numsockets;i++)
1368 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
1369 printf("calling LHNET_CloseSocket(<%s>)\n", addressstring2);
1370 LHNET_CloseSocket(sock[i]);
1373 printf("calling LHNET_Shutdown()\n");
1377 printf("Testing code for lhnet.c\nusage: lhnettest <localportnumber> [<sendnumberoftimes> <sendaddress:port> <sendmessage>]\n");