2 // Written by Forest Hale 2003-06-15 and placed into public domain.
12 #include <sys/socket.h>
13 #include <sys/ioctl.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
21 #include <proto/socket.h>
24 // for Z_Malloc/Z_Free in quake
25 #ifndef STANDALONETEST
31 #define Con_Print printf
32 #define Con_Printf printf
33 #define Z_Malloc malloc
40 #define EWOULDBLOCK WSAEWOULDBLOCK
41 #define ECONNREFUSED WSAECONNREFUSED
43 #define SOCKETERRNO WSAGetLastError()
45 #define IOC_VENDOR 0x18000000
46 #define _WSAIOW(x,y) (IOC_IN|(x)|(y))
47 #define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
50 #elif defined(__MORPHOS__)
51 #define ioctlsocket IoctlSocket
52 #define closesocket CloseSocket
53 #define SOCKETERRNO Errno()
57 #define ioctlsocket ioctl
58 #define closesocket close
59 #define SOCKETERRNO errno
61 #define SOCKLEN_T socklen_t
64 // to make LHNETADDRESS_FromString resolve repeated hostnames faster, cache them
65 #define MAX_NAMECACHE 64
66 static struct namecache_s
68 lhnetaddress_t address;
69 double expirationtime;
72 namecache[MAX_NAMECACHE];
73 static int namecacheposition = 0;
75 int LHNETADDRESS_FromPort(lhnetaddress_t *address, int addresstype, int port)
81 case LHNETADDRESSTYPE_LOOP:
82 // local:port (loopback)
83 memset(address, 0, sizeof(*address));
84 address->addresstype = LHNETADDRESSTYPE_LOOP;
85 address->addressdata.loop.port = port;
87 case LHNETADDRESSTYPE_INET4:
88 // 0.0.0.0:port (INADDR_ANY, binds to all interfaces)
89 memset(address, 0, sizeof(*address));
90 address->addresstype = LHNETADDRESSTYPE_INET4;
91 address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY;
92 address->addressdata.inet4.port = htons((unsigned short)port);
94 case LHNETADDRESSTYPE_INET6:
95 // [0:0:0:0:0:0:0:0]:port (IN6ADDR_ANY, binds to all interfaces)
96 memset(address, 0, sizeof(*address));
97 address->addresstype = LHNETADDRESSTYPE_INET6;
98 address->addressdata.inet6.family = LHNETADDRESSTYPE_INET6_FAMILY;
99 address->addressdata.inet6.port = htons((unsigned short)port);
105 int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int defaultport)
107 int i, port, namelen, d1, d2, d3, d4;
108 struct hostent *hostentry;
111 if (!address || !string || !*string)
113 memset(address, 0, sizeof(*address));
114 address->addresstype = LHNETADDRESSTYPE_NONE;
116 colon = strrchr(string, ':');
118 port = atoi(colon + 1);
120 colon = string + strlen(string);
123 namelen = colon - string;
126 if (string[0] == '[' && namelen > 0 && string[namelen-1] == ']') // ipv6
131 memcpy(name, string, namelen);
134 if (!strcmp(name, "local"))
136 address->addresstype = LHNETADDRESSTYPE_LOOP;
137 address->addressdata.loop.port = port;
140 // try to parse as dotted decimal ipv4 address first
141 // note this supports partial ip addresses
142 d1 = d2 = d3 = d4 = 0;
143 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)
145 // parsed a valid ipv4 address
146 address->addresstype = LHNETADDRESSTYPE_INET4;
147 address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY;
148 address->addressdata.inet4.port = htons((unsigned short)port);
149 address->addressdata.inet4.address[0] = (unsigned char)d1;
150 address->addressdata.inet4.address[1] = (unsigned char)d2;
151 address->addressdata.inet4.address[2] = (unsigned char)d3;
152 address->addressdata.inet4.address[3] = (unsigned char)d4;
153 #ifdef STANDALONETEST
154 printf("manual parsing of ipv4 dotted decimal address \"%s\" successful: %d.%d.%d.%d:%d\n", string, (int)address->addressdata.inet4.address[0], (int)address->addressdata.inet4.address[1], (int)address->addressdata.inet4.address[2], (int)address->addressdata.inet4.address[3], (int)ntohs(address->addressdata.inet4.port));
158 for (i = 0;i < MAX_NAMECACHE;i++)
159 if (!strcmp(namecache[i].name, name))
161 #ifdef STANDALONETEST
162 if (i < MAX_NAMECACHE)
164 if (i < MAX_NAMECACHE && realtime < namecache[i].expirationtime)
167 *address = namecache[i].address;
168 if (address->addresstype == LHNETADDRESSTYPE_INET6)
170 address->addressdata.inet6.port = htons((unsigned short)port);
173 else if (address->addresstype == LHNETADDRESSTYPE_INET4)
175 address->addressdata.inet4.port = htons((unsigned short)port);
180 // try gethostbyname (handles dns and other ip formats)
181 hostentry = gethostbyname(name);
184 if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET6_FAMILY)
187 address->addresstype = LHNETADDRESSTYPE_INET6;
188 address->addressdata.inet6.family = hostentry->h_addrtype;
189 address->addressdata.inet6.port = htons((unsigned short)port);
190 memcpy(address->addressdata.inet6.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet6.address));
191 for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
192 namecache[namecacheposition].name[i] = name[i];
193 namecache[namecacheposition].name[i] = 0;
194 #ifndef STANDALONETEST
195 namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
197 namecache[namecacheposition].address = *address;
198 namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
199 #ifdef STANDALONETEST
200 printf("gethostbyname(\"%s\") returned ipv6 address [%x:%x:%x:%x:%x:%x:%x:%x]:%d\n", name, (int)address->addressdata.inet6.address[0], (int)address->addressdata.inet6.address[1], (int)address->addressdata.inet6.address[2], (int)address->addressdata.inet6.address[3], (int)address->addressdata.inet6.address[4], (int)address->addressdata.inet6.address[5], (int)address->addressdata.inet6.address[6], (int)address->addressdata.inet6.address[7], (int)ntohs(address->addressdata.inet6.port));
204 else if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET4_FAMILY)
207 address->addresstype = LHNETADDRESSTYPE_INET4;
208 address->addressdata.inet4.family = hostentry->h_addrtype;
209 address->addressdata.inet4.port = htons((unsigned short)port);
210 memcpy(address->addressdata.inet4.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet4.address));
211 for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
212 namecache[namecacheposition].name[i] = name[i];
213 namecache[namecacheposition].name[i] = 0;
214 #ifndef STANDALONETEST
215 namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
217 namecache[namecacheposition].address = *address;
218 namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
219 #ifdef STANDALONETEST
220 printf("gethostbyname(\"%s\") returned ipv4 address %d.%d.%d.%d:%d\n", name, (int)address->addressdata.inet4.address[0], (int)address->addressdata.inet4.address[1], (int)address->addressdata.inet4.address[2], (int)address->addressdata.inet4.address[3], (int)ntohs(address->addressdata.inet4.port));
225 #ifdef STANDALONETEST
226 printf("gethostbyname failed on address \"%s\"\n", name);
228 for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
229 namecache[namecacheposition].name[i] = name[i];
230 namecache[namecacheposition].name[i] = 0;
231 #ifndef STANDALONETEST
232 namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
234 namecache[namecacheposition].address.addresstype = LHNETADDRESSTYPE_NONE;
235 namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
239 int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int stringbuffersize, int includeport)
242 if (!address || !string || stringbuffersize < 1)
244 switch(address->addresstype)
248 case LHNETADDRESSTYPE_LOOP:
251 if (stringbuffersize >= 12)
253 sprintf(string, "local:%d", (int)address->addressdata.loop.port);
259 if (stringbuffersize >= 6)
261 memcpy(string, "local", 6);
266 case LHNETADDRESSTYPE_INET4:
269 if (stringbuffersize >= 22)
271 sprintf(string, "%d.%d.%d.%d:%d", (int)address->addressdata.inet4.address[0], (int)address->addressdata.inet4.address[1], (int)address->addressdata.inet4.address[2], (int)address->addressdata.inet4.address[3], (int)ntohs(address->addressdata.inet4.port));
277 if (stringbuffersize >= 16)
279 sprintf(string, "%d.%d.%d.%d", (int)address->addressdata.inet4.address[0], (int)address->addressdata.inet4.address[1], (int)address->addressdata.inet4.address[2], (int)address->addressdata.inet4.address[3]);
284 case LHNETADDRESSTYPE_INET6:
287 if (stringbuffersize >= 88)
289 sprintf(string, "[%x:%x:%x:%x:%x:%x:%x:%x]:%d", (int)address->addressdata.inet6.address[0], (int)address->addressdata.inet6.address[1], (int)address->addressdata.inet6.address[2], (int)address->addressdata.inet6.address[3], (int)address->addressdata.inet6.address[4], (int)address->addressdata.inet6.address[5], (int)address->addressdata.inet6.address[6], (int)address->addressdata.inet6.address[7], (int)ntohs(address->addressdata.inet6.port));
295 if (stringbuffersize >= 80)
297 sprintf(string, "%x:%x:%x:%x:%x:%x:%x:%x", (int)address->addressdata.inet6.address[0], (int)address->addressdata.inet6.address[1], (int)address->addressdata.inet6.address[2], (int)address->addressdata.inet6.address[3], (int)address->addressdata.inet6.address[4], (int)address->addressdata.inet6.address[5], (int)address->addressdata.inet6.address[6], (int)address->addressdata.inet6.address[7]);
306 int LHNETADDRESS_GetAddressType(const lhnetaddress_t *address)
309 return address->addresstype;
311 return LHNETADDRESSTYPE_NONE;
314 int LHNETADDRESS_GetPort(const lhnetaddress_t *address)
318 switch(address->addresstype)
320 case LHNETADDRESSTYPE_LOOP:
321 return address->addressdata.loop.port;
322 case LHNETADDRESSTYPE_INET4:
323 return ntohs(address->addressdata.inet4.port);
324 case LHNETADDRESSTYPE_INET6:
325 return ntohs(address->addressdata.inet6.port);
331 int LHNETADDRESS_SetPort(lhnetaddress_t *address, int port)
335 switch(address->addresstype)
337 case LHNETADDRESSTYPE_LOOP:
338 address->addressdata.loop.port = port;
340 case LHNETADDRESSTYPE_INET4:
341 address->addressdata.inet4.port = htons((unsigned short)port);
343 case LHNETADDRESSTYPE_INET6:
344 address->addressdata.inet6.port = htons((unsigned short)port);
351 int LHNETADDRESS_Compare(const lhnetaddress_t *address1, const lhnetaddress_t *address2)
353 if (!address1 || !address2)
355 if (address1->addresstype != address2->addresstype)
357 switch(address1->addresstype)
359 case LHNETADDRESSTYPE_LOOP:
360 if (address1->addressdata.loop.port != address2->addressdata.loop.port)
363 case LHNETADDRESSTYPE_INET4:
364 if (address1->addressdata.inet4.family != address2->addressdata.inet4.family)
366 if (memcmp(address1->addressdata.inet4.address, address2->addressdata.inet4.address, sizeof(address1->addressdata.inet4.address)))
368 if (address1->addressdata.inet4.port != address2->addressdata.inet4.port)
371 case LHNETADDRESSTYPE_INET6:
372 if (address1->addressdata.inet6.family != address2->addressdata.inet6.family)
374 if (memcmp(address1->addressdata.inet6.address, address2->addressdata.inet6.address, sizeof(address1->addressdata.inet6.address)))
376 if (address1->addressdata.inet6.port != address2->addressdata.inet6.port)
384 typedef struct lhnetpacket_s
391 #ifndef STANDALONETEST
392 double sentdoubletime;
394 struct lhnetpacket_s *next, *prev;
398 static int lhnet_active;
399 static lhnetsocket_t lhnet_socketlist;
400 static lhnetpacket_t lhnet_packetlist;
402 static int lhnet_didWSAStartup = 0;
403 static WSADATA lhnet_winsockdata;
406 void LHNET_Init(void)
410 lhnet_socketlist.next = lhnet_socketlist.prev = &lhnet_socketlist;
411 lhnet_packetlist.next = lhnet_packetlist.prev = &lhnet_packetlist;
414 lhnet_didWSAStartup = !WSAStartup(MAKEWORD(1, 1), &lhnet_winsockdata);
415 if (!lhnet_didWSAStartup)
416 Con_Print("LHNET_Init: WSAStartup failed, networking disabled\n");
420 void LHNET_Shutdown(void)
425 while (lhnet_socketlist.next != &lhnet_socketlist)
426 LHNET_CloseSocket(lhnet_socketlist.next);
427 while (lhnet_packetlist.next != &lhnet_packetlist)
429 p = lhnet_packetlist.next;
430 p->prev->next = p->next;
431 p->next->prev = p->prev;
435 if (lhnet_didWSAStartup)
437 lhnet_didWSAStartup = 0;
444 static const char *LHNETPRIVATE_StrError(void)
447 int i = WSAGetLastError();
450 case WSAEINTR: return "WSAEINTR";
451 case WSAEBADF: return "WSAEBADF";
452 case WSAEACCES: return "WSAEACCES";
453 case WSAEFAULT: return "WSAEFAULT";
454 case WSAEINVAL: return "WSAEINVAL";
455 case WSAEMFILE: return "WSAEMFILE";
456 case WSAEWOULDBLOCK: return "WSAEWOULDBLOCK";
457 case WSAEINPROGRESS: return "WSAEINPROGRESS";
458 case WSAEALREADY: return "WSAEALREADY";
459 case WSAENOTSOCK: return "WSAENOTSOCK";
460 case WSAEDESTADDRREQ: return "WSAEDESTADDRREQ";
461 case WSAEMSGSIZE: return "WSAEMSGSIZE";
462 case WSAEPROTOTYPE: return "WSAEPROTOTYPE";
463 case WSAENOPROTOOPT: return "WSAENOPROTOOPT";
464 case WSAEPROTONOSUPPORT: return "WSAEPROTONOSUPPORT";
465 case WSAESOCKTNOSUPPORT: return "WSAESOCKTNOSUPPORT";
466 case WSAEOPNOTSUPP: return "WSAEOPNOTSUPP";
467 case WSAEPFNOSUPPORT: return "WSAEPFNOSUPPORT";
468 case WSAEAFNOSUPPORT: return "WSAEAFNOSUPPORT";
469 case WSAEADDRINUSE: return "WSAEADDRINUSE";
470 case WSAEADDRNOTAVAIL: return "WSAEADDRNOTAVAIL";
471 case WSAENETDOWN: return "WSAENETDOWN";
472 case WSAENETUNREACH: return "WSAENETUNREACH";
473 case WSAENETRESET: return "WSAENETRESET";
474 case WSAECONNABORTED: return "WSAECONNABORTED";
475 case WSAECONNRESET: return "WSAECONNRESET";
476 case WSAENOBUFS: return "WSAENOBUFS";
477 case WSAEISCONN: return "WSAEISCONN";
478 case WSAENOTCONN: return "WSAENOTCONN";
479 case WSAESHUTDOWN: return "WSAESHUTDOWN";
480 case WSAETOOMANYREFS: return "WSAETOOMANYREFS";
481 case WSAETIMEDOUT: return "WSAETIMEDOUT";
482 case WSAECONNREFUSED: return "WSAECONNREFUSED";
483 case WSAELOOP: return "WSAELOOP";
484 case WSAENAMETOOLONG: return "WSAENAMETOOLONG";
485 case WSAEHOSTDOWN: return "WSAEHOSTDOWN";
486 case WSAEHOSTUNREACH: return "WSAEHOSTUNREACH";
487 case WSAENOTEMPTY: return "WSAENOTEMPTY";
488 case WSAEPROCLIM: return "WSAEPROCLIM";
489 case WSAEUSERS: return "WSAEUSERS";
490 case WSAEDQUOT: return "WSAEDQUOT";
491 case WSAESTALE: return "WSAESTALE";
492 case WSAEREMOTE: return "WSAEREMOTE";
493 case WSAEDISCON: return "WSAEDISCON";
494 case 0: return "no error";
495 default: return "unknown WSAE error";
498 return strerror(errno);
502 void LHNET_SleepUntilPacket_Microseconds(int microseconds)
510 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
512 if (s->address.addresstype == LHNETADDRESSTYPE_INET4 || s->address.addresstype == LHNETADDRESSTYPE_INET6)
514 if (lastfd < s->inetsocket)
515 lastfd = s->inetsocket;
516 FD_SET(s->inetsocket, &fdreadset);
519 tv.tv_sec = microseconds / 1000000;
520 tv.tv_usec = microseconds % 1000000;
521 select(lastfd + 1, &fdreadset, NULL, NULL, &tv);
524 lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
526 lhnetsocket_t *lhnetsocket, *s;
529 lhnetsocket = (lhnetsocket_t *)Z_Malloc(sizeof(*lhnetsocket));
532 memset(lhnetsocket, 0, sizeof(*lhnetsocket));
533 lhnetsocket->address = *address;
534 switch(lhnetsocket->address.addresstype)
536 case LHNETADDRESSTYPE_LOOP:
537 if (lhnetsocket->address.addressdata.loop.port == 0)
539 // allocate a port dynamically
540 // this search will always terminate because there is never
541 // an allocated socket with port 0, so if the number wraps it
542 // will find the port is unused, and then refuse to use port
543 // 0, causing an intentional failure condition
544 lhnetsocket->address.addressdata.loop.port = 1024;
547 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
548 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.addressdata.loop.port == lhnetsocket->address.addressdata.loop.port)
550 if (s == &lhnet_socketlist)
552 lhnetsocket->address.addressdata.loop.port++;
555 // check if the port is available
556 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
557 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.addressdata.loop.port == lhnetsocket->address.addressdata.loop.port)
559 if (s == &lhnet_socketlist && lhnetsocket->address.addressdata.loop.port != 0)
561 lhnetsocket->next = &lhnet_socketlist;
562 lhnetsocket->prev = lhnetsocket->next->prev;
563 lhnetsocket->next->prev = lhnetsocket;
564 lhnetsocket->prev->next = lhnetsocket;
568 case LHNETADDRESSTYPE_INET4:
569 case LHNETADDRESSTYPE_INET6:
571 if (lhnet_didWSAStartup)
574 if ((lhnetsocket->inetsocket = socket(address->addresstype == LHNETADDRESSTYPE_INET6 ? LHNETADDRESSTYPE_INET6_FAMILY : LHNETADDRESSTYPE_INET4_FAMILY, SOCK_DGRAM, IPPROTO_UDP)) != -1)
582 if (ioctlsocket(lhnetsocket->inetsocket, FIONBIO, &_true) != -1)
585 namelen = address->addresstype == LHNETADDRESSTYPE_INET6 ? sizeof(lhnetsocket->address.addressdata.inet6) : sizeof(lhnetsocket->address.addressdata.inet4);
586 if (bind(lhnetsocket->inetsocket, (struct sockaddr *)&lhnetsocket->address.addressdata, namelen) != -1)
589 getsockname(lhnetsocket->inetsocket, (struct sockaddr *)&lhnetsocket->address.addressdata, &namelen);
590 // enable broadcast on this socket
591 setsockopt(lhnetsocket->inetsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i));
592 lhnetsocket->next = &lhnet_socketlist;
593 lhnetsocket->prev = lhnetsocket->next->prev;
594 lhnetsocket->next->prev = lhnetsocket;
595 lhnetsocket->prev->next = lhnetsocket;
597 if (ioctlsocket(lhnetsocket->inetsocket, SIO_UDP_CONNRESET, &_false) == -1)
598 Con_DPrintf("LHNET_OpenSocket_Connectionless: ioctlsocket SIO_UDP_CONNRESET returned error: %s\n", LHNETPRIVATE_StrError());
603 Con_Printf("LHNET_OpenSocket_Connectionless: bind returned error: %s\n", LHNETPRIVATE_StrError());
606 Con_Printf("LHNET_OpenSocket_Connectionless: ioctlsocket returned error: %s\n", LHNETPRIVATE_StrError());
607 closesocket(lhnetsocket->inetsocket);
610 Con_Printf("LHNET_OpenSocket_Connectionless: socket returned error: %s\n", LHNETPRIVATE_StrError());
614 Con_Print("LHNET_OpenSocket_Connectionless: can't open a socket (WSAStartup failed during LHNET_Init)\n");
625 void LHNET_CloseSocket(lhnetsocket_t *lhnetsocket)
629 // unlink from socket list
630 if (lhnetsocket->next == NULL)
632 lhnetsocket->next->prev = lhnetsocket->prev;
633 lhnetsocket->prev->next = lhnetsocket->next;
634 lhnetsocket->next = NULL;
635 lhnetsocket->prev = NULL;
637 // no special close code for loopback, just inet
638 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4 || lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
640 closesocket(lhnetsocket->inetsocket);
646 lhnetaddress_t *LHNET_AddressFromSocket(lhnetsocket_t *sock)
649 return &sock->address;
654 int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, lhnetaddress_t *address)
657 if (!lhnetsocket || !address || !content || maxcontentlength < 1)
659 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
662 lhnetpacket_t *p, *pnext;
663 // scan for any old packets to timeout while searching for a packet
664 // that is waiting to be delivered to this socket
665 currenttime = time(NULL);
666 for (p = lhnet_packetlist.next;p != &lhnet_packetlist;p = pnext)
669 if (p->timeout < currenttime)
672 p->next->prev = p->prev;
673 p->prev->next = p->next;
677 #ifndef STANDALONETEST
678 if (cl_netlocalping.value && (realtime - cl_netlocalping.value * (1.0 / 2000.0)) < p->sentdoubletime)
681 if (value == 0 && p->destinationport == lhnetsocket->address.addressdata.loop.port)
683 if (p->length <= maxcontentlength)
685 *address = lhnetsocket->address;
686 address->addressdata.loop.port = p->sourceport;
687 memcpy(content, p->data, p->length);
693 p->next->prev = p->prev;
694 p->prev->next = p->next;
699 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
701 unsigned int inetaddresslength;
702 address->addresstype = LHNETADDRESSTYPE_NONE;
703 inetaddresslength = sizeof(address->addressdata.inet4);
704 value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, (struct sockaddr *)&address->addressdata.inet4, &inetaddresslength);
707 address->addresstype = LHNETADDRESSTYPE_INET4;
710 else if (value == -1)
713 if (e == EWOULDBLOCK)
718 Con_Print("Connection refused\n");
721 Con_Printf("LHNET_Read: recvfrom returned error: %s\n", LHNETPRIVATE_StrError());
724 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
726 unsigned int inetaddresslength;
727 address->addresstype = LHNETADDRESSTYPE_NONE;
728 inetaddresslength = sizeof(address->addressdata.inet6);
729 value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, (struct sockaddr *)&address->addressdata.inet6, &inetaddresslength);
732 address->addresstype = LHNETADDRESSTYPE_INET6;
735 else if (value == -1)
738 if (e == EWOULDBLOCK)
743 Con_Print("Connection refused\n");
746 Con_Printf("LHNET_Read: recvfrom returned error: %s\n", LHNETPRIVATE_StrError());
752 int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentlength, const lhnetaddress_t *address)
755 if (!lhnetsocket || !address || !content || contentlength < 1)
757 if (lhnetsocket->address.addresstype != address->addresstype)
759 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
762 p = (lhnetpacket_t *)Z_Malloc(sizeof(*p) + contentlength);
763 p->data = (void *)(p + 1);
764 memcpy(p->data, content, contentlength);
765 p->length = contentlength;
766 p->sourceport = lhnetsocket->address.addressdata.loop.port;
767 p->destinationport = address->addressdata.loop.port;
768 p->timeout = time(NULL) + 10;
769 p->next = &lhnet_packetlist;
770 p->prev = p->next->prev;
773 #ifndef STANDALONETEST
774 p->sentdoubletime = realtime;
776 value = contentlength;
778 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
780 value = sendto(lhnetsocket->inetsocket, content, contentlength, 0, (struct sockaddr *)&address->addressdata.inet4, sizeof(address->addressdata.inet4));
783 if (SOCKETERRNO == EWOULDBLOCK)
785 Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError());
788 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
790 value = sendto(lhnetsocket->inetsocket, content, contentlength, 0, (struct sockaddr *)&address->addressdata.inet6, sizeof(address->addressdata.inet6));
793 if (SOCKETERRNO == EWOULDBLOCK)
795 Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError());
801 #ifdef STANDALONETEST
802 int main(int argc, char **argv)
805 char *buffer = "test", buffer2[1024];
806 int blen = strlen(buffer);
808 lhnetsocket_t *sock1;
809 lhnetsocket_t *sock2;
810 lhnetaddress_t myaddy1;
811 lhnetaddress_t myaddy2;
812 lhnetaddress_t myaddy3;
813 lhnetaddress_t localhostaddy1;
814 lhnetaddress_t localhostaddy2;
818 printf("calling LHNET_Init\n");
821 printf("calling LHNET_FromPort twice to create two local addresses\n");
822 LHNETADDRESS_FromPort(&myaddy1, LHNETADDRESSTYPE_INET4, 4000);
823 LHNETADDRESS_FromPort(&myaddy2, LHNETADDRESSTYPE_INET4, 4001);
824 LHNETADDRESS_FromString(&localhostaddy1, "127.0.0.1", 4000);
825 LHNETADDRESS_FromString(&localhostaddy2, "127.0.0.1", 4001);
827 printf("calling LHNET_OpenSocket_Connectionless twice to create two local sockets\n");
828 sock1 = LHNET_OpenSocket_Connectionless(&myaddy1);
829 sock2 = LHNET_OpenSocket_Connectionless(&myaddy2);
831 printf("calling LHNET_Write to send a packet from the first socket to the second socket\n");
832 test1 = LHNET_Write(sock1, buffer, blen, &localhostaddy2);
833 printf("sleeping briefly\n");
839 printf("calling LHNET_Read on the second socket to read the packet sent from the first socket\n");
840 test2 = LHNET_Read(sock2, buffer2, b2len - 1, &myaddy3);
842 Con_Printf("socket to socket test succeeded\n");
844 Con_Printf("socket to socket test failed\n");
847 printf("press any key to exit\n");
851 printf("calling LHNET_Shutdown\n");
856 lhnetsocket_t *sock[16], *sendsock;
865 int sendmessagelength;
866 lhnetaddress_t destaddress;
867 lhnetaddress_t receiveaddress;
868 lhnetaddress_t sockaddress[16];
869 char buffer[1536], addressstring[128], addressstring2[128];
870 if ((argc == 2 || argc == 5) && (port = atoi(argv[1])) >= 1 && port < 65535)
872 printf("calling LHNET_Init()\n");
876 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_LOOP, port);
877 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET4, port);
878 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET6, port+1);
882 sendmessagelength = 0;
884 for (i = 0;i < numsockets;i++)
886 LHNETADDRESS_ToString(&sockaddress[i], addressstring, sizeof(addressstring), 1);
887 printf("calling LHNET_OpenSocket_Connectionless(<%s>)\n", addressstring);
888 if ((sock[i] = LHNET_OpenSocket_Connectionless(&sockaddress[i])))
890 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
891 printf("opened socket successfully (address \"%s\")\n", addressstring2);
895 printf("failed to open socket\n");
906 count = atoi(argv[2]);
907 if (LHNETADDRESS_FromString(&destaddress, argv[3], -1))
909 sendmessage = argv[4];
910 sendmessagelength = strlen(sendmessage);
912 for (i = 0;i < numsockets;i++)
913 if (sock[i] && LHNETADDRESS_GetAddressType(&destaddress) == LHNETADDRESS_GetAddressType(&sockaddress[i]))
915 if (sendsock == NULL)
917 printf("Could not find an open socket matching the addresstype (%i) of destination address, switching to listen only mode\n", LHNETADDRESS_GetAddressType(&destaddress));
923 printf("LHNETADDRESS_FromString did not like the address \"%s\", switching to listen only mode\n", argv[3]);
927 printf("started, now listening for \"exit\" on the opened sockets\n");
928 oldtime = time(NULL);
936 for (i = 0;i < numsockets;i++)
940 length = LHNET_Read(sock[i], buffer, sizeof(buffer), &receiveaddress);
942 printf("localsock read error: length < 0");
943 else if (length > 0 && length < (int)sizeof(buffer))
946 LHNETADDRESS_ToString(&receiveaddress, addressstring, sizeof(addressstring), 1);
947 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
948 printf("received message \"%s\" from \"%s\" on socket \"%s\"\n", buffer, addressstring, addressstring2);
949 if (!strcmp(buffer, "exit"))
956 if (argc == 5 && count > 0)
958 newtime = time(NULL);
959 if (newtime != oldtime)
961 LHNETADDRESS_ToString(&destaddress, addressstring, sizeof(addressstring), 1);
962 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sendsock), addressstring2, sizeof(addressstring2), 1);
963 printf("calling LHNET_Write(<%s>, \"%s\", %i, <%s>)\n", addressstring2, sendmessage, sendmessagelength, addressstring);
964 length = LHNET_Write(sendsock, sendmessage, sendmessagelength, &destaddress);
965 if (length == sendmessagelength)
966 printf("sent successfully\n");
968 printf("LH_Write failed, returned %i (length of message was %i)\n", length, strlen(argv[4]));
972 printf("Done sending, still listening for \"exit\"\n");
976 for (i = 0;i < numsockets;i++)
980 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
981 printf("calling LHNET_CloseSocket(<%s>)\n", addressstring2);
982 LHNET_CloseSocket(sock[i]);
985 printf("calling LHNET_Shutdown()\n");
989 printf("Testing code for lhnet.c\nusage: lhnettest <localportnumber> [<sendnumberoftimes> <sendaddress:port> <sendmessage>]\n");