2 // Written by Forest Hale 2003-06-15 and placed into public domain.
16 #include <sys/socket.h>
17 #include <sys/ioctl.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
25 #include <proto/socket.h>
28 // for Z_Malloc/Z_Free in quake
29 #ifndef STANDALONETEST
34 #define Con_Print printf
35 #define Con_Printf printf
36 #define Z_Malloc malloc
43 #define EWOULDBLOCK WSAEWOULDBLOCK
44 #define ECONNREFUSED WSAECONNREFUSED
46 #define SOCKETERRNO WSAGetLastError()
48 #define IOC_VENDOR 0x18000000
49 #define _WSAIOW(x,y) (IOC_IN|(x)|(y))
50 #define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
53 #elif defined(__MORPHOS__)
54 #define ioctlsocket IoctlSocket
55 #define closesocket CloseSocket
56 #define SOCKETERRNO Errno()
60 #define ioctlsocket ioctl
61 #define closesocket close
62 #define SOCKETERRNO errno
64 #define SOCKLEN_T socklen_t
67 // to make LHNETADDRESS_FromString resolve repeated hostnames faster, cache them
68 #define MAX_NAMECACHE 64
69 static struct namecache_s
71 lhnetaddress_t address;
72 double expirationtime;
75 namecache[MAX_NAMECACHE];
76 static int namecacheposition = 0;
78 int LHNETADDRESS_FromPort(lhnetaddress_t *address, int addresstype, int port)
84 case LHNETADDRESSTYPE_LOOP:
85 // local:port (loopback)
86 memset(address, 0, sizeof(*address));
87 address->addresstype = LHNETADDRESSTYPE_LOOP;
88 address->addressdata.loop.port = port;
90 case LHNETADDRESSTYPE_INET4:
91 // 0.0.0.0:port (INADDR_ANY, binds to all interfaces)
92 memset(address, 0, sizeof(*address));
93 address->addresstype = LHNETADDRESSTYPE_INET4;
94 address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY;
95 address->addressdata.inet4.port = htons((unsigned short)port);
97 case LHNETADDRESSTYPE_INET6:
98 // [0:0:0:0:0:0:0:0]:port (IN6ADDR_ANY, binds to all interfaces)
99 memset(address, 0, sizeof(*address));
100 address->addresstype = LHNETADDRESSTYPE_INET6;
101 address->addressdata.inet6.family = LHNETADDRESSTYPE_INET6_FAMILY;
102 address->addressdata.inet6.port = htons((unsigned short)port);
108 int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int defaultport)
110 int i, port, namelen, d1, d2, d3, d4;
111 struct hostent *hostentry;
114 if (!address || !string || !*string)
116 memset(address, 0, sizeof(*address));
117 address->addresstype = LHNETADDRESSTYPE_NONE;
119 colon = strrchr(string, ':');
121 port = atoi(colon + 1);
123 colon = string + strlen(string);
126 namelen = colon - string;
129 if (string[0] == '[' && namelen > 0 && string[namelen-1] == ']') // ipv6
134 memcpy(name, string, namelen);
137 if (!strcmp(name, "local"))
139 address->addresstype = LHNETADDRESSTYPE_LOOP;
140 address->addressdata.loop.port = port;
143 // try to parse as dotted decimal ipv4 address first
144 // note this supports partial ip addresses
145 d1 = d2 = d3 = d4 = 0;
146 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)
148 // parsed a valid ipv4 address
149 address->addresstype = LHNETADDRESSTYPE_INET4;
150 address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY;
151 address->addressdata.inet4.port = htons((unsigned short)port);
152 address->addressdata.inet4.address[0] = (unsigned char)d1;
153 address->addressdata.inet4.address[1] = (unsigned char)d2;
154 address->addressdata.inet4.address[2] = (unsigned char)d3;
155 address->addressdata.inet4.address[3] = (unsigned char)d4;
156 #ifdef STANDALONETEST
157 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));
161 for (i = 0;i < MAX_NAMECACHE;i++)
162 if (!strcmp(namecache[i].name, name))
164 #ifdef STANDALONETEST
165 if (i < MAX_NAMECACHE)
167 if (i < MAX_NAMECACHE && realtime < namecache[i].expirationtime)
170 *address = namecache[i].address;
171 if (address->addresstype == LHNETADDRESSTYPE_INET6)
173 address->addressdata.inet6.port = htons((unsigned short)port);
176 else if (address->addresstype == LHNETADDRESSTYPE_INET4)
178 address->addressdata.inet4.port = htons((unsigned short)port);
183 // try gethostbyname (handles dns and other ip formats)
184 hostentry = gethostbyname(name);
187 if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET6_FAMILY)
190 address->addresstype = LHNETADDRESSTYPE_INET6;
191 address->addressdata.inet6.family = hostentry->h_addrtype;
192 address->addressdata.inet6.port = htons((unsigned short)port);
193 memcpy(address->addressdata.inet6.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet6.address));
194 for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
195 namecache[namecacheposition].name[i] = name[i];
196 namecache[namecacheposition].name[i] = 0;
197 #ifndef STANDALONETEST
198 namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
200 namecache[namecacheposition].address = *address;
201 namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
202 #ifdef STANDALONETEST
203 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));
207 else if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET4_FAMILY)
210 address->addresstype = LHNETADDRESSTYPE_INET4;
211 address->addressdata.inet4.family = hostentry->h_addrtype;
212 address->addressdata.inet4.port = htons((unsigned short)port);
213 memcpy(address->addressdata.inet4.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet4.address));
214 for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
215 namecache[namecacheposition].name[i] = name[i];
216 namecache[namecacheposition].name[i] = 0;
217 #ifndef STANDALONETEST
218 namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
220 namecache[namecacheposition].address = *address;
221 namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
222 #ifdef STANDALONETEST
223 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));
228 #ifdef STANDALONETEST
229 printf("gethostbyname failed on address \"%s\"\n", name);
231 for (i = 0;i < (int)sizeof(namecache[namecacheposition].name)-1 && name[i];i++)
232 namecache[namecacheposition].name[i] = name[i];
233 namecache[namecacheposition].name[i] = 0;
234 #ifndef STANDALONETEST
235 namecache[namecacheposition].expirationtime = realtime + 12 * 3600; // 12 hours
237 namecache[namecacheposition].address.addresstype = LHNETADDRESSTYPE_NONE;
238 namecacheposition = (namecacheposition + 1) % MAX_NAMECACHE;
242 int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int stringbuffersize, int includeport)
245 if (!address || !string || stringbuffersize < 1)
247 switch(address->addresstype)
251 case LHNETADDRESSTYPE_LOOP:
254 if (stringbuffersize >= 12)
256 sprintf(string, "local:%d", (int)address->addressdata.loop.port);
262 if (stringbuffersize >= 6)
264 memcpy(string, "local", 6);
269 case LHNETADDRESSTYPE_INET4:
272 if (stringbuffersize >= 22)
274 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));
280 if (stringbuffersize >= 16)
282 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]);
287 case LHNETADDRESSTYPE_INET6:
290 if (stringbuffersize >= 88)
292 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));
298 if (stringbuffersize >= 80)
300 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]);
309 int LHNETADDRESS_GetAddressType(const lhnetaddress_t *address)
312 return address->addresstype;
314 return LHNETADDRESSTYPE_NONE;
317 int LHNETADDRESS_GetPort(const lhnetaddress_t *address)
321 switch(address->addresstype)
323 case LHNETADDRESSTYPE_LOOP:
324 return address->addressdata.loop.port;
325 case LHNETADDRESSTYPE_INET4:
326 return ntohs(address->addressdata.inet4.port);
327 case LHNETADDRESSTYPE_INET6:
328 return ntohs(address->addressdata.inet6.port);
334 int LHNETADDRESS_SetPort(lhnetaddress_t *address, int port)
338 switch(address->addresstype)
340 case LHNETADDRESSTYPE_LOOP:
341 address->addressdata.loop.port = port;
343 case LHNETADDRESSTYPE_INET4:
344 address->addressdata.inet4.port = htons((unsigned short)port);
346 case LHNETADDRESSTYPE_INET6:
347 address->addressdata.inet6.port = htons((unsigned short)port);
354 int LHNETADDRESS_Compare(const lhnetaddress_t *address1, const lhnetaddress_t *address2)
356 if (!address1 || !address2)
358 if (address1->addresstype != address2->addresstype)
360 switch(address1->addresstype)
362 case LHNETADDRESSTYPE_LOOP:
363 if (address1->addressdata.loop.port != address2->addressdata.loop.port)
366 case LHNETADDRESSTYPE_INET4:
367 if (address1->addressdata.inet4.family != address2->addressdata.inet4.family)
369 if (memcmp(address1->addressdata.inet4.address, address2->addressdata.inet4.address, sizeof(address1->addressdata.inet4.address)))
371 if (address1->addressdata.inet4.port != address2->addressdata.inet4.port)
374 case LHNETADDRESSTYPE_INET6:
375 if (address1->addressdata.inet6.family != address2->addressdata.inet6.family)
377 if (memcmp(address1->addressdata.inet6.address, address2->addressdata.inet6.address, sizeof(address1->addressdata.inet6.address)))
379 if (address1->addressdata.inet6.port != address2->addressdata.inet6.port)
387 typedef struct lhnetpacket_s
394 #ifndef STANDALONETEST
395 double sentdoubletime;
397 struct lhnetpacket_s *next, *prev;
401 static int lhnet_active;
402 static lhnetsocket_t lhnet_socketlist;
403 static lhnetpacket_t lhnet_packetlist;
405 static int lhnet_didWSAStartup = 0;
406 static WSADATA lhnet_winsockdata;
409 void LHNET_Init(void)
413 lhnet_socketlist.next = lhnet_socketlist.prev = &lhnet_socketlist;
414 lhnet_packetlist.next = lhnet_packetlist.prev = &lhnet_packetlist;
417 lhnet_didWSAStartup = !WSAStartup(MAKEWORD(1, 1), &lhnet_winsockdata);
418 if (!lhnet_didWSAStartup)
419 Con_Print("LHNET_Init: WSAStartup failed, networking disabled\n");
423 void LHNET_Shutdown(void)
428 while (lhnet_socketlist.next != &lhnet_socketlist)
429 LHNET_CloseSocket(lhnet_socketlist.next);
430 while (lhnet_packetlist.next != &lhnet_packetlist)
432 p = lhnet_packetlist.next;
433 p->prev->next = p->next;
434 p->next->prev = p->prev;
438 if (lhnet_didWSAStartup)
440 lhnet_didWSAStartup = 0;
447 static const char *LHNETPRIVATE_StrError(void)
450 int i = WSAGetLastError();
453 case WSAEINTR: return "WSAEINTR";
454 case WSAEBADF: return "WSAEBADF";
455 case WSAEACCES: return "WSAEACCES";
456 case WSAEFAULT: return "WSAEFAULT";
457 case WSAEINVAL: return "WSAEINVAL";
458 case WSAEMFILE: return "WSAEMFILE";
459 case WSAEWOULDBLOCK: return "WSAEWOULDBLOCK";
460 case WSAEINPROGRESS: return "WSAEINPROGRESS";
461 case WSAEALREADY: return "WSAEALREADY";
462 case WSAENOTSOCK: return "WSAENOTSOCK";
463 case WSAEDESTADDRREQ: return "WSAEDESTADDRREQ";
464 case WSAEMSGSIZE: return "WSAEMSGSIZE";
465 case WSAEPROTOTYPE: return "WSAEPROTOTYPE";
466 case WSAENOPROTOOPT: return "WSAENOPROTOOPT";
467 case WSAEPROTONOSUPPORT: return "WSAEPROTONOSUPPORT";
468 case WSAESOCKTNOSUPPORT: return "WSAESOCKTNOSUPPORT";
469 case WSAEOPNOTSUPP: return "WSAEOPNOTSUPP";
470 case WSAEPFNOSUPPORT: return "WSAEPFNOSUPPORT";
471 case WSAEAFNOSUPPORT: return "WSAEAFNOSUPPORT";
472 case WSAEADDRINUSE: return "WSAEADDRINUSE";
473 case WSAEADDRNOTAVAIL: return "WSAEADDRNOTAVAIL";
474 case WSAENETDOWN: return "WSAENETDOWN";
475 case WSAENETUNREACH: return "WSAENETUNREACH";
476 case WSAENETRESET: return "WSAENETRESET";
477 case WSAECONNABORTED: return "WSAECONNABORTED";
478 case WSAECONNRESET: return "WSAECONNRESET";
479 case WSAENOBUFS: return "WSAENOBUFS";
480 case WSAEISCONN: return "WSAEISCONN";
481 case WSAENOTCONN: return "WSAENOTCONN";
482 case WSAESHUTDOWN: return "WSAESHUTDOWN";
483 case WSAETOOMANYREFS: return "WSAETOOMANYREFS";
484 case WSAETIMEDOUT: return "WSAETIMEDOUT";
485 case WSAECONNREFUSED: return "WSAECONNREFUSED";
486 case WSAELOOP: return "WSAELOOP";
487 case WSAENAMETOOLONG: return "WSAENAMETOOLONG";
488 case WSAEHOSTDOWN: return "WSAEHOSTDOWN";
489 case WSAEHOSTUNREACH: return "WSAEHOSTUNREACH";
490 case WSAENOTEMPTY: return "WSAENOTEMPTY";
491 case WSAEPROCLIM: return "WSAEPROCLIM";
492 case WSAEUSERS: return "WSAEUSERS";
493 case WSAEDQUOT: return "WSAEDQUOT";
494 case WSAESTALE: return "WSAESTALE";
495 case WSAEREMOTE: return "WSAEREMOTE";
496 case WSAEDISCON: return "WSAEDISCON";
497 case 0: return "no error";
498 default: return "unknown WSAE error";
501 return strerror(errno);
505 void LHNET_SleepUntilPacket_Microseconds(int microseconds)
513 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
515 if (s->address.addresstype == LHNETADDRESSTYPE_INET4 || s->address.addresstype == LHNETADDRESSTYPE_INET6)
517 if (lastfd < s->inetsocket)
518 lastfd = s->inetsocket;
519 FD_SET(s->inetsocket, &fdreadset);
522 tv.tv_sec = microseconds / 1000000;
523 tv.tv_usec = microseconds % 1000000;
524 select(lastfd + 1, &fdreadset, NULL, NULL, &tv);
527 lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
529 lhnetsocket_t *lhnetsocket, *s;
532 lhnetsocket = (lhnetsocket_t *)Z_Malloc(sizeof(*lhnetsocket));
535 memset(lhnetsocket, 0, sizeof(*lhnetsocket));
536 lhnetsocket->address = *address;
537 switch(lhnetsocket->address.addresstype)
539 case LHNETADDRESSTYPE_LOOP:
540 if (lhnetsocket->address.addressdata.loop.port == 0)
542 // allocate a port dynamically
543 // this search will always terminate because there is never
544 // an allocated socket with port 0, so if the number wraps it
545 // will find the port is unused, and then refuse to use port
546 // 0, causing an intentional failure condition
547 lhnetsocket->address.addressdata.loop.port = 1024;
550 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
551 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.addressdata.loop.port == lhnetsocket->address.addressdata.loop.port)
553 if (s == &lhnet_socketlist)
555 lhnetsocket->address.addressdata.loop.port++;
558 // check if the port is available
559 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
560 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.addressdata.loop.port == lhnetsocket->address.addressdata.loop.port)
562 if (s == &lhnet_socketlist && lhnetsocket->address.addressdata.loop.port != 0)
564 lhnetsocket->next = &lhnet_socketlist;
565 lhnetsocket->prev = lhnetsocket->next->prev;
566 lhnetsocket->next->prev = lhnetsocket;
567 lhnetsocket->prev->next = lhnetsocket;
571 case LHNETADDRESSTYPE_INET4:
572 case LHNETADDRESSTYPE_INET6:
574 if (lhnet_didWSAStartup)
577 if ((lhnetsocket->inetsocket = socket(address->addresstype == LHNETADDRESSTYPE_INET6 ? LHNETADDRESSTYPE_INET6_FAMILY : LHNETADDRESSTYPE_INET4_FAMILY, SOCK_DGRAM, IPPROTO_UDP)) != -1)
585 if (ioctlsocket(lhnetsocket->inetsocket, FIONBIO, &_true) != -1)
588 namelen = address->addresstype == LHNETADDRESSTYPE_INET6 ? sizeof(lhnetsocket->address.addressdata.inet6) : sizeof(lhnetsocket->address.addressdata.inet4);
589 if (bind(lhnetsocket->inetsocket, (struct sockaddr *)&lhnetsocket->address.addressdata, namelen) != -1)
592 getsockname(lhnetsocket->inetsocket, (struct sockaddr *)&lhnetsocket->address.addressdata, &namelen);
593 // enable broadcast on this socket
594 setsockopt(lhnetsocket->inetsocket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i));
595 lhnetsocket->next = &lhnet_socketlist;
596 lhnetsocket->prev = lhnetsocket->next->prev;
597 lhnetsocket->next->prev = lhnetsocket;
598 lhnetsocket->prev->next = lhnetsocket;
600 if (ioctlsocket(lhnetsocket->inetsocket, SIO_UDP_CONNRESET, &_false) == -1)
601 Con_DPrintf("LHNET_OpenSocket_Connectionless: ioctlsocket SIO_UDP_CONNRESET returned error: %s\n", LHNETPRIVATE_StrError());
606 Con_Printf("LHNET_OpenSocket_Connectionless: bind returned error: %s\n", LHNETPRIVATE_StrError());
609 Con_Printf("LHNET_OpenSocket_Connectionless: ioctlsocket returned error: %s\n", LHNETPRIVATE_StrError());
610 closesocket(lhnetsocket->inetsocket);
613 Con_Printf("LHNET_OpenSocket_Connectionless: socket returned error: %s\n", LHNETPRIVATE_StrError());
617 Con_Print("LHNET_OpenSocket_Connectionless: can't open a socket (WSAStartup failed during LHNET_Init)\n");
628 void LHNET_CloseSocket(lhnetsocket_t *lhnetsocket)
632 // unlink from socket list
633 if (lhnetsocket->next == NULL)
635 lhnetsocket->next->prev = lhnetsocket->prev;
636 lhnetsocket->prev->next = lhnetsocket->next;
637 lhnetsocket->next = NULL;
638 lhnetsocket->prev = NULL;
640 // no special close code for loopback, just inet
641 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4 || lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
643 closesocket(lhnetsocket->inetsocket);
649 lhnetaddress_t *LHNET_AddressFromSocket(lhnetsocket_t *sock)
652 return &sock->address;
657 int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, lhnetaddress_t *address)
660 if (!lhnetsocket || !address || !content || maxcontentlength < 1)
662 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
665 lhnetpacket_t *p, *pnext;
666 // scan for any old packets to timeout while searching for a packet
667 // that is waiting to be delivered to this socket
668 currenttime = time(NULL);
669 for (p = lhnet_packetlist.next;p != &lhnet_packetlist;p = pnext)
672 if (p->timeout < currenttime)
675 p->next->prev = p->prev;
676 p->prev->next = p->next;
680 #ifndef STANDALONETEST
681 if (cl_netlocalping.value && (realtime - cl_netlocalping.value * (1.0 / 2000.0)) < p->sentdoubletime)
684 if (value == 0 && p->destinationport == lhnetsocket->address.addressdata.loop.port)
686 if (p->length <= maxcontentlength)
688 *address = lhnetsocket->address;
689 address->addressdata.loop.port = p->sourceport;
690 memcpy(content, p->data, p->length);
696 p->next->prev = p->prev;
697 p->prev->next = p->next;
702 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
704 unsigned int inetaddresslength;
705 address->addresstype = LHNETADDRESSTYPE_NONE;
706 inetaddresslength = sizeof(address->addressdata.inet4);
707 value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, (struct sockaddr *)&address->addressdata.inet4, &inetaddresslength);
710 address->addresstype = LHNETADDRESSTYPE_INET4;
713 else if (value == -1)
716 if (e == EWOULDBLOCK)
721 Con_Print("Connection refused\n");
724 Con_Printf("LHNET_Read: recvfrom returned error: %s\n", LHNETPRIVATE_StrError());
727 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
729 unsigned int inetaddresslength;
730 address->addresstype = LHNETADDRESSTYPE_NONE;
731 inetaddresslength = sizeof(address->addressdata.inet6);
732 value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, (struct sockaddr *)&address->addressdata.inet6, &inetaddresslength);
735 address->addresstype = LHNETADDRESSTYPE_INET6;
738 else if (value == -1)
741 if (e == EWOULDBLOCK)
746 Con_Print("Connection refused\n");
749 Con_Printf("LHNET_Read: recvfrom returned error: %s\n", LHNETPRIVATE_StrError());
755 int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentlength, const lhnetaddress_t *address)
758 if (!lhnetsocket || !address || !content || contentlength < 1)
760 if (lhnetsocket->address.addresstype != address->addresstype)
762 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
765 p = (lhnetpacket_t *)Z_Malloc(sizeof(*p) + contentlength);
766 p->data = (void *)(p + 1);
767 memcpy(p->data, content, contentlength);
768 p->length = contentlength;
769 p->sourceport = lhnetsocket->address.addressdata.loop.port;
770 p->destinationport = address->addressdata.loop.port;
771 p->timeout = time(NULL) + 10;
772 p->next = &lhnet_packetlist;
773 p->prev = p->next->prev;
776 #ifndef STANDALONETEST
777 p->sentdoubletime = realtime;
779 value = contentlength;
781 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
783 value = sendto(lhnetsocket->inetsocket, content, contentlength, 0, (struct sockaddr *)&address->addressdata.inet4, sizeof(address->addressdata.inet4));
786 if (SOCKETERRNO == EWOULDBLOCK)
788 Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError());
791 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
793 value = sendto(lhnetsocket->inetsocket, content, contentlength, 0, (struct sockaddr *)&address->addressdata.inet6, sizeof(address->addressdata.inet6));
796 if (SOCKETERRNO == EWOULDBLOCK)
798 Con_Printf("LHNET_Write: sendto returned error: %s\n", LHNETPRIVATE_StrError());
804 #ifdef STANDALONETEST
805 int main(int argc, char **argv)
808 char *buffer = "test", buffer2[1024];
809 int blen = strlen(buffer);
811 lhnetsocket_t *sock1;
812 lhnetsocket_t *sock2;
813 lhnetaddress_t myaddy1;
814 lhnetaddress_t myaddy2;
815 lhnetaddress_t myaddy3;
816 lhnetaddress_t localhostaddy1;
817 lhnetaddress_t localhostaddy2;
821 printf("calling LHNET_Init\n");
824 printf("calling LHNET_FromPort twice to create two local addresses\n");
825 LHNETADDRESS_FromPort(&myaddy1, LHNETADDRESSTYPE_INET4, 4000);
826 LHNETADDRESS_FromPort(&myaddy2, LHNETADDRESSTYPE_INET4, 4001);
827 LHNETADDRESS_FromString(&localhostaddy1, "127.0.0.1", 4000);
828 LHNETADDRESS_FromString(&localhostaddy2, "127.0.0.1", 4001);
830 printf("calling LHNET_OpenSocket_Connectionless twice to create two local sockets\n");
831 sock1 = LHNET_OpenSocket_Connectionless(&myaddy1);
832 sock2 = LHNET_OpenSocket_Connectionless(&myaddy2);
834 printf("calling LHNET_Write to send a packet from the first socket to the second socket\n");
835 test1 = LHNET_Write(sock1, buffer, blen, &localhostaddy2);
836 printf("sleeping briefly\n");
842 printf("calling LHNET_Read on the second socket to read the packet sent from the first socket\n");
843 test2 = LHNET_Read(sock2, buffer2, b2len - 1, &myaddy3);
845 Con_Printf("socket to socket test succeeded\n");
847 Con_Printf("socket to socket test failed\n");
850 printf("press any key to exit\n");
854 printf("calling LHNET_Shutdown\n");
859 lhnetsocket_t *sock[16], *sendsock;
868 int sendmessagelength;
869 lhnetaddress_t destaddress;
870 lhnetaddress_t receiveaddress;
871 lhnetaddress_t sockaddress[16];
872 char buffer[1536], addressstring[128], addressstring2[128];
873 if ((argc == 2 || argc == 5) && (port = atoi(argv[1])) >= 1 && port < 65535)
875 printf("calling LHNET_Init()\n");
879 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_LOOP, port);
880 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET4, port);
881 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET6, port+1);
885 sendmessagelength = 0;
887 for (i = 0;i < numsockets;i++)
889 LHNETADDRESS_ToString(&sockaddress[i], addressstring, sizeof(addressstring), 1);
890 printf("calling LHNET_OpenSocket_Connectionless(<%s>)\n", addressstring);
891 if ((sock[i] = LHNET_OpenSocket_Connectionless(&sockaddress[i])))
893 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
894 printf("opened socket successfully (address \"%s\")\n", addressstring2);
898 printf("failed to open socket\n");
909 count = atoi(argv[2]);
910 if (LHNETADDRESS_FromString(&destaddress, argv[3], -1))
912 sendmessage = argv[4];
913 sendmessagelength = strlen(sendmessage);
915 for (i = 0;i < numsockets;i++)
916 if (sock[i] && LHNETADDRESS_GetAddressType(&destaddress) == LHNETADDRESS_GetAddressType(&sockaddress[i]))
918 if (sendsock == NULL)
920 printf("Could not find an open socket matching the addresstype (%i) of destination address, switching to listen only mode\n", LHNETADDRESS_GetAddressType(&destaddress));
926 printf("LHNETADDRESS_FromString did not like the address \"%s\", switching to listen only mode\n", argv[3]);
930 printf("started, now listening for \"exit\" on the opened sockets\n");
931 oldtime = time(NULL);
939 for (i = 0;i < numsockets;i++)
943 length = LHNET_Read(sock[i], buffer, sizeof(buffer), &receiveaddress);
945 printf("localsock read error: length < 0");
946 else if (length > 0 && length < (int)sizeof(buffer))
949 LHNETADDRESS_ToString(&receiveaddress, addressstring, sizeof(addressstring), 1);
950 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
951 printf("received message \"%s\" from \"%s\" on socket \"%s\"\n", buffer, addressstring, addressstring2);
952 if (!strcmp(buffer, "exit"))
959 if (argc == 5 && count > 0)
961 newtime = time(NULL);
962 if (newtime != oldtime)
964 LHNETADDRESS_ToString(&destaddress, addressstring, sizeof(addressstring), 1);
965 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sendsock), addressstring2, sizeof(addressstring2), 1);
966 printf("calling LHNET_Write(<%s>, \"%s\", %i, <%s>)\n", addressstring2, sendmessage, sendmessagelength, addressstring);
967 length = LHNET_Write(sendsock, sendmessage, sendmessagelength, &destaddress);
968 if (length == sendmessagelength)
969 printf("sent successfully\n");
971 printf("LH_Write failed, returned %i (length of message was %i)\n", length, strlen(argv[4]));
975 printf("Done sending, still listening for \"exit\"\n");
979 for (i = 0;i < numsockets;i++)
983 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
984 printf("calling LHNET_CloseSocket(<%s>)\n", addressstring2);
985 LHNET_CloseSocket(sock[i]);
988 printf("calling LHNET_Shutdown()\n");
992 printf("Testing code for lhnet.c\nusage: lhnettest <localportnumber> [<sendnumberoftimes> <sendaddress:port> <sendmessage>]\n");