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