PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/libs/l_net/l_net_wins.c

https://gitlab.com/illwieckz/netradiant
C | 761 lines | 444 code | 73 blank | 244 comment | 109 complexity | bcd579ff3dd4df6311c9b9663f9202c8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0
  1. /*
  2. Copyright (C) 1999-2006 Id Software, Inc. and contributors.
  3. For a list of contributors, see the accompanying CONTRIBUTORS file.
  4. This file is part of GtkRadiant.
  5. GtkRadiant is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. GtkRadiant is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GtkRadiant; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. //===========================================================================
  18. //
  19. // Name: l_net_wins.c
  20. // Function: WinSock
  21. // Programmer: MrElusive
  22. // Last update: -
  23. // Tab Size: 3
  24. // Notes:
  25. //===========================================================================
  26. #include <windows.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "l_net.h"
  31. #include "l_net_wins.h"
  32. //#include <winsock.h>
  33. //#include "mpdosock.h"
  34. #define WinError WinPrint
  35. typedef struct tag_error_struct
  36. {
  37. int errnum;
  38. LPSTR errstr;
  39. } ERROR_STRUCT;
  40. #define NET_NAMELEN 64
  41. char my_tcpip_address[NET_NAMELEN];
  42. const int DEFAULTnet_hostport = 26000;
  43. const int MAXHOSTNAMELEN = 256;
  44. static int net_acceptsocket = -1; // socket for fielding new connections
  45. static int net_controlsocket;
  46. static int net_hostport; // udp port number for acceptsocket
  47. static int net_broadcastsocket = 0;
  48. //static qboolean ifbcastinit = qfalse;
  49. static struct sockaddr_s broadcastaddr;
  50. static unsigned long myAddr;
  51. WSADATA winsockdata;
  52. ERROR_STRUCT errlist[] = {
  53. {WSAEINTR, "WSAEINTR - Interrupted"},
  54. {WSAEBADF, "WSAEBADF - Bad file number"},
  55. {WSAEFAULT, "WSAEFAULT - Bad address"},
  56. {WSAEINVAL, "WSAEINVAL - Invalid argument"},
  57. {WSAEMFILE, "WSAEMFILE - Too many open files"},
  58. /*
  59. * Windows Sockets definitions of regular Berkeley error constants
  60. */
  61. {WSAEWOULDBLOCK, "WSAEWOULDBLOCK - Socket marked as non-blocking"},
  62. {WSAEINPROGRESS, "WSAEINPROGRESS - Blocking call in progress"},
  63. {WSAEALREADY, "WSAEALREADY - Command already completed"},
  64. {WSAENOTSOCK, "WSAENOTSOCK - Descriptor is not a socket"},
  65. {WSAEDESTADDRREQ, "WSAEDESTADDRREQ - Destination address required"},
  66. {WSAEMSGSIZE, "WSAEMSGSIZE - Data size too large"},
  67. {WSAEPROTOTYPE, "WSAEPROTOTYPE - Protocol is of wrong type for this socket"},
  68. {WSAENOPROTOOPT, "WSAENOPROTOOPT - Protocol option not supported for this socket type"},
  69. {WSAEPROTONOSUPPORT, "WSAEPROTONOSUPPORT - Protocol is not supported"},
  70. {WSAESOCKTNOSUPPORT, "WSAESOCKTNOSUPPORT - Socket type not supported by this address family"},
  71. {WSAEOPNOTSUPP, "WSAEOPNOTSUPP - Option not supported"},
  72. {WSAEPFNOSUPPORT, "WSAEPFNOSUPPORT - "},
  73. {WSAEAFNOSUPPORT, "WSAEAFNOSUPPORT - Address family not supported by this protocol"},
  74. {WSAEADDRINUSE, "WSAEADDRINUSE - Address is in use"},
  75. {WSAEADDRNOTAVAIL, "WSAEADDRNOTAVAIL - Address not available from local machine"},
  76. {WSAENETDOWN, "WSAENETDOWN - Network subsystem is down"},
  77. {WSAENETUNREACH, "WSAENETUNREACH - Network cannot be reached"},
  78. {WSAENETRESET, "WSAENETRESET - Connection has been dropped"},
  79. {WSAECONNABORTED, "WSAECONNABORTED - Connection aborted"},
  80. {WSAECONNRESET, "WSAECONNRESET - Connection reset"},
  81. {WSAENOBUFS, "WSAENOBUFS - No buffer space available"},
  82. {WSAEISCONN, "WSAEISCONN - Socket is already connected"},
  83. {WSAENOTCONN, "WSAENOTCONN - Socket is not connected"},
  84. {WSAESHUTDOWN, "WSAESHUTDOWN - Socket has been shut down"},
  85. {WSAETOOMANYREFS, "WSAETOOMANYREFS - Too many references"},
  86. {WSAETIMEDOUT, "WSAETIMEDOUT - Command timed out"},
  87. {WSAECONNREFUSED, "WSAECONNREFUSED - Connection refused"},
  88. {WSAELOOP, "WSAELOOP - "},
  89. {WSAENAMETOOLONG, "WSAENAMETOOLONG - "},
  90. {WSAEHOSTDOWN, "WSAEHOSTDOWN - Host is down"},
  91. {WSAEHOSTUNREACH, "WSAEHOSTUNREACH - "},
  92. {WSAENOTEMPTY, "WSAENOTEMPTY - "},
  93. {WSAEPROCLIM, "WSAEPROCLIM - "},
  94. {WSAEUSERS, "WSAEUSERS - "},
  95. {WSAEDQUOT, "WSAEDQUOT - "},
  96. {WSAESTALE, "WSAESTALE - "},
  97. {WSAEREMOTE, "WSAEREMOTE - "},
  98. /*
  99. * Extended Windows Sockets error constant definitions
  100. */
  101. {WSASYSNOTREADY, "WSASYSNOTREADY - Network subsystem not ready"},
  102. {WSAVERNOTSUPPORTED, "WSAVERNOTSUPPORTED - Version not supported"},
  103. {WSANOTINITIALISED, "WSANOTINITIALISED - WSAStartup() has not been successfully called"},
  104. /*
  105. * Other error constants.
  106. */
  107. {WSAHOST_NOT_FOUND, "WSAHOST_NOT_FOUND - Host not found"},
  108. {WSATRY_AGAIN, "WSATRY_AGAIN - Host not found or SERVERFAIL"},
  109. {WSANO_RECOVERY, "WSANO_RECOVERY - Non-recoverable error"},
  110. {WSANO_DATA, "WSANO_DATA - (or WSANO_ADDRESS) - No data record of requested type"},
  111. {-1, NULL}
  112. };
  113. //===========================================================================
  114. //
  115. // Parameter: -
  116. // Returns: -
  117. // Changes Globals: -
  118. //===========================================================================
  119. const char *WINS_ErrorMessage( int error ){
  120. int search = 0;
  121. if ( !error ) {
  122. return "No error occurred";
  123. }
  124. for ( search = 0; errlist[search].errstr; search++ )
  125. {
  126. if ( error == errlist[search].errnum ) {
  127. return errlist[search].errstr;
  128. }
  129. } //end for
  130. return "Unknown error";
  131. } //end of the function WINS_ErrorMessage
  132. //===========================================================================
  133. //
  134. // Parameter: -
  135. // Returns: -
  136. // Changes Globals: -
  137. //===========================================================================
  138. int WINS_Init( void ){
  139. int i;
  140. struct hostent *local;
  141. char buff[MAXHOSTNAMELEN];
  142. struct sockaddr_s addr;
  143. char *p;
  144. int r;
  145. WORD wVersionRequested;
  146. wVersionRequested = MAKEWORD( 1, 1 );
  147. r = WSAStartup( wVersionRequested, &winsockdata );
  148. if ( r ) {
  149. WinPrint( "Winsock initialization failed.\n" );
  150. return -1;
  151. }
  152. /*
  153. i = COM_CheckParm ("-udpport");
  154. if (i == 0)*/
  155. net_hostport = DEFAULTnet_hostport;
  156. /*
  157. else if (i < com_argc-1)
  158. net_hostport = Q_atoi (com_argv[i+1]);
  159. else
  160. Sys_Error ("WINS_Init: you must specify a number after -udpport");
  161. */
  162. // determine my name & address
  163. gethostname( buff, MAXHOSTNAMELEN );
  164. local = gethostbyname( buff );
  165. if(local && local->h_addr_list && local->h_addr_list[0])
  166. myAddr = *(int *)local->h_addr_list[0];
  167. else
  168. myAddr = inet_addr("127.0.0.1");
  169. // if the quake hostname isn't set, set it to the machine name
  170. // if (Q_strcmp(hostname.string, "UNNAMED") == 0)
  171. {
  172. // see if it's a text IP address (well, close enough)
  173. for ( p = buff; *p; p++ )
  174. if ( ( *p < '0' || *p > '9' ) && *p != '.' ) {
  175. break;
  176. }
  177. // if it is a real name, strip off the domain; we only want the host
  178. if ( *p ) {
  179. for ( i = 0; i < 15; i++ )
  180. if ( buff[i] == '.' ) {
  181. break;
  182. }
  183. buff[i] = 0;
  184. }
  185. // Cvar_Set ("hostname", buff);
  186. }
  187. if ( ( net_controlsocket = WINS_OpenSocket( 0 ) ) == -1 ) {
  188. WinError( "WINS_Init: Unable to open control socket\n" );
  189. }
  190. ( (struct sockaddr_in *)&broadcastaddr )->sin_family = AF_INET;
  191. ( (struct sockaddr_in *)&broadcastaddr )->sin_addr.s_addr = INADDR_BROADCAST;
  192. ( (struct sockaddr_in *)&broadcastaddr )->sin_port = htons( (u_short)net_hostport );
  193. WINS_GetSocketAddr( net_controlsocket, &addr );
  194. strcpy( my_tcpip_address, WINS_AddrToString( &addr ) );
  195. p = strrchr( my_tcpip_address, ':' );
  196. if ( p ) {
  197. *p = 0;
  198. }
  199. WinPrint( "Winsock Initialized\n" );
  200. return net_controlsocket;
  201. } //end of the function WINS_Init
  202. //===========================================================================
  203. //
  204. // Parameter: -
  205. // Returns: -
  206. // Changes Globals: -
  207. //===========================================================================
  208. char *WINS_MyAddress( void ){
  209. return my_tcpip_address;
  210. } //end of the function WINS_MyAddress
  211. //===========================================================================
  212. //
  213. // Parameter: -
  214. // Returns: -
  215. // Changes Globals: -
  216. //===========================================================================
  217. void WINS_Shutdown( void ){
  218. //WINS_Listen(0);
  219. WINS_CloseSocket( net_controlsocket );
  220. WSACleanup();
  221. //
  222. //WinPrint("Winsock Shutdown\n");
  223. } //end of the function WINS_Shutdown
  224. //===========================================================================
  225. //
  226. // Parameter: -
  227. // Returns: -
  228. // Changes Globals: -
  229. //===========================================================================
  230. /*
  231. void WINS_Listen(int state)
  232. {
  233. // enable listening
  234. if (state)
  235. {
  236. if (net_acceptsocket != -1)
  237. return;
  238. if ((net_acceptsocket = WINS_OpenSocket (net_hostport)) == -1)
  239. WinError ("WINS_Listen: Unable to open accept socket\n");
  240. return;
  241. }
  242. // disable listening
  243. if (net_acceptsocket == -1)
  244. return;
  245. WINS_CloseSocket (net_acceptsocket);
  246. net_acceptsocket = -1;
  247. } //end of the function WINS_Listen*/
  248. //===========================================================================
  249. //
  250. // Parameter: -
  251. // Returns: -
  252. // Changes Globals: -
  253. //===========================================================================
  254. int WINS_OpenSocket( int port ){
  255. int newsocket;
  256. struct sockaddr_in address;
  257. u_long _true = 1;
  258. if ( ( newsocket = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) == -1 ) {
  259. WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  260. return -1;
  261. } //end if
  262. if ( ioctlsocket( newsocket, FIONBIO, &_true ) == -1 ) {
  263. WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  264. closesocket( newsocket );
  265. return -1;
  266. } //end if
  267. memset( (char *) &address, 0, sizeof( address ) );
  268. address.sin_family = AF_INET;
  269. address.sin_addr.s_addr = INADDR_ANY;
  270. address.sin_port = htons( (u_short)port );
  271. if ( bind( newsocket, (void *)&address, sizeof( address ) ) == -1 ) {
  272. WinPrint( "WINS_OpenSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  273. closesocket( newsocket );
  274. return -1;
  275. } //end if
  276. return newsocket;
  277. } //end of the function WINS_OpenSocket
  278. //===========================================================================
  279. //
  280. // Parameter: -
  281. // Returns: -
  282. // Changes Globals: -
  283. //===========================================================================
  284. int WINS_OpenReliableSocket( int port ){
  285. int newsocket;
  286. struct sockaddr_in address;
  287. BOOL _true = 0xFFFFFFFF;
  288. //IPPROTO_TCP
  289. //
  290. if ( ( newsocket = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) {
  291. WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  292. return -1;
  293. } //end if
  294. memset( (char *) &address, 0, sizeof( address ) );
  295. address.sin_family = AF_INET;
  296. address.sin_addr.s_addr = htonl( INADDR_ANY );
  297. address.sin_port = htons( (u_short)port );
  298. if ( bind( newsocket, (void *)&address, sizeof( address ) ) == -1 ) {
  299. WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  300. closesocket( newsocket );
  301. return -1;
  302. } //end if
  303. //
  304. if ( setsockopt( newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof( int ) ) == SOCKET_ERROR ) {
  305. WinPrint( "WINS_OpenReliableSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  306. WinPrint( "setsockopt error\n" );
  307. } //end if
  308. return newsocket;
  309. } //end of the function WINS_OpenReliableSocket
  310. //===========================================================================
  311. //
  312. // Parameter: -
  313. // Returns: -
  314. // Changes Globals: -
  315. //===========================================================================
  316. int WINS_Listen( int socket ){
  317. u_long _true = 1;
  318. if ( ioctlsocket( socket, FIONBIO, &_true ) == -1 ) {
  319. WinPrint( "WINS_Listen: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  320. return -1;
  321. } //end if
  322. if ( listen( socket, SOMAXCONN ) == SOCKET_ERROR ) {
  323. WinPrint( "WINS_Listen: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  324. return -1;
  325. } //end if
  326. return 0;
  327. } //end of the function WINS_Listen
  328. //===========================================================================
  329. //
  330. // Parameter: -
  331. // Returns: -
  332. // Changes Globals: -
  333. //===========================================================================
  334. int WINS_Accept( int socket, struct sockaddr_s *addr ){
  335. int addrlen = sizeof( struct sockaddr_s );
  336. int newsocket;
  337. BOOL _true = 1;
  338. newsocket = accept( socket, (struct sockaddr *)addr, &addrlen );
  339. if ( newsocket == INVALID_SOCKET ) {
  340. if ( WSAGetLastError() == WSAEWOULDBLOCK ) {
  341. return -1;
  342. }
  343. WinPrint( "WINS_Accept: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  344. return -1;
  345. } //end if
  346. //
  347. if ( setsockopt( newsocket, IPPROTO_TCP, TCP_NODELAY, (void *) &_true, sizeof( int ) ) == SOCKET_ERROR ) {
  348. WinPrint( "WINS_Accept: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  349. WinPrint( "setsockopt error\n" );
  350. } //end if
  351. return newsocket;
  352. } //end of the function WINS_Accept
  353. //===========================================================================
  354. //
  355. // Parameter: -
  356. // Returns: -
  357. // Changes Globals: -
  358. //===========================================================================
  359. int WINS_CloseSocket( int socket ){
  360. /*
  361. if (socket == net_broadcastsocket)
  362. net_broadcastsocket = 0;
  363. */
  364. // shutdown(socket, SD_SEND);
  365. if ( closesocket( socket ) == SOCKET_ERROR ) {
  366. WinPrint( "WINS_CloseSocket: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  367. return SOCKET_ERROR;
  368. } //end if
  369. return 0;
  370. } //end of the function WINS_CloseSocket
  371. //===========================================================================
  372. // this lets you type only as much of the net address as required, using
  373. // the local network components to fill in the rest
  374. //
  375. // Parameter: -
  376. // Returns: -
  377. // Changes Globals: -
  378. //===========================================================================
  379. static int PartialIPAddress( char *in, struct sockaddr_s *hostaddr ){
  380. char buff[256];
  381. char *b;
  382. int addr;
  383. int num;
  384. int mask;
  385. buff[0] = '.';
  386. b = buff;
  387. strcpy( buff + 1, in );
  388. if ( buff[1] == '.' ) {
  389. b++;
  390. }
  391. addr = 0;
  392. mask = -1;
  393. while ( *b == '.' )
  394. {
  395. num = 0;
  396. if ( *++b < '0' || *b > '9' ) {
  397. return -1;
  398. }
  399. while ( !( *b < '0' || *b > '9' ) )
  400. num = num * 10 + *( b++ ) - '0';
  401. mask <<= 8;
  402. addr = ( addr << 8 ) + num;
  403. }
  404. hostaddr->sa_family = AF_INET;
  405. ( (struct sockaddr_in *)hostaddr )->sin_port = htons( (u_short)net_hostport );
  406. ( (struct sockaddr_in *)hostaddr )->sin_addr.s_addr = ( myAddr & htonl( mask ) ) | htonl( addr );
  407. return 0;
  408. } //end of the function PartialIPAddress
  409. //===========================================================================
  410. //
  411. // Parameter: -
  412. // Returns: -
  413. // Changes Globals: -
  414. //===========================================================================
  415. int WINS_Connect( int socket, struct sockaddr_s *addr ){
  416. int ret;
  417. u_long _true2 = 0xFFFFFFFF;
  418. ret = connect( socket, (struct sockaddr *)addr, sizeof( struct sockaddr_s ) );
  419. if ( ret == SOCKET_ERROR ) {
  420. WinPrint( "WINS_Connect: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  421. return -1;
  422. } //end if
  423. if ( ioctlsocket( socket, FIONBIO, &_true2 ) == -1 ) {
  424. WinPrint( "WINS_Connect: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  425. return -1;
  426. } //end if
  427. return 0;
  428. } //end of the function WINS_Connect
  429. //===========================================================================
  430. //
  431. // Parameter: -
  432. // Returns: -
  433. // Changes Globals: -
  434. //===========================================================================
  435. int WINS_CheckNewConnections( void ){
  436. char buf[4];
  437. if ( net_acceptsocket == -1 ) {
  438. return -1;
  439. }
  440. if ( recvfrom( net_acceptsocket, buf, 4, MSG_PEEK, NULL, NULL ) > 0 ) {
  441. return net_acceptsocket;
  442. }
  443. return -1;
  444. } //end of the function WINS_CheckNewConnections
  445. //===========================================================================
  446. // returns the number of bytes read
  447. // 0 if no bytes available
  448. // -1 on failure
  449. //
  450. // Parameter: -
  451. // Returns: -
  452. // Changes Globals: -
  453. //===========================================================================
  454. int WINS_Read( int socket, byte *buf, int len, struct sockaddr_s *addr ){
  455. int addrlen = sizeof( struct sockaddr_s );
  456. int ret, errno;
  457. if ( addr ) {
  458. ret = recvfrom( socket, buf, len, 0, (struct sockaddr *)addr, &addrlen );
  459. if ( ret == -1 ) {
  460. errno = WSAGetLastError();
  461. if ( errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED ) {
  462. return 0;
  463. }
  464. } //end if
  465. } //end if
  466. else
  467. {
  468. ret = recv( socket, buf, len, 0 );
  469. if ( ret == SOCKET_ERROR ) {
  470. errno = WSAGetLastError();
  471. if ( errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED ) {
  472. return 0;
  473. }
  474. } //end if
  475. } //end else
  476. if ( ret == SOCKET_ERROR ) {
  477. WinPrint( "WINS_Read: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  478. } //end if
  479. return ret;
  480. } //end of the function WINS_Read
  481. //===========================================================================
  482. //
  483. // Parameter: -
  484. // Returns: -
  485. // Changes Globals: -
  486. //===========================================================================
  487. int WINS_MakeSocketBroadcastCapable( int socket ){
  488. int i = 1;
  489. // make this socket broadcast capable
  490. if ( setsockopt( socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof( i ) ) < 0 ) {
  491. return -1;
  492. }
  493. net_broadcastsocket = socket;
  494. return 0;
  495. } //end of the function WINS_MakeSocketBroadcastCapable
  496. //===========================================================================
  497. //
  498. // Parameter: -
  499. // Returns: -
  500. // Changes Globals: -
  501. //===========================================================================
  502. int WINS_Broadcast( int socket, byte *buf, int len ){
  503. int ret;
  504. if ( socket != net_broadcastsocket ) {
  505. if ( net_broadcastsocket != 0 ) {
  506. WinError( "Attempted to use multiple broadcasts sockets\n" );
  507. }
  508. ret = WINS_MakeSocketBroadcastCapable( socket );
  509. if ( ret == -1 ) {
  510. WinPrint( "Unable to make socket broadcast capable\n" );
  511. return ret;
  512. }
  513. }
  514. return WINS_Write( socket, buf, len, &broadcastaddr );
  515. } //end of the function WINS_Broadcast
  516. //===========================================================================
  517. // returns qtrue on success or qfalse on failure
  518. //
  519. // Parameter: -
  520. // Returns: -
  521. // Changes Globals: -
  522. //===========================================================================
  523. int WINS_Write( int socket, byte *buf, int len, struct sockaddr_s *addr ){
  524. int ret, written;
  525. if ( addr ) {
  526. written = 0;
  527. while ( written < len )
  528. {
  529. ret = sendto( socket, &buf[written], len - written, 0, (struct sockaddr *)addr, sizeof( struct sockaddr_s ) );
  530. if ( ret == SOCKET_ERROR ) {
  531. if ( WSAGetLastError() != WSAEWOULDBLOCK ) {
  532. return qfalse;
  533. }
  534. Sleep( 1000 );
  535. } //end if
  536. else
  537. {
  538. written += ret;
  539. }
  540. }
  541. } //end if
  542. else
  543. {
  544. written = 0;
  545. while ( written < len )
  546. {
  547. ret = send( socket, buf, len, 0 );
  548. if ( ret == SOCKET_ERROR ) {
  549. if ( WSAGetLastError() != WSAEWOULDBLOCK ) {
  550. return qfalse;
  551. }
  552. Sleep( 1000 );
  553. } //end if
  554. else
  555. {
  556. written += ret;
  557. }
  558. }
  559. } //end else
  560. if ( ret == SOCKET_ERROR ) {
  561. WinPrint( "WINS_Write: %s\n", WINS_ErrorMessage( WSAGetLastError() ) );
  562. } //end if
  563. return ( ret == len );
  564. } //end of the function WINS_Write
  565. //===========================================================================
  566. //
  567. // Parameter: -
  568. // Returns: -
  569. // Changes Globals: -
  570. //===========================================================================
  571. char *WINS_AddrToString( struct sockaddr_s *addr ){
  572. static char buffer[22];
  573. int haddr;
  574. haddr = ntohl( ( (struct sockaddr_in *)addr )->sin_addr.s_addr );
  575. 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 ) );
  576. return buffer;
  577. } //end of the function WINS_AddrToString
  578. //===========================================================================
  579. //
  580. // Parameter: -
  581. // Returns: -
  582. // Changes Globals: -
  583. //===========================================================================
  584. int WINS_StringToAddr( char *string, struct sockaddr_s *addr ){
  585. int ha1, ha2, ha3, ha4, hp;
  586. int ipaddr;
  587. sscanf( string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp );
  588. ipaddr = ( ha1 << 24 ) | ( ha2 << 16 ) | ( ha3 << 8 ) | ha4;
  589. addr->sa_family = AF_INET;
  590. ( (struct sockaddr_in *)addr )->sin_addr.s_addr = htonl( ipaddr );
  591. ( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)hp );
  592. return 0;
  593. } //end of the function WINS_StringToAddr
  594. //===========================================================================
  595. //
  596. // Parameter: -
  597. // Returns: -
  598. // Changes Globals: -
  599. //===========================================================================
  600. int WINS_GetSocketAddr( int socket, struct sockaddr_s *addr ){
  601. int addrlen = sizeof( struct sockaddr_s );
  602. unsigned int a;
  603. memset( addr, 0, sizeof( struct sockaddr_s ) );
  604. getsockname( socket, (struct sockaddr *)addr, &addrlen );
  605. a = ( (struct sockaddr_in *)addr )->sin_addr.s_addr;
  606. if ( a == 0 || a == inet_addr( "127.0.0.1" ) ) {
  607. ( (struct sockaddr_in *)addr )->sin_addr.s_addr = myAddr;
  608. }
  609. return 0;
  610. } //end of the function WINS_GetSocketAddr
  611. //===========================================================================
  612. //
  613. // Parameter: -
  614. // Returns: -
  615. // Changes Globals: -
  616. //===========================================================================
  617. int WINS_GetNameFromAddr( struct sockaddr_s *addr, char *name ){
  618. struct hostent *hostentry;
  619. hostentry = gethostbyaddr( (char *)&( (struct sockaddr_in *)addr )->sin_addr, sizeof( struct in_addr ), AF_INET );
  620. if ( hostentry ) {
  621. strncpy( name, (char *)hostentry->h_name, NET_NAMELEN - 1 );
  622. return 0;
  623. }
  624. strcpy( name, WINS_AddrToString( addr ) );
  625. return 0;
  626. } //end of the function WINS_GetNameFromAddr
  627. //===========================================================================
  628. //
  629. // Parameter: -
  630. // Returns: -
  631. // Changes Globals: -
  632. //===========================================================================
  633. int WINS_GetAddrFromName( char *name, struct sockaddr_s *addr ){
  634. struct hostent *hostentry;
  635. if ( name[0] >= '0' && name[0] <= '9' ) {
  636. return PartialIPAddress( name, addr );
  637. }
  638. hostentry = gethostbyname( name );
  639. if ( !hostentry ) {
  640. return -1;
  641. }
  642. addr->sa_family = AF_INET;
  643. ( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)net_hostport );
  644. ( (struct sockaddr_in *)addr )->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
  645. return 0;
  646. } //end of the function WINS_GetAddrFromName
  647. //===========================================================================
  648. //
  649. // Parameter: -
  650. // Returns: -
  651. // Changes Globals: -
  652. //===========================================================================
  653. int WINS_AddrCompare( struct sockaddr_s *addr1, struct sockaddr_s *addr2 ){
  654. if ( addr1->sa_family != addr2->sa_family ) {
  655. return -1;
  656. }
  657. if ( ( (struct sockaddr_in *)addr1 )->sin_addr.s_addr != ( (struct sockaddr_in *)addr2 )->sin_addr.s_addr ) {
  658. return -1;
  659. }
  660. if ( ( (struct sockaddr_in *)addr1 )->sin_port != ( (struct sockaddr_in *)addr2 )->sin_port ) {
  661. return 1;
  662. }
  663. return 0;
  664. } //end of the function WINS_AddrCompare
  665. //===========================================================================
  666. //
  667. // Parameter: -
  668. // Returns: -
  669. // Changes Globals: -
  670. //===========================================================================
  671. int WINS_GetSocketPort( struct sockaddr_s *addr ){
  672. return ntohs( ( (struct sockaddr_in *)addr )->sin_port );
  673. } //end of the function WINS_GetSocketPort
  674. //===========================================================================
  675. //
  676. // Parameter: -
  677. // Returns: -
  678. // Changes Globals: -
  679. //===========================================================================
  680. int WINS_SetSocketPort( struct sockaddr_s *addr, int port ){
  681. ( (struct sockaddr_in *)addr )->sin_port = htons( (u_short)port );
  682. return 0;
  683. } //end of the function WINS_SetSocketPort