2 // Written by Forest Hale 2003-06-15 and placed into public domain.
5 // Windows XP or higher is required for getaddrinfo, but the inclusion of wspiapi provides fallbacks for older versions
6 # define _WIN32_WINNT 0x0501
14 #ifndef STANDALONETEST
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <sys/ioctl.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
35 #include <proto/socket.h>
38 // for Z_Malloc/Z_Free in quake
39 #ifndef STANDALONETEST
44 #define Con_Print printf
45 #define Con_Printf printf
46 #define Z_Malloc malloc
53 #define EWOULDBLOCK WSAEWOULDBLOCK
54 #define ECONNREFUSED WSAECONNREFUSED
56 #define SOCKETERRNO WSAGetLastError()
58 #define IOC_VENDOR 0x18000000
59 #define _WSAIOW(x,y) (IOC_IN|(x)|(y))
60 #define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
63 #elif defined(__MORPHOS__)
64 #define ioctlsocket IoctlSocket
65 #define closesocket CloseSocket
66 #define SOCKETERRNO Errno()
70 #define ioctlsocket ioctl
71 #define closesocket close
72 #define SOCKETERRNO errno
74 #define SOCKLEN_T socklen_t
77 typedef struct lhnetaddressnative_s
79 lhnetaddresstype_t addresstype;
84 struct sockaddr_in in;
85 struct sockaddr_in6 in6;
91 // to make LHNETADDRESS_FromString resolve repeated hostnames faster, cache them
92 #define MAX_NAMECACHE 64
93 static struct namecache_s
95 lhnetaddressnative_t address;
96 double expirationtime;
99 namecache[MAX_NAMECACHE];
100 static int namecacheposition = 0;
102 int LHNETADDRESS_FromPort(lhnetaddress_t *vaddress, lhnetaddresstype_t addresstype, int port)
104 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
111 case LHNETADDRESSTYPE_LOOP:
112 // local:port (loopback)
113 memset(address, 0, sizeof(*address));
114 address->addresstype = LHNETADDRESSTYPE_LOOP;
115 address->port = port;
117 case LHNETADDRESSTYPE_INET4:
118 // 0.0.0.0:port (INADDR_ANY, binds to all interfaces)
119 memset(address, 0, sizeof(*address));
120 address->addresstype = LHNETADDRESSTYPE_INET4;
121 address->port = port;
122 address->addr.in.sin_family = AF_INET;
123 address->addr.in.sin_port = htons((unsigned short)port);
125 case LHNETADDRESSTYPE_INET6:
126 // [0:0:0:0:0:0:0:0]:port (IN6ADDR_ANY, binds to all interfaces)
127 memset(address, 0, sizeof(*address));
128 address->addresstype = LHNETADDRESSTYPE_INET6;
129 address->port = port;
130 address->addr.in6.sin6_family = AF_INET6;
131 address->addr.in6.sin6_port = htons((unsigned short)port);
137 int LHNETADDRESS_Resolve(lhnetaddressnative_t *address, const char *name, int port)
140 struct addrinfo hints;
141 struct addrinfo* addrinf;
144 dpsnprintf (port_buff, sizeof (port_buff), "%d", port);
145 port_buff[sizeof (port_buff) - 1] = '\0';
147 memset(&hints, 0, sizeof (hints));
148 hints.ai_family = AF_UNSPEC;
149 hints.ai_socktype = SOCK_DGRAM;
150 //hints.ai_flags = AI_PASSIVE;
152 err = getaddrinfo(name, port_buff, &hints, &addrinf);
153 if (err != 0 || addrinf == NULL)
155 if (addrinf->ai_addr->sa_family != AF_INET6 && addrinf->ai_addr->sa_family != AF_INET)
157 freeaddrinfo (addrinf);
162 if (addrinf->ai_addr->sa_family == AF_INET6)
164 address->addresstype = LHNETADDRESSTYPE_INET6;
165 memcpy(&address->addr.in6, addrinf->ai_addr, sizeof(address->addr.in6));
169 address->addresstype = LHNETADDRESSTYPE_INET4;
170 memcpy(&address->addr.in, addrinf->ai_addr, sizeof(address->addr.in));
172 address->port = port;
174 freeaddrinfo (addrinf);
178 int LHNETADDRESS_FromString(lhnetaddress_t *vaddress, const char *string, int defaultport)
180 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
181 int i, port, d1, d2, d3, d4, resolved;
185 #ifdef STANDALONETEST
188 const char* addr_start;
189 const char* addr_end = NULL;
190 const char* port_name = NULL;
191 int addr_family = AF_UNSPEC;
193 if (!address || !string || !*string)
195 memset(address, 0, sizeof(*address));
196 address->addresstype = LHNETADDRESSTYPE_NONE;
199 // If it's a bracketed IPv6 address
200 if (string[0] == '[')
202 const char* end_bracket = strchr(string, ']');
204 if (end_bracket == NULL)
207 if (end_bracket[1] == ':')
208 port_name = end_bracket + 2;
209 else if (end_bracket[1] != '\0')
212 addr_family = AF_INET6;
213 addr_start = &string[1];
214 addr_end = end_bracket;
218 const char* first_colon;
222 // If it's a numeric non-bracket IPv6 address (-> no port),
223 // or it's a numeric IPv4 address, or a name, with a port
224 first_colon = strchr(string, ':');
225 if (first_colon != NULL)
227 const char* last_colon = strrchr(first_colon + 1, ':');
229 // If it's an numeric IPv4 address, or a name, with a port
230 if (last_colon == NULL)
232 addr_end = first_colon;
233 port_name = first_colon + 1;
236 addr_family = AF_INET6;
240 if (addr_end != NULL)
241 namelen = addr_end - addr_start;
243 namelen = strlen (addr_start);
245 if (namelen >= sizeof(name))
246 namelen = sizeof(name) - 1;
247 memcpy (name, addr_start, namelen);
251 port = atoi(port_name);
257 if (!strcmp(name, "local"))
259 address->addresstype = LHNETADDRESSTYPE_LOOP;
260 address->port = port;
263 // try to parse as dotted decimal ipv4 address first
264 // note this supports partial ip addresses
265 d1 = d2 = d3 = d4 = 0;
267 #define sscanf sscanf_s
269 if (addr_family != AF_INET6 &&
270 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)
272 // parsed a valid ipv4 address
273 address->addresstype = LHNETADDRESSTYPE_INET4;
274 address->port = port;
275 address->addr.in.sin_family = AF_INET;
276 address->addr.in.sin_port = htons((unsigned short)port);
277 a = (unsigned char *)&address->addr.in.sin_addr;
282 #ifdef STANDALONETEST
283 LHNETADDRESS_ToString(address, string2, sizeof(string2), 1);
284 printf("manual parsing of ipv4 dotted decimal address \"%s\" successful: %s\n", string, string2);
288 for (i = 0;i < MAX_NAMECACHE;i++)
289 if (!strcmp(namecache[i].name, name))
291 #ifdef STANDALONETEST
292 if (i < MAX_NAMECACHE)
294 if (i < MAX_NAMECACHE && realtime < namecache[i].expirationtime)
297 *address = namecache[i].address;
298 address->port = port;
299 if (address->addresstype == LHNETADDRESSTYPE_INET6)
301 address->addr.in6.sin6_port = htons((unsigned short)port);
304 else if (address->addresstype == LHNETADDRESSTYPE_INET4)
306 address->addr.in.sin_port = htons((unsigned short)port);
312 for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
313 namecache[namecacheposition].name[i] = name[i];
314 namecache[namecacheposition].name[i] = 0;
315 #ifndef STANDALONETEST
316 namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
319 // try resolving the address (handles dns and other ip formats)
320 resolved = LHNETADDRESS_Resolve(address, name, port);
323 #ifdef STANDALONETEST
324 const char *protoname;
326 switch (address->addresstype)
328 case LHNETADDRESSTYPE_INET6:
331 case LHNETADDRESSTYPE_INET4:
335 protoname = "UNKNOWN";
338 LHNETADDRESS_ToString(vaddress, string2, sizeof(string2), 1);
339 Con_Printf("LHNETADDRESS_Resolve(\"%s\") returned %s address %s\n", string, protoname, string2);
341 namecache[namecacheposition].address = *address;
345 #ifdef STANDALONETEST
346 printf("name resolution failed on address \"%s\"\n", name);
348 namecache[namecacheposition].address.addresstype = LHNETADDRESSTYPE_NONE;
351 namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
355 int LHNETADDRESS_ToString(const lhnetaddress_t *vaddress, char *string, int stringbuffersize, int includeport)
357 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
358 const unsigned char *a;
360 if (!address || !string || stringbuffersize < 1)
362 switch(address->addresstype)
366 case LHNETADDRESSTYPE_LOOP:
369 if (stringbuffersize >= 12)
371 dpsnprintf(string, stringbuffersize, "local:%d", address->port);
377 if (stringbuffersize >= 6)
379 memcpy(string, "local", 6);
384 case LHNETADDRESSTYPE_INET4:
385 a = (const unsigned char *)(&address->addr.in.sin_addr);
388 if (stringbuffersize >= 22)
390 dpsnprintf(string, stringbuffersize, "%d.%d.%d.%d:%d", a[0], a[1], a[2], a[3], address->port);
396 if (stringbuffersize >= 16)
398 dpsnprintf(string, stringbuffersize, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
403 case LHNETADDRESSTYPE_INET6:
404 a = (const unsigned char *)(&address->addr.in6.sin6_addr);
407 if (stringbuffersize >= 88)
409 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);
415 if (stringbuffersize >= 80)
417 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]);
426 int LHNETADDRESS_GetAddressType(const lhnetaddress_t *address)
429 return address->addresstype;
431 return LHNETADDRESSTYPE_NONE;
434 const char *LHNETADDRESS_GetInterfaceName(const lhnetaddress_t *vaddress)
436 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
438 if (address && address->addresstype == LHNETADDRESSTYPE_INET6)
442 static char ifname [IF_NAMESIZE];
444 if (if_indextoname(address->addr.in6.sin6_scope_id, ifname) == ifname)
449 // The Win32 API doesn't have if_indextoname() until Windows Vista,
450 // but luckily it just uses the interface ID as the interface name
452 static char ifname [16];
454 if (dpsnprintf(ifname, sizeof(ifname), "%lu", address->addr.in6.sin6_scope_id) > 0)
463 int LHNETADDRESS_GetPort(const lhnetaddress_t *address)
467 return address->port;
470 int LHNETADDRESS_SetPort(lhnetaddress_t *vaddress, int port)
472 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
475 address->port = port;
476 switch(address->addresstype)
478 case LHNETADDRESSTYPE_LOOP:
480 case LHNETADDRESSTYPE_INET4:
481 address->addr.in.sin_port = htons((unsigned short)port);
483 case LHNETADDRESSTYPE_INET6:
484 address->addr.in6.sin6_port = htons((unsigned short)port);
491 int LHNETADDRESS_Compare(const lhnetaddress_t *vaddress1, const lhnetaddress_t *vaddress2)
493 lhnetaddressnative_t *address1 = (lhnetaddressnative_t *)vaddress1;
494 lhnetaddressnative_t *address2 = (lhnetaddressnative_t *)vaddress2;
495 if (!address1 || !address2)
497 if (address1->addresstype != address2->addresstype)
499 switch(address1->addresstype)
501 case LHNETADDRESSTYPE_LOOP:
502 if (address1->port != address2->port)
505 case LHNETADDRESSTYPE_INET4:
506 if (address1->addr.in.sin_family != address2->addr.in.sin_family)
508 if (memcmp(&address1->addr.in.sin_addr, &address2->addr.in.sin_addr, sizeof(address1->addr.in.sin_addr)))
510 if (address1->port != address2->port)
513 case LHNETADDRESSTYPE_INET6:
514 if (address1->addr.in6.sin6_family != address2->addr.in6.sin6_family)
516 if (memcmp(&address1->addr.in6.sin6_addr, &address2->addr.in6.sin6_addr, sizeof(address1->addr.in6.sin6_addr)))
518 if (address1->port != address2->port)
526 typedef struct lhnetpacket_s
533 #ifndef STANDALONETEST
534 double sentdoubletime;
536 struct lhnetpacket_s *next, *prev;
540 static int lhnet_active;
541 static lhnetsocket_t lhnet_socketlist;
542 static lhnetpacket_t lhnet_packetlist;
544 static int lhnet_didWSAStartup = 0;
545 static WSADATA lhnet_winsockdata;
548 void LHNET_Init(void)
552 lhnet_socketlist.next = lhnet_socketlist.prev = &lhnet_socketlist;
553 lhnet_packetlist.next = lhnet_packetlist.prev = &lhnet_packetlist;
556 lhnet_didWSAStartup = !WSAStartup(MAKEWORD(1, 1), &lhnet_winsockdata);
557 if (!lhnet_didWSAStartup)
558 Con_Print("LHNET_Init: WSAStartup failed, networking disabled\n");
562 void LHNET_Shutdown(void)
567 while (lhnet_socketlist.next != &lhnet_socketlist)
568 LHNET_CloseSocket(lhnet_socketlist.next);
569 while (lhnet_packetlist.next != &lhnet_packetlist)
571 p = lhnet_packetlist.next;
572 p->prev->next = p->next;
573 p->next->prev = p->prev;
577 if (lhnet_didWSAStartup)
579 lhnet_didWSAStartup = 0;
586 static const char *LHNETPRIVATE_StrError(void)
589 int i = WSAGetLastError();
592 case WSAEINTR: return "WSAEINTR";
593 case WSAEBADF: return "WSAEBADF";
594 case WSAEACCES: return "WSAEACCES";
595 case WSAEFAULT: return "WSAEFAULT";
596 case WSAEINVAL: return "WSAEINVAL";
597 case WSAEMFILE: return "WSAEMFILE";
598 case WSAEWOULDBLOCK: return "WSAEWOULDBLOCK";
599 case WSAEINPROGRESS: return "WSAEINPROGRESS";
600 case WSAEALREADY: return "WSAEALREADY";
601 case WSAENOTSOCK: return "WSAENOTSOCK";
602 case WSAEDESTADDRREQ: return "WSAEDESTADDRREQ";
603 case WSAEMSGSIZE: return "WSAEMSGSIZE";
604 case WSAEPROTOTYPE: return "WSAEPROTOTYPE";
605 case WSAENOPROTOOPT: return "WSAENOPROTOOPT";
606 case WSAEPROTONOSUPPORT: return "WSAEPROTONOSUPPORT";
607 case WSAESOCKTNOSUPPORT: return "WSAESOCKTNOSUPPORT";
608 case WSAEOPNOTSUPP: return "WSAEOPNOTSUPP";
609 case WSAEPFNOSUPPORT: return "WSAEPFNOSUPPORT";
610 case WSAEAFNOSUPPORT: return "WSAEAFNOSUPPORT";
611 case WSAEADDRINUSE: return "WSAEADDRINUSE";
612 case WSAEADDRNOTAVAIL: return "WSAEADDRNOTAVAIL";
613 case WSAENETDOWN: return "WSAENETDOWN";
614 case WSAENETUNREACH: return "WSAENETUNREACH";
615 case WSAENETRESET: return "WSAENETRESET";
616 case WSAECONNABORTED: return "WSAECONNABORTED";
617 case WSAECONNRESET: return "WSAECONNRESET";
618 case WSAENOBUFS: return "WSAENOBUFS";
619 case WSAEISCONN: return "WSAEISCONN";
620 case WSAENOTCONN: return "WSAENOTCONN";
621 case WSAESHUTDOWN: return "WSAESHUTDOWN";
622 case WSAETOOMANYREFS: return "WSAETOOMANYREFS";
623 case WSAETIMEDOUT: return "WSAETIMEDOUT";
624 case WSAECONNREFUSED: return "WSAECONNREFUSED";
625 case WSAELOOP: return "WSAELOOP";
626 case WSAENAMETOOLONG: return "WSAENAMETOOLONG";
627 case WSAEHOSTDOWN: return "WSAEHOSTDOWN";
628 case WSAEHOSTUNREACH: return "WSAEHOSTUNREACH";
629 case WSAENOTEMPTY: return "WSAENOTEMPTY";
630 case WSAEPROCLIM: return "WSAEPROCLIM";
631 case WSAEUSERS: return "WSAEUSERS";
632 case WSAEDQUOT: return "WSAEDQUOT";
633 case WSAESTALE: return "WSAESTALE";
634 case WSAEREMOTE: return "WSAEREMOTE";
635 case WSAEDISCON: return "WSAEDISCON";
636 case 0: return "no error";
637 default: return "unknown WSAE error";
640 return strerror(errno);
644 void LHNET_SleepUntilPacket_Microseconds(int microseconds)
652 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
654 if (s->address.addresstype == LHNETADDRESSTYPE_INET4 || s->address.addresstype == LHNETADDRESSTYPE_INET6)
656 if (lastfd < s->inetsocket)
657 lastfd = s->inetsocket;
658 FD_SET((unsigned int)s->inetsocket, &fdreadset);
661 tv.tv_sec = microseconds / 1000000;
662 tv.tv_usec = microseconds % 1000000;
663 select(lastfd + 1, &fdreadset, NULL, NULL, &tv);
666 lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
668 lhnetsocket_t *lhnetsocket, *s;
671 lhnetsocket = (lhnetsocket_t *)Z_Malloc(sizeof(*lhnetsocket));
674 memset(lhnetsocket, 0, sizeof(*lhnetsocket));
675 lhnetsocket->address = *address;
676 switch(lhnetsocket->address.addresstype)
678 case LHNETADDRESSTYPE_LOOP:
679 if (lhnetsocket->address.port == 0)
681 // allocate a port dynamically
682 // this search will always terminate because there is never
683 // an allocated socket with port 0, so if the number wraps it
684 // will find the port is unused, and then refuse to use port
685 // 0, causing an intentional failure condition
686 lhnetsocket->address.port = 1024;
689 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
690 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.port == lhnetsocket->address.port)
692 if (s == &lhnet_socketlist)
694 lhnetsocket->address.port++;
697 // check if the port is available
698 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
699 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.port == lhnetsocket->address.port)
701 if (s == &lhnet_socketlist && lhnetsocket->address.port != 0)
703 lhnetsocket->next = &lhnet_socketlist;
704 lhnetsocket->prev = lhnetsocket->next->prev;
705 lhnetsocket->next->prev = lhnetsocket;
706 lhnetsocket->prev->next = lhnetsocket;
710 case LHNETADDRESSTYPE_INET4:
711 case LHNETADDRESSTYPE_INET6:
713 if (lhnet_didWSAStartup)
716 if ((lhnetsocket->inetsocket = socket(address->addresstype == LHNETADDRESSTYPE_INET6 ? PF_INET6 : PF_INET, SOCK_DGRAM, IPPROTO_UDP)) != -1)
724 if (ioctlsocket(lhnetsocket->inetsocket, FIONBIO, &_true) != -1)
727 // We need to set this flag to tell the OS that we only listen on IPv6. If we don't
728 // most OSes will create a dual-protocol socket that also listens on IPv4. In this case
729 // if an IPv4 socket is already bound to the port we want, our bind() call will fail.
731 if (address->addresstype != LHNETADDRESSTYPE_INET6
732 || setsockopt (lhnetsocket->inetsocket, IPPROTO_IPV6, IPV6_V6ONLY,
733 (const char *)&ipv6_only, sizeof(ipv6_only)) == 0
735 // The Win32 API only supports IPV6_V6ONLY since Windows Vista, but fortunately
736 // the default value is what we want on Win32 anyway (IPV6_V6ONLY = true)
737 || SOCKETERRNO == WSAENOPROTOOPT
742 lhnetaddressnative_t *localaddress = (lhnetaddressnative_t *)&lhnetsocket->address;
745 if (address->addresstype == LHNETADDRESSTYPE_INET6)
747 namelen = sizeof(localaddress->addr.in6);
748 bindresult = bind(lhnetsocket->inetsocket, &localaddress->addr.sock, namelen);
749 if (bindresult != -1)
750 getsockname(lhnetsocket->inetsocket, &localaddress->addr.sock, &namelen);
754 namelen = sizeof(localaddress->addr.in);
755 bindresult = bind(lhnetsocket->inetsocket, &localaddress->addr.sock, namelen);
756 if (bindresult != -1)
757 getsockname(lhnetsocket->inetsocket, &localaddress->addr.sock, &namelen);
759 if (bindresult != -1)
762 // enable broadcast on this socket
763 setsockopt(lhnetsocket->inetsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i));
764 lhnetsocket->next = &lhnet_socketlist;
765 lhnetsocket->prev = lhnetsocket->next->prev;
766 lhnetsocket->next->prev = lhnetsocket;
767 lhnetsocket->prev->next = lhnetsocket;
769 if (ioctlsocket(lhnetsocket->inetsocket, SIO_UDP_CONNRESET, &_false) == -1)
770 Con_DPrintf("LHNET_OpenSocket_Connectionless: ioctlsocket SIO_UDP_CONNRESET returned error: %s\n", LHNETPRIVATE_StrError());
775 Con_Printf("LHNET_OpenSocket_Connectionless: bind returned error: %s\n", LHNETPRIVATE_StrError());
779 Con_Printf("LHNET_OpenSocket_Connectionless: setsockopt(IPV6_V6ONLY) returned error: %s\n", LHNETPRIVATE_StrError());
783 Con_Printf("LHNET_OpenSocket_Connectionless: ioctlsocket returned error: %s\n", LHNETPRIVATE_StrError());
784 closesocket(lhnetsocket->inetsocket);
787 Con_Printf("LHNET_OpenSocket_Connectionless: socket returned error: %s\n", LHNETPRIVATE_StrError());
791 Con_Print("LHNET_OpenSocket_Connectionless: can't open a socket (WSAStartup failed during LHNET_Init)\n");
802 void LHNET_CloseSocket(lhnetsocket_t *lhnetsocket)
806 // unlink from socket list
807 if (lhnetsocket->next == NULL)
809 lhnetsocket->next->prev = lhnetsocket->prev;
810 lhnetsocket->prev->next = lhnetsocket->next;
811 lhnetsocket->next = NULL;
812 lhnetsocket->prev = NULL;
814 // no special close code for loopback, just inet
815 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4 || lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
817 closesocket(lhnetsocket->inetsocket);
823 lhnetaddress_t *LHNET_AddressFromSocket(lhnetsocket_t *sock)
826 return &sock->address;
831 int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, lhnetaddress_t *vaddress)
833 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
835 if (!lhnetsocket || !address || !content || maxcontentlength < 1)
837 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
840 lhnetpacket_t *p, *pnext;
841 // scan for any old packets to timeout while searching for a packet
842 // that is waiting to be delivered to this socket
843 currenttime = time(NULL);
844 for (p = lhnet_packetlist.next;p != &lhnet_packetlist;p = pnext)
847 if (p->timeout < currenttime)
850 p->next->prev = p->prev;
851 p->prev->next = p->next;
855 #ifndef STANDALONETEST
856 if (cl_netlocalping.value && (realtime - cl_netlocalping.value * (1.0 / 2000.0)) < p->sentdoubletime)
859 if (value == 0 && p->destinationport == lhnetsocket->address.port)
861 if (p->length <= maxcontentlength)
863 lhnetaddressnative_t *localaddress = (lhnetaddressnative_t *)&lhnetsocket->address;
864 *address = *localaddress;
865 address->port = p->sourceport;
866 memcpy(content, p->data, p->length);
872 p->next->prev = p->prev;
873 p->prev->next = p->next;
878 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
880 SOCKLEN_T inetaddresslength;
881 address->addresstype = LHNETADDRESSTYPE_NONE;
882 inetaddresslength = sizeof(address->addr.in);
883 value = recvfrom(lhnetsocket->inetsocket, (char *)content, maxcontentlength, 0, &address->addr.sock, &inetaddresslength);
886 address->addresstype = LHNETADDRESSTYPE_INET4;
887 address->port = ntohs(address->addr.in.sin_port);
890 else if (value == -1)
893 if (e == EWOULDBLOCK)
898 Con_Print("Connection refused\n");
901 Con_Printf("LHNET_Read: recvfrom returned error: %s\n", LHNETPRIVATE_StrError());
904 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
906 SOCKLEN_T inetaddresslength;
907 address->addresstype = LHNETADDRESSTYPE_NONE;
908 inetaddresslength = sizeof(address->addr.in6);
909 value = recvfrom(lhnetsocket->inetsocket, (char *)content, maxcontentlength, 0, &address->addr.sock, &inetaddresslength);
912 address->addresstype = LHNETADDRESSTYPE_INET6;
913 address->port = ntohs(address->addr.in6.sin6_port);
916 else if (value == -1)
919 if (e == EWOULDBLOCK)
924 Con_Print("Connection refused\n");
927 Con_Printf("LHNET_Read: recvfrom returned error: %s\n", LHNETPRIVATE_StrError());
933 int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentlength, const lhnetaddress_t *vaddress)
935 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
937 if (!lhnetsocket || !address || !content || contentlength < 1)
939 if (lhnetsocket->address.addresstype != address->addresstype)
941 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
944 p = (lhnetpacket_t *)Z_Malloc(sizeof(*p) + contentlength);
945 p->data = (void *)(p + 1);
946 memcpy(p->data, content, contentlength);
947 p->length = contentlength;
948 p->sourceport = lhnetsocket->address.port;
949 p->destinationport = address->port;
950 p->timeout = time(NULL) + 10;
951 p->next = &lhnet_packetlist;
952 p->prev = p->next->prev;
955 #ifndef STANDALONETEST
956 p->sentdoubletime = realtime;
958 value = contentlength;
960 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
962 value = sendto(lhnetsocket->inetsocket, (char *)content, contentlength, 0, (struct sockaddr *)&address->addr.in, sizeof(struct sockaddr_in));
965 if (SOCKETERRNO == EWOULDBLOCK)
967 Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError());
970 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
972 value = sendto(lhnetsocket->inetsocket, (char *)content, contentlength, 0, (struct sockaddr *)&address->addr.in6, sizeof(struct sockaddr_in6));
975 if (SOCKETERRNO == EWOULDBLOCK)
977 Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError());
983 #ifdef STANDALONETEST
984 int main(int argc, char **argv)
987 char *buffer = "test", buffer2[1024];
988 int blen = strlen(buffer);
990 lhnetsocket_t *sock1;
991 lhnetsocket_t *sock2;
992 lhnetaddress_t myaddy1;
993 lhnetaddress_t myaddy2;
994 lhnetaddress_t myaddy3;
995 lhnetaddress_t localhostaddy1;
996 lhnetaddress_t localhostaddy2;
1000 printf("calling LHNET_Init\n");
1003 printf("calling LHNET_FromPort twice to create two local addresses\n");
1004 LHNETADDRESS_FromPort(&myaddy1, LHNETADDRESSTYPE_INET4, 4000);
1005 LHNETADDRESS_FromPort(&myaddy2, LHNETADDRESSTYPE_INET4, 4001);
1006 LHNETADDRESS_FromString(&localhostaddy1, "127.0.0.1", 4000);
1007 LHNETADDRESS_FromString(&localhostaddy2, "127.0.0.1", 4001);
1009 printf("calling LHNET_OpenSocket_Connectionless twice to create two local sockets\n");
1010 sock1 = LHNET_OpenSocket_Connectionless(&myaddy1);
1011 sock2 = LHNET_OpenSocket_Connectionless(&myaddy2);
1013 printf("calling LHNET_Write to send a packet from the first socket to the second socket\n");
1014 test1 = LHNET_Write(sock1, buffer, blen, &localhostaddy2);
1015 printf("sleeping briefly\n");
1021 printf("calling LHNET_Read on the second socket to read the packet sent from the first socket\n");
1022 test2 = LHNET_Read(sock2, buffer2, b2len - 1, &myaddy3);
1024 Con_Printf("socket to socket test succeeded\n");
1026 Con_Printf("socket to socket test failed\n");
1029 printf("press any key to exit\n");
1033 printf("calling LHNET_Shutdown\n");
1035 printf("exiting\n");
1038 lhnetsocket_t *sock[16], *sendsock;
1047 int sendmessagelength;
1048 lhnetaddress_t destaddress;
1049 lhnetaddress_t receiveaddress;
1050 lhnetaddress_t sockaddress[16];
1051 char buffer[1536], addressstring[128], addressstring2[128];
1052 if ((argc == 2 || argc == 5) && (port = atoi(argv[1])) >= 1 && port < 65535)
1054 printf("calling LHNET_Init()\n");
1058 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_LOOP, port);
1059 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET4, port);
1060 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET6, port+1);
1064 sendmessagelength = 0;
1066 for (i = 0;i < numsockets;i++)
1068 LHNETADDRESS_ToString(&sockaddress[i], addressstring, sizeof(addressstring), 1);
1069 printf("calling LHNET_OpenSocket_Connectionless(<%s>)\n", addressstring);
1070 if ((sock[i] = LHNET_OpenSocket_Connectionless(&sockaddress[i])))
1072 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
1073 printf("opened socket successfully (address \"%s\")\n", addressstring2);
1077 printf("failed to open socket\n");
1088 count = atoi(argv[2]);
1089 if (LHNETADDRESS_FromString(&destaddress, argv[3], -1))
1091 sendmessage = argv[4];
1092 sendmessagelength = strlen(sendmessage);
1094 for (i = 0;i < numsockets;i++)
1095 if (sock[i] && LHNETADDRESS_GetAddressType(&destaddress) == LHNETADDRESS_GetAddressType(&sockaddress[i]))
1097 if (sendsock == NULL)
1099 printf("Could not find an open socket matching the addresstype (%i) of destination address, switching to listen only mode\n", LHNETADDRESS_GetAddressType(&destaddress));
1105 printf("LHNETADDRESS_FromString did not like the address \"%s\", switching to listen only mode\n", argv[3]);
1109 printf("started, now listening for \"exit\" on the opened sockets\n");
1110 oldtime = time(NULL);
1118 for (i = 0;i < numsockets;i++)
1122 length = LHNET_Read(sock[i], buffer, sizeof(buffer), &receiveaddress);
1124 printf("localsock read error: length < 0");
1125 else if (length > 0 && length < (int)sizeof(buffer))
1128 LHNETADDRESS_ToString(&receiveaddress, addressstring, sizeof(addressstring), 1);
1129 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
1130 printf("received message \"%s\" from \"%s\" on socket \"%s\"\n", buffer, addressstring, addressstring2);
1131 if (!strcmp(buffer, "exit"))
1138 if (argc == 5 && count > 0)
1140 newtime = time(NULL);
1141 if (newtime != oldtime)
1143 LHNETADDRESS_ToString(&destaddress, addressstring, sizeof(addressstring), 1);
1144 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sendsock), addressstring2, sizeof(addressstring2), 1);
1145 printf("calling LHNET_Write(<%s>, \"%s\", %i, <%s>)\n", addressstring2, sendmessage, sendmessagelength, addressstring);
1146 length = LHNET_Write(sendsock, sendmessage, sendmessagelength, &destaddress);
1147 if (length == sendmessagelength)
1148 printf("sent successfully\n");
1150 printf("LH_Write failed, returned %i (length of message was %i)\n", length, strlen(argv[4]));
1154 printf("Done sending, still listening for \"exit\"\n");
1158 for (i = 0;i < numsockets;i++)
1162 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
1163 printf("calling LHNET_CloseSocket(<%s>)\n", addressstring2);
1164 LHNET_CloseSocket(sock[i]);
1167 printf("calling LHNET_Shutdown()\n");
1171 printf("Testing code for lhnet.c\nusage: lhnettest <localportnumber> [<sendnumberoftimes> <sendaddress:port> <sendmessage>]\n");