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
27 #define Con_Print printf
28 #define Con_Printf printf
29 #define Z_Malloc malloc
35 int LHNETADDRESS_FromPort(lhnetaddress_t *address, int addresstype, int port)
41 case LHNETADDRESSTYPE_LOOP:
42 // local:port (loopback)
43 memset(address, 0, sizeof(*address));
44 address->addresstype = LHNETADDRESSTYPE_LOOP;
45 address->addressdata.loop.port = port;
47 case LHNETADDRESSTYPE_INET4:
48 // 0.0.0.0:port (INADDR_ANY, binds to all interfaces)
49 memset(address, 0, sizeof(*address));
50 address->addresstype = LHNETADDRESSTYPE_INET4;
51 address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY;
52 address->addressdata.inet4.port = htons((unsigned short)port);
54 case LHNETADDRESSTYPE_INET6:
55 // [0:0:0:0:0:0:0:0]:port (IN6ADDR_ANY, binds to all interfaces)
56 memset(address, 0, sizeof(*address));
57 address->addresstype = LHNETADDRESSTYPE_INET6;
58 address->addressdata.inet6.family = LHNETADDRESSTYPE_INET6_FAMILY;
59 address->addressdata.inet6.port = htons((unsigned short)port);
65 int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int defaultport)
67 int port, namelen, d1, d2, d3, d4;
68 struct hostent *hostentry;
71 if (!address || !string)
73 memset(address, 0, sizeof(*address));
74 address->addresstype = LHNETADDRESSTYPE_NONE;
76 colon = strrchr(string, ':');
78 port = atoi(colon + 1);
80 colon = string + strlen(string);
83 namelen = colon - string;
86 if (string[0] == '[' && namelen > 0 && string[namelen-1] == ']') // ipv6
91 memcpy(name, string, namelen);
94 if (!strcmp(name, "local"))
96 address->addresstype = LHNETADDRESSTYPE_LOOP;
97 address->addressdata.loop.port = port;
100 // try to parse as dotted decimal ipv4 address first
101 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)
103 // parsed a valid ipv4 address
104 address->addresstype = LHNETADDRESSTYPE_INET4;
105 address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY;
106 address->addressdata.inet4.port = htons((unsigned short)port);
107 address->addressdata.inet4.address[0] = (unsigned char)d1;
108 address->addressdata.inet4.address[1] = (unsigned char)d2;
109 address->addressdata.inet4.address[2] = (unsigned char)d3;
110 address->addressdata.inet4.address[3] = (unsigned char)d4;
111 #ifdef STANDALONETEST
112 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));
116 // try gethostbyname (handles dns and other ip formats)
117 hostentry = gethostbyname(name);
120 if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET6_FAMILY)
123 address->addresstype = LHNETADDRESSTYPE_INET6;
124 address->addressdata.inet6.family = hostentry->h_addrtype;
125 address->addressdata.inet6.port = htons((unsigned short)port);
126 memcpy(address->addressdata.inet6.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet6.address));
127 #ifdef STANDALONETEST
128 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));
132 else if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET4_FAMILY)
135 address->addresstype = LHNETADDRESSTYPE_INET4;
136 address->addressdata.inet4.family = hostentry->h_addrtype;
137 address->addressdata.inet4.port = htons((unsigned short)port);
138 memcpy(address->addressdata.inet4.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet4.address));
139 #ifdef STANDALONETEST
140 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));
145 #ifdef STANDALONETEST
146 printf("gethostbyname failed on address \"%s\"\n", name);
151 int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int stringbuffersize, int includeport)
154 if (!address || !string || stringbuffersize < 1)
156 switch(address->addresstype)
160 case LHNETADDRESSTYPE_LOOP:
163 if (stringbuffersize >= 12)
165 sprintf(string, "local:%d", (int)address->addressdata.loop.port);
171 if (stringbuffersize >= 6)
173 strcpy(string, "local");
178 case LHNETADDRESSTYPE_INET4:
181 if (stringbuffersize >= 22)
183 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));
189 if (stringbuffersize >= 16)
191 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]);
196 case LHNETADDRESSTYPE_INET6:
199 if (stringbuffersize >= 88)
201 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));
207 if (stringbuffersize >= 80)
209 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]);
218 int LHNETADDRESS_GetAddressType(const lhnetaddress_t *address)
221 return address->addresstype;
223 return LHNETADDRESSTYPE_NONE;
226 int LHNETADDRESS_GetPort(const lhnetaddress_t *address)
230 switch(address->addresstype)
232 case LHNETADDRESSTYPE_LOOP:
233 return address->addressdata.loop.port;
234 case LHNETADDRESSTYPE_INET4:
235 return ntohs(address->addressdata.inet4.port);
236 case LHNETADDRESSTYPE_INET6:
237 return ntohs(address->addressdata.inet6.port);
243 int LHNETADDRESS_SetPort(lhnetaddress_t *address, int port)
247 switch(address->addresstype)
249 case LHNETADDRESSTYPE_LOOP:
250 address->addressdata.loop.port = port;
252 case LHNETADDRESSTYPE_INET4:
253 address->addressdata.inet4.port = htons((unsigned short)port);
255 case LHNETADDRESSTYPE_INET6:
256 address->addressdata.inet6.port = htons((unsigned short)port);
263 int LHNETADDRESS_Compare(const lhnetaddress_t *address1, const lhnetaddress_t *address2)
265 if (!address1 || !address2)
267 if (address1->addresstype != address2->addresstype)
269 switch(address1->addresstype)
271 case LHNETADDRESSTYPE_LOOP:
272 if (address1->addressdata.loop.port != address2->addressdata.loop.port)
275 case LHNETADDRESSTYPE_INET4:
276 if (address1->addressdata.inet4.family != address2->addressdata.inet4.family)
278 if (memcmp(address1->addressdata.inet4.address, address2->addressdata.inet4.address, sizeof(address1->addressdata.inet4.address)))
280 if (address1->addressdata.inet4.port != address2->addressdata.inet4.port)
283 case LHNETADDRESSTYPE_INET6:
284 if (address1->addressdata.inet6.family != address2->addressdata.inet6.family)
286 if (memcmp(address1->addressdata.inet6.address, address2->addressdata.inet6.address, sizeof(address1->addressdata.inet6.address)))
288 if (address1->addressdata.inet6.port != address2->addressdata.inet6.port)
296 typedef struct lhnetpacket_s
303 #ifndef STANDALONETEST
304 double sentdoubletime;
306 struct lhnetpacket_s *next, *prev;
310 static int lhnet_active;
311 static lhnetsocket_t lhnet_socketlist;
312 static lhnetpacket_t lhnet_packetlist;
314 static int lhnet_didWSAStartup = 0;
315 static WSADATA lhnet_winsockdata;
318 void LHNET_Init(void)
322 lhnet_socketlist.next = lhnet_socketlist.prev = &lhnet_socketlist;
323 lhnet_packetlist.next = lhnet_packetlist.prev = &lhnet_packetlist;
327 void LHNET_Shutdown(void)
332 while (lhnet_socketlist.next != &lhnet_socketlist)
333 LHNET_CloseSocket(lhnet_socketlist.next);
334 while (lhnet_packetlist.next != &lhnet_packetlist)
336 p = lhnet_packetlist.next;
337 p->prev->next = p->next;
338 p->next->prev = p->prev;
344 static const char *LHNETPRIVATE_StrError(void)
347 int i = WSAGetLastError();
350 case WSAEINTR: return "WSAEINTR";
351 case WSAEBADF: return "WSAEBADF";
352 case WSAEACCES: return "WSAEACCES";
353 case WSAEFAULT: return "WSAEFAULT";
354 case WSAEINVAL: return "WSAEINVAL";
355 case WSAEMFILE: return "WSAEMFILE";
356 case WSAEWOULDBLOCK: return "WSAEWOULDBLOCK";
357 case WSAEINPROGRESS: return "WSAEINPROGRESS";
358 case WSAEALREADY: return "WSAEALREADY";
359 case WSAENOTSOCK: return "WSAENOTSOCK";
360 case WSAEDESTADDRREQ: return "WSAEDESTADDRREQ";
361 case WSAEMSGSIZE: return "WSAEMSGSIZE";
362 case WSAEPROTOTYPE: return "WSAEPROTOTYPE";
363 case WSAENOPROTOOPT: return "WSAENOPROTOOPT";
364 case WSAEPROTONOSUPPORT: return "WSAEPROTONOSUPPORT";
365 case WSAESOCKTNOSUPPORT: return "WSAESOCKTNOSUPPORT";
366 case WSAEOPNOTSUPP: return "WSAEOPNOTSUPP";
367 case WSAEPFNOSUPPORT: return "WSAEPFNOSUPPORT";
368 case WSAEAFNOSUPPORT: return "WSAEAFNOSUPPORT";
369 case WSAEADDRINUSE: return "WSAEADDRINUSE";
370 case WSAEADDRNOTAVAIL: return "WSAEADDRNOTAVAIL";
371 case WSAENETDOWN: return "WSAENETDOWN";
372 case WSAENETUNREACH: return "WSAENETUNREACH";
373 case WSAENETRESET: return "WSAENETRESET";
374 case WSAECONNABORTED: return "WSAECONNABORTED";
375 case WSAECONNRESET: return "WSAECONNRESET";
376 case WSAENOBUFS: return "WSAENOBUFS";
377 case WSAEISCONN: return "WSAEISCONN";
378 case WSAENOTCONN: return "WSAENOTCONN";
379 case WSAESHUTDOWN: return "WSAESHUTDOWN";
380 case WSAETOOMANYREFS: return "WSAETOOMANYREFS";
381 case WSAETIMEDOUT: return "WSAETIMEDOUT";
382 case WSAECONNREFUSED: return "WSAECONNREFUSED";
383 case WSAELOOP: return "WSAELOOP";
384 case WSAENAMETOOLONG: return "WSAENAMETOOLONG";
385 case WSAEHOSTDOWN: return "WSAEHOSTDOWN";
386 case WSAEHOSTUNREACH: return "WSAEHOSTUNREACH";
387 case WSAENOTEMPTY: return "WSAENOTEMPTY";
388 case WSAEPROCLIM: return "WSAEPROCLIM";
389 case WSAEUSERS: return "WSAEUSERS";
390 case WSAEDQUOT: return "WSAEDQUOT";
391 case WSAESTALE: return "WSAESTALE";
392 case WSAEREMOTE: return "WSAEREMOTE";
393 case WSAEDISCON: return "WSAEDISCON";
394 case 0: return "no error";
395 default: return "unknown WSAE error";
398 return strerror(errno);
402 lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
404 lhnetsocket_t *lhnetsocket, *s;
407 lhnetsocket = Z_Malloc(sizeof(*lhnetsocket));
410 memset(lhnetsocket, 0, sizeof(*lhnetsocket));
411 lhnetsocket->address = *address;
412 switch(lhnetsocket->address.addresstype)
414 case LHNETADDRESSTYPE_LOOP:
415 if (lhnetsocket->address.addressdata.loop.port == 0)
417 // allocate a port dynamically
418 // this search will always terminate because there is never
419 // an allocated socket with port 0, so if the number wraps it
420 // will find the port is unused, and then refuse to use port
421 // 0, causing an intentional failure condition
422 lhnetsocket->address.addressdata.loop.port = 1024;
425 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
426 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.addressdata.loop.port == lhnetsocket->address.addressdata.loop.port)
428 if (s == &lhnet_socketlist)
430 lhnetsocket->address.addressdata.loop.port++;
433 // check if the port is available
434 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
435 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.addressdata.loop.port == lhnetsocket->address.addressdata.loop.port)
437 if (s == &lhnet_socketlist && lhnetsocket->address.addressdata.loop.port != 0)
439 lhnetsocket->next = &lhnet_socketlist;
440 lhnetsocket->prev = lhnetsocket->next->prev;
441 lhnetsocket->next->prev = lhnetsocket;
442 lhnetsocket->prev->next = lhnetsocket;
446 case LHNETADDRESSTYPE_INET4:
447 case LHNETADDRESSTYPE_INET6:
449 if (lhnet_didWSAStartup || (lhnet_didWSAStartup = !WSAStartup(MAKEWORD(1, 1), &lhnet_winsockdata)))
452 if ((lhnetsocket->inetsocket = socket(address->addresstype == LHNETADDRESSTYPE_INET6 ? LHNETADDRESSTYPE_INET6_FAMILY : LHNETADDRESSTYPE_INET4_FAMILY, SOCK_DGRAM, IPPROTO_UDP)) != -1)
456 if (ioctlsocket(lhnetsocket->inetsocket, FIONBIO, &_true) != -1)
459 if (ioctl(lhnetsocket->inetsocket, FIONBIO, &_true) != -1)
467 namelen = address->addresstype == LHNETADDRESSTYPE_INET6 ? sizeof(lhnetsocket->address.addressdata.inet6) : sizeof(lhnetsocket->address.addressdata.inet4);
468 if (bind(lhnetsocket->inetsocket, (void *)&lhnetsocket->address.addressdata, namelen) != -1)
470 getsockname(lhnetsocket->inetsocket, (void *)&lhnetsocket->address.addressdata, &namelen);
471 lhnetsocket->next = &lhnet_socketlist;
472 lhnetsocket->prev = lhnetsocket->next->prev;
473 lhnetsocket->next->prev = lhnetsocket;
474 lhnetsocket->prev->next = lhnetsocket;
478 Con_Printf("LHNET_OpenSocket_Connectionless: bind returned error: %s\n", LHNETPRIVATE_StrError());
481 Con_Printf("LHNET_OpenSocket_Connectionless: ioctlsocket returned error: %s\n", LHNETPRIVATE_StrError());
483 closesocket(lhnetsocket->inetsocket);
485 close(lhnetsocket->inetsocket);
489 Con_Printf("LHNET_OpenSocket_Connectionless: socket returned error: %s\n", LHNETPRIVATE_StrError());
493 Con_Print("LHNET_OpenSocket_Connectionless: WSAStartup failed\n");
504 void LHNET_CloseSocket(lhnetsocket_t *lhnetsocket)
508 // unlink from socket list
509 if (lhnetsocket->next == NULL)
511 lhnetsocket->next->prev = lhnetsocket->prev;
512 lhnetsocket->prev->next = lhnetsocket->next;
513 lhnetsocket->next = NULL;
514 lhnetsocket->prev = NULL;
516 // no special close code for loopback, just inet
517 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4 || lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
520 closesocket(lhnetsocket->inetsocket);
522 close(lhnetsocket->inetsocket);
526 if (lhnet_socketlist.next == &lhnet_socketlist && lhnet_didWSAStartup)
528 lhnet_didWSAStartup = 0;
536 lhnetaddress_t *LHNET_AddressFromSocket(lhnetsocket_t *sock)
539 return &sock->address;
544 int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, lhnetaddress_t *address)
547 if (!lhnetsocket || !address || !content || maxcontentlength < 1)
549 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
552 lhnetpacket_t *p, *pnext;
553 // scan for any old packets to timeout while searching for a packet
554 // that is waiting to be delivered to this socket
555 currenttime = time(NULL);
556 for (p = lhnet_packetlist.next;p != &lhnet_packetlist;p = pnext)
559 if (p->timeout < currenttime)
562 p->next->prev = p->prev;
563 p->prev->next = p->next;
567 #ifndef STANDALONETEST
568 if (p->sentdoubletime && Sys_DoubleTime() < p->sentdoubletime)
571 if (value == 0 && p->destinationport == lhnetsocket->address.addressdata.loop.port)
573 if (p->length <= maxcontentlength)
575 *address = lhnetsocket->address;
576 address->addressdata.loop.port = p->sourceport;
577 memcpy(content, p->data, p->length);
583 p->next->prev = p->prev;
584 p->prev->next = p->next;
589 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
591 int inetaddresslength;
592 address->addresstype = LHNETADDRESSTYPE_NONE;
593 inetaddresslength = sizeof(address->addressdata.inet4);
594 value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, (struct sockaddr *)&address->addressdata.inet4, &inetaddresslength);
597 address->addresstype = LHNETADDRESSTYPE_INET4;
600 else if (value == -1)
603 int e = WSAGetLastError();
604 if (e == WSAEWOULDBLOCK)
608 case WSAECONNREFUSED:
609 Con_Print("Connection refused\n");
613 if (errno == EWOULDBLOCK)
618 Con_Print("Connection refused\n");
624 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
626 int inetaddresslength;
627 address->addresstype = LHNETADDRESSTYPE_NONE;
628 inetaddresslength = sizeof(address->addressdata.inet6);
629 value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, (struct sockaddr *)&address->addressdata.inet6, &inetaddresslength);
632 address->addresstype = LHNETADDRESSTYPE_INET6;
635 else if (value == -1)
638 int e = WSAGetLastError();
639 if (e == WSAEWOULDBLOCK)
643 case WSAECONNREFUSED:
644 Con_Print("Connection refused\n");
648 if (errno == EWOULDBLOCK)
653 Con_Print("Connection refused\n");
662 int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentlength, const lhnetaddress_t *address)
665 if (!lhnetsocket || !address || !content || contentlength < 1)
667 if (lhnetsocket->address.addresstype != address->addresstype)
669 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
672 p = Z_Malloc(sizeof(*p) + contentlength);
673 p->data = (void *)(p + 1);
674 memcpy(p->data, content, contentlength);
675 p->length = contentlength;
676 p->sourceport = lhnetsocket->address.addressdata.loop.port;
677 p->destinationport = address->addressdata.loop.port;
678 p->timeout = time(NULL) + 10;
679 p->next = &lhnet_packetlist;
680 p->prev = p->next->prev;
683 #ifndef STANDALONETEST
684 if (cl_netlocalping_min.value || cl_netlocalping_max.value)
685 p->sentdoubletime = Sys_DoubleTime() + lhrandom(cl_netlocalping_min.value, cl_netlocalping_max.value) * (0.5 / 1000.0);
687 value = contentlength;
689 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
691 value = sendto(lhnetsocket->inetsocket, content, contentlength, 0, (struct sockaddr *)&address->addressdata.inet4, sizeof(address->addressdata.inet4));
695 int e = WSAGetLastError();
696 if (e == WSAEWOULDBLOCK)
699 if (errno == EWOULDBLOCK)
704 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
706 value = sendto(lhnetsocket->inetsocket, content, contentlength, 0, (struct sockaddr *)&address->addressdata.inet6, sizeof(address->addressdata.inet6));
710 int e = WSAGetLastError();
711 if (e == WSAEWOULDBLOCK)
714 if (errno == EWOULDBLOCK)
722 #ifdef STANDALONETEST
723 int main(int argc, char **argv)
725 lhnetsocket_t *sock[16], *sendsock;
734 int sendmessagelength;
735 lhnetaddress_t destaddress;
736 lhnetaddress_t receiveaddress;
737 lhnetaddress_t sockaddress[16];
738 char buffer[1536], addressstring[128], addressstring2[128];
739 if ((argc == 2 || argc == 5) && (port = atoi(argv[1])) >= 1 && port < 65535)
741 printf("calling LHNET_Init()\n");
745 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_LOOP, port);
746 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET4, port);
747 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET6, port+1);
751 sendmessagelength = 0;
753 for (i = 0;i < numsockets;i++)
755 LHNETADDRESS_ToString(&sockaddress[i], addressstring, sizeof(addressstring), 1);
756 printf("calling LHNET_OpenSocket_Connectionless(<%s>)\n", addressstring);
757 if ((sock[i] = LHNET_OpenSocket_Connectionless(&sockaddress[i])))
759 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
760 printf("opened socket successfully (address \"%s\")\n", addressstring2);
764 printf("failed to open socket\n");
775 count = atoi(argv[2]);
776 if (LHNETADDRESS_FromString(&destaddress, argv[3], -1))
778 sendmessage = argv[4];
779 sendmessagelength = strlen(sendmessage);
781 for (i = 0;i < numsockets;i++)
782 if (sock[i] && LHNETADDRESS_GetAddressType(&destaddress) == LHNETADDRESS_GetAddressType(&sockaddress[i]))
784 if (sendsock == NULL)
786 printf("Could not find an open socket matching the addresstype (%i) of destination address, switching to listen only mode\n", LHNETADDRESS_GetAddressType(&destaddress));
792 printf("LHNETADDRESS_FromString did not like the address \"%s\", switching to listen only mode\n", argv[3]);
796 printf("started, now listening for \"exit\" on the opened sockets\n");
797 oldtime = time(NULL);
805 for (i = 0;i < numsockets;i++)
809 length = LHNET_Read(sock[i], buffer, sizeof(buffer), &receiveaddress);
811 printf("localsock read error: length < 0");
812 else if (length > 0 && length < (int)sizeof(buffer))
815 LHNETADDRESS_ToString(&receiveaddress, addressstring, sizeof(addressstring), 1);
816 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
817 printf("received message \"%s\" from \"%s\" on socket \"%s\"\n", buffer, addressstring, addressstring2);
818 if (!strcmp(buffer, "exit"))
825 if (argc == 5 && count > 0)
827 newtime = time(NULL);
828 if (newtime != oldtime)
830 LHNETADDRESS_ToString(&destaddress, addressstring, sizeof(addressstring), 1);
831 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sendsock), addressstring2, sizeof(addressstring2), 1);
832 printf("calling LHNET_Write(<%s>, \"%s\", %i, <%s>)\n", addressstring2, sendmessage, sendmessagelength, addressstring);
833 length = LHNET_Write(sendsock, sendmessage, sendmessagelength, &destaddress);
834 if (length == sendmessagelength)
835 printf("sent successfully\n");
837 printf("LH_Write failed, returned %i (length of message was %i)\n", length, strlen(argv[4]));
841 printf("Done sending, still listening for \"exit\"\n");
845 for (i = 0;i < numsockets;i++)
849 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
850 printf("calling LHNET_CloseSocket(<%s>)\n", addressstring2);
851 LHNET_CloseSocket(sock[i]);
854 printf("calling LHNET_Shutdown()\n");
858 printf("Testing code for lhnet.c\nusage: lhnettest <localportnumber> [<sendnumberoftimes> <sendaddress:port> <sendmessage>]\n");