2 // Written by Forest Hale 2003-06-15 and placed into public domain.
12 //#include <netinet/in.h>
13 //#include <arpa/inet.h>
15 #include <sys/socket.h>
16 #include <sys/ioctl.h>
20 // for Z_Malloc/Z_Free in quake
21 #ifndef STANDALONETEST
24 #define Z_Malloc malloc
30 int LHNETADDRESS_FromPort(lhnetaddress_t *address, int addresstype, int port)
34 case LHNETADDRESSTYPE_LOOP:
35 // local:port (loopback)
36 memset(address, 0, sizeof(*address));
37 address->addresstype = LHNETADDRESSTYPE_LOOP;
38 address->addressdata.loop.port = port;
40 case LHNETADDRESSTYPE_INET4:
41 // 0.0.0.0:port (INADDR_ANY, binds to all interfaces)
42 memset(address, 0, sizeof(*address));
43 address->addresstype = LHNETADDRESSTYPE_INET4;
44 address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY;
45 address->addressdata.inet4.port = htons((unsigned short)port);
47 case LHNETADDRESSTYPE_INET6:
48 // [0:0:0:0:0:0:0:0]:port (IN6ADDR_ANY, binds to all interfaces)
49 memset(address, 0, sizeof(*address));
50 address->addresstype = LHNETADDRESSTYPE_INET6;
51 address->addressdata.inet6.family = LHNETADDRESSTYPE_INET6_FAMILY;
52 address->addressdata.inet6.port = htons((unsigned short)port);
58 int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int defaultport)
60 int port, namelen, d1, d2, d3, d4;
61 struct hostent *hostentry;
64 memset(address, 0, sizeof(*address));
65 address->addresstype = LHNETADDRESSTYPE_NONE;
67 colon = strrchr(string, ':');
69 port = atoi(colon + 1);
71 colon = string + strlen(string);
74 namelen = colon - string;
77 if (string[0] == '[' && namelen > 0 && string[namelen-1] == ']') // ipv6
82 memcpy(name, string, namelen);
85 if (!strcmp(name, "local"))
87 address->addresstype = LHNETADDRESSTYPE_LOOP;
88 address->addressdata.loop.port = port;
91 // try to parse with gethostbyname first, because it can handle ipv4 and
92 // ipv6 (in various address formats), as well as dns names
93 hostentry = gethostbyname(name);
96 if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET6_FAMILY)
99 address->addresstype = LHNETADDRESSTYPE_INET6;
100 address->addressdata.inet6.family = hostentry->h_addrtype;
101 address->addressdata.inet6.port = htons((unsigned short)port);
102 memcpy(address->addressdata.inet6.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet6.address));
103 #ifdef STANDALONETEST
104 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));
108 else if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET4_FAMILY)
111 address->addresstype = LHNETADDRESSTYPE_INET4;
112 address->addressdata.inet4.family = hostentry->h_addrtype;
113 address->addressdata.inet4.port = htons((unsigned short)port);
114 memcpy(address->addressdata.inet4.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet4.address));
115 #ifdef STANDALONETEST
116 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));
121 // failed, try to parse as an ipv4 address as a fallback (is this needed?)
122 #ifdef STANDALONETEST
123 printf("gethostbyname and gethostbyaddr failed on address \"%s\"\n", name);
125 if (sscanf(name, "%d.%d.%d.%d", &d1, &d2, &d3, &d4) == 4 && (unsigned int)d1 < 256 && (unsigned int)d2 < 256 && (unsigned int)d3 < 256 && (unsigned int)d4 < 256)
127 // parsed a valid ipv4 address
128 address->addresstype = LHNETADDRESSTYPE_INET4;
129 address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY;
130 address->addressdata.inet4.port = htons((unsigned short)port);
131 address->addressdata.inet4.address[0] = (unsigned char)d1;
132 address->addressdata.inet4.address[1] = (unsigned char)d2;
133 address->addressdata.inet4.address[2] = (unsigned char)d3;
134 address->addressdata.inet4.address[3] = (unsigned char)d4;
135 #ifdef STANDALONETEST
136 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));
143 int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int stringbuffersize, int includeport)
146 switch(address->addresstype)
150 case LHNETADDRESSTYPE_LOOP:
153 if (stringbuffersize >= 12)
155 sprintf(string, "local:%d", (int)address->addressdata.loop.port);
161 if (stringbuffersize >= 6)
163 strcpy(string, "local");
168 case LHNETADDRESSTYPE_INET4:
171 if (stringbuffersize >= 22)
173 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));
179 if (stringbuffersize >= 16)
181 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]);
186 case LHNETADDRESSTYPE_INET6:
189 if (stringbuffersize >= 88)
191 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));
197 if (stringbuffersize >= 80)
199 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]);
208 int LHNETADDRESS_GetAddressType(const lhnetaddress_t *address)
210 return address->addresstype;
213 int LHNETADDRESS_GetPort(const lhnetaddress_t *address)
215 switch(address->addresstype)
217 case LHNETADDRESSTYPE_LOOP:
218 return address->addressdata.loop.port;
219 case LHNETADDRESSTYPE_INET4:
220 return ntohs(address->addressdata.inet4.port);
221 case LHNETADDRESSTYPE_INET6:
222 return ntohs(address->addressdata.inet6.port);
228 int LHNETADDRESS_SetPort(lhnetaddress_t *address, int port)
230 switch(address->addresstype)
232 case LHNETADDRESSTYPE_LOOP:
233 address->addressdata.loop.port = port;
235 case LHNETADDRESSTYPE_INET4:
236 address->addressdata.inet4.port = htons((unsigned short)port);
238 case LHNETADDRESSTYPE_INET6:
239 address->addressdata.inet6.port = htons((unsigned short)port);
246 int LHNETADDRESS_Compare(const lhnetaddress_t *address1, const lhnetaddress_t *address2)
248 if (address1->addresstype != address2->addresstype)
250 switch(address1->addresstype)
252 case LHNETADDRESSTYPE_LOOP:
253 if (address1->addressdata.loop.port != address2->addressdata.loop.port)
256 case LHNETADDRESSTYPE_INET4:
257 if (address1->addressdata.inet4.family != address2->addressdata.inet4.family)
259 if (memcmp(address1->addressdata.inet4.address, address2->addressdata.inet4.address, sizeof(address1->addressdata.inet4.address)))
261 if (address1->addressdata.inet4.port != address2->addressdata.inet4.port)
264 case LHNETADDRESSTYPE_INET6:
265 if (address1->addressdata.inet6.family != address2->addressdata.inet6.family)
267 if (memcmp(address1->addressdata.inet6.address, address2->addressdata.inet6.address, sizeof(address1->addressdata.inet6.address)))
269 if (address1->addressdata.inet6.port != address2->addressdata.inet6.port)
277 typedef struct lhnetpacket_s
284 struct lhnetpacket_s *next, *prev;
288 static int lhnet_active;
289 static lhnetsocket_t lhnet_socketlist;
290 static lhnetpacket_t lhnet_packetlist;
292 static int lhnet_didWSAStartup = 0;
293 static WSADATA lhnet_winsockdata;
296 void LHNET_Init(void)
300 lhnet_socketlist.next = lhnet_socketlist.prev = &lhnet_socketlist;
301 lhnet_packetlist.next = lhnet_packetlist.prev = &lhnet_packetlist;
305 void LHNET_Shutdown(void)
310 while (lhnet_socketlist.next != &lhnet_socketlist)
311 LHNET_CloseSocket(lhnet_socketlist.next);
312 while (lhnet_packetlist.next != &lhnet_packetlist)
314 p = lhnet_packetlist.next;
315 p->prev->next = p->next;
316 p->next->prev = p->prev;
322 lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
324 lhnetsocket_t *lhnetsocket, *s;
325 lhnetsocket = Z_Malloc(sizeof(*lhnetsocket));
328 memset(lhnetsocket, 0, sizeof(*lhnetsocket));
329 lhnetsocket->address = *address;
330 switch(lhnetsocket->address.addresstype)
332 case LHNETADDRESSTYPE_LOOP:
333 if (lhnetsocket->address.addressdata.loop.port == 0)
335 // allocate a port dynamically
336 // this search will always terminate because there is never
337 // an allocated socket with port 0, so if the number wraps it
338 // will find the port is unused, and then refuse to use port
339 // 0, causing an intentional failure condition
340 lhnetsocket->address.addressdata.loop.port = 1024;
343 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
344 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.addressdata.loop.port == lhnetsocket->address.addressdata.loop.port)
346 if (s == &lhnet_socketlist)
348 lhnetsocket->address.addressdata.loop.port++;
351 // check if the port is available
352 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
353 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.addressdata.loop.port == lhnetsocket->address.addressdata.loop.port)
355 if (s == &lhnet_socketlist && lhnetsocket->address.addressdata.loop.port != 0)
357 lhnetsocket->next = &lhnet_socketlist;
358 lhnetsocket->prev = lhnetsocket->next->prev;
359 lhnetsocket->next->prev = lhnetsocket;
360 lhnetsocket->prev->next = lhnetsocket;
364 case LHNETADDRESSTYPE_INET4:
365 case LHNETADDRESSTYPE_INET6:
367 if (!lhnet_didWSAStartup && !WSAStartup(MAKEWORD(1, 1), &lhnet_winsockdata))
369 lhnet_didWSAStartup = 1;
373 if (address->addresstype == LHNETADDRESSTYPE_INET6)
374 lhnetsocket->inetsocket = socket(LHNETADDRESSTYPE_INET6_FAMILY, SOCK_DGRAM, IPPROTO_UDP);
376 lhnetsocket->inetsocket = socket(LHNETADDRESSTYPE_INET4_FAMILY, SOCK_DGRAM, IPPROTO_UDP);
377 if (lhnetsocket->inetsocket != -1)
381 if (ioctlsocket(lhnetsocket->inetsocket, FIONBIO, &_true) != -1)
384 if (ioctl(lhnetsocket->inetsocket, FIONBIO, &_true) != -1)
387 if (bind(lhnetsocket->inetsocket, (void *)&lhnetsocket->address.addressdata, address->addresstype == LHNETADDRESSTYPE_INET6 ? sizeof(lhnetsocket->address.addressdata.inet6) : sizeof(lhnetsocket->address.addressdata.inet4)) != -1)
389 lhnetsocket->next = &lhnet_socketlist;
390 lhnetsocket->prev = lhnetsocket->next->prev;
391 lhnetsocket->next->prev = lhnetsocket;
392 lhnetsocket->prev->next = lhnetsocket;
397 closesocket(lhnetsocket->inetsocket);
399 close(lhnetsocket->inetsocket);
412 void LHNET_CloseSocket(lhnetsocket_t *lhnetsocket)
416 // unlink from socket list
417 if (lhnetsocket->next == NULL)
419 lhnetsocket->next->prev = lhnetsocket->prev;
420 lhnetsocket->prev->next = lhnetsocket->next;
421 lhnetsocket->next = NULL;
422 lhnetsocket->prev = NULL;
424 // no special close code for loopback, just inet
425 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4 || lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
428 closesocket(lhnetsocket->inetsocket);
430 close(lhnetsocket->inetsocket);
434 if (lhnet_socketlist.next == &lhnet_socketlist && lhnet_didWSAStartup)
436 lhnet_didWSAStartup = 0;
444 lhnetaddress_t *LHNET_AddressFromSocket(lhnetsocket_t *sock)
446 return &sock->address;
449 int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, lhnetaddress_t *address)
452 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
455 lhnetpacket_t *p, *pnext;
456 // scan for any old packets to timeout while searching for a packet
457 // that is waiting to be delivered to this socket
458 currenttime = time(NULL);
459 for (p = lhnet_packetlist.next;p != &lhnet_packetlist;p = pnext)
462 if (value == 0 && p->destinationport == lhnetsocket->address.addressdata.loop.port)
464 if (p->length <= maxcontentlength)
466 *address = lhnetsocket->address;
467 address->addressdata.loop.port = p->sourceport;
468 memcpy(content, p->data, p->length);
474 p->next->prev = p->prev;
475 p->prev->next = p->next;
478 else if (p->timeout < currenttime)
481 p->next->prev = p->prev;
482 p->prev->next = p->next;
487 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
489 int inetaddresslength;
490 address->addresstype = LHNETADDRESSTYPE_NONE;
491 inetaddresslength = sizeof(address->addressdata.inet4);
492 value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, (struct sockaddr *)&address->addressdata.inet4, &inetaddresslength);
495 address->addresstype = LHNETADDRESSTYPE_INET4;
498 else if (value == -1)
501 int e = WSAGetLastError();
502 if (e == WSAEWOULDBLOCK || e == WSAECONNREFUSED)
505 if (errno == EWOULDBLOCK || errno == ECONNREFUSED)
510 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
512 int inetaddresslength;
513 address->addresstype = LHNETADDRESSTYPE_NONE;
514 inetaddresslength = sizeof(address->addressdata.inet6);
515 value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, (struct sockaddr *)&address->addressdata.inet6, &inetaddresslength);
518 address->addresstype = LHNETADDRESSTYPE_INET6;
521 else if (value == -1)
524 int e = WSAGetLastError();
525 if (e == WSAEWOULDBLOCK || e == WSAECONNREFUSED)
528 if (errno == EWOULDBLOCK || errno == ECONNREFUSED)
536 int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentlength, const lhnetaddress_t *address)
539 if (lhnetsocket->address.addresstype != address->addresstype)
541 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
544 p = Z_Malloc(sizeof(*p) + contentlength);
545 p->data = (void *)(p + 1);
546 memcpy(p->data, content, contentlength);
547 p->length = contentlength;
548 p->sourceport = lhnetsocket->address.addressdata.loop.port;
549 p->destinationport = address->addressdata.loop.port;
550 p->timeout = time(NULL) + 10;
551 p->next = &lhnet_packetlist;
552 p->prev = p->next->prev;
555 value = contentlength;
557 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
559 value = sendto(lhnetsocket->inetsocket, content, contentlength, 0, (struct sockaddr *)&address->addressdata.inet4, sizeof(address->addressdata.inet4));
563 int e = WSAGetLastError();
564 if (e == WSAEWOULDBLOCK)
567 if (errno == EWOULDBLOCK)
572 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
574 value = sendto(lhnetsocket->inetsocket, content, contentlength, 0, (struct sockaddr *)&address->addressdata.inet6, sizeof(address->addressdata.inet6));
578 int e = WSAGetLastError();
579 if (e == WSAEWOULDBLOCK)
582 if (errno == EWOULDBLOCK)
590 #ifdef STANDALONETEST
591 int main(int argc, char **argv)
593 lhnetsocket_t *sock[16], *sendsock;
602 int sendmessagelength;
603 lhnetaddress_t destaddress;
604 lhnetaddress_t receiveaddress;
605 lhnetaddress_t sockaddress[16];
606 char buffer[1536], addressstring[128], addressstring2[128];
607 if ((argc == 2 || argc == 5) && (port = atoi(argv[1])) >= 1 && port < 65535)
609 printf("calling LHNET_Init()\n");
613 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_LOOP, port);
614 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET4, port);
615 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET6, port+1);
619 sendmessagelength = 0;
621 for (i = 0;i < numsockets;i++)
623 LHNETADDRESS_ToString(&sockaddress[i], addressstring, sizeof(addressstring), 1);
624 printf("calling LHNET_OpenSocket_Connectionless(<%s>)\n", addressstring);
625 if ((sock[i] = LHNET_OpenSocket_Connectionless(&sockaddress[i])))
627 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
628 printf("opened socket successfully (address \"%s\")\n", addressstring2);
632 printf("failed to open socket\n");
643 count = atoi(argv[2]);
644 if (LHNETADDRESS_FromString(&destaddress, argv[3], -1))
646 sendmessage = argv[4];
647 sendmessagelength = strlen(sendmessage);
649 for (i = 0;i < numsockets;i++)
650 if (sock[i] && LHNETADDRESS_GetAddressType(&destaddress) == LHNETADDRESS_GetAddressType(&sockaddress[i]))
652 if (sendsock == NULL)
654 printf("Could not find an open socket matching the addresstype (%i) of destination address, switching to listen only mode\n", LHNETADDRESS_GetAddressType(&destaddress));
660 printf("LHNETADDRESS_FromString did not like the address \"%s\", switching to listen only mode\n", argv[3]);
664 printf("started, now listening for \"exit\" on the opened sockets\n");
665 oldtime = time(NULL);
673 for (i = 0;i < numsockets;i++)
677 length = LHNET_Read(sock[i], buffer, sizeof(buffer), &receiveaddress);
679 printf("localsock read error: length < 0");
680 else if (length > 0 && length < (int)sizeof(buffer))
683 LHNETADDRESS_ToString(&receiveaddress, addressstring, sizeof(addressstring), 1);
684 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
685 printf("received message \"%s\" from \"%s\" on socket \"%s\"\n", buffer, addressstring, addressstring2);
686 if (!strcmp(buffer, "exit"))
693 if (argc == 5 && count > 0)
695 newtime = time(NULL);
696 if (newtime != oldtime)
698 LHNETADDRESS_ToString(&destaddress, addressstring, sizeof(addressstring), 1);
699 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sendsock), addressstring2, sizeof(addressstring2), 1);
700 printf("calling LHNET_Write(<%s>, \"%s\", %i, <%s>)\n", addressstring2, sendmessage, sendmessagelength, addressstring);
701 length = LHNET_Write(sendsock, sendmessage, sendmessagelength, &destaddress);
702 if (length == sendmessagelength)
703 printf("sent successfully\n");
705 printf("LH_Write failed, returned %i (length of message was %i)\n", length, strlen(argv[4]));
709 printf("Done sending, still listening for \"exit\"\n");
713 for (i = 0;i < numsockets;i++)
717 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
718 printf("calling LHNET_CloseSocket(<%s>)\n", addressstring2);
719 LHNET_CloseSocket(sock[i]);
722 printf("calling LHNET_Shutdown()\n");
726 printf("Testing code for lhnet.c\nusage: lhnettest <localportnumber> [<sendnumberoftimes> <sendaddress:port> <sendmessage>]\n");