net_mp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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_mpath.c
  16. #include <dpmi.h>
  17. #include "quakedef.h"
  18. #include "mpdosock.h"
  19. short flat_selector;
  20. int WSAGetLastError(void);
  21. void sockets_flush(void);
  22. extern cvar_t hostname;
  23. #define MAXHOSTNAMELEN 256
  24. static int net_acceptsocket = -1; // socket for fielding new connections
  25. static int net_controlsocket;
  26. static int net_broadcastsocket = 0;
  27. //static qboolean ifbcastinit = false;
  28. static struct qsockaddr broadcastaddr;
  29. static unsigned long myAddr;
  30. #include "net_mp.h"
  31. //=============================================================================
  32. int MPATH_Init (void)
  33. {
  34. int i;
  35. struct hostent *local = NULL;
  36. char buff[MAXHOSTNAMELEN];
  37. struct qsockaddr addr;
  38. char *p;
  39. if (COM_CheckParm ("-mpath") == 0)
  40. return -1;
  41. flat_selector = __dpmi_allocate_ldt_descriptors(1);
  42. if (flat_selector == -1) {
  43. Con_Printf("MPATH_Init: Can't get flat selector\n");
  44. return -1;
  45. }
  46. if (__dpmi_set_segment_base_address(flat_selector, 0) == -1) {
  47. Con_Printf("MPATH_Init: Can't seg flat base!\n");
  48. return -1;
  49. }
  50. if (__dpmi_set_segment_limit(flat_selector, 0xffffffff) == -1) {
  51. Con_Printf("MPATH_Init: Can't set segment limit\n");
  52. return -1;
  53. }
  54. // determine my name & address
  55. if (gethostname(buff, MAXHOSTNAMELEN) == 0)
  56. local = gethostbyname(buff);
  57. if (local)
  58. {
  59. myAddr = *(int *)local->h_addr_list[0];
  60. // if the quake hostname isn't set, set it to the machine name
  61. if (Q_strcmp(hostname.string, "UNNAMED") == 0)
  62. {
  63. // see if it's a text IP address (well, close enough)
  64. for (p = buff; *p; p++)
  65. if ((*p < '0' || *p > '9') && *p != '.')
  66. break;
  67. // if it is a real name, strip off the domain; we only want the host
  68. if (*p)
  69. {
  70. for (i = 0; i < 15; i++)
  71. if (buff[i] == '.')
  72. break;
  73. buff[i] = 0;
  74. }
  75. Cvar_Set ("hostname", buff);
  76. }
  77. }
  78. if ((net_controlsocket = MPATH_OpenSocket (0)) == -1)
  79. Sys_Error("MPATH_Init: Unable to open control socket\n");
  80. ((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET;
  81. ((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = INADDR_BROADCAST;
  82. ((struct sockaddr_in *)&broadcastaddr)->sin_port = htons(net_hostport);
  83. MPATH_GetSocketAddr (net_controlsocket, &addr);
  84. Q_strcpy(my_tcpip_address, MPATH_AddrToString (&addr));
  85. p = Q_strrchr (my_tcpip_address, ':');
  86. if (p)
  87. *p = 0;
  88. Con_Printf("MPath Initialized\n");
  89. tcpipAvailable = true;
  90. return net_controlsocket;
  91. }
  92. //=============================================================================
  93. void MPATH_Shutdown (void)
  94. {
  95. MPATH_Listen (false);
  96. MPATH_CloseSocket (net_controlsocket);
  97. }
  98. //=============================================================================
  99. void MPATH_Listen (qboolean state)
  100. {
  101. // enable listening
  102. if (state)
  103. {
  104. if (net_acceptsocket != -1)
  105. return;
  106. if ((net_acceptsocket = MPATH_OpenSocket (net_hostport)) == -1)
  107. Sys_Error ("MPATH_Listen: Unable to open accept socket\n");
  108. return;
  109. }
  110. // disable listening
  111. if (net_acceptsocket == -1)
  112. return;
  113. MPATH_CloseSocket (net_acceptsocket);
  114. net_acceptsocket = -1;
  115. }
  116. //=============================================================================
  117. int MPATH_OpenSocket (int port)
  118. {
  119. int newsocket;
  120. struct sockaddr_in address;
  121. u_long _true = 1;
  122. if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  123. return -1;
  124. if (ioctlsocket (newsocket, FIONBIO, &_true) == -1)
  125. goto ErrorReturn;
  126. address.sin_family = AF_INET;
  127. address.sin_addr.s_addr = INADDR_ANY;
  128. address.sin_port = htons(port);
  129. if( bind (newsocket, (void *)&address, sizeof(address)) == -1)
  130. goto ErrorReturn;
  131. return newsocket;
  132. ErrorReturn:
  133. closesocket (newsocket);
  134. return -1;
  135. }
  136. //=============================================================================
  137. int MPATH_CloseSocket (int socket)
  138. {
  139. if (socket == net_broadcastsocket)
  140. net_broadcastsocket = 0;
  141. return closesocket (socket);
  142. }
  143. //=============================================================================
  144. /*
  145. ============
  146. PartialIPAddress
  147. this lets you type only as much of the net address as required, using
  148. the local network components to fill in the rest
  149. ============
  150. */
  151. static int PartialIPAddress (char *in, struct qsockaddr *hostaddr)
  152. {
  153. char buff[256];
  154. char *b;
  155. int addr;
  156. int num;
  157. int mask;
  158. int run;
  159. int port;
  160. buff[0] = '.';
  161. b = buff;
  162. strcpy(buff+1, in);
  163. if (buff[1] == '.')
  164. b++;
  165. addr = 0;
  166. mask=-1;
  167. while (*b == '.')
  168. {
  169. b++;
  170. num = 0;
  171. run = 0;
  172. while (!( *b < '0' || *b > '9'))
  173. {
  174. num = num*10 + *b++ - '0';
  175. if (++run > 3)
  176. return -1;
  177. }
  178. if ((*b < '0' || *b > '9') && *b != '.' && *b != ':' && *b != 0)
  179. return -1;
  180. if (num < 0 || num > 255)
  181. return -1;
  182. mask<<=8;
  183. addr = (addr<<8) + num;
  184. }
  185. if (*b++ == ':')
  186. port = Q_atoi(b);
  187. else
  188. port = net_hostport;
  189. hostaddr->sa_family = AF_INET;
  190. ((struct sockaddr_in *)hostaddr)->sin_port = htons((short)port);
  191. ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr = (myAddr & htonl(mask)) | htonl(addr);
  192. return 0;
  193. }
  194. //=============================================================================
  195. int MPATH_Connect (int socket, struct qsockaddr *addr)
  196. {
  197. return 0;
  198. }
  199. //=============================================================================
  200. int MPATH_CheckNewConnections (void)
  201. {
  202. char buf[4];
  203. if (net_acceptsocket == -1)
  204. return -1;
  205. if (recvfrom (net_acceptsocket, buf, 4, MSG_PEEK, NULL, NULL) > 0)
  206. return net_acceptsocket;
  207. return -1;
  208. }
  209. //=============================================================================
  210. int MPATH_Read (int socket, byte *buf, int len, struct qsockaddr *addr)
  211. {
  212. int addrlen = sizeof (struct qsockaddr);
  213. int ret;
  214. ret = recvfrom (socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
  215. if (ret == -1)
  216. {
  217. int errno = WSAGetLastError();
  218. if (errno == WSAEWOULDBLOCK || errno == WSAECONNREFUSED)
  219. return 0;
  220. }
  221. return ret;
  222. }
  223. //=============================================================================
  224. int MPATH_MakeSocketBroadcastCapable (int socket)
  225. {
  226. int i = 1;
  227. // make this socket broadcast capable
  228. if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0)
  229. return -1;
  230. net_broadcastsocket = socket;
  231. return 0;
  232. }
  233. //=============================================================================
  234. int MPATH_Broadcast (int socket, byte *buf, int len)
  235. {
  236. int ret;
  237. if (socket != net_broadcastsocket)
  238. {
  239. if (net_broadcastsocket != 0)
  240. Sys_Error("Attempted to use multiple broadcasts sockets\n");
  241. ret = MPATH_MakeSocketBroadcastCapable (socket);
  242. if (ret == -1)
  243. {
  244. Con_Printf("Unable to make socket broadcast capable\n");
  245. return ret;
  246. }
  247. }
  248. return MPATH_Write (socket, buf, len, &broadcastaddr);
  249. }
  250. //=============================================================================
  251. int MPATH_Write (int socket, byte *buf, int len, struct qsockaddr *addr)
  252. {
  253. int ret;
  254. ret = sendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
  255. if (ret == -1)
  256. if (WSAGetLastError() == WSAEWOULDBLOCK)
  257. return 0;
  258. sockets_flush();
  259. return ret;
  260. }
  261. //=============================================================================
  262. char *MPATH_AddrToString (struct qsockaddr *addr)
  263. {
  264. static char buffer[22];
  265. int haddr;
  266. haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr);
  267. 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));
  268. return buffer;
  269. }
  270. //=============================================================================
  271. int MPATH_StringToAddr (char *string, struct qsockaddr *addr)
  272. {
  273. int ha1, ha2, ha3, ha4, hp;
  274. int ipaddr;
  275. sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
  276. ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
  277. addr->sa_family = AF_INET;
  278. ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
  279. ((struct sockaddr_in *)addr)->sin_port = htons(hp);
  280. return 0;
  281. }
  282. //=============================================================================
  283. int MPATH_GetSocketAddr (int socket, struct qsockaddr *addr)
  284. {
  285. int addrlen = sizeof(struct qsockaddr);
  286. unsigned int a;
  287. Q_memset(addr, 0, sizeof(struct qsockaddr));
  288. getsockname(socket, (struct sockaddr *)addr, &addrlen);
  289. a = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  290. if (a == 0 || a == inet_addr("127.0.0.1"))
  291. ((struct sockaddr_in *)addr)->sin_addr.s_addr = myAddr;
  292. return 0;
  293. }
  294. //=============================================================================
  295. int MPATH_GetNameFromAddr (struct qsockaddr *addr, char *name)
  296. {
  297. struct hostent *hostentry;
  298. hostentry = gethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
  299. if (hostentry)
  300. {
  301. Q_strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
  302. return 0;
  303. }
  304. Q_strcpy (name, MPATH_AddrToString (addr));
  305. return 0;
  306. }
  307. //=============================================================================
  308. int MPATH_GetAddrFromName(char *name, struct qsockaddr *addr)
  309. {
  310. struct hostent *hostentry;
  311. if (name[0] >= '0' && name[0] <= '9')
  312. return PartialIPAddress (name, addr);
  313. hostentry = gethostbyname (name);
  314. if (!hostentry)
  315. return -1;
  316. addr->sa_family = AF_INET;
  317. ((struct sockaddr_in *)addr)->sin_port = htons(net_hostport);
  318. ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
  319. return 0;
  320. }
  321. //=============================================================================
  322. int MPATH_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2)
  323. {
  324. if (addr1->sa_family != addr2->sa_family)
  325. return -1;
  326. if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr)
  327. return -1;
  328. if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port)
  329. return 1;
  330. return 0;
  331. }
  332. //=============================================================================
  333. int MPATH_GetSocketPort (struct qsockaddr *addr)
  334. {
  335. return ntohs(((struct sockaddr_in *)addr)->sin_port);
  336. }
  337. int MPATH_SetSocketPort (struct qsockaddr *addr, int port)
  338. {
  339. ((struct sockaddr_in *)addr)->sin_port = htons(port);
  340. return 0;
  341. }
  342. //=============================================================================