2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
22 //===========================================================================
26 // Programmer: MrElusive
27 // Last update: TTimo: cross-platform version, l_net library
30 //===========================================================================
32 //#include <windows.h>
38 #include "l_net_wins.h"
40 #include <arpa/inet.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/ioctl.h>
44 #include <netinet/in.h>
45 #include <netinet/tcp.h>
48 #define SOCKET_ERROR -1
49 #define INVALID_SOCKET -1
51 extern void WinPrint(char *str, ...);
53 #define WinError WinPrint
58 #define ioctlsocket ioctl
59 #define closesocket close
67 typedef struct tag_error_struct
74 typedef struct tag_error_struct
80 #define NET_NAMELEN 64
82 static char my_tcpip_address[NET_NAMELEN];
84 #define DEFAULTnet_hostport 26000
86 #define MAXHOSTNAMELEN 256
88 static int net_acceptsocket = -1; // socket for fielding new connections
89 static int net_controlsocket;
90 static int net_hostport; // udp port number for acceptsocket
91 static int net_broadcastsocket = 0;
92 //static qboolean ifbcastinit = qfalse;
93 //static struct sockaddr_s broadcastaddr;
94 static struct sockaddr_s broadcastaddr;
96 static unsigned long myAddr;
98 ERROR_STRUCT errlist[] = {
99 {EACCES,"EACCES - The address is protected, user is not root"},
100 {EAGAIN,"EAGAIN - Operation on non-blocking socket that cannot return immediatly"},
101 {EBADF, "EBADF - sockfd is not a valid descriptor"},
102 {EFAULT, "EFAULT - The parameter is not in a writable part of the user address space"},
103 {EINVAL,"EINVAL - The socket is already bound to an address"},
104 {ENOBUFS,"ENOBUFS - not enough memory"},
105 {ENOMEM, "ENOMEM - not enough memory"},
106 {ENOTCONN, "ENOTCONN - not connected"},
107 {ENOTSOCK,"ENOTSOCK - Argument is file descriptor not a socket"},
108 {EOPNOTSUPP,"ENOTSUPP - The referenced socket is not of type SOCK_STREAM"},
109 {EPERM, "EPERM - Firewall rules forbid connection"},
113 //===========================================================================
117 // Changes Globals: -
118 //===========================================================================
119 char *WINS_ErrorMessage(int error)
123 if (!error) return "No error occurred";
125 for (search = 0; errlist[search].errstr; search++)
127 if (error == errlist[search].errnum)
128 return (char *)errlist[search].errstr;
131 return "Unknown error";
132 } //end of the function WINS_ErrorMessage
133 //===========================================================================
137 // Changes Globals: -
138 //===========================================================================
142 struct hostent *local;
143 char buff[MAXHOSTNAMELEN];
144 struct sockaddr_s addr;
147 linux doesn't have anything to initialize for the net
148 "Windows .. built for the internet .. the internet .. built with unix"
152 WORD wVersionRequested;
154 wVersionRequested = MAKEWORD(2, 2);
156 r = WSAStartup (wVersionRequested, &winsockdata);
160 WinPrint("Winsock initialization failed.\n");
165 i = COM_CheckParm ("-udpport");
167 net_hostport = DEFAULTnet_hostport;
169 else if (i < com_argc-1)
170 net_hostport = Q_atoi (com_argv[i+1]);
172 Sys_Error ("WINS_Init: you must specify a number after -udpport");
175 // determine my name & address
176 gethostname(buff, MAXHOSTNAMELEN);
177 local = gethostbyname(buff);
179 // When hostname can not be resolved, return gracefully
182 WinError("WINS_Init: Unable to resolve hostname\n");
186 myAddr = *(int *)local->h_addr_list[0];
188 // if the quake hostname isn't set, set it to the machine name
189 // if (Q_strcmp(hostname.string, "UNNAMED") == 0)
191 // see if it's a text IP address (well, close enough)
192 for (p = buff; *p; p++)
193 if ((*p < '0' || *p > '9') && *p != '.')
196 // if it is a real name, strip off the domain; we only want the host
199 for (i = 0; i < 15; i++)
204 // Cvar_Set ("hostname", buff);
207 //++timo WTF is that net_controlsocket? it's sole purpose is to retrieve the local IP?
208 if ((net_controlsocket = WINS_OpenSocket (0)) == SOCKET_ERROR)
209 WinError("WINS_Init: Unable to open control socket\n");
211 ((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET;
212 ((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = INADDR_BROADCAST;
213 ((struct sockaddr_in *)&broadcastaddr)->sin_port = htons((u_short)net_hostport);
215 WINS_GetSocketAddr (net_controlsocket, &addr);
216 strcpy(my_tcpip_address, WINS_AddrToString (&addr));
217 p = strrchr (my_tcpip_address, ':');
219 WinPrint("Winsock Initialized\n");
221 return net_controlsocket;
222 } //end of the function WINS_Init
223 //===========================================================================
227 // Changes Globals: -
228 //===========================================================================
229 char *WINS_MyAddress(void)
231 return my_tcpip_address;
232 } //end of the function WINS_MyAddress
233 //===========================================================================
237 // Changes Globals: -
238 //===========================================================================
239 void WINS_Shutdown(void)
242 WINS_CloseSocket(net_controlsocket);
245 WinPrint("Winsock Shutdown\n");
246 } //end of the function WINS_Shutdown
247 //===========================================================================
251 // Changes Globals: -
252 //===========================================================================
254 void WINS_Listen(int state)
259 if (net_acceptsocket != -1)
261 if ((net_acceptsocket = WINS_OpenSocket (net_hostport)) == -1)
262 WinError ("WINS_Listen: Unable to open accept socket\n");
267 if (net_acceptsocket == -1)
269 WINS_CloseSocket (net_acceptsocket);
270 net_acceptsocket = -1;
271 } //end of the function WINS_Listen*/
272 //===========================================================================
276 // Changes Globals: -
277 //===========================================================================
278 int WINS_OpenSocket(int port)
281 struct sockaddr_in address;
284 if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
286 WinPrint("WINS_OpenSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
290 if (ioctlsocket (newsocket, FIONBIO, &_true) == SOCKET_ERROR)
292 WinPrint("WINS_OpenSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
293 closesocket(newsocket);
297 memset((char *) &address, 0, sizeof(address));
298 address.sin_family = AF_INET;
299 address.sin_addr.s_addr = INADDR_ANY;
300 address.sin_port = htons((u_short)port);
301 if( bind (newsocket, (void *)&address, sizeof(address)) == -1)
303 WinPrint("WINS_OpenSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
304 closesocket(newsocket);
309 } //end of the function WINS_OpenSocket
310 //===========================================================================
314 // Changes Globals: -
315 //===========================================================================
316 int WINS_OpenReliableSocket(int port)
319 struct sockaddr_in address;
320 qboolean _true = 0xFFFFFFFF;
324 if ((newsocket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
326 WinPrint("WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
330 memset((char *) &address, 0, sizeof(address));
331 address.sin_family = AF_INET;
332 address.sin_addr.s_addr = htonl(INADDR_ANY);
333 address.sin_port = htons((u_short)port);
334 if (bind(newsocket, (void *)&address, sizeof(address)) == -1)
336 WinPrint("WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
337 closesocket(newsocket);
342 if (setsockopt(newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof(int)) == -1)
344 WinPrint("WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
345 WinPrint("setsockopt error\n");
349 } //end of the function WINS_OpenReliableSocket
350 //===========================================================================
354 // Changes Globals: -
355 //===========================================================================
356 int WINS_Listen(int socket)
360 if (ioctlsocket(socket, FIONBIO, &_true) == -1)
362 WinPrint("WINS_Listen: %s\n", WINS_ErrorMessage(WSAGetLastError()));
365 if (listen(socket, SOMAXCONN) == SOCKET_ERROR)
367 WinPrint("WINS_Listen: %s\n", WINS_ErrorMessage(WSAGetLastError()));
371 } //end of the function WINS_Listen
372 //===========================================================================
376 // Changes Globals: -
377 //===========================================================================
378 int WINS_Accept(int socket, struct sockaddr_s *addr)
380 int addrlen = sizeof (struct sockaddr_s);
384 newsocket = accept(socket, (struct sockaddr *)addr, &addrlen);
385 if (newsocket == INVALID_SOCKET)
387 if (errno == EAGAIN) return -1;
388 WinPrint("WINS_Accept: %s\n", WINS_ErrorMessage(WSAGetLastError()));
392 if (setsockopt(newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof(int)) == SOCKET_ERROR)
394 WinPrint("WINS_Accept: %s\n", WINS_ErrorMessage(WSAGetLastError()));
395 WinPrint("setsockopt error\n");
398 } //end of the function WINS_Accept
399 //===========================================================================
403 // Changes Globals: -
404 //===========================================================================
405 int WINS_CloseSocket(int socket)
408 if (socket == net_broadcastsocket)
409 net_broadcastsocket = 0;
411 // shutdown(socket, SD_SEND);
413 if (closesocket(socket) == SOCKET_ERROR)
415 WinPrint("WINS_CloseSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
419 } //end of the function WINS_CloseSocket
420 //===========================================================================
421 // this lets you type only as much of the net address as required, using
422 // the local network components to fill in the rest
426 // Changes Globals: -
427 //===========================================================================
428 static int PartialIPAddress (char *in, struct sockaddr_s *hostaddr)
439 if (buff[1] == '.') b++;
446 if (*++b < '0' || *b > '9') return -1;
447 while (!( *b < '0' || *b > '9'))
448 num = num*10 + *(b++) - '0';
450 addr = (addr<<8) + num;
453 hostaddr->sa_family = AF_INET;
454 ((struct sockaddr_in *)hostaddr)->sin_port = htons((u_short)net_hostport);
455 ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr = (myAddr & htonl(mask)) | htonl(addr);
458 } //end of the function PartialIPAddress
459 //===========================================================================
463 // Changes Globals: -
464 //===========================================================================
465 int WINS_Connect(int socket, struct sockaddr_s *addr)
468 u_long _true2 = 0xFFFFFFFF;
470 ret = connect(socket, (struct sockaddr *)addr, sizeof(struct sockaddr_s));
471 if (ret == SOCKET_ERROR)
473 WinPrint("WINS_Connect: %s\n", WINS_ErrorMessage(WSAGetLastError()));
476 if (ioctlsocket(socket, FIONBIO, &_true2) == -1)
478 WinPrint("WINS_Connect: %s\n", WINS_ErrorMessage(WSAGetLastError()));
482 } //end of the function WINS_Connect
483 //===========================================================================
487 // Changes Globals: -
488 //===========================================================================
489 int WINS_CheckNewConnections(void)
493 if (net_acceptsocket == -1)
496 if (recvfrom(net_acceptsocket, buf, 4, MSG_PEEK, NULL, NULL) > 0)
497 return net_acceptsocket;
499 } //end of the function WINS_CheckNewConnections
500 //===========================================================================
501 // returns the number of bytes read
502 // 0 if no bytes available
507 // Changes Globals: -
508 //===========================================================================
509 int WINS_Read(int socket, byte *buf, int len, struct sockaddr_s *addr)
511 int addrlen = sizeof (struct sockaddr_s);
516 ret = recvfrom(socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
519 // errno = WSAGetLastError();
521 if (errno == EAGAIN || errno == ENOTCONN)
527 ret = recv(socket, buf, len, 0);
528 // if there's no data on the socket ret == -1 and errno == EAGAIN
529 // MSDN states that if ret == 0 the socket has been closed
530 // man recv doesn't say anything
533 if (ret == SOCKET_ERROR)
535 // errno = WSAGetLastError();
537 if (errno == EAGAIN || errno == ENOTCONN)
541 if (ret == SOCKET_ERROR)
543 WinPrint("WINS_Read: %s\n", WINS_ErrorMessage(WSAGetLastError()));
546 } //end of the function WINS_Read
547 //===========================================================================
551 // Changes Globals: -
552 //===========================================================================
553 int WINS_MakeSocketBroadcastCapable (int socket)
557 // make this socket broadcast capable
558 if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0)
560 net_broadcastsocket = socket;
563 } //end of the function WINS_MakeSocketBroadcastCapable
564 //===========================================================================
568 // Changes Globals: -
569 //===========================================================================
570 int WINS_Broadcast (int socket, byte *buf, int len)
574 if (socket != net_broadcastsocket)
576 if (net_broadcastsocket != 0)
577 WinError("Attempted to use multiple broadcasts sockets\n");
578 ret = WINS_MakeSocketBroadcastCapable (socket);
581 WinPrint("Unable to make socket broadcast capable\n");
586 return WINS_Write (socket, buf, len, &broadcastaddr);
587 } //end of the function WINS_Broadcast
588 //===========================================================================
589 // returns qtrue on success or qfalse on failure
593 // Changes Globals: -
594 //===========================================================================
595 int WINS_Write(int socket, byte *buf, int len, struct sockaddr_s *addr)
597 int ret = 0, written;
604 ret = sendto (socket, &buf[written], len-written, 0, (struct sockaddr *)addr, sizeof(struct sockaddr_s));
605 if (ret == SOCKET_ERROR)
607 if (WSAGetLastError() != EAGAIN)
609 //++timo FIXME: what is this used for?
623 ret = send(socket, buf, len, 0);
624 if (ret == SOCKET_ERROR)
626 if (WSAGetLastError() != EAGAIN)
628 //++timo FIXME: what is this used for?
637 if (ret == SOCKET_ERROR)
639 WinPrint("WINS_Write: %s\n", WINS_ErrorMessage(WSAGetLastError()));
642 } //end of the function WINS_Write
643 //===========================================================================
647 // Changes Globals: -
648 //===========================================================================
649 char *WINS_AddrToString (struct sockaddr_s *addr)
651 static char buffer[22];
654 haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr);
655 sprintf(buffer, "%d.%d.%d.%d:%d", (haddr >> 24) & 0xff, (haddr >> 16) & 0xff, (haddr >> 8) & 0xff, haddr & 0xff, ntohs(((struct sockaddr_in *)addr)->sin_port));
657 } //end of the function WINS_AddrToString
658 //===========================================================================
662 // Changes Globals: -
663 //===========================================================================
664 int WINS_StringToAddr(char *string, struct sockaddr_s *addr)
666 int ha1, ha2, ha3, ha4, hp;
669 sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
670 ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
672 addr->sa_family = AF_INET;
673 ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
674 ((struct sockaddr_in *)addr)->sin_port = htons((u_short)hp);
676 } //end of the function WINS_StringToAddr
677 //===========================================================================
681 // Changes Globals: -
682 //===========================================================================
683 int WINS_GetSocketAddr(int socket, struct sockaddr_s *addr)
685 int addrlen = sizeof(struct sockaddr_s);
688 memset(addr, 0, sizeof(struct sockaddr_s));
689 getsockname(socket, (struct sockaddr *)addr, &addrlen);
690 a = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
691 if (a == 0 || a == inet_addr("127.0.0.1"))
692 ((struct sockaddr_in *)addr)->sin_addr.s_addr = myAddr;
695 } //end of the function WINS_GetSocketAddr
696 //===========================================================================
700 // Changes Globals: -
701 //===========================================================================
702 int WINS_GetNameFromAddr (struct sockaddr_s *addr, char *name)
704 struct hostent *hostentry;
706 hostentry = gethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
709 strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
713 strcpy (name, WINS_AddrToString (addr));
715 } //end of the function WINS_GetNameFromAddr
716 //===========================================================================
720 // Changes Globals: -
721 //===========================================================================
722 int WINS_GetAddrFromName(char *name, struct sockaddr_s *addr)
724 struct hostent *hostentry;
726 if (name[0] >= '0' && name[0] <= '9')
727 return PartialIPAddress (name, addr);
729 hostentry = gethostbyname (name);
733 addr->sa_family = AF_INET;
734 ((struct sockaddr_in *)addr)->sin_port = htons((u_short)net_hostport);
735 ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
738 } //end of the function WINS_GetAddrFromName
739 //===========================================================================
743 // Changes Globals: -
744 //===========================================================================
745 int WINS_AddrCompare (struct sockaddr_s *addr1, struct sockaddr_s *addr2)
747 if (addr1->sa_family != addr2->sa_family)
750 if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr)
753 if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port)
757 } //end of the function WINS_AddrCompare
758 //===========================================================================
762 // Changes Globals: -
763 //===========================================================================
764 int WINS_GetSocketPort (struct sockaddr_s *addr)
766 return ntohs(((struct sockaddr_in *)addr)->sin_port);
767 } //end of the function WINS_GetSocketPort
768 //===========================================================================
772 // Changes Globals: -
773 //===========================================================================
774 int WINS_SetSocketPort (struct sockaddr_s *addr, int port)
776 ((struct sockaddr_in *)addr)->sin_port = htons((u_short)port);
778 } //end of the function WINS_SetSocketPort