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;
148 linux doesn't have anything to initialize for the net
149 "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);
178 myAddr = *(int *)local->h_addr_list[0];
180 // if the quake hostname isn't set, set it to the machine name
181 // if (Q_strcmp(hostname.string, "UNNAMED") == 0)
183 // see if it's a text IP address (well, close enough)
184 for (p = buff; *p; p++)
185 if ((*p < '0' || *p > '9') && *p != '.')
188 // if it is a real name, strip off the domain; we only want the host
191 for (i = 0; i < 15; i++)
196 // Cvar_Set ("hostname", buff);
199 //++timo WTF is that net_controlsocket? it's sole purpose is to retrieve the local IP?
200 if ((net_controlsocket = WINS_OpenSocket (0)) == SOCKET_ERROR)
201 WinError("WINS_Init: Unable to open control socket\n");
203 ((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET;
204 ((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = INADDR_BROADCAST;
205 ((struct sockaddr_in *)&broadcastaddr)->sin_port = htons((u_short)net_hostport);
207 WINS_GetSocketAddr (net_controlsocket, &addr);
208 strcpy(my_tcpip_address, WINS_AddrToString (&addr));
209 p = strrchr (my_tcpip_address, ':');
211 WinPrint("Winsock Initialized\n");
213 return net_controlsocket;
214 } //end of the function WINS_Init
215 //===========================================================================
219 // Changes Globals: -
220 //===========================================================================
221 char *WINS_MyAddress(void)
223 return my_tcpip_address;
224 } //end of the function WINS_MyAddress
225 //===========================================================================
229 // Changes Globals: -
230 //===========================================================================
231 void WINS_Shutdown(void)
234 WINS_CloseSocket(net_controlsocket);
237 WinPrint("Winsock Shutdown\n");
238 } //end of the function WINS_Shutdown
239 //===========================================================================
243 // Changes Globals: -
244 //===========================================================================
246 void WINS_Listen(int state)
251 if (net_acceptsocket != -1)
253 if ((net_acceptsocket = WINS_OpenSocket (net_hostport)) == -1)
254 WinError ("WINS_Listen: Unable to open accept socket\n");
259 if (net_acceptsocket == -1)
261 WINS_CloseSocket (net_acceptsocket);
262 net_acceptsocket = -1;
263 } //end of the function WINS_Listen*/
264 //===========================================================================
268 // Changes Globals: -
269 //===========================================================================
270 int WINS_OpenSocket(int port)
273 struct sockaddr_in address;
276 if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
278 WinPrint("WINS_OpenSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
282 if (ioctlsocket (newsocket, FIONBIO, &_true) == SOCKET_ERROR)
284 WinPrint("WINS_OpenSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
285 closesocket(newsocket);
289 memset((char *) &address, 0, sizeof(address));
290 address.sin_family = AF_INET;
291 address.sin_addr.s_addr = INADDR_ANY;
292 address.sin_port = htons((u_short)port);
293 if( bind (newsocket, (void *)&address, sizeof(address)) == -1)
295 WinPrint("WINS_OpenSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
296 closesocket(newsocket);
301 } //end of the function WINS_OpenSocket
302 //===========================================================================
306 // Changes Globals: -
307 //===========================================================================
308 int WINS_OpenReliableSocket(int port)
311 struct sockaddr_in address;
312 qboolean _true = 0xFFFFFFFF;
316 if ((newsocket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
318 WinPrint("WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
322 memset((char *) &address, 0, sizeof(address));
323 address.sin_family = AF_INET;
324 address.sin_addr.s_addr = htonl(INADDR_ANY);
325 address.sin_port = htons((u_short)port);
326 if (bind(newsocket, (void *)&address, sizeof(address)) == -1)
328 WinPrint("WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
329 closesocket(newsocket);
334 if (setsockopt(newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof(int)) == -1)
336 WinPrint("WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
337 WinPrint("setsockopt error\n");
341 } //end of the function WINS_OpenReliableSocket
342 //===========================================================================
346 // Changes Globals: -
347 //===========================================================================
348 int WINS_Listen(int socket)
352 if (ioctlsocket(socket, FIONBIO, &_true) == -1)
354 WinPrint("WINS_Listen: %s\n", WINS_ErrorMessage(WSAGetLastError()));
357 if (listen(socket, SOMAXCONN) == SOCKET_ERROR)
359 WinPrint("WINS_Listen: %s\n", WINS_ErrorMessage(WSAGetLastError()));
363 } //end of the function WINS_Listen
364 //===========================================================================
368 // Changes Globals: -
369 //===========================================================================
370 int WINS_Accept(int socket, struct sockaddr_s *addr)
372 int addrlen = sizeof (struct sockaddr_s);
376 newsocket = accept(socket, (struct sockaddr *)addr, &addrlen);
377 if (newsocket == INVALID_SOCKET)
379 if (errno == EAGAIN) return -1;
380 WinPrint("WINS_Accept: %s\n", WINS_ErrorMessage(WSAGetLastError()));
384 if (setsockopt(newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof(int)) == SOCKET_ERROR)
386 WinPrint("WINS_Accept: %s\n", WINS_ErrorMessage(WSAGetLastError()));
387 WinPrint("setsockopt error\n");
390 } //end of the function WINS_Accept
391 //===========================================================================
395 // Changes Globals: -
396 //===========================================================================
397 int WINS_CloseSocket(int socket)
400 if (socket == net_broadcastsocket)
401 net_broadcastsocket = 0;
403 // shutdown(socket, SD_SEND);
405 if (closesocket(socket) == SOCKET_ERROR)
407 WinPrint("WINS_CloseSocket: %s\n", WINS_ErrorMessage(WSAGetLastError()));
411 } //end of the function WINS_CloseSocket
412 //===========================================================================
413 // this lets you type only as much of the net address as required, using
414 // the local network components to fill in the rest
418 // Changes Globals: -
419 //===========================================================================
420 static int PartialIPAddress (char *in, struct sockaddr_s *hostaddr)
431 if (buff[1] == '.') b++;
438 if (*++b < '0' || *b > '9') return -1;
439 while (!( *b < '0' || *b > '9'))
440 num = num*10 + *(b++) - '0';
442 addr = (addr<<8) + num;
445 hostaddr->sa_family = AF_INET;
446 ((struct sockaddr_in *)hostaddr)->sin_port = htons((u_short)net_hostport);
447 ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr = (myAddr & htonl(mask)) | htonl(addr);
450 } //end of the function PartialIPAddress
451 //===========================================================================
455 // Changes Globals: -
456 //===========================================================================
457 int WINS_Connect(int socket, struct sockaddr_s *addr)
460 u_long _true2 = 0xFFFFFFFF;
462 ret = connect(socket, (struct sockaddr *)addr, sizeof(struct sockaddr_s));
463 if (ret == SOCKET_ERROR)
465 WinPrint("WINS_Connect: %s\n", WINS_ErrorMessage(WSAGetLastError()));
468 if (ioctlsocket(socket, FIONBIO, &_true2) == -1)
470 WinPrint("WINS_Connect: %s\n", WINS_ErrorMessage(WSAGetLastError()));
474 } //end of the function WINS_Connect
475 //===========================================================================
479 // Changes Globals: -
480 //===========================================================================
481 int WINS_CheckNewConnections(void)
485 if (net_acceptsocket == -1)
488 if (recvfrom(net_acceptsocket, buf, 4, MSG_PEEK, NULL, NULL) > 0)
489 return net_acceptsocket;
491 } //end of the function WINS_CheckNewConnections
492 //===========================================================================
493 // returns the number of bytes read
494 // 0 if no bytes available
499 // Changes Globals: -
500 //===========================================================================
501 int WINS_Read(int socket, byte *buf, int len, struct sockaddr_s *addr)
503 int addrlen = sizeof (struct sockaddr_s);
508 ret = recvfrom(socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
511 // errno = WSAGetLastError();
513 if (errno == EAGAIN || errno == ENOTCONN)
519 ret = recv(socket, buf, len, 0);
520 // if there's no data on the socket ret == -1 and errno == EAGAIN
521 // MSDN states that if ret == 0 the socket has been closed
522 // man recv doesn't say anything
525 if (ret == SOCKET_ERROR)
527 // errno = WSAGetLastError();
529 if (errno == EAGAIN || errno == ENOTCONN)
533 if (ret == SOCKET_ERROR)
535 WinPrint("WINS_Read: %s\n", WINS_ErrorMessage(WSAGetLastError()));
538 } //end of the function WINS_Read
539 //===========================================================================
543 // Changes Globals: -
544 //===========================================================================
545 int WINS_MakeSocketBroadcastCapable (int socket)
549 // make this socket broadcast capable
550 if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0)
552 net_broadcastsocket = socket;
555 } //end of the function WINS_MakeSocketBroadcastCapable
556 //===========================================================================
560 // Changes Globals: -
561 //===========================================================================
562 int WINS_Broadcast (int socket, byte *buf, int len)
566 if (socket != net_broadcastsocket)
568 if (net_broadcastsocket != 0)
569 WinError("Attempted to use multiple broadcasts sockets\n");
570 ret = WINS_MakeSocketBroadcastCapable (socket);
573 WinPrint("Unable to make socket broadcast capable\n");
578 return WINS_Write (socket, buf, len, &broadcastaddr);
579 } //end of the function WINS_Broadcast
580 //===========================================================================
581 // returns qtrue on success or qfalse on failure
585 // Changes Globals: -
586 //===========================================================================
587 int WINS_Write(int socket, byte *buf, int len, struct sockaddr_s *addr)
596 ret = sendto (socket, &buf[written], len-written, 0, (struct sockaddr *)addr, sizeof(struct sockaddr_s));
597 if (ret == SOCKET_ERROR)
599 if (WSAGetLastError() != EAGAIN)
601 //++timo FIXME: what is this used for?
615 ret = send(socket, buf, len, 0);
616 if (ret == SOCKET_ERROR)
618 if (WSAGetLastError() != EAGAIN)
620 //++timo FIXME: what is this used for?
629 if (ret == SOCKET_ERROR)
631 WinPrint("WINS_Write: %s\n", WINS_ErrorMessage(WSAGetLastError()));
634 } //end of the function WINS_Write
635 //===========================================================================
639 // Changes Globals: -
640 //===========================================================================
641 char *WINS_AddrToString (struct sockaddr_s *addr)
643 static char buffer[22];
646 haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr);
647 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));
649 } //end of the function WINS_AddrToString
650 //===========================================================================
654 // Changes Globals: -
655 //===========================================================================
656 int WINS_StringToAddr(char *string, struct sockaddr_s *addr)
658 int ha1, ha2, ha3, ha4, hp;
661 sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
662 ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
664 addr->sa_family = AF_INET;
665 ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
666 ((struct sockaddr_in *)addr)->sin_port = htons((u_short)hp);
668 } //end of the function WINS_StringToAddr
669 //===========================================================================
673 // Changes Globals: -
674 //===========================================================================
675 int WINS_GetSocketAddr(int socket, struct sockaddr_s *addr)
677 int addrlen = sizeof(struct sockaddr_s);
680 memset(addr, 0, sizeof(struct sockaddr_s));
681 getsockname(socket, (struct sockaddr *)addr, &addrlen);
682 a = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
683 if (a == 0 || a == inet_addr("127.0.0.1"))
684 ((struct sockaddr_in *)addr)->sin_addr.s_addr = myAddr;
687 } //end of the function WINS_GetSocketAddr
688 //===========================================================================
692 // Changes Globals: -
693 //===========================================================================
694 int WINS_GetNameFromAddr (struct sockaddr_s *addr, char *name)
696 struct hostent *hostentry;
698 hostentry = gethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
701 strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
705 strcpy (name, WINS_AddrToString (addr));
707 } //end of the function WINS_GetNameFromAddr
708 //===========================================================================
712 // Changes Globals: -
713 //===========================================================================
714 int WINS_GetAddrFromName(char *name, struct sockaddr_s *addr)
716 struct hostent *hostentry;
718 if (name[0] >= '0' && name[0] <= '9')
719 return PartialIPAddress (name, addr);
721 hostentry = gethostbyname (name);
725 addr->sa_family = AF_INET;
726 ((struct sockaddr_in *)addr)->sin_port = htons((u_short)net_hostport);
727 ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
730 } //end of the function WINS_GetAddrFromName
731 //===========================================================================
735 // Changes Globals: -
736 //===========================================================================
737 int WINS_AddrCompare (struct sockaddr_s *addr1, struct sockaddr_s *addr2)
739 if (addr1->sa_family != addr2->sa_family)
742 if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr)
745 if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port)
749 } //end of the function WINS_AddrCompare
750 //===========================================================================
754 // Changes Globals: -
755 //===========================================================================
756 int WINS_GetSocketPort (struct sockaddr_s *addr)
758 return ntohs(((struct sockaddr_in *)addr)->sin_port);
759 } //end of the function WINS_GetSocketPort
760 //===========================================================================
764 // Changes Globals: -
765 //===========================================================================
766 int WINS_SetSocketPort (struct sockaddr_s *addr, int port)
768 ((struct sockaddr_in *)addr)->sin_port = htons((u_short)port);
770 } //end of the function WINS_SetSocketPort