]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/l_net/l_net.c
set eol-style
[xonotic/netradiant.git] / libs / l_net / l_net.c
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 //====================================================================
23 //
24 // Name:                        l_net.c
25 // Function:            -
26 // Programmer:          MrElusive
27 // Last update:         -
28 // Tab size:            3
29 // Notes:
30 //====================================================================
31
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include "l_net.h"
37 #include "l_net_wins.h"
38
39 #define GetMemory malloc
40 #define FreeMemory free
41
42 #define qtrue   1
43 #define qfalse  0
44
45 #ifdef _DEBUG
46 void WinPrint(char *str, ...)
47 {
48         va_list argptr;
49   char text[4096];
50
51   va_start (argptr,str);
52         vsprintf (text, str, argptr);
53         va_end (argptr);
54
55   printf(text);
56 }
57 #else
58 void WinPrint(char *str, ...)
59 {
60 }
61 #endif
62
63 //===========================================================================
64 //
65 // Parameter:                           -
66 // Returns:                                     -
67 // Changes Globals:             -
68 //===========================================================================
69 void Net_SetAddressPort(address_t *address, int port)
70 {
71         sockaddr_t addr;
72
73         WINS_StringToAddr(address->ip, &addr);
74         WINS_SetSocketPort(&addr, port);
75         strcpy(address->ip, WINS_AddrToString(&addr));
76 } //end of the function Net_SetAddressPort
77 //===========================================================================
78 //
79 // Parameter:                           -
80 // Returns:                                     -
81 // Changes Globals:             -
82 //===========================================================================
83 int Net_AddressCompare(address_t *addr1, address_t *addr2)
84 {
85 #ifdef _WIN32
86         return stricmp(addr1->ip, addr2->ip);
87 #endif
88 #ifdef __linux__
89         return strcasecmp(addr1->ip, addr2->ip);
90 #endif
91 } //end of the function Net_AddressCompare
92 //===========================================================================
93 //
94 // Parameter:                           -
95 // Returns:                                     -
96 // Changes Globals:             -
97 //===========================================================================
98 void Net_SocketToAddress(socket_t *sock, address_t *address)
99 {
100         strcpy(address->ip, WINS_AddrToString(&sock->addr));
101 } //end of the function Net_SocketToAddress
102 //===========================================================================
103 //
104 // Parameter:                           -
105 // Returns:                                     -
106 // Changes Globals:             -
107 //===========================================================================
108 int Net_Send(socket_t *sock, netmessage_t *msg)
109 {
110         int size;
111
112         size = msg->size;
113         msg->size = 0;
114         NMSG_WriteLong(msg, size-4);
115         msg->size = size;
116         //WinPrint("Net_Send: message of size %d\n", sendmsg.size);
117         return WINS_Write(sock->socket, msg->data, msg->size, NULL);
118 } //end of the function Net_SendSocketReliable
119 //===========================================================================
120 // returns the number of bytes recieved
121 // -1 on error
122 //
123 // Parameter:                           -
124 // Returns:                                     -
125 // Changes Globals:             -
126 //===========================================================================
127 int Net_Receive(socket_t *sock, netmessage_t *msg)
128 {
129         int curread;
130
131         if (sock->remaining > 0)
132         {
133                 curread = WINS_Read(sock->socket, &sock->msg.data[sock->msg.size], sock->remaining, NULL);
134                 if (curread == -1)
135                 {
136                         WinPrint("Net_Receive: read error\n");
137                         return -1;
138                 } //end if
139                 sock->remaining -= curread;
140                 sock->msg.size += curread;
141                 if (sock->remaining <= 0)
142                 {
143                         sock->remaining = 0;
144                         memcpy(msg, &sock->msg, sizeof(netmessage_t));
145                         sock->msg.size = 0;
146                         return msg->size - 4;
147                 } //end if
148                 return 0;
149         } //end if
150         sock->msg.size = WINS_Read(sock->socket, sock->msg.data, 4, NULL);
151         if (sock->msg.size == 0) return 0;
152         if (sock->msg.size == -1)
153         {
154                 WinPrint("Net_Receive: size header read error\n");
155                 return -1;
156         } //end if
157         //WinPrint("Net_Receive: message size header %d\n", msg->size);
158         sock->msg.read = 0;
159         sock->remaining = NMSG_ReadLong(&sock->msg);
160         if (sock->remaining == 0) return 0;
161         if (sock->remaining < 0 || sock->remaining > MAX_NETMESSAGE)
162         {
163                 WinPrint("Net_Receive: invalid message size %d\n", sock->remaining);
164                 return -1;
165         } //end if
166         //try to read the message
167         curread = WINS_Read(sock->socket, &sock->msg.data[sock->msg.size], sock->remaining, NULL);
168         if (curread == -1)
169         {
170                 WinPrint("Net_Receive: read error\n");
171                 return -1;
172         } //end if
173         sock->remaining -= curread;
174         sock->msg.size += curread;
175         if (sock->remaining <= 0)
176         {
177                 sock->remaining = 0;
178                 memcpy(msg, &sock->msg, sizeof(netmessage_t));
179                 sock->msg.size = 0;
180                 return msg->size - 4;
181         } //end if
182         //the message has not been completely read yet
183 #ifdef _DEBUG
184   printf("++timo TODO: debug the Net_Receive on big size messages\n");
185 #endif
186         return 0;
187 } //end of the function Net_Receive
188 //===========================================================================
189 //
190 // Parameter:                           -
191 // Returns:                                     -
192 // Changes Globals:             -
193 //===========================================================================
194 socket_t *Net_AllocSocket(void)
195 {
196         socket_t *sock;
197
198         sock = (socket_t *) GetMemory(sizeof(socket_t));
199         memset(sock, 0, sizeof(socket_t));
200         return sock;
201 } //end of the function Net_AllocSocket
202 //===========================================================================
203 //
204 // Parameter:                           -
205 // Returns:                                     -
206 // Changes Globals:             -
207 //===========================================================================
208 void Net_FreeSocket(socket_t *sock)
209 {
210         FreeMemory(sock);
211 } //end of the function Net_FreeSocket
212 //===========================================================================
213 //
214 // Parameter:                           -
215 // Returns:                                     -
216 // Changes Globals:             -
217 //===========================================================================
218 socket_t *Net_Connect(address_t *address, int port)
219 {
220         int newsock;
221         socket_t *sock;
222         sockaddr_t sendaddr;
223
224         // see if we can resolve the host name
225         WINS_StringToAddr(address->ip, &sendaddr);
226
227   newsock = WINS_OpenReliableSocket(port);
228         if (newsock == -1) return NULL;
229
230         sock = Net_AllocSocket();
231         if (sock == NULL)
232         {
233                 WINS_CloseSocket(newsock);
234                 return NULL;
235         } //end if
236         sock->socket = newsock;
237
238         //connect to the host
239         if (WINS_Connect(newsock, &sendaddr) == -1)
240         {
241                 Net_FreeSocket(sock);
242                 WINS_CloseSocket(newsock);
243                 WinPrint("Net_Connect: error connecting\n");
244                 return NULL;
245         } //end if
246
247         memcpy(&sock->addr, &sendaddr, sizeof(sockaddr_t));
248         //now we can send messages
249         //
250         return sock;
251 } //end of the function Net_Connect
252
253 //===========================================================================
254 //
255 // Parameter:                           -
256 // Returns:                                     -
257 // Changes Globals:             -
258 //===========================================================================
259 socket_t *Net_ListenSocket(int port)
260 {
261         int newsock;
262         socket_t *sock;
263
264         newsock = WINS_OpenReliableSocket(port);
265         if (newsock == -1) return NULL;
266
267         if (WINS_Listen(newsock) == -1)
268         {
269                 WINS_CloseSocket(newsock);
270                 return NULL;
271         } //end if
272         sock = Net_AllocSocket();
273         if (sock == NULL)
274         {
275                 WINS_CloseSocket(newsock);
276                 return NULL;
277         } //end if
278         sock->socket = newsock;
279         WINS_GetSocketAddr(newsock, &sock->addr);
280         WinPrint("listen socket opened at %s\n", WINS_AddrToString(&sock->addr));
281         //
282         return sock;
283 } //end of the function Net_ListenSocket
284 //===========================================================================
285 //
286 // Parameter:                           -
287 // Returns:                                     -
288 // Changes Globals:             -
289 //===========================================================================
290 socket_t *Net_Accept(socket_t *sock)
291 {
292         int newsocket;
293         sockaddr_t sendaddr;
294         socket_t *newsock;
295
296         newsocket = WINS_Accept(sock->socket, &sendaddr);
297         if (newsocket == -1) return NULL;
298
299         newsock = Net_AllocSocket();
300         if (newsock == NULL)
301         {
302                 WINS_CloseSocket(newsocket);
303                 return NULL;
304         } //end if
305         newsock->socket = newsocket;
306         memcpy(&newsock->addr, &sendaddr, sizeof(sockaddr_t));
307         //
308         return newsock;
309 } //end of the function Net_Accept
310 //===========================================================================
311 //
312 // Parameter:                           -
313 // Returns:                                     -
314 // Changes Globals:             -
315 //===========================================================================
316 void Net_Disconnect(socket_t *sock)
317 {
318         WINS_CloseSocket(sock->socket);
319         Net_FreeSocket(sock);
320 } //end of the function Net_Disconnect
321 //===========================================================================
322 //
323 // Parameter:                           -
324 // Returns:                                     -
325 // Changes Globals:             -
326 //===========================================================================
327 void Net_StringToAddress(char *string, address_t *address)
328 {
329         strcpy(address->ip, string);
330 } //end of the function Net_StringToAddress
331 //===========================================================================
332 //
333 // Parameter:                           -
334 // Returns:                                     -
335 // Changes Globals:             -
336 //===========================================================================
337 void Net_MyAddress(address_t *address)
338 {
339         strcpy(address->ip, WINS_MyAddress());
340 } //end of the function Net_MyAddress
341 //===========================================================================
342 //
343 // Parameter:                           -
344 // Returns:                                     -
345 // Changes Globals:             -
346 //===========================================================================
347 int Net_Setup(void)
348 {
349         WINS_Init();
350         //
351         WinPrint("my address is %s\n", WINS_MyAddress());
352         //
353         return qtrue;
354 } //end of the function Net_Setup
355 //===========================================================================
356 //
357 // Parameter:                           -
358 // Returns:                                     -
359 // Changes Globals:             -
360 //===========================================================================
361 void Net_Shutdown(void)
362 {
363         WINS_Shutdown();
364 } //end of the function Net_Shutdown
365 //===========================================================================
366 //
367 // Parameter:                           -
368 // Returns:                                     -
369 // Changes Globals:             -
370 //===========================================================================
371 void NMSG_Clear(netmessage_t *msg)
372 {
373         msg->size = 4;
374 } //end of the function NMSG_Clear
375 //===========================================================================
376 //
377 // Parameter:                           -
378 // Returns:                                     -
379 // Changes Globals:             -
380 //===========================================================================
381 void NMSG_WriteChar (netmessage_t *msg, int c)
382 {
383         if (c < -128 || c > 127)
384                 WinPrint("NMSG_WriteChar: range error\n");
385
386         if (msg->size >= MAX_NETMESSAGE)
387         {
388                 WinPrint("NMSG_WriteChar: overflow\n");
389                 return;
390         } //end if
391         msg->data[msg->size] = c;
392         msg->size++;
393 } //end of the function NMSG_WriteChar
394 //===========================================================================
395 //
396 // Parameter:                           -
397 // Returns:                                     -
398 // Changes Globals:             -
399 //===========================================================================
400 void NMSG_WriteByte(netmessage_t *msg, int c)
401 {
402         if (c < -128 || c > 127)
403                 WinPrint("NMSG_WriteByte: range error\n");
404
405         if (msg->size + 1 >= MAX_NETMESSAGE)
406         {
407                 WinPrint("NMSG_WriteByte: overflow\n");
408                 return;
409         } //end if
410         msg->data[msg->size] = c;
411         msg->size++;
412 } //end of the function NMSG_WriteByte
413 //===========================================================================
414 //
415 // Parameter:                           -
416 // Returns:                                     -
417 // Changes Globals:             -
418 //===========================================================================
419 void NMSG_WriteShort(netmessage_t *msg, int c)
420 {
421         if (c < ((short)0x8000) || c > (short)0x7fff)
422                 WinPrint("NMSG_WriteShort: range error");
423
424         if (msg->size + 2 >= MAX_NETMESSAGE)
425         {
426                 WinPrint("NMSG_WriteShort: overflow\n");
427                 return;
428         } //end if
429         msg->data[msg->size] = c&0xff;
430         msg->data[msg->size+1] = c>>8;
431         msg->size += 2;
432 } //end of the function NMSG_WriteShort
433 //===========================================================================
434 //
435 // Parameter:                           -
436 // Returns:                                     -
437 // Changes Globals:             -
438 //===========================================================================
439 void NMSG_WriteLong(netmessage_t *msg, int c)
440 {
441         if (msg->size + 4 >= MAX_NETMESSAGE)
442         {
443                 WinPrint("NMSG_WriteLong: overflow\n");
444                 return;
445         } //end if
446         msg->data[msg->size] = c&0xff;
447         msg->data[msg->size+1] = (c>>8)&0xff;
448         msg->data[msg->size+2] = (c>>16)&0xff;
449         msg->data[msg->size+3] = c>>24;
450         msg->size += 4;
451 } //end of the function NMSG_WriteLong
452 //===========================================================================
453 //
454 // Parameter:                           -
455 // Returns:                                     -
456 // Changes Globals:             -
457 //===========================================================================
458 void NMSG_WriteFloat(netmessage_t *msg, float c)
459 {
460         if (msg->size + 4 >= MAX_NETMESSAGE)
461         {
462                 WinPrint("NMSG_WriteLong: overflow\n");
463                 return;
464         } //end if
465         msg->data[msg->size] = *((int *)&c)&0xff;
466         msg->data[msg->size+1] = (*((int *)&c)>>8)&0xff;
467         msg->data[msg->size+2] = (*((int *)&c)>>16)&0xff;
468         msg->data[msg->size+3] = *((int *)&c)>>24;
469         msg->size += 4;
470 } //end of the function NMSG_WriteFloat
471 //===========================================================================
472 //
473 // Parameter:                           -
474 // Returns:                                     -
475 // Changes Globals:             -
476 //===========================================================================
477 void NMSG_WriteString(netmessage_t *msg, char *string)
478 {
479         if (msg->size + strlen(string) + 1 >= MAX_NETMESSAGE)
480         {
481                 WinPrint("NMSG_WriteString: overflow\n");
482                 return;
483         } //end if
484         strcpy(&msg->data[msg->size], string);
485         msg->size += strlen(string) + 1;
486 } //end of the function NMSG_WriteString
487 //===========================================================================
488 //
489 // Parameter:                           -
490 // Returns:                                     -
491 // Changes Globals:             -
492 //===========================================================================
493 void NMSG_ReadStart(netmessage_t *msg)
494 {
495         msg->readoverflow = qfalse;
496         msg->read = 4;
497 } //end of the function NMSG_ReadStart
498 //===========================================================================
499 //
500 // Parameter:                           -
501 // Returns:                                     -
502 // Changes Globals:             -
503 //===========================================================================
504 int NMSG_ReadChar(netmessage_t *msg)
505 {
506         if (msg->size + 1 > msg->size)
507         {
508                 msg->readoverflow = qtrue;
509                 WinPrint("NMSG_ReadChar: read overflow\n");
510                 return 0;
511         } //end if
512         msg->read++;
513         return msg->data[msg->read-1];
514 } //end of the function NMSG_ReadChar
515 //===========================================================================
516 //
517 // Parameter:                           -
518 // Returns:                                     -
519 // Changes Globals:             -
520 //===========================================================================
521 int NMSG_ReadByte(netmessage_t *msg)
522 {
523         if (msg->read + 1 > msg->size)
524         {
525                 msg->readoverflow = qtrue;
526                 WinPrint("NMSG_ReadByte: read overflow\n");
527                 return 0;
528         } //end if
529         msg->read++;
530         return msg->data[msg->read-1];
531 } //end of the function NMSG_ReadByte
532 //===========================================================================
533 //
534 // Parameter:                           -
535 // Returns:                                     -
536 // Changes Globals:             -
537 //===========================================================================
538 int NMSG_ReadShort(netmessage_t *msg)
539 {
540         int c;
541
542         if (msg->read + 2 > msg->size)
543         {
544                 msg->readoverflow = qtrue;
545                 WinPrint("NMSG_ReadShort: read overflow\n");
546                 return 0;
547         } //end if
548         c = (short)(msg->data[msg->read] + (msg->data[msg->read+1]<<8));
549         msg->read += 2;
550         return c;
551 } //end of the function NMSG_ReadShort
552 //===========================================================================
553 //
554 // Parameter:                           -
555 // Returns:                                     -
556 // Changes Globals:             -
557 //===========================================================================
558 int NMSG_ReadLong(netmessage_t *msg)
559 {
560         int c;
561
562         if (msg->read + 4 > msg->size)
563         {
564                 msg->readoverflow = qtrue;
565                 WinPrint("NMSG_ReadLong: read overflow\n");
566                 return 0;
567         } //end if
568         c = msg->data[msg->read]
569                 + (msg->data[msg->read+1]<<8)
570                 + (msg->data[msg->read+2]<<16)
571                 + (msg->data[msg->read+3]<<24);
572         msg->read += 4;
573         return c;
574 } //end of the function NMSG_ReadLong
575 //===========================================================================
576 //
577 // Parameter:                           -
578 // Returns:                                     -
579 // Changes Globals:             -
580 //===========================================================================
581 float NMSG_ReadFloat(netmessage_t *msg)
582 {
583         int c;
584
585         if (msg->read + 4 > msg->size)
586         {
587                 msg->readoverflow = qtrue;
588                 WinPrint("NMSG_ReadLong: read overflow\n");
589                 return 0;
590         } //end if
591         c = msg->data[msg->read]
592                 + (msg->data[msg->read+1]<<8)
593                 + (msg->data[msg->read+2]<<16)
594                 + (msg->data[msg->read+3]<<24);
595         msg->read += 4;
596         return *(float *)&c;
597 } //end of the function NMSG_ReadFloat
598 //===========================================================================
599 //
600 // Parameter:                           -
601 // Returns:                                     -
602 // Changes Globals:             -
603 //===========================================================================
604 char *NMSG_ReadString(netmessage_t *msg)
605 {
606         static char     string[2048];
607         int l, c;
608         
609         l = 0;
610         do
611         {
612                 if (msg->read + 1 > msg->size)
613                 {
614                         msg->readoverflow = qtrue;
615                         WinPrint("NMSG_ReadString: read overflow\n");
616                         string[l] = 0;
617                         return string;
618                 } //end if
619                 c = msg->data[msg->read];
620                 msg->read++;
621                 if (c == 0) break;
622                 string[l] = c;
623                 l++;
624         } while (l < sizeof(string)-1);
625         string[l] = 0;
626         return string;
627 } //end of the function NMSG_ReadString