2 // Written by Forest Hale 2003-06-15 and placed into public domain.
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/ioctl.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
27 #include <proto/socket.h>
30 // for Z_Malloc/Z_Free in quake
31 #ifndef STANDALONETEST
36 #define Con_Print printf
37 #define Con_Printf printf
38 #define Z_Malloc malloc
45 #define EWOULDBLOCK WSAEWOULDBLOCK
46 #define ECONNREFUSED WSAECONNREFUSED
48 #define SOCKETERRNO WSAGetLastError()
50 #define IOC_VENDOR 0x18000000
51 #define _WSAIOW(x,y) (IOC_IN|(x)|(y))
52 #define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
55 #elif defined(__MORPHOS__)
56 #define ioctlsocket IoctlSocket
57 #define closesocket CloseSocket
58 #define SOCKETERRNO Errno()
62 #define ioctlsocket ioctl
63 #define closesocket close
64 #define SOCKETERRNO errno
66 #define SOCKLEN_T socklen_t
69 typedef struct lhnetaddressnative_s
76 struct sockaddr_in in;
77 struct sockaddr_in6 in6;
83 // to make LHNETADDRESS_FromString resolve repeated hostnames faster, cache them
84 #define MAX_NAMECACHE 64
85 static struct namecache_s
87 lhnetaddressnative_t address;
88 double expirationtime;
91 namecache[MAX_NAMECACHE];
92 static int namecacheposition = 0;
94 int LHNETADDRESS_FromPort(lhnetaddress_t *vaddress, int addresstype, int port)
96 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
101 case LHNETADDRESSTYPE_LOOP:
102 // local:port (loopback)
103 memset(address, 0, sizeof(*address));
104 address->addresstype = LHNETADDRESSTYPE_LOOP;
105 address->port = port;
107 case LHNETADDRESSTYPE_INET4:
108 // 0.0.0.0:port (INADDR_ANY, binds to all interfaces)
109 memset(address, 0, sizeof(*address));
110 address->addresstype = LHNETADDRESSTYPE_INET4;
111 address->port = port;
112 address->addr.in.sin_family = AF_INET;
113 address->addr.in.sin_port = htons((unsigned short)port);
115 case LHNETADDRESSTYPE_INET6:
116 // [0:0:0:0:0:0:0:0]:port (IN6ADDR_ANY, binds to all interfaces)
117 memset(address, 0, sizeof(*address));
118 address->addresstype = LHNETADDRESSTYPE_INET6;
119 address->port = port;
120 address->addr.in6.sin6_family = AF_INET6;
121 address->addr.in6.sin6_port = htons((unsigned short)port);
127 int LHNETADDRESS_FromString(lhnetaddress_t *vaddress, const char *string, int defaultport)
129 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
130 int i, port, namelen, d1, d2, d3, d4;
131 struct hostent *hostentry;
135 #ifdef STANDALONETEST
138 if (!address || !string || !*string)
140 memset(address, 0, sizeof(*address));
141 address->addresstype = LHNETADDRESSTYPE_NONE;
143 colon = strrchr(string, ':');
145 port = atoi(colon + 1);
147 colon = string + strlen(string);
150 namelen = colon - string;
153 if (string[0] == '[' && namelen > 0 && string[namelen-1] == ']') // ipv6
158 memcpy(name, string, namelen);
161 if (!strcmp(name, "local"))
163 address->addresstype = LHNETADDRESSTYPE_LOOP;
164 address->port = port;
167 // try to parse as dotted decimal ipv4 address first
168 // note this supports partial ip addresses
169 d1 = d2 = d3 = d4 = 0;
171 #define sscanf sscanf_s
173 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)
175 // parsed a valid ipv4 address
176 address->addresstype = LHNETADDRESSTYPE_INET4;
177 address->port = port;
178 address->addr.in.sin_family = AF_INET;
179 address->addr.in.sin_port = htons((unsigned short)port);
180 a = (unsigned char *)&address->addr.in.sin_addr;
185 #ifdef STANDALONETEST
186 LHNETADDRESS_ToString(address, string2, sizeof(string2), 1);
187 printf("manual parsing of ipv4 dotted decimal address \"%s\" successful: %s\n", string, string2);
191 for (i = 0;i < MAX_NAMECACHE;i++)
192 if (!strcmp(namecache[i].name, name))
194 #ifdef STANDALONETEST
195 if (i < MAX_NAMECACHE)
197 if (i < MAX_NAMECACHE && realtime < namecache[i].expirationtime)
200 *address = namecache[i].address;
201 address->port = port;
202 if (address->addresstype == LHNETADDRESSTYPE_INET6)
204 address->addr.in6.sin6_port = htons((unsigned short)port);
207 else if (address->addresstype == LHNETADDRESSTYPE_INET4)
209 address->addr.in.sin_port = htons((unsigned short)port);
214 // try gethostbyname (handles dns and other ip formats)
215 hostentry = gethostbyname(name);
218 if (hostentry->h_addrtype == AF_INET6)
221 address->addresstype = LHNETADDRESSTYPE_INET6;
222 address->port = port;
223 address->addr.in6.sin6_family = hostentry->h_addrtype;
224 address->addr.in6.sin6_port = htons((unsigned short)port);
225 memcpy(&address->addr.in6.sin6_addr, hostentry->h_addr_list[0], sizeof(address->addr.in6.sin6_addr));
226 for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
227 namecache[namecacheposition].name[i] = name[i];
228 namecache[namecacheposition].name[i] = 0;
229 #ifndef STANDALONETEST
230 namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
232 namecache[namecacheposition].address = *address;
233 namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
234 #ifdef STANDALONETEST
235 LHNETADDRESS_ToString(address, string2, sizeof(string2), 1);
236 printf("gethostbyname(\"%s\") returned ipv6 address %s\n", string, string2);
240 else if (hostentry->h_addrtype == AF_INET)
243 address->addresstype = LHNETADDRESSTYPE_INET4;
244 address->port = port;
245 address->addr.in.sin_family = hostentry->h_addrtype;
246 address->addr.in.sin_port = htons((unsigned short)port);
247 memcpy(&address->addr.in.sin_addr, hostentry->h_addr_list[0], sizeof(address->addr.in.sin_addr));
248 for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
249 namecache[namecacheposition].name[i] = name[i];
250 namecache[namecacheposition].name[i] = 0;
251 #ifndef STANDALONETEST
252 namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
254 namecache[namecacheposition].address = *address;
255 namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
256 #ifdef STANDALONETEST
257 LHNETADDRESS_ToString(address, string2, sizeof(string2), 1);
258 printf("gethostbyname(\"%s\") returned ipv4 address %s\n", string, string2);
263 #ifdef STANDALONETEST
264 printf("gethostbyname failed on address \"%s\"\n", name);
266 for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
267 namecache[namecacheposition].name[i] = name[i];
268 namecache[namecacheposition].name[i] = 0;
269 #ifndef STANDALONETEST
270 namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
272 namecache[namecacheposition].address.addresstype = LHNETADDRESSTYPE_NONE;
273 namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
277 int LHNETADDRESS_ToString(const lhnetaddress_t *vaddress, char *string, int stringbuffersize, int includeport)
279 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
280 const unsigned char *a;
282 if (!address || !string || stringbuffersize < 1)
284 switch(address->addresstype)
288 case LHNETADDRESSTYPE_LOOP:
291 if (stringbuffersize >= 12)
293 dpsnprintf(string, stringbuffersize, "local:%d", address->port);
299 if (stringbuffersize >= 6)
301 memcpy(string, "local", 6);
306 case LHNETADDRESSTYPE_INET4:
307 a = (const unsigned char *)(&address->addr.in.sin_addr);
310 if (stringbuffersize >= 22)
312 dpsnprintf(string, stringbuffersize, "%d.%d.%d.%d:%d", a[0], a[1], a[2], a[3], address->port);
318 if (stringbuffersize >= 16)
320 dpsnprintf(string, stringbuffersize, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
325 case LHNETADDRESSTYPE_INET6:
326 a = (const unsigned char *)(&address->addr.in6.sin6_addr);
329 if (stringbuffersize >= 88)
331 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);
337 if (stringbuffersize >= 80)
339 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]);
348 int LHNETADDRESS_GetAddressType(const lhnetaddress_t *address)
351 return address->addresstype;
353 return LHNETADDRESSTYPE_NONE;
356 int LHNETADDRESS_GetPort(const lhnetaddress_t *address)
360 return address->port;
363 int LHNETADDRESS_SetPort(lhnetaddress_t *vaddress, int port)
365 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
368 address->port = port;
369 switch(address->addresstype)
371 case LHNETADDRESSTYPE_LOOP:
373 case LHNETADDRESSTYPE_INET4:
374 address->addr.in.sin_port = htons((unsigned short)port);
376 case LHNETADDRESSTYPE_INET6:
377 address->addr.in6.sin6_port = htons((unsigned short)port);
384 int LHNETADDRESS_Compare(const lhnetaddress_t *vaddress1, const lhnetaddress_t *vaddress2)
386 lhnetaddressnative_t *address1 = (lhnetaddressnative_t *)vaddress1;
387 lhnetaddressnative_t *address2 = (lhnetaddressnative_t *)vaddress2;
388 if (!address1 || !address2)
390 if (address1->addresstype != address2->addresstype)
392 switch(address1->addresstype)
394 case LHNETADDRESSTYPE_LOOP:
395 if (address1->port != address2->port)
398 case LHNETADDRESSTYPE_INET4:
399 if (address1->addr.in.sin_family != address2->addr.in.sin_family)
401 if (memcmp(&address1->addr.in.sin_addr, &address2->addr.in.sin_addr, sizeof(address1->addr.in.sin_addr)))
403 if (address1->port != address2->port)
406 case LHNETADDRESSTYPE_INET6:
407 if (address1->addr.in6.sin6_family != address2->addr.in6.sin6_family)
409 if (memcmp(&address1->addr.in6.sin6_addr, &address2->addr.in6.sin6_addr, sizeof(address1->addr.in6.sin6_addr)))
411 if (address1->port != address2->port)
419 typedef struct lhnetpacket_s
426 #ifndef STANDALONETEST
427 double sentdoubletime;
429 struct lhnetpacket_s *next, *prev;
433 static int lhnet_active;
434 static lhnetsocket_t lhnet_socketlist;
435 static lhnetpacket_t lhnet_packetlist;
437 static int lhnet_didWSAStartup = 0;
438 static WSADATA lhnet_winsockdata;
441 void LHNET_Init(void)
445 lhnet_socketlist.next = lhnet_socketlist.prev = &lhnet_socketlist;
446 lhnet_packetlist.next = lhnet_packetlist.prev = &lhnet_packetlist;
449 lhnet_didWSAStartup = !WSAStartup(MAKEWORD(1, 1), &lhnet_winsockdata);
450 if (!lhnet_didWSAStartup)
451 Con_Print("LHNET_Init: WSAStartup failed, networking disabled\n");
455 void LHNET_Shutdown(void)
460 while (lhnet_socketlist.next != &lhnet_socketlist)
461 LHNET_CloseSocket(lhnet_socketlist.next);
462 while (lhnet_packetlist.next != &lhnet_packetlist)
464 p = lhnet_packetlist.next;
465 p->prev->next = p->next;
466 p->next->prev = p->prev;
470 if (lhnet_didWSAStartup)
472 lhnet_didWSAStartup = 0;
479 static const char *LHNETPRIVATE_StrError(void)
482 int i = WSAGetLastError();
485 case WSAEINTR: return "WSAEINTR";
486 case WSAEBADF: return "WSAEBADF";
487 case WSAEACCES: return "WSAEACCES";
488 case WSAEFAULT: return "WSAEFAULT";
489 case WSAEINVAL: return "WSAEINVAL";
490 case WSAEMFILE: return "WSAEMFILE";
491 case WSAEWOULDBLOCK: return "WSAEWOULDBLOCK";
492 case WSAEINPROGRESS: return "WSAEINPROGRESS";
493 case WSAEALREADY: return "WSAEALREADY";
494 case WSAENOTSOCK: return "WSAENOTSOCK";
495 case WSAEDESTADDRREQ: return "WSAEDESTADDRREQ";
496 case WSAEMSGSIZE: return "WSAEMSGSIZE";
497 case WSAEPROTOTYPE: return "WSAEPROTOTYPE";
498 case WSAENOPROTOOPT: return "WSAENOPROTOOPT";
499 case WSAEPROTONOSUPPORT: return "WSAEPROTONOSUPPORT";
500 case WSAESOCKTNOSUPPORT: return "WSAESOCKTNOSUPPORT";
501 case WSAEOPNOTSUPP: return "WSAEOPNOTSUPP";
502 case WSAEPFNOSUPPORT: return "WSAEPFNOSUPPORT";
503 case WSAEAFNOSUPPORT: return "WSAEAFNOSUPPORT";
504 case WSAEADDRINUSE: return "WSAEADDRINUSE";
505 case WSAEADDRNOTAVAIL: return "WSAEADDRNOTAVAIL";
506 case WSAENETDOWN: return "WSAENETDOWN";
507 case WSAENETUNREACH: return "WSAENETUNREACH";
508 case WSAENETRESET: return "WSAENETRESET";
509 case WSAECONNABORTED: return "WSAECONNABORTED";
510 case WSAECONNRESET: return "WSAECONNRESET";
511 case WSAENOBUFS: return "WSAENOBUFS";
512 case WSAEISCONN: return "WSAEISCONN";
513 case WSAENOTCONN: return "WSAENOTCONN";
514 case WSAESHUTDOWN: return "WSAESHUTDOWN";
515 case WSAETOOMANYREFS: return "WSAETOOMANYREFS";
516 case WSAETIMEDOUT: return "WSAETIMEDOUT";
517 case WSAECONNREFUSED: return "WSAECONNREFUSED";
518 case WSAELOOP: return "WSAELOOP";
519 case WSAENAMETOOLONG: return "WSAENAMETOOLONG";
520 case WSAEHOSTDOWN: return "WSAEHOSTDOWN";
521 case WSAEHOSTUNREACH: return "WSAEHOSTUNREACH";
522 case WSAENOTEMPTY: return "WSAENOTEMPTY";
523 case WSAEPROCLIM: return "WSAEPROCLIM";
524 case WSAEUSERS: return "WSAEUSERS";
525 case WSAEDQUOT: return "WSAEDQUOT";
526 case WSAESTALE: return "WSAESTALE";
527 case WSAEREMOTE: return "WSAEREMOTE";
528 case WSAEDISCON: return "WSAEDISCON";
529 case 0: return "no error";
530 default: return "unknown WSAE error";
533 return strerror(errno);
537 void LHNET_SleepUntilPacket_Microseconds(int microseconds)
545 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
547 if (s->address.addresstype == LHNETADDRESSTYPE_INET4 || s->address.addresstype == LHNETADDRESSTYPE_INET6)
549 if (lastfd < s->inetsocket)
550 lastfd = s->inetsocket;
551 FD_SET((unsigned int)s->inetsocket, &fdreadset);
554 tv.tv_sec = microseconds / 1000000;
555 tv.tv_usec = microseconds % 1000000;
556 select(lastfd + 1, &fdreadset, NULL, NULL, &tv);
559 lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
561 lhnetsocket_t *lhnetsocket, *s;
564 lhnetsocket = (lhnetsocket_t *)Z_Malloc(sizeof(*lhnetsocket));
567 memset(lhnetsocket, 0, sizeof(*lhnetsocket));
568 lhnetsocket->address = *address;
569 switch(lhnetsocket->address.addresstype)
571 case LHNETADDRESSTYPE_LOOP:
572 if (lhnetsocket->address.port == 0)
574 // allocate a port dynamically
575 // this search will always terminate because there is never
576 // an allocated socket with port 0, so if the number wraps it
577 // will find the port is unused, and then refuse to use port
578 // 0, causing an intentional failure condition
579 lhnetsocket->address.port = 1024;
582 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
583 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.port == lhnetsocket->address.port)
585 if (s == &lhnet_socketlist)
587 lhnetsocket->address.port++;
590 // check if the port is available
591 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
592 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.port == lhnetsocket->address.port)
594 if (s == &lhnet_socketlist && lhnetsocket->address.port != 0)
596 lhnetsocket->next = &lhnet_socketlist;
597 lhnetsocket->prev = lhnetsocket->next->prev;
598 lhnetsocket->next->prev = lhnetsocket;
599 lhnetsocket->prev->next = lhnetsocket;
603 case LHNETADDRESSTYPE_INET4:
604 case LHNETADDRESSTYPE_INET6:
606 if (lhnet_didWSAStartup)
609 if ((lhnetsocket->inetsocket = socket(address->addresstype == LHNETADDRESSTYPE_INET6 ? PF_INET6 : PF_INET, SOCK_DGRAM, IPPROTO_UDP)) != -1)
617 if (ioctlsocket(lhnetsocket->inetsocket, FIONBIO, &_true) != -1)
619 lhnetaddressnative_t *localaddress = (lhnetaddressnative_t *)&lhnetsocket->address;
622 if (address->addresstype == LHNETADDRESSTYPE_INET6)
624 namelen = sizeof(localaddress->addr.in6);
625 bindresult = bind(lhnetsocket->inetsocket, &localaddress->addr.sock, namelen);
626 if (bindresult != -1)
627 getsockname(lhnetsocket->inetsocket, &localaddress->addr.sock, &namelen);
631 namelen = sizeof(localaddress->addr.in);
632 bindresult = bind(lhnetsocket->inetsocket, &localaddress->addr.sock, namelen);
633 if (bindresult != -1)
634 getsockname(lhnetsocket->inetsocket, &localaddress->addr.sock, &namelen);
636 if (bindresult != -1)
639 // enable broadcast on this socket
640 setsockopt(lhnetsocket->inetsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i));
641 lhnetsocket->next = &lhnet_socketlist;
642 lhnetsocket->prev = lhnetsocket->next->prev;
643 lhnetsocket->next->prev = lhnetsocket;
644 lhnetsocket->prev->next = lhnetsocket;
646 if (ioctlsocket(lhnetsocket->inetsocket, SIO_UDP_CONNRESET, &_false) == -1)
647 Con_DPrintf("LHNET_OpenSocket_Connectionless: ioctlsocket SIO_UDP_CONNRESET returned error: %s\n", LHNETPRIVATE_StrError());
652 Con_Printf("LHNET_OpenSocket_Connectionless: bind returned error: %s\n", LHNETPRIVATE_StrError());
655 Con_Printf("LHNET_OpenSocket_Connectionless: ioctlsocket returned error: %s\n", LHNETPRIVATE_StrError());
656 closesocket(lhnetsocket->inetsocket);
659 Con_Printf("LHNET_OpenSocket_Connectionless: socket returned error: %s\n", LHNETPRIVATE_StrError());
663 Con_Print("LHNET_OpenSocket_Connectionless: can't open a socket (WSAStartup failed during LHNET_Init)\n");
674 void LHNET_CloseSocket(lhnetsocket_t *lhnetsocket)
678 // unlink from socket list
679 if (lhnetsocket->next == NULL)
681 lhnetsocket->next->prev = lhnetsocket->prev;
682 lhnetsocket->prev->next = lhnetsocket->next;
683 lhnetsocket->next = NULL;
684 lhnetsocket->prev = NULL;
686 // no special close code for loopback, just inet
687 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4 || lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
689 closesocket(lhnetsocket->inetsocket);
695 lhnetaddress_t *LHNET_AddressFromSocket(lhnetsocket_t *sock)
698 return &sock->address;
703 int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, lhnetaddress_t *vaddress)
705 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
707 if (!lhnetsocket || !address || !content || maxcontentlength < 1)
709 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
712 lhnetpacket_t *p, *pnext;
713 // scan for any old packets to timeout while searching for a packet
714 // that is waiting to be delivered to this socket
715 currenttime = time(NULL);
716 for (p = lhnet_packetlist.next;p != &lhnet_packetlist;p = pnext)
719 if (p->timeout < currenttime)
722 p->next->prev = p->prev;
723 p->prev->next = p->next;
727 #ifndef STANDALONETEST
728 if (cl_netlocalping.value && (realtime - cl_netlocalping.value * (1.0 / 2000.0)) < p->sentdoubletime)
731 if (value == 0 && p->destinationport == lhnetsocket->address.port)
733 if (p->length <= maxcontentlength)
735 lhnetaddressnative_t *localaddress = (lhnetaddressnative_t *)&lhnetsocket->address;
736 *address = *localaddress;
737 address->port = p->sourceport;
738 memcpy(content, p->data, p->length);
744 p->next->prev = p->prev;
745 p->prev->next = p->next;
750 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
752 unsigned int inetaddresslength;
753 address->addresstype = LHNETADDRESSTYPE_NONE;
754 inetaddresslength = sizeof(address->addr.in);
755 value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, &address->addr.sock, &inetaddresslength);
758 address->addresstype = LHNETADDRESSTYPE_INET4;
759 address->port = ntohs(address->addr.in.sin_port);
762 else if (value == -1)
765 if (e == EWOULDBLOCK)
770 Con_Print("Connection refused\n");
773 Con_Printf("LHNET_Read: recvfrom returned error: %s\n", LHNETPRIVATE_StrError());
776 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
778 unsigned int inetaddresslength;
779 address->addresstype = LHNETADDRESSTYPE_NONE;
780 inetaddresslength = sizeof(address->addr.in6);
781 value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, &address->addr.sock, &inetaddresslength);
784 address->addresstype = LHNETADDRESSTYPE_INET6;
785 address->port = ntohs(address->addr.in6.sin6_port);
788 else if (value == -1)
791 if (e == EWOULDBLOCK)
796 Con_Print("Connection refused\n");
799 Con_Printf("LHNET_Read: recvfrom returned error: %s\n", LHNETPRIVATE_StrError());
805 int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentlength, const lhnetaddress_t *vaddress)
807 lhnetaddressnative_t *address = (lhnetaddressnative_t *)vaddress;
809 if (!lhnetsocket || !address || !content || contentlength < 1)
811 if (lhnetsocket->address.addresstype != address->addresstype)
813 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
816 p = (lhnetpacket_t *)Z_Malloc(sizeof(*p) + contentlength);
817 p->data = (void *)(p + 1);
818 memcpy(p->data, content, contentlength);
819 p->length = contentlength;
820 p->sourceport = lhnetsocket->address.port;
821 p->destinationport = address->port;
822 p->timeout = time(NULL) + 10;
823 p->next = &lhnet_packetlist;
824 p->prev = p->next->prev;
827 #ifndef STANDALONETEST
828 p->sentdoubletime = realtime;
830 value = contentlength;
832 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
834 value = sendto(lhnetsocket->inetsocket, content, contentlength, 0, (struct sockaddr *)&address->addr.in, sizeof(struct sockaddr_in));
837 if (SOCKETERRNO == EWOULDBLOCK)
839 Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError());
842 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
844 value = sendto(lhnetsocket->inetsocket, content, contentlength, 0, (struct sockaddr *)&address->addr.in6, sizeof(struct sockaddr_in6));
847 if (SOCKETERRNO == EWOULDBLOCK)
849 Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError());
855 #ifdef STANDALONETEST
856 int main(int argc, char **argv)
859 char *buffer = "test", buffer2[1024];
860 int blen = strlen(buffer);
862 lhnetsocket_t *sock1;
863 lhnetsocket_t *sock2;
864 lhnetaddress_t myaddy1;
865 lhnetaddress_t myaddy2;
866 lhnetaddress_t myaddy3;
867 lhnetaddress_t localhostaddy1;
868 lhnetaddress_t localhostaddy2;
872 printf("calling LHNET_Init\n");
875 printf("calling LHNET_FromPort twice to create two local addresses\n");
876 LHNETADDRESS_FromPort(&myaddy1, LHNETADDRESSTYPE_INET4, 4000);
877 LHNETADDRESS_FromPort(&myaddy2, LHNETADDRESSTYPE_INET4, 4001);
878 LHNETADDRESS_FromString(&localhostaddy1, "127.0.0.1", 4000);
879 LHNETADDRESS_FromString(&localhostaddy2, "127.0.0.1", 4001);
881 printf("calling LHNET_OpenSocket_Connectionless twice to create two local sockets\n");
882 sock1 = LHNET_OpenSocket_Connectionless(&myaddy1);
883 sock2 = LHNET_OpenSocket_Connectionless(&myaddy2);
885 printf("calling LHNET_Write to send a packet from the first socket to the second socket\n");
886 test1 = LHNET_Write(sock1, buffer, blen, &localhostaddy2);
887 printf("sleeping briefly\n");
893 printf("calling LHNET_Read on the second socket to read the packet sent from the first socket\n");
894 test2 = LHNET_Read(sock2, buffer2, b2len - 1, &myaddy3);
896 Con_Printf("socket to socket test succeeded\n");
898 Con_Printf("socket to socket test failed\n");
901 printf("press any key to exit\n");
905 printf("calling LHNET_Shutdown\n");
910 lhnetsocket_t *sock[16], *sendsock;
919 int sendmessagelength;
920 lhnetaddress_t destaddress;
921 lhnetaddress_t receiveaddress;
922 lhnetaddress_t sockaddress[16];
923 char buffer[1536], addressstring[128], addressstring2[128];
924 if ((argc == 2 || argc == 5) && (port = atoi(argv[1])) >= 1 && port < 65535)
926 printf("calling LHNET_Init()\n");
930 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_LOOP, port);
931 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET4, port);
932 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET6, port+1);
936 sendmessagelength = 0;
938 for (i = 0;i < numsockets;i++)
940 LHNETADDRESS_ToString(&sockaddress[i], addressstring, sizeof(addressstring), 1);
941 printf("calling LHNET_OpenSocket_Connectionless(<%s>)\n", addressstring);
942 if ((sock[i] = LHNET_OpenSocket_Connectionless(&sockaddress[i])))
944 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
945 printf("opened socket successfully (address \"%s\")\n", addressstring2);
949 printf("failed to open socket\n");
960 count = atoi(argv[2]);
961 if (LHNETADDRESS_FromString(&destaddress, argv[3], -1))
963 sendmessage = argv[4];
964 sendmessagelength = strlen(sendmessage);
966 for (i = 0;i < numsockets;i++)
967 if (sock[i] && LHNETADDRESS_GetAddressType(&destaddress) == LHNETADDRESS_GetAddressType(&sockaddress[i]))
969 if (sendsock == NULL)
971 printf("Could not find an open socket matching the addresstype (%i) of destination address, switching to listen only mode\n", LHNETADDRESS_GetAddressType(&destaddress));
977 printf("LHNETADDRESS_FromString did not like the address \"%s\", switching to listen only mode\n", argv[3]);
981 printf("started, now listening for \"exit\" on the opened sockets\n");
982 oldtime = time(NULL);
990 for (i = 0;i < numsockets;i++)
994 length = LHNET_Read(sock[i], buffer, sizeof(buffer), &receiveaddress);
996 printf("localsock read error: length < 0");
997 else if (length > 0 && length < (int)sizeof(buffer))
1000 LHNETADDRESS_ToString(&receiveaddress, addressstring, sizeof(addressstring), 1);
1001 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
1002 printf("received message \"%s\" from \"%s\" on socket \"%s\"\n", buffer, addressstring, addressstring2);
1003 if (!strcmp(buffer, "exit"))
1010 if (argc == 5 && count > 0)
1012 newtime = time(NULL);
1013 if (newtime != oldtime)
1015 LHNETADDRESS_ToString(&destaddress, addressstring, sizeof(addressstring), 1);
1016 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sendsock), addressstring2, sizeof(addressstring2), 1);
1017 printf("calling LHNET_Write(<%s>, \"%s\", %i, <%s>)\n", addressstring2, sendmessage, sendmessagelength, addressstring);
1018 length = LHNET_Write(sendsock, sendmessage, sendmessagelength, &destaddress);
1019 if (length == sendmessagelength)
1020 printf("sent successfully\n");
1022 printf("LH_Write failed, returned %i (length of message was %i)\n", length, strlen(argv[4]));
1026 printf("Done sending, still listening for \"exit\"\n");
1030 for (i = 0;i < numsockets;i++)
1034 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
1035 printf("calling LHNET_CloseSocket(<%s>)\n", addressstring2);
1036 LHNET_CloseSocket(sock[i]);
1039 printf("calling LHNET_Shutdown()\n");
1043 printf("Testing code for lhnet.c\nusage: lhnettest <localportnumber> [<sendnumberoftimes> <sendaddress:port> <sendmessage>]\n");