Forgot to include lhnet in the previous commit and tried to abort the commit but...
[divverent/darkplaces.git] / lhnet.c
1
2 // Written by Forest Hale 2003-06-15 and placed into public domain.
3
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <time.h>
7 #include <string.h>
8 #ifdef WIN32
9 #include <winsock.h>
10 #else
11 #include <netdb.h>
12 //#include <netinet/in.h>
13 //#include <arpa/inet.h>
14 #include <unistd.h>
15 #include <sys/socket.h>
16 #include <sys/ioctl.h>
17 #include <errno.h>
18 #endif
19
20 // for Z_Malloc/Z_Free in quake
21 #if 1
22 #include "zone.h"
23 #else
24 #define Z_Malloc malloc
25 #define Z_Free free
26 #endif
27
28 #include "lhnet.h"
29
30 int LHNETADDRESS_FromPort(lhnetaddress_t *address, int addresstype, int port)
31 {
32         switch(addresstype)
33         {
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;
39                 return 1;
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(port);
46                 return 1;
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(port);
53                 return 1;
54         }
55         return 0;
56 }
57
58 int LHNETADDRESS_FromString(lhnetaddress_t *address, const char *string, int defaultport)
59 {
60         int i, port, namelen, number;
61         struct hostent *hostentry;
62         const char *colon;
63         char name[128];
64         memset(address, 0, sizeof(*address));
65         address->addresstype = LHNETADDRESSTYPE_NONE;
66         port = 0;
67         colon = strrchr(string, ':');
68         if (colon)
69                 port = atoi(colon + 1);
70         else
71                 colon = string + strlen(string);
72         if (port == 0)
73                 port = defaultport;
74         namelen = colon - string;
75         if (namelen > 127)
76                 namelen = 127;
77         if (string[0] == '[' && namelen > 0 && string[namelen-1] == ']') // ipv6
78         {
79                 string++;
80                 namelen -= 2;
81         }
82         memcpy(name, string, namelen);
83         name[namelen] = 0;
84         // handle loopback
85         if (!strcmp(name, "local"))
86         {
87                 address->addresstype = LHNETADDRESSTYPE_LOOP;
88                 address->addressdata.loop.port = port;
89                 return 1;
90         }
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         for (i = 0;i < 3;i++)
94         {
95                 if (i == 0)
96                         hostentry = gethostbyaddr(name, namelen, LHNETADDRESSTYPE_INET6_FAMILY);
97                 else if (i == 1)
98                         hostentry = gethostbyaddr(name, namelen, LHNETADDRESSTYPE_INET4_FAMILY);
99                 else
100                         hostentry = gethostbyname(name);
101                 if (hostentry)
102                 {
103                         if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET6_FAMILY)
104                         {
105                                 // great it worked
106                                 address->addresstype = LHNETADDRESSTYPE_INET6;
107                                 address->addressdata.inet6.family = hostentry->h_addrtype;
108                                 address->addressdata.inet6.port = htons(port);
109                                 memcpy(address->addressdata.inet6.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet6.address));
110 #ifdef STANDALONETEST
111                                 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));
112 #endif
113                                 return 1;
114                         }
115                         else if (hostentry->h_addrtype == LHNETADDRESSTYPE_INET4_FAMILY)
116                         {
117                                 // great it worked
118                                 address->addresstype = LHNETADDRESSTYPE_INET4;
119                                 address->addressdata.inet4.family = hostentry->h_addrtype;
120                                 address->addressdata.inet4.port = htons(port);
121                                 memcpy(address->addressdata.inet4.address, hostentry->h_addr_list[0], sizeof(address->addressdata.inet4.address));
122 #ifdef STANDALONETEST
123                                 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));
124 #endif
125                                 return 1;
126                         }
127                 }
128         }
129         // failed, try to parse as an ipv4 address as a fallback (is this needed?)
130 #ifdef STANDALONETEST
131         printf("gethostbyname and gethostbyaddr failed on address \"%s\"\n", name);
132 #endif
133         for (i = 0, number = 0;i < 4;string++)
134         {
135                 if (*string >= '0' && *string <= '9')
136                         number = number * 10 + (*string - '0');
137                 else if (number < 256 && (*string == '.' || *string == ':'))
138                 {
139                         address->addressdata.inet4.address[i++] = number;
140                         number = 0;
141                 }
142                 else
143                         break;
144                 if (*string == 0 || *string == ':')
145                         break;
146         }
147         if (i == 4)
148         {
149                 // parsed a valid ipv4 address
150                 address->addresstype = LHNETADDRESSTYPE_INET4;
151                 address->addressdata.inet4.family = LHNETADDRESSTYPE_INET4_FAMILY;
152                 address->addressdata.inet4.port = htons(port);
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));
155 #endif
156                 return 1;
157         }
158         return 0;
159 }
160
161 int LHNETADDRESS_ToString(const lhnetaddress_t *address, char *string, int stringbuffersize, int includeport)
162 {
163         *string = 0;
164         switch(address->addresstype)
165         {
166         default:
167                 break;
168         case LHNETADDRESSTYPE_LOOP:
169                 if (includeport)
170                 {
171                         if (stringbuffersize >= 12)
172                         {
173                                 sprintf(string, "local:%d", (int)address->addressdata.loop.port);
174                                 return 1;
175                         }
176                 }
177                 else
178                 {
179                         if (stringbuffersize >= 6)
180                         {
181                                 strcpy(string, "local");
182                                 return 1;
183                         }
184                 }
185                 break;
186         case LHNETADDRESSTYPE_INET4:
187                 if (includeport)
188                 {
189                         if (stringbuffersize >= 22)
190                         {
191                                 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));
192                                 return 1;
193                         }
194                 }
195                 else
196                 {
197                         if (stringbuffersize >= 16)
198                         {
199                                 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]);
200                                 return 1;
201                         }
202                 }
203                 break;
204         case LHNETADDRESSTYPE_INET6:
205                 if (includeport)
206                 {
207                         if (stringbuffersize >= 88)
208                         {
209                                 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));
210                                 return 1;
211                         }
212                 }
213                 else
214                 {
215                         if (stringbuffersize >= 80)
216                         {
217                                 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                                 return 1;
219                         }
220                 }
221                 break;
222         }
223         return 0;
224 }
225
226 int LHNETADDRESS_GetAddressType(const lhnetaddress_t *address)
227 {
228         return address->addresstype;
229 }
230
231 int LHNETADDRESS_GetPort(const lhnetaddress_t *address)
232 {
233         switch(address->addresstype)
234         {
235         case LHNETADDRESSTYPE_LOOP:
236                 return address->addressdata.loop.port;
237         case LHNETADDRESSTYPE_INET4:
238                 return ntohs(address->addressdata.inet4.port);
239         case LHNETADDRESSTYPE_INET6:
240                 return ntohs(address->addressdata.inet6.port);
241         default:
242                 return -1;
243         }
244 }
245
246 int LHNETADDRESS_SetPort(lhnetaddress_t *address, int port)
247 {
248         switch(address->addresstype)
249         {
250         case LHNETADDRESSTYPE_LOOP:
251                 address->addressdata.loop.port = port;
252                 return 1;
253         case LHNETADDRESSTYPE_INET4:
254                 address->addressdata.inet4.port = htons(port);
255                 return 1;
256         case LHNETADDRESSTYPE_INET6:
257                 address->addressdata.inet6.port = htons(port);
258                 return 1;
259         default:
260                 return 0;
261         }
262 }
263
264 int LHNETADDRESS_Compare(const lhnetaddress_t *address1, const lhnetaddress_t *address2)
265 {
266         if (address1->addresstype != address2->addresstype)
267                 return 1;
268         switch(address1->addresstype)
269         {
270         case LHNETADDRESSTYPE_LOOP:
271                 if (address1->addressdata.loop.port != address2->addressdata.loop.port)
272                         return -1;
273                 return 0;
274         case LHNETADDRESSTYPE_INET4:
275                 if (address1->addressdata.inet4.family != address2->addressdata.inet4.family)
276                         return 1;
277                 if (memcmp(address1->addressdata.inet4.address, address2->addressdata.inet4.address, sizeof(address1->addressdata.inet4.address)))
278                         return 1;
279                 if (address1->addressdata.inet4.port != address2->addressdata.inet4.port)
280                         return -1;
281                 return 0;
282         case LHNETADDRESSTYPE_INET6:
283                 if (address1->addressdata.inet6.family != address2->addressdata.inet6.family)
284                         return 1;
285                 if (memcmp(address1->addressdata.inet6.address, address2->addressdata.inet6.address, sizeof(address1->addressdata.inet6.address)))
286                         return 1;
287                 if (address1->addressdata.inet6.port != address2->addressdata.inet6.port)
288                         return -1;
289                 return 0;
290         default:
291                 return 1;
292         }
293 }
294
295 typedef struct lhnetpacket_s
296 {
297         void *data;
298         int length;
299         int sourceport;
300         int destinationport;
301         time_t timeout;
302         struct lhnetpacket_s *next, *prev;
303 }
304 lhnetpacket_t;
305
306 static int lhnet_active;
307 static lhnetsocket_t lhnet_socketlist;
308 static lhnetpacket_t lhnet_packetlist;
309 #ifdef WIN32
310 static int lhnet_didWSAStartup = 0;
311 static WSADATA lhnet_winsockdata;
312 #endif
313
314 void LHNET_Init(void)
315 {
316         if (lhnet_active)
317                 return;
318         lhnet_socketlist.next = lhnet_socketlist.prev = &lhnet_socketlist;
319         lhnet_packetlist.next = lhnet_packetlist.prev = &lhnet_packetlist;
320         lhnet_active = 1;
321 }
322
323 void LHNET_Shutdown(void)
324 {
325         lhnetpacket_t *p;
326         if (!lhnet_active)
327                 return;
328         while (lhnet_socketlist.next != &lhnet_socketlist)
329                 LHNET_CloseSocket(lhnet_socketlist.next);
330         while (lhnet_packetlist.next != &lhnet_packetlist)
331         {
332                 p = lhnet_packetlist.next;
333                 p->prev->next = p->next;
334                 p->next->prev = p->prev;
335                 Z_Free(p);
336         }
337         lhnet_active = 0;
338 }
339
340 lhnetsocket_t *LHNET_OpenSocket_Connectionless(lhnetaddress_t *address)
341 {
342         lhnetsocket_t *lhnetsocket, *s;
343         lhnetsocket = Z_Malloc(sizeof(*lhnetsocket));
344         if (lhnetsocket)
345         {
346                 memset(lhnetsocket, 0, sizeof(*lhnetsocket));
347                 lhnetsocket->address = *address;
348                 switch(lhnetsocket->address.addresstype)
349                 {
350                 case LHNETADDRESSTYPE_LOOP:
351                         if (lhnetsocket->address.addressdata.loop.port == 0)
352                         {
353                                 // allocate a port dynamically
354                                 // this search will always terminate because there is never
355                                 // an allocated socket with port 0, so if the number wraps it
356                                 // will find the port is unused, and then refuse to use port
357                                 // 0, causing an intentional failure condition
358                                 lhnetsocket->address.addressdata.loop.port = 1024;
359                                 for (;;)
360                                 {
361                                         for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
362                                                 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.addressdata.loop.port == lhnetsocket->address.addressdata.loop.port)
363                                                         break;
364                                         if (s == &lhnet_socketlist)
365                                                 break;
366                                         lhnetsocket->address.addressdata.loop.port++;
367                                 }
368                         }
369                         // check if the port is available
370                         for (s = lhnet_socketlist.next;s != &lhnet_socketlist;s = s->next)
371                                 if (s->address.addresstype == lhnetsocket->address.addresstype && s->address.addressdata.loop.port == lhnetsocket->address.addressdata.loop.port)
372                                         break;
373                         if (s == &lhnet_socketlist && lhnetsocket->address.addressdata.loop.port != 0)
374                         {
375                                 lhnetsocket->next = &lhnet_socketlist;
376                                 lhnetsocket->prev = lhnetsocket->next->prev;
377                                 lhnetsocket->next->prev = lhnetsocket;
378                                 lhnetsocket->prev->next = lhnetsocket;
379                                 return lhnetsocket;
380                         }
381                         break;
382                 case LHNETADDRESSTYPE_INET4:
383                 case LHNETADDRESSTYPE_INET6:
384 #ifdef WIN32
385                         if (!lhnet_didWSAStartup && !WSAStartup(MAKEWORD(1, 1), &lhnet_winsockdata))
386                         {
387                                 lhnet_didWSAStartup = 1;
388 #else
389                         {
390 #endif
391                                 if (address->addresstype == LHNETADDRESSTYPE_INET6)
392                                         lhnetsocket->inetsocket = socket(LHNETADDRESSTYPE_INET6_FAMILY, SOCK_DGRAM, IPPROTO_UDP);
393                                 else
394                                         lhnetsocket->inetsocket = socket(LHNETADDRESSTYPE_INET4_FAMILY, SOCK_DGRAM, IPPROTO_UDP);
395                                 if (lhnetsocket->inetsocket != -1)
396                                 {
397 #ifdef WIN32
398                                         u_long _true = 1;
399                                         if (ioctlsocket(lhnetsocket->inetsocket, FIONBIO, &_true) != -1)
400 #else
401                                         char _true = 1;
402                                         if (ioctl(lhnetsocket->inetsocket, FIONBIO, &_true) != -1)
403 #endif
404                                         {
405                                                 if (bind(lhnetsocket->inetsocket, (void *)&lhnetsocket->address.addressdata, address->addresstype == LHNETADDRESSTYPE_INET6 ? sizeof(lhnetsocket->address.addressdata.inet6) : sizeof(lhnetsocket->address.addressdata.inet4)) != -1)
406                                                 {
407                                                         lhnetsocket->next = &lhnet_socketlist;
408                                                         lhnetsocket->prev = lhnetsocket->next->prev;
409                                                         lhnetsocket->next->prev = lhnetsocket;
410                                                         lhnetsocket->prev->next = lhnetsocket;
411                                                         return lhnetsocket;
412                                                 }
413                                         }
414 #ifdef WIN32
415                                         closesocket(lhnetsocket->inetsocket);
416 #else
417                                         close(lhnetsocket->inetsocket);
418 #endif
419                                 }
420                         }
421                         break;
422                 default:
423                         break;
424                 }
425                 Z_Free(lhnetsocket);
426         }
427         return NULL;
428 }
429
430 void LHNET_CloseSocket(lhnetsocket_t *lhnetsocket)
431 {
432         if (lhnetsocket)
433         {
434                 // unlink from socket list
435                 if (lhnetsocket->next == NULL)
436                         return; // invalid!
437                 lhnetsocket->next->prev = lhnetsocket->prev;
438                 lhnetsocket->prev->next = lhnetsocket->next;
439                 lhnetsocket->next = NULL;
440                 lhnetsocket->prev = NULL;
441
442                 // no special close code for loopback, just inet
443                 if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4 || lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
444                 {
445 #ifdef WIN32
446                         closesocket(lhnetsocket->inetsocket);
447 #else
448                         close(lhnetsocket->inetsocket);
449 #endif
450                 }
451 #ifdef WIN32
452                 if (lhnet_socketlist.next == &lhnet_socketlist && lhnet_didWSAStartup)
453                 {
454                         lhnet_didWSAStartup = 0;
455                         WSACleanup();
456                 }
457 #endif
458                 Z_Free(lhnetsocket);
459         }
460 }
461
462 lhnetaddress_t *LHNET_AddressFromSocket(lhnetsocket_t *sock)
463 {
464         return &sock->address;
465 }
466
467 int LHNET_Read(lhnetsocket_t *lhnetsocket, void *content, int maxcontentlength, lhnetaddress_t *address)
468 {
469         int value = 0;
470         if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
471         {
472                 time_t currenttime;
473                 lhnetpacket_t *p, *pnext;
474                 // scan for any old packets to timeout while searching for a packet
475                 // that is waiting to be delivered to this socket
476                 currenttime = time(NULL);
477                 for (p = lhnet_packetlist.next;p != &lhnet_packetlist;p = pnext)
478                 {
479                         pnext = p->next;
480                         if (value == 0 && p->destinationport == lhnetsocket->address.addressdata.loop.port)
481                         {
482                                 if (p->length <= maxcontentlength)
483                                 {
484                                         *address = lhnetsocket->address;
485                                         address->addressdata.loop.port = p->sourceport;
486                                         memcpy(content, p->data, p->length);
487                                         value = p->length;
488                                 }
489                                 else
490                                         value = -1;
491                                 // unlink and free
492                                 p->next->prev = p->prev;
493                                 p->prev->next = p->next;
494                                 Z_Free(p);
495                         }
496                         else if (p->timeout < currenttime)
497                         {
498                                 // unlink and free
499                                 p->next->prev = p->prev;
500                                 p->prev->next = p->next;
501                                 Z_Free(p);
502                         }
503                 }
504         }
505         else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
506         {
507                 int inetaddresslength;
508                 address->addresstype = LHNETADDRESSTYPE_NONE;
509                 inetaddresslength = sizeof(address->addressdata.inet4);
510                 value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, (struct sockaddr *)&address->addressdata.inet4, &inetaddresslength);
511                 if (value > 0)
512                 {
513                         address->addresstype = LHNETADDRESSTYPE_INET4;
514                         return value;
515                 }
516                 else if (value == -1)
517                 {
518 #ifdef WIN32
519                         int e = WSAGetLastError();
520                         if (e == WSAEWOULDBLOCK || e == WSAECONNREFUSED)
521                                 return 0;
522 #else
523                         if (errno == EWOULDBLOCK || errno == ECONNREFUSED)
524                                 return 0;
525 #endif
526                 }
527         }
528         else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
529         {
530                 int inetaddresslength;
531                 address->addresstype = LHNETADDRESSTYPE_NONE;
532                 inetaddresslength = sizeof(address->addressdata.inet6);
533                 value = recvfrom(lhnetsocket->inetsocket, content, maxcontentlength, 0, (struct sockaddr *)&address->addressdata.inet6, &inetaddresslength);
534                 if (value > 0)
535                 {
536                         address->addresstype = LHNETADDRESSTYPE_INET6;
537                         return value;
538                 }
539                 else if (value == -1)
540                 {
541 #ifdef WIN32
542                         int e = WSAGetLastError();
543                         if (e == WSAEWOULDBLOCK || e == WSAECONNREFUSED)
544                                 return 0;
545 #else
546                         if (errno == EWOULDBLOCK || errno == ECONNREFUSED)
547                                 return 0;
548 #endif
549                 }
550         }
551         return value;
552 }
553
554 int LHNET_Write(lhnetsocket_t *lhnetsocket, const void *content, int contentlength, const lhnetaddress_t *address)
555 {
556         int value = -1;
557         if (lhnetsocket->address.addresstype != address->addresstype)
558                 return -1;
559         if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_LOOP)
560         {
561                 lhnetpacket_t *p;
562                 p = Z_Malloc(sizeof(*p) + contentlength);
563                 p->data = (void *)(p + 1);
564                 memcpy(p->data, content, contentlength);
565                 p->length = contentlength;
566                 p->sourceport = lhnetsocket->address.addressdata.loop.port;
567                 p->destinationport = address->addressdata.loop.port;
568                 p->timeout = time(NULL) + 10;
569                 p->next = &lhnet_packetlist;
570                 p->prev = p->next->prev;
571                 p->next->prev = p;
572                 p->prev->next = p;
573                 value = contentlength;
574         }
575         else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET4)
576         {
577                 value = sendto(lhnetsocket->inetsocket, content, contentlength, 0, (struct sockaddr *)&address->addressdata.inet4, sizeof(address->addressdata.inet4));
578                 if (value == -1)
579                 {
580 #ifdef WIN32
581                         int e = WSAGetLastError();
582                         if (e == WSAEWOULDBLOCK)
583                                 return 0;
584 #else
585                         if (errno == EWOULDBLOCK)
586                                 return 0;
587 #endif
588                 }
589         }
590         else if (lhnetsocket->address.addresstype == LHNETADDRESSTYPE_INET6)
591         {
592                 value = sendto(lhnetsocket->inetsocket, content, contentlength, 0, (struct sockaddr *)&address->addressdata.inet6, sizeof(address->addressdata.inet6));
593                 if (value == -1)
594                 {
595 #ifdef WIN32
596                         int e = WSAGetLastError();
597                         if (e == WSAEWOULDBLOCK)
598                                 return 0;
599 #else
600                         if (errno == EWOULDBLOCK)
601                                 return 0;
602 #endif
603                 }
604         }
605         return value;
606 }
607
608 #ifdef STANDALONETEST
609 int main(int argc, char **argv)
610 {
611         lhnetsocket_t *sock[16], *sendsock;
612         int i;
613         int numsockets;
614         int count;
615         int length;
616         int port;
617         time_t oldtime;
618         time_t newtime;
619         char *sendmessage;
620         int sendmessagelength;
621         lhnetaddress_t destaddress;
622         lhnetaddress_t receiveaddress;
623         lhnetaddress_t sockaddress[16];
624         char buffer[1536], addressstring[128], addressstring2[128];
625         if ((argc == 2 || argc == 5) && (port = atoi(argv[1])) >= 1 && port < 65535)
626         {
627                 printf("calling LHNET_Init()\n");
628                 LHNET_Init();
629
630                 numsockets = 0;
631                 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_LOOP, port);
632                 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET4, port);
633                 LHNETADDRESS_FromPort(&sockaddress[numsockets++], LHNETADDRESSTYPE_INET6, port+1);
634
635                 sendsock = NULL;
636                 sendmessage = NULL;
637                 sendmessagelength = 0;
638
639                 for (i = 0;i < numsockets;i++)
640                 {
641                         LHNETADDRESS_ToString(&sockaddress[i], addressstring, sizeof(addressstring), 1);
642                         printf("calling LHNET_OpenSocket_Connectionless(<%s>)\n", addressstring);
643                         if ((sock[i] = LHNET_OpenSocket_Connectionless(&sockaddress[i])))
644                         {
645                                 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
646                                 printf("opened socket successfully (address \"%s\")\n", addressstring2);
647                         }
648                         else
649                         {
650                                 printf("failed to open socket\n");
651                                 if (i == 0)
652                                 {
653                                         LHNET_Shutdown();
654                                         return -1;
655                                 }
656                         }
657                 }
658                 count = 0;
659                 if (argc == 5)
660                 {
661                         count = atoi(argv[2]);
662                         if (LHNETADDRESS_FromString(&destaddress, argv[3], -1))
663                         {
664                                 sendmessage = argv[4];
665                                 sendmessagelength = strlen(sendmessage);
666                                 sendsock = NULL;
667                                 for (i = 0;i < numsockets;i++)
668                                         if (sock[i] && LHNETADDRESS_GetAddressType(&destaddress) == LHNETADDRESS_GetAddressType(&sockaddress[i]))
669                                                 sendsock = sock[i];
670                                 if (sendsock == NULL)
671                                 {
672                                         printf("Could not find an open socket matching the addresstype (%i) of destination address, switching to listen only mode\n", LHNETADDRESS_GetAddressType(&destaddress));
673                                         argc = 2;
674                                 }
675                         }
676                         else
677                         {
678                                 printf("LHNETADDRESS_FromString did not like the address \"%s\", switching to listen only mode\n", argv[3]);
679                                 argc = 2;
680                         }
681                 }
682                 printf("started, now listening for \"exit\" on the opened sockets\n");
683                 oldtime = time(NULL);
684                 for(;;)
685                 {
686 #ifdef WIN32
687                         Sleep(1);
688 #else
689                         usleep(1);
690 #endif
691                         for (i = 0;i < numsockets;i++)
692                         {
693                                 if (sock[i])
694                                 {
695                                         length = LHNET_Read(sock[i], buffer, sizeof(buffer), &receiveaddress);
696                                         if (length < 0)
697                                                 printf("localsock read error: length < 0");
698                                         else if (length > 0 && length < (int)sizeof(buffer))
699                                         {
700                                                 buffer[length] = 0;
701                                                 LHNETADDRESS_ToString(&receiveaddress, addressstring, sizeof(addressstring), 1);
702                                                 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
703                                                 printf("received message \"%s\" from \"%s\" on socket \"%s\"\n", buffer, addressstring, addressstring2);
704                                                 if (!strcmp(buffer, "exit"))
705                                                         break;
706                                         }
707                                 }
708                         }
709                         if (i < numsockets)
710                                 break;
711                         if (argc == 5 && count > 0)
712                         {
713                                 newtime = time(NULL);
714                                 if (newtime != oldtime)
715                                 {
716                                         LHNETADDRESS_ToString(&destaddress, addressstring, sizeof(addressstring), 1);
717                                         LHNETADDRESS_ToString(LHNET_AddressFromSocket(sendsock), addressstring2, sizeof(addressstring2), 1);
718                                         printf("calling LHNET_Write(<%s>, \"%s\", %i, <%s>)\n", addressstring2, sendmessage, sendmessagelength, addressstring);
719                                         length = LHNET_Write(sendsock, sendmessage, sendmessagelength, &destaddress);
720                                         if (length == sendmessagelength)
721                                                 printf("sent successfully\n");
722                                         else
723                                                 printf("LH_Write failed, returned %i (length of message was %i)\n", length, strlen(argv[4]));
724                                         oldtime = newtime;
725                                         count--;
726                                         if (count <= 0)
727                                                 printf("Done sending, still listening for \"exit\"\n");
728                                 }
729                         }
730                 }
731                 for (i = 0;i < numsockets;i++)
732                 {
733                         if (sock[i])
734                         {
735                                 LHNETADDRESS_ToString(LHNET_AddressFromSocket(sock[i]), addressstring2, sizeof(addressstring2), 1);
736                                 printf("calling LHNET_CloseSocket(<%s>)\n", addressstring2);
737                                 LHNET_CloseSocket(sock[i]);
738                         }
739                 }
740                 printf("calling LHNET_Shutdown()\n");
741                 LHNET_Shutdown();
742                 return 0;
743         }
744         printf("Testing code for lhnet.c\nusage: lhnettest <localportnumber> [<sendnumberoftimes> <sendaddress:port> <sendmessage>]\n");
745         return -1;
746 }
747 #endif
748