net_wins.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // net_wins.c
  16. #include "quakedef.h"
  17. #include "winquake.h"
  18. extern cvar_t hostname;
  19. #define MAXHOSTNAMELEN 256
  20. static int net_acceptsocket = -1; // socket for fielding new connections
  21. static int net_controlsocket;
  22. static int net_broadcastsocket = 0;
  23. static struct qsockaddr broadcastaddr;
  24. static unsigned long myAddr;
  25. qboolean winsock_lib_initialized;
  26. int (PASCAL FAR *pWSAStartup)(WORD wVersionRequired, LPWSADATA lpWSAData);
  27. int (PASCAL FAR *pWSACleanup)(void);
  28. int (PASCAL FAR *pWSAGetLastError)(void);
  29. SOCKET (PASCAL FAR *psocket)(int af, int type, int protocol);
  30. int (PASCAL FAR *pioctlsocket)(SOCKET s, long cmd, u_long FAR *argp);
  31. int (PASCAL FAR *psetsockopt)(SOCKET s, int level, int optname,
  32. const char FAR * optval, int optlen);
  33. int (PASCAL FAR *precvfrom)(SOCKET s, char FAR * buf, int len, int flags,
  34. struct sockaddr FAR *from, int FAR * fromlen);
  35. int (PASCAL FAR *psendto)(SOCKET s, const char FAR * buf, int len, int flags,
  36. const struct sockaddr FAR *to, int tolen);
  37. int (PASCAL FAR *pclosesocket)(SOCKET s);
  38. int (PASCAL FAR *pgethostname)(char FAR * name, int namelen);
  39. struct hostent FAR * (PASCAL FAR *pgethostbyname)(const char FAR * name);
  40. struct hostent FAR * (PASCAL FAR *pgethostbyaddr)(const char FAR * addr,
  41. int len, int type);
  42. int (PASCAL FAR *pgetsockname)(SOCKET s, struct sockaddr FAR *name,
  43. int FAR * namelen);
  44. #include "net_wins.h"
  45. int winsock_initialized = 0;
  46. WSADATA winsockdata;
  47. //=============================================================================
  48. static double blocktime;
  49. BOOL PASCAL FAR BlockingHook(void)
  50. {
  51. MSG msg;
  52. BOOL ret;
  53. if ((Sys_FloatTime() - blocktime) > 2.0)
  54. {
  55. WSACancelBlockingCall();
  56. return FALSE;
  57. }
  58. /* get the next message, if any */
  59. ret = (BOOL) PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
  60. /* if we got one, process it */
  61. if (ret) {
  62. TranslateMessage(&msg);
  63. DispatchMessage(&msg);
  64. }
  65. /* TRUE if we got a message */
  66. return ret;
  67. }
  68. void WINS_GetLocalAddress()
  69. {
  70. struct hostent *local = NULL;
  71. char buff[MAXHOSTNAMELEN];
  72. unsigned long addr;
  73. if (myAddr != INADDR_ANY)
  74. return;
  75. if (pgethostname(buff, MAXHOSTNAMELEN) == SOCKET_ERROR)
  76. return;
  77. blocktime = Sys_FloatTime();
  78. WSASetBlockingHook(BlockingHook);
  79. local = pgethostbyname(buff);
  80. WSAUnhookBlockingHook();
  81. if (local == NULL)
  82. return;
  83. myAddr = *(int *)local->h_addr_list[0];
  84. addr = ntohl(myAddr);
  85. sprintf(my_tcpip_address, "%d.%d.%d.%d", (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff, addr & 0xff);
  86. }
  87. int WINS_Init (void)
  88. {
  89. int i;
  90. char buff[MAXHOSTNAMELEN];
  91. char *p;
  92. int r;
  93. WORD wVersionRequested;
  94. HINSTANCE hInst;
  95. // initialize the Winsock function vectors (we do this instead of statically linking
  96. // so we can run on Win 3.1, where there isn't necessarily Winsock)
  97. hInst = LoadLibrary("wsock32.dll");
  98. if (hInst == NULL)
  99. {
  100. Con_SafePrintf ("Failed to load winsock.dll\n");
  101. winsock_lib_initialized = false;
  102. return -1;
  103. }
  104. winsock_lib_initialized = true;
  105. pWSAStartup = (void *)GetProcAddress(hInst, "WSAStartup");
  106. pWSACleanup = (void *)GetProcAddress(hInst, "WSACleanup");
  107. pWSAGetLastError = (void *)GetProcAddress(hInst, "WSAGetLastError");
  108. psocket = (void *)GetProcAddress(hInst, "socket");
  109. pioctlsocket = (void *)GetProcAddress(hInst, "ioctlsocket");
  110. psetsockopt = (void *)GetProcAddress(hInst, "setsockopt");
  111. precvfrom = (void *)GetProcAddress(hInst, "recvfrom");
  112. psendto = (void *)GetProcAddress(hInst, "sendto");
  113. pclosesocket = (void *)GetProcAddress(hInst, "closesocket");
  114. pgethostname = (void *)GetProcAddress(hInst, "gethostname");
  115. pgethostbyname = (void *)GetProcAddress(hInst, "gethostbyname");
  116. pgethostbyaddr = (void *)GetProcAddress(hInst, "gethostbyaddr");
  117. pgetsockname = (void *)GetProcAddress(hInst, "getsockname");
  118. if (!pWSAStartup || !pWSACleanup || !pWSAGetLastError ||
  119. !psocket || !pioctlsocket || !psetsockopt ||
  120. !precvfrom || !psendto || !pclosesocket ||
  121. !pgethostname || !pgethostbyname || !pgethostbyaddr ||
  122. !pgetsockname)
  123. {
  124. Con_SafePrintf ("Couldn't GetProcAddress from winsock.dll\n");
  125. return -1;
  126. }
  127. if (COM_CheckParm ("-noudp"))
  128. return -1;
  129. if (winsock_initialized == 0)
  130. {
  131. wVersionRequested = MAKEWORD(1, 1);
  132. r = pWSAStartup (MAKEWORD(1, 1), &winsockdata);
  133. if (r)
  134. {
  135. Con_SafePrintf ("Winsock initialization failed.\n");
  136. return -1;
  137. }
  138. }
  139. winsock_initialized++;
  140. // determine my name
  141. if (pgethostname(buff, MAXHOSTNAMELEN) == SOCKET_ERROR)
  142. {
  143. Con_DPrintf ("Winsock TCP/IP Initialization failed.\n");
  144. if (--winsock_initialized == 0)
  145. pWSACleanup ();
  146. return -1;
  147. }
  148. // if the quake hostname isn't set, set it to the machine name
  149. if (Q_strcmp(hostname.string, "UNNAMED") == 0)
  150. {
  151. // see if it's a text IP address (well, close enough)
  152. for (p = buff; *p; p++)
  153. if ((*p < '0' || *p > '9') && *p != '.')
  154. break;
  155. // if it is a real name, strip off the domain; we only want the host
  156. if (*p)
  157. {
  158. for (i = 0; i < 15; i++)
  159. if (buff[i] == '.')
  160. break;
  161. buff[i] = 0;
  162. }
  163. Cvar_Set ("hostname", buff);
  164. }
  165. i = COM_CheckParm ("-ip");
  166. if (i)
  167. {
  168. if (i < com_argc-1)
  169. {
  170. myAddr = inet_addr(com_argv[i+1]);
  171. if (myAddr == INADDR_NONE)
  172. Sys_Error ("%s is not a valid IP address", com_argv[i+1]);
  173. strcpy(my_tcpip_address, com_argv[i+1]);
  174. }
  175. else
  176. {
  177. Sys_Error ("NET_Init: you must specify an IP address after -ip");
  178. }
  179. }
  180. else
  181. {
  182. myAddr = INADDR_ANY;
  183. strcpy(my_tcpip_address, "INADDR_ANY");
  184. }
  185. if ((net_controlsocket = WINS_OpenSocket (0)) == -1)
  186. {
  187. Con_Printf("WINS_Init: Unable to open control socket\n");
  188. if (--winsock_initialized == 0)
  189. pWSACleanup ();
  190. return -1;
  191. }
  192. ((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET;
  193. ((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = INADDR_BROADCAST;
  194. ((struct sockaddr_in *)&broadcastaddr)->sin_port = htons((unsigned short)net_hostport);
  195. Con_Printf("Winsock TCP/IP Initialized\n");
  196. tcpipAvailable = true;
  197. return net_controlsocket;
  198. }
  199. //=============================================================================
  200. void WINS_Shutdown (void)
  201. {
  202. WINS_Listen (false);
  203. WINS_CloseSocket (net_controlsocket);
  204. if (--winsock_initialized == 0)
  205. pWSACleanup ();
  206. }
  207. //=============================================================================
  208. void WINS_Listen (qboolean state)
  209. {
  210. // enable listening
  211. if (state)
  212. {
  213. if (net_acceptsocket != -1)
  214. return;
  215. WINS_GetLocalAddress();
  216. if ((net_acceptsocket = WINS_OpenSocket (net_hostport)) == -1)
  217. Sys_Error ("WINS_Listen: Unable to open accept socket\n");
  218. return;
  219. }
  220. // disable listening
  221. if (net_acceptsocket == -1)
  222. return;
  223. WINS_CloseSocket (net_acceptsocket);
  224. net_acceptsocket = -1;
  225. }
  226. //=============================================================================
  227. int WINS_OpenSocket (int port)
  228. {
  229. int newsocket;
  230. struct sockaddr_in address;
  231. u_long _true = 1;
  232. if ((newsocket = psocket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  233. return -1;
  234. if (pioctlsocket (newsocket, FIONBIO, &_true) == -1)
  235. goto ErrorReturn;
  236. address.sin_family = AF_INET;
  237. address.sin_addr.s_addr = myAddr;
  238. address.sin_port = htons((unsigned short)port);
  239. if( bind (newsocket, (void *)&address, sizeof(address)) == 0)
  240. return newsocket;
  241. Sys_Error ("Unable to bind to %s", WINS_AddrToString((struct qsockaddr *)&address));
  242. ErrorReturn:
  243. pclosesocket (newsocket);
  244. return -1;
  245. }
  246. //=============================================================================
  247. int WINS_CloseSocket (int socket)
  248. {
  249. if (socket == net_broadcastsocket)
  250. net_broadcastsocket = 0;
  251. return pclosesocket (socket);
  252. }
  253. //=============================================================================
  254. /*
  255. ============
  256. PartialIPAddress
  257. this lets you type only as much of the net address as required, using
  258. the local network components to fill in the rest
  259. ============
  260. */
  261. static int PartialIPAddress (char *in, struct qsockaddr *hostaddr)
  262. {
  263. char buff[256];
  264. char *b;
  265. int addr;
  266. int num;
  267. int mask;
  268. int run;
  269. int port;
  270. buff[0] = '.';
  271. b = buff;
  272. strcpy(buff+1, in);
  273. if (buff[1] == '.')
  274. b++;
  275. addr = 0;
  276. mask=-1;
  277. while (*b == '.')
  278. {
  279. b++;
  280. num = 0;
  281. run = 0;
  282. while (!( *b < '0' || *b > '9'))
  283. {
  284. num = num*10 + *b++ - '0';
  285. if (++run > 3)
  286. return -1;
  287. }
  288. if ((*b < '0' || *b > '9') && *b != '.' && *b != ':' && *b != 0)
  289. return -1;
  290. if (num < 0 || num > 255)
  291. return -1;
  292. mask<<=8;
  293. addr = (addr<<8) + num;
  294. }
  295. if (*b++ == ':')
  296. port = Q_atoi(b);
  297. else
  298. port = net_hostport;
  299. hostaddr->sa_family = AF_INET;
  300. ((struct sockaddr_in *)hostaddr)->sin_port = htons((short)port);
  301. ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr = (myAddr & htonl(mask)) | htonl(addr);
  302. return 0;
  303. }
  304. //=============================================================================
  305. int WINS_Connect (int socket, struct qsockaddr *addr)
  306. {
  307. return 0;
  308. }
  309. //=============================================================================
  310. int WINS_CheckNewConnections (void)
  311. {
  312. char buf[4096];
  313. if (net_acceptsocket == -1)
  314. return -1;
  315. if (precvfrom (net_acceptsocket, buf, sizeof(buf), MSG_PEEK, NULL, NULL) > 0)
  316. {
  317. return net_acceptsocket;
  318. }
  319. return -1;
  320. }
  321. //=============================================================================
  322. int WINS_Read (int socket, byte *buf, int len, struct qsockaddr *addr)
  323. {
  324. int addrlen = sizeof (struct qsockaddr);
  325. int ret;
  326. ret = precvfrom (socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
  327. if (ret == -1)
  328. {
  329. int errno = pWSAGetLastError();
  330. if (errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED)
  331. return 0;
  332. }
  333. return ret;
  334. }
  335. //=============================================================================
  336. int WINS_MakeSocketBroadcastCapable (int socket)
  337. {
  338. int i = 1;
  339. // make this socket broadcast capable
  340. if (psetsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0)
  341. return -1;
  342. net_broadcastsocket = socket;
  343. return 0;
  344. }
  345. //=============================================================================
  346. int WINS_Broadcast (int socket, byte *buf, int len)
  347. {
  348. int ret;
  349. if (socket != net_broadcastsocket)
  350. {
  351. if (net_broadcastsocket != 0)
  352. Sys_Error("Attempted to use multiple broadcasts sockets\n");
  353. WINS_GetLocalAddress();
  354. ret = WINS_MakeSocketBroadcastCapable (socket);
  355. if (ret == -1)
  356. {
  357. Con_Printf("Unable to make socket broadcast capable\n");
  358. return ret;
  359. }
  360. }
  361. return WINS_Write (socket, buf, len, &broadcastaddr);
  362. }
  363. //=============================================================================
  364. int WINS_Write (int socket, byte *buf, int len, struct qsockaddr *addr)
  365. {
  366. int ret;
  367. ret = psendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
  368. if (ret == -1)
  369. if (pWSAGetLastError() == WSAEWOULDBLOCK)
  370. return 0;
  371. return ret;
  372. }
  373. //=============================================================================
  374. char *WINS_AddrToString (struct qsockaddr *addr)
  375. {
  376. static char buffer[22];
  377. int haddr;
  378. haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr);
  379. 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));
  380. return buffer;
  381. }
  382. //=============================================================================
  383. int WINS_StringToAddr (char *string, struct qsockaddr *addr)
  384. {
  385. int ha1, ha2, ha3, ha4, hp;
  386. int ipaddr;
  387. sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
  388. ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
  389. addr->sa_family = AF_INET;
  390. ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
  391. ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)hp);
  392. return 0;
  393. }
  394. //=============================================================================
  395. int WINS_GetSocketAddr (int socket, struct qsockaddr *addr)
  396. {
  397. int addrlen = sizeof(struct qsockaddr);
  398. unsigned int a;
  399. Q_memset(addr, 0, sizeof(struct qsockaddr));
  400. pgetsockname(socket, (struct sockaddr *)addr, &addrlen);
  401. a = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  402. if (a == 0 || a == inet_addr("127.0.0.1"))
  403. ((struct sockaddr_in *)addr)->sin_addr.s_addr = myAddr;
  404. return 0;
  405. }
  406. //=============================================================================
  407. int WINS_GetNameFromAddr (struct qsockaddr *addr, char *name)
  408. {
  409. struct hostent *hostentry;
  410. hostentry = pgethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
  411. if (hostentry)
  412. {
  413. Q_strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
  414. return 0;
  415. }
  416. Q_strcpy (name, WINS_AddrToString (addr));
  417. return 0;
  418. }
  419. //=============================================================================
  420. int WINS_GetAddrFromName(char *name, struct qsockaddr *addr)
  421. {
  422. struct hostent *hostentry;
  423. if (name[0] >= '0' && name[0] <= '9')
  424. return PartialIPAddress (name, addr);
  425. hostentry = pgethostbyname (name);
  426. if (!hostentry)
  427. return -1;
  428. addr->sa_family = AF_INET;
  429. ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)net_hostport);
  430. ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
  431. return 0;
  432. }
  433. //=============================================================================
  434. int WINS_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2)
  435. {
  436. if (addr1->sa_family != addr2->sa_family)
  437. return -1;
  438. if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr)
  439. return -1;
  440. if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port)
  441. return 1;
  442. return 0;
  443. }
  444. //=============================================================================
  445. int WINS_GetSocketPort (struct qsockaddr *addr)
  446. {
  447. return ntohs(((struct sockaddr_in *)addr)->sin_port);
  448. }
  449. int WINS_SetSocketPort (struct qsockaddr *addr, int port)
  450. {
  451. ((struct sockaddr_in *)addr)->sin_port = htons((unsigned short)port);
  452. return 0;
  453. }
  454. //=============================================================================