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 Z_Malloc malloc
33 int LHNETADDRESS_FromPort(lhnetaddress_t *address, int addresstype, int port)
39 case LHNETADDRESSTYPE_LOOP:
40 // local:port (loopback)
41 memset(address, 0, sizeof(*address));
42 address->addresstype = LHNETADDRESSTYPE_LOOP;
43 address->addressdata.loop.port = port;
45 case LHNETADDRESSTYPE_INET4:
46 // 0.0.0.0:port (INADDR_ANY, binds to all interfaces)
47 memset(address, 0, sizeof(*address));
48 address->addresstype = LHNETADDRESSTYPE_INET4;
49 address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY;
50 address->addressdata.inet4.port = htons((unsigned short)port);
52 case LHNETADDRESSTYPE_INET6:
53 // [0:0:0:0:0:0:0:0]:port (IN6ADDR_ANY, binds to all interfaces)
54 memset(address, 0, sizeof(*address));
55 address->addresstype = LHNETADDRESSTYPE_INET6;
56 address->addressdata.inet6.family = LHNETADDRESSTYPE_INET6_FAMILY;
57 address->addressdata.inet6.port = htons((unsigned short)port);
63 int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int defaultport)
65 int port, namelen, d1, d2, d3, d4;
66 struct hostent *hostentry;
69 if (!address || !string)
71 memset(address, 0, sizeof(*address));
72 address->addresstype = LHNETADDRESSTYPE_NONE;
74 colon = strrchr(string, ':');
76 port = atoi(colon + 1);
78 colon = string + strlen(string);
81 namelen = colon - string;
84 if (string[0] == '[' && namelen > 0 && string[namelen-1] == ']') // ipv6
89 memcpy(name, string, namelen);
92 if (!strcmp(name, "local"))
94 address->addresstype = LHNETADDRESSTYPE_LOOP;
95 address->addressdata.loop.port = port;
98 // try to parse as dotted decimal ipv4 address first
99 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)
101 // parsed a valid ipv4 address
102 address->addresstype = LHNETADDRESSTYPE_INET4;
103 address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY;
104 address->addressdata.inet4.port = htons((unsigned short)port);
105 address->addressdata.inet4.address[0] = (unsigned char)d1;
106 address->addressdata.inet4.address[1] = (unsigned char)d2;
107 address->addressdata.inet4.address[2] = (unsigned char)d3;
108 address->addressdata.inet4.address[3] = (unsigned char)d4;
109 #ifdef STANDALONETEST
110 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));
114 // try gethostbyname (handles dns and other ip formats)
115 hostentry = gethostbyname(name);
118 if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET6_FAMILY)
121 address->addresstype = LHNETADDRESSTYPE_INET6;
122 address->addressdata.inet6.family = hostentry->h_addrtype;
123 address->addressdata.inet6.port = htons((unsigned short)port);
124 memcpy(address->addressdata.inet6.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet6.address));
125 #ifdef STANDALONETEST
126 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));
130 else if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET4_FAMILY)
133 address->addresstype = LHNETADDRESSTYPE_INET4;
134 address->addressdata.inet4.family = hostentry->h_addrtype;
135 address->addressdata.inet4.port = htons((unsigned short)port);
136 memcpy(address->addressdata.inet4.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet4.address));
137 #ifdef STANDALONETEST
138 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));
143 #ifdef STANDALONETEST
144 printf("gethostbyname failed on address \"%s\"\n", name);
149 int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int stringbuffersize, int includeport)
152 if (!address || !string || stringbuffersize < 1)
154 switch(address->addresstype)
158 case LHNETADDRESSTYPE_LOOP:
161 if (stringbuffersize >= 12)
163 sprintf(string, "local:%d", (int)address->addressdata.loop.port);
169 if (stringbuffersize >= 6)
171 strcpy(string, "local");
176 case LHNETADDRESSTYPE_INET4:
179 if (stringbuffersize >= 22)
181 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));
187 if (stringbuffersize >= 16)
189 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]);
194 case LHNETADDRESSTYPE_INET6:
197 if (stringbuffersize >= 88)
199 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));
205 if (stringbuffersize >= 80)
207 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]);
216 int LHNETADDRESS_GetAddressType(const lhnetaddress_t *address)
219 return address->addresstype;
221 return LHNETADDRESSTYPE_NONE;
224 int LHNETADDRESS_GetPort(const lhnetaddress_t *address)
228 switch(address->addresstype)
230 case LHNETADDRESSTYPE_LOOP:
231 return address->addressdata.loop.port;
232 case LHNETADDRESSTYPE_INET4:
233 return ntohs(address->addressdata.inet4.port);
234 case LHNETADDRESSTYPE_INET6:
235 return ntohs(address->addressdata.inet6.port);
241 int LHNETADDRESS_SetPort(lhnetaddress_t *address, int port)
245 switch(address->addresstype)
247 case LHNETADDRESSTYPE_LOOP:
248 address->addressdata.loop.port = port;
250 case LHNETADDRESSTYPE_INET4:
251 address->addressdata.inet4.port = htons((unsigned short)port);
253 case LHNETADDRESSTYPE_INET6:
254 address->addressdata.inet6.port = htons((unsigned short)port);
261 int LHNETADDRESS_Compare(const lhnetaddress_t *address1, const lhnetaddress_t *address2)
263 if (!address1 || !address2)
265 if (address1->addresstype != address2->addresstype)
267 switch(address1->addresstype)
269 case LHNETADDRESSTYPE_LOOP:
270 if (address1->addressdata.loop.port != address2->addressdata.loop.port)
273 case LHNETADDRESSTYPE_INET4:
274 if (address1->addressdata.inet4.family != address2->addressdata.inet4.family)
276 if (memcmp(address1->addressdata.inet4.address, address2->addressdata.inet4.address, sizeof(address1->addressdata.inet4.address)))
278 if (address1->addressdata.inet4.port != address2->addressdata.inet4.port)
281 case LHNETADDRESSTYPE_INET6:
282 if (address1->addressdata.inet6.family != address2->addressdata.inet6.family)
284 if (memcmp(address1->addressdata.inet6.address, address2->addressdata.inet6.address, sizeof(address1->addressdata.inet6.address)))
286 if (address1->addressdata.inet6.port != address2->addressdata.inet6.port)
294 typedef struct lhnetpacket_s
301 #ifndef STANDALONETEST
302 double sentdoubletime;
304 struct lhnetpacket_s *next, *prev;
308 static int lhnet_active;
309 static lhnetsocket_t lhnet_socketlist;
310 static lhnetpacket_t lhnet_packetlist;
312 static int lhnet_didWSAStartup = 0;
313 static WSADATA lhnet_winsockdata;
316 void LHNET_Init(void)
320 lhnet_socketlist.next = lhnet_socketlist.prev = &lhnet_socketlist;
321 lhnet_packetlist.next = lhnet_packetlist.prev = &lhnet_packetlist;
325 void LHNET_Shutdown(void)
330 while (lhnet_socketlist.next != &lhnet_socketlist)
331 LHNET_CloseSocket(lhnet_socketlist.next);
332 while (lhnet_packetlist.next != &lhnet_packetlist)
334 p = lhnet_packetlist.next;
335 p->prev->next = p->next;
336 p->next->prev = p->prev;
342 static const char *LHNETPRIVATE_StrError(void)
345 int i = WSAGetLastError();
348 case WSAEINTR: return "WSAEINTR";
349 case WSAEBADF: return "WSAEBADF";
350 case WSAEACCES: return "WSAEACCES";
351 case WSAEFAULT: return "WSAEFAULT";
352 case WSAEINVAL: return "WSAEINVAL";
353 case WSAEMFILE: return "WSAEMFILE";
354 case WSAEWOULDBLOCK: return "WSAEWOULDBLOCK";
355 case WSAEINPROGRESS: return "WSAEINPROGRESS";
356 case WSAEALREADY: return "WSAEALREADY";
357 case WSAENOTSOCK: return "WSAENOTSOCK";
358 case WSAEDESTADDRREQ: return "WSAEDESTADDRREQ";
359 case WSAEMSGSIZE: return "WSAEMSGSIZE";
360 case WSAEPROTOTYPE: return "WSAEPROTOTYPE";
361 case WSAENOPROTOOPT: return "WSAENOPROTOOPT";
362 case WSAEPROTONOSUPPORT: return "WSAEPROTONOSUPPORT";
363 case WSAESOCKTNOSUPPORT: return "WSAESOCKTNOSUPPORT";
364 case WSAEOPNOTSUPP: return "WSAEOPNOTSUPP";
365 case WSAEPFNOSUPPORT: return "WSAEPFNOSUPPORT";
366 case WSAEAFNOSUPPORT: return "WSAEAFNOSUPPORT";
367 case WSAEADDRINUSE: return "WSAEADDRINUSE";
368 case WSAEADDRNOTAVAIL: return "WSAEADDRNOTAVAIL";
369 case WSAENETDOWN: return "WSAENETDOWN";
370 case WSAENETUNREACH: return "WSAENETUNREACH";
371 case WSAENETRESET: return "WSAENETRESET";
372 case WSAECONNABORTED: return "WSAECONNABORTED";
373 case WSAECONNRESET: return "WSAECONNRESET";
374 case WSAENOBUFS: return "WSAENOBUFS";
375 case WSAEISCONN: return "WSAEISCONN";
376 case WSAENOTCONN: return "WSAENOTCONN";
377 case WSAESHUTDOWN: return "WSAESHUTDOWN";
378 case WSAETOOMANYREFS: return "WSAETOOMANYREFS";
379 case WSAETIMEDOUT: return "WSAETIMEDOUT";
380 case WSAECONNREFUSED: return "WSAECONNREFUSED";
381 case WSAELOOP: return "WSAELOOP";
382 case WSAENAMETOOLONG: return "WSAENAMETOOLONG";
383 case WSAEHOSTDOWN: return "WSAEHOSTDOWN";
384 case WSAEHOSTUNREACH: return "WSAEHOSTUNREACH";
385 case WSAENOTEMPTY: return "WSAENOTEMPTY";
386 case WSAEPROCLIM: return "WSAEPROCLIM";
387 case WSAEUSERS: return "WSAEUSERS";
388 case WSAEDQUOT: return "WSAEDQUOT";
389 case WSAESTALE: return "WSAESTALE";
390 case WSAEREMOTE: return "WSAEREMOTE";
391 case WSAEDISCON: return "WSAEDISCON";
392 case 0: return "no error";
393 default: return "unknown WSAE error";
396 return strerror(errno);
400 lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
402 lhnetsocket_t *lhnetsocket, *s;
405 lhnetsocket = Z_Malloc(sizeof(*lhnetsocket));
408 memset(lhnetsocket, 0, sizeof(*lhnetsocket));
409 lhnetsocket->address = *address;
410 switch(lhnetsocket->address.addresstype)
412 case LHNETADDRESSTYPE_LOOP:
413 if (lhnetsocket->address.addressdata.loop.port == 0)
415 // allocate a port dynamically
416 // this search will always terminate because there is never
417 // an allocated socket with port 0, so if the number wraps it
418 // will find the port is unused, and then refuse to use port
419 // 0, causing an intentional failure condition
420 lhnetsocket->address.addressdata.loop.port = 1024;
423 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
424 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.addressdata.loop.port == lhnetsocket->address.addressdata.loop.port)
426 if (s == &lhnet_socketlist)
428 lhnetsocket->address.addressdata.loop.port++;
431 // check if the port is available
432 for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
433 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.addressdata.loop.port == lhnetsocket->address.addressdata.loop.port)
435 if (s == &lhnet_socketlist && lhnetsocket->address.addressdata.loop.port != 0)
437 lhnetsocket->next = &lhnet_socketlist;
438 lhnetsocket->prev = lhnetsocket->next->prev;
439 lhnetsocket->next->prev = lhnetsocket;
440 lhnetsocket->prev->next = lhnetsocket;
444 case LHNETADDRESSTYPE_INET4:
445 case LHNETADDRESSTYPE_INET6:
447 if (lhnet_didWSAStartup || (lhnet_didWSAStartup = !WSAStartup(MAKEWORD(1, 1), &lhnet_winsockdata)))
450 if ((lhnetsocket->inetsocket = socket(address->addresstype == LHNETADDRESSTYPE_INET6 ? LHNETADDRESSTYPE_INET6_FAMILY : LHNETADDRESSTYPE_INET4_FAMILY, SOCK_DGRAM, IPPROTO_UDP)) != -1)
454 if (ioctlsocket(lhnetsocket->inetsocket, FIONBIO, &_true) != -1)
457 if (ioctl(lhnetsocket->inetsocket, FIONBIO, &_true) != -1)
460 size_t namelen = address->addresstype == LHNETADDRESSTYPE_INET6 ? sizeof(lhnetsocket->address.addressdata.inet6) : sizeof(lhnetsocket->address.addressdata.inet4);
461 if (bind(lhnetsocket->inetsocket, (void *)&lhnetsocket->address.addressdata, namelen) != -1)
463 getsockname(lhnetsocket->inetsocket, (void *)&lhnetsocket->address.addressdata, &namelen);
464 lhnetsocket->next = &lhnet_socketlist;
465 lhnetsocket->prev = lhnetsocket->next->prev;
466 lhnetsocket->next->prev = lhnetsocket;
467 lhnetsocket->prev->next = lhnetsocket;
471 Con_Printf("LHNET_OpenSocket_Connectionless: bind returned error: %s\n", LHNETPRIVATE_StrError());
474 Con_Printf("LHNET_OpenSocket_Connectionless: ioctlsocket returned error: %s\n", LHNETPRIVATE_StrError());
476 closesocket(lhnetsocket->inetsocket);
478 close(lhnetsocket->inetsocket);
482 Con_Printf("LHNET_OpenSocket_Connectionless: socket returned error: %s\n", LHNETPRIVATE_StrError());
486 Con_Print("LHNET_OpenSocket_Connectionless: WSAStartup failed\n");
497 void LHNET_CloseSocket(lhnetsocket_t *lhnetsocket)
501 // unlink from socket list
502 if (lhnetsocket->next == NULL)
504 lhnetsocket->next->prev = lhnetsocket->prev;
505 lhnetsocket->prev->next = lhnetsocket->next;
506 lhnetsocket->next = NULL;
507 lhnetsocket->prev = NULL;
509 // no special close code for loopback, just inet
510 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4 || lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
513 closesocket(lhnetsocket->inetsocket);
515 close(lhnetsocket->inetsocket);
519 if (lhnet_socketlist.next == &lhnet_socketlist && lhnet_didWSAStartup)
521 lhnet_didWSAStartup = 0;
529 lhnetaddress_t *LHNET_AddressFromSocket(lhnetsocket_t *sock)
532 return &sock->address;
537 int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, lhnetaddress_t *address)
540 if (!lhnetsocket || !address || !content || maxcontentlength < 1)
542 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
545 lhnetpacket_t *p, *pnext;
546 // scan for any old packets to timeout while searching for a packet
547 // that is waiting to be delivered to this socket
548 currenttime = time(NULL);
549 for (p = lhnet_packetlist.next;p != &lhnet_packetlist;p = pnext)
552 if (p->timeout < currenttime)
555 p->next->prev = p->prev;
556 p->prev->next = p->next;
560 #ifndef STANDALONETEST
561 if (p->sentdoubletime && Sys_DoubleTime() < p->sentdoubletime)
564 if (value == 0 && p->destinationport == lhnetsocket->address.addressdata.loop.port)
566 if (p->length <= maxcontentlength)
568 *address = lhnetsocket->address;
569 address->addressdata.loop.port = p->sourceport;
570 memcpy(content, p->data, p->length);
576 p->next->prev = p->prev;
577 p->prev->next = p->next;
582 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
584 int inetaddresslength;
585 address->addresstype = LHNETADDRESSTYPE_NONE;
586 inetaddresslength = sizeof(address->addressdata.inet4);
587 value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, (struct sockaddr *)&address->addressdata.inet4, &inetaddresslength);
590 address->addresstype = LHNETADDRESSTYPE_INET4;
593 else if (value == -1)
596 int e = WSAGetLastError();
597 if (e == WSAEWOULDBLOCK)
601 case WSAECONNREFUSED:
602 Con_Print("Connection refused\n");
606 if (errno == EWOULDBLOCK)
611 Con_Print("Connection refused\n");
617 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
619 int inetaddresslength;
620 address->addresstype = LHNETADDRESSTYPE_NONE;
621 inetaddresslength = sizeof(address->addressdata.inet6);
622 value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, (struct sockaddr *)&address->addressdata.inet6, &inetaddresslength);
625 address->addresstype = LHNETADDRESSTYPE_INET6;
628 else if (value == -1)
631 int e = WSAGetLastError();
632 if (e == WSAEWOULDBLOCK)
636 case WSAECONNREFUSED:
637 Con_Print("Connection refused\n");
641 if (errno == EWOULDBLOCK)
646 Con_Print("Connection refused\n");
655 int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentlength, const lhnetaddress_t *address)
658 if (!lhnetsocket || !address || !content || contentlength < 1)
660 if (lhnetsocket->address.addresstype != address->addresstype)
662 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
665 p = Z_Malloc(sizeof(*p) + contentlength);
666 p->data = (void *)(p + 1);
667 memcpy(p->data, content, contentlength);
668 p->length = contentlength;
669 p->sourceport = lhnetsocket->address.addressdata.loop.port;
670 p->destinationport = address->addressdata.loop.port;
671 p->timeout = time(NULL) + 10;
672 p->next = &lhnet_packetlist;
673 p->prev = p->next->prev;
676 #ifndef STANDALONETEST
677 if (cl_fakelocalping_min.value || cl_fakelocalping_max.value)
678 p->sentdoubletime = Sys_DoubleTime() + lhrandom(cl_fakelocalping_min.value, cl_fakelocalping_max.value) * (0.5 / 1000.0);
680 value = contentlength;
682 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
684 value = sendto(lhnetsocket->inetsocket, content, contentlength, 0, (struct sockaddr *)&address->addressdata.inet4, sizeof(address->addressdata.inet4));
688 int e = WSAGetLastError();
689 if (e == WSAEWOULDBLOCK)
692 if (errno == EWOULDBLOCK)
697 else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
699 value = sendto(lhnetsocket->inetsocket, content, contentlength, 0, (struct sockaddr *)&address->addressdata.inet6, sizeof(address->addressdata.inet6));
703 int e = WSAGetLastError();
704 if (e == WSAEWOULDBLOCK)
707 if (errno == EWOULDBLOCK)
715 #ifdef STANDALONETEST
716 int main(int argc, char **argv)
718 lhnetsocket_t *sock[16], *sendsock;
727 int sendmessagelength;
728 lhnetaddress_t destaddress;
729 lhnetaddress_t receiveaddress;
730 lhnetaddress_t sockaddress[16];
731 char buffer[1536], addressstring[128], addressstring2[128];
732 if ((argc == 2 || argc == 5) && (port = atoi(argv[1])) >= 1 && port < 65535)
734 printf("calling LHNET_Init()\n");
738 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_LOOP, port);
739 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET4, port);
740 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET6, port+1);
744 sendmessagelength = 0;
746 for (i = 0;i < numsockets;i++)
748 LHNETADDRESS_ToString(&sockaddress[i], addressstring, sizeof(addressstring), 1);
749 printf("calling LHNET_OpenSocket_Connectionless(<%s>)\n", addressstring);
750 if ((sock[i] = LHNET_OpenSocket_Connectionless(&sockaddress[i])))
752 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
753 printf("opened socket successfully (address \"%s\")\n", addressstring2);
757 printf("failed to open socket\n");
768 count = atoi(argv[2]);
769 if (LHNETADDRESS_FromString(&destaddress, argv[3], -1))
771 sendmessage = argv[4];
772 sendmessagelength = strlen(sendmessage);
774 for (i = 0;i < numsockets;i++)
775 if (sock[i] && LHNETADDRESS_GetAddressType(&destaddress) == LHNETADDRESS_GetAddressType(&sockaddress[i]))
777 if (sendsock == NULL)
779 printf("Could not find an open socket matching the addresstype (%i) of destination address, switching to listen only mode\n", LHNETADDRESS_GetAddressType(&destaddress));
785 printf("LHNETADDRESS_FromString did not like the address \"%s\", switching to listen only mode\n", argv[3]);
789 printf("started, now listening for \"exit\" on the opened sockets\n");
790 oldtime = time(NULL);
798 for (i = 0;i < numsockets;i++)
802 length = LHNET_Read(sock[i], buffer, sizeof(buffer), &receiveaddress);
804 printf("localsock read error: length < 0");
805 else if (length > 0 && length < (int)sizeof(buffer))
808 LHNETADDRESS_ToString(&receiveaddress, addressstring, sizeof(addressstring), 1);
809 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
810 printf("received message \"%s\" from \"%s\" on socket \"%s\"\n", buffer, addressstring, addressstring2);
811 if (!strcmp(buffer, "exit"))
818 if (argc == 5 && count > 0)
820 newtime = time(NULL);
821 if (newtime != oldtime)
823 LHNETADDRESS_ToString(&destaddress, addressstring, sizeof(addressstring), 1);
824 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sendsock), addressstring2, sizeof(addressstring2), 1);
825 printf("calling LHNET_Write(<%s>, \"%s\", %i, <%s>)\n", addressstring2, sendmessage, sendmessagelength, addressstring);
826 length = LHNET_Write(sendsock, sendmessage, sendmessagelength, &destaddress);
827 if (length == sendmessagelength)
828 printf("sent successfully\n");
830 printf("LH_Write failed, returned %i (length of message was %i)\n", length, strlen(argv[4]));
834 printf("Done sending, still listening for \"exit\"\n");
838 for (i = 0;i < numsockets;i++)
842 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
843 printf("calling LHNET_CloseSocket(<%s>)\n", addressstring2);
844 LHNET_CloseSocket(sock[i]);
847 printf("calling LHNET_Shutdown()\n");
851 printf("Testing code for lhnet.c\nusage: lhnettest <localportnumber> [<sendnumberoftimes> <sendaddress:port> <sendmessage>]\n");