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