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