net_udp.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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_udp.c
  16. #include "quakedef.h"
  17. #include <sys/types.h>
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #include <netdb.h>
  21. #include <sys/param.h>
  22. #include <sys/ioctl.h>
  23. #include <errno.h>
  24. #ifdef __sun__
  25. #include <sys/filio.h>
  26. #endif
  27. #ifdef NeXT
  28. #include <libc.h>
  29. #endif
  30. extern int gethostname (char *, int);
  31. extern int close (int);
  32. extern cvar_t hostname;
  33. static int net_acceptsocket = -1; // socket for fielding new connections
  34. static int net_controlsocket;
  35. static int net_broadcastsocket = 0;
  36. static struct qsockaddr broadcastaddr;
  37. static unsigned long myAddr;
  38. #include "net_udp.h"
  39. //=============================================================================
  40. int UDP_Init (void)
  41. {
  42. struct hostent *local;
  43. char buff[MAXHOSTNAMELEN];
  44. struct qsockaddr addr;
  45. char *colon;
  46. if (COM_CheckParm ("-noudp"))
  47. return -1;
  48. // determine my name & address
  49. gethostname(buff, MAXHOSTNAMELEN);
  50. local = gethostbyname(buff);
  51. myAddr = *(int *)local->h_addr_list[0];
  52. // if the quake hostname isn't set, set it to the machine name
  53. if (Q_strcmp(hostname.string, "UNNAMED") == 0)
  54. {
  55. buff[15] = 0;
  56. Cvar_Set ("hostname", buff);
  57. }
  58. if ((net_controlsocket = UDP_OpenSocket (0)) == -1)
  59. Sys_Error("UDP_Init: Unable to open control socket\n");
  60. ((struct sockaddr_in *)&broadcastaddr)->sin_family = AF_INET;
  61. ((struct sockaddr_in *)&broadcastaddr)->sin_addr.s_addr = INADDR_BROADCAST;
  62. ((struct sockaddr_in *)&broadcastaddr)->sin_port = htons(net_hostport);
  63. UDP_GetSocketAddr (net_controlsocket, &addr);
  64. Q_strcpy(my_tcpip_address, UDP_AddrToString (&addr));
  65. colon = Q_strrchr (my_tcpip_address, ':');
  66. if (colon)
  67. *colon = 0;
  68. Con_Printf("UDP Initialized\n");
  69. tcpipAvailable = true;
  70. return net_controlsocket;
  71. }
  72. //=============================================================================
  73. void UDP_Shutdown (void)
  74. {
  75. UDP_Listen (false);
  76. UDP_CloseSocket (net_controlsocket);
  77. }
  78. //=============================================================================
  79. void UDP_Listen (qboolean state)
  80. {
  81. // enable listening
  82. if (state)
  83. {
  84. if (net_acceptsocket != -1)
  85. return;
  86. if ((net_acceptsocket = UDP_OpenSocket (net_hostport)) == -1)
  87. Sys_Error ("UDP_Listen: Unable to open accept socket\n");
  88. return;
  89. }
  90. // disable listening
  91. if (net_acceptsocket == -1)
  92. return;
  93. UDP_CloseSocket (net_acceptsocket);
  94. net_acceptsocket = -1;
  95. }
  96. //=============================================================================
  97. int UDP_OpenSocket (int port)
  98. {
  99. int newsocket;
  100. struct sockaddr_in address;
  101. qboolean _true = true;
  102. if ((newsocket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  103. return -1;
  104. if (ioctl (newsocket, FIONBIO, (char *)&_true) == -1)
  105. goto ErrorReturn;
  106. address.sin_family = AF_INET;
  107. address.sin_addr.s_addr = INADDR_ANY;
  108. address.sin_port = htons(port);
  109. if( bind (newsocket, (void *)&address, sizeof(address)) == -1)
  110. goto ErrorReturn;
  111. return newsocket;
  112. ErrorReturn:
  113. close (newsocket);
  114. return -1;
  115. }
  116. //=============================================================================
  117. int UDP_CloseSocket (int socket)
  118. {
  119. if (socket == net_broadcastsocket)
  120. net_broadcastsocket = 0;
  121. return close (socket);
  122. }
  123. //=============================================================================
  124. /*
  125. ============
  126. PartialIPAddress
  127. this lets you type only as much of the net address as required, using
  128. the local network components to fill in the rest
  129. ============
  130. */
  131. static int PartialIPAddress (char *in, struct qsockaddr *hostaddr)
  132. {
  133. char buff[256];
  134. char *b;
  135. int addr;
  136. int num;
  137. int mask;
  138. int run;
  139. int port;
  140. buff[0] = '.';
  141. b = buff;
  142. strcpy(buff+1, in);
  143. if (buff[1] == '.')
  144. b++;
  145. addr = 0;
  146. mask=-1;
  147. while (*b == '.')
  148. {
  149. b++;
  150. num = 0;
  151. run = 0;
  152. while (!( *b < '0' || *b > '9'))
  153. {
  154. num = num*10 + *b++ - '0';
  155. if (++run > 3)
  156. return -1;
  157. }
  158. if ((*b < '0' || *b > '9') && *b != '.' && *b != ':' && *b != 0)
  159. return -1;
  160. if (num < 0 || num > 255)
  161. return -1;
  162. mask<<=8;
  163. addr = (addr<<8) + num;
  164. }
  165. if (*b++ == ':')
  166. port = Q_atoi(b);
  167. else
  168. port = net_hostport;
  169. hostaddr->sa_family = AF_INET;
  170. ((struct sockaddr_in *)hostaddr)->sin_port = htons((short)port);
  171. ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr = (myAddr & htonl(mask)) | htonl(addr);
  172. return 0;
  173. }
  174. //=============================================================================
  175. int UDP_Connect (int socket, struct qsockaddr *addr)
  176. {
  177. return 0;
  178. }
  179. //=============================================================================
  180. int UDP_CheckNewConnections (void)
  181. {
  182. unsigned long available;
  183. if (net_acceptsocket == -1)
  184. return -1;
  185. if (ioctl (net_acceptsocket, FIONREAD, &available) == -1)
  186. Sys_Error ("UDP: ioctlsocket (FIONREAD) failed\n");
  187. if (available)
  188. return net_acceptsocket;
  189. return -1;
  190. }
  191. //=============================================================================
  192. int UDP_Read (int socket, byte *buf, int len, struct qsockaddr *addr)
  193. {
  194. int addrlen = sizeof (struct qsockaddr);
  195. int ret;
  196. ret = recvfrom (socket, buf, len, 0, (struct sockaddr *)addr, &addrlen);
  197. if (ret == -1 && (errno == EWOULDBLOCK || errno == ECONNREFUSED))
  198. return 0;
  199. return ret;
  200. }
  201. //=============================================================================
  202. int UDP_MakeSocketBroadcastCapable (int socket)
  203. {
  204. int i = 1;
  205. // make this socket broadcast capable
  206. if (setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&i, sizeof(i)) < 0)
  207. return -1;
  208. net_broadcastsocket = socket;
  209. return 0;
  210. }
  211. //=============================================================================
  212. int UDP_Broadcast (int socket, byte *buf, int len)
  213. {
  214. int ret;
  215. if (socket != net_broadcastsocket)
  216. {
  217. if (net_broadcastsocket != 0)
  218. Sys_Error("Attempted to use multiple broadcasts sockets\n");
  219. ret = UDP_MakeSocketBroadcastCapable (socket);
  220. if (ret == -1)
  221. {
  222. Con_Printf("Unable to make socket broadcast capable\n");
  223. return ret;
  224. }
  225. }
  226. return UDP_Write (socket, buf, len, &broadcastaddr);
  227. }
  228. //=============================================================================
  229. int UDP_Write (int socket, byte *buf, int len, struct qsockaddr *addr)
  230. {
  231. int ret;
  232. ret = sendto (socket, buf, len, 0, (struct sockaddr *)addr, sizeof(struct qsockaddr));
  233. if (ret == -1 && errno == EWOULDBLOCK)
  234. return 0;
  235. return ret;
  236. }
  237. //=============================================================================
  238. char *UDP_AddrToString (struct qsockaddr *addr)
  239. {
  240. static char buffer[22];
  241. int haddr;
  242. haddr = ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr);
  243. 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));
  244. return buffer;
  245. }
  246. //=============================================================================
  247. int UDP_StringToAddr (char *string, struct qsockaddr *addr)
  248. {
  249. int ha1, ha2, ha3, ha4, hp;
  250. int ipaddr;
  251. sscanf(string, "%d.%d.%d.%d:%d", &ha1, &ha2, &ha3, &ha4, &hp);
  252. ipaddr = (ha1 << 24) | (ha2 << 16) | (ha3 << 8) | ha4;
  253. addr->sa_family = AF_INET;
  254. ((struct sockaddr_in *)addr)->sin_addr.s_addr = htonl(ipaddr);
  255. ((struct sockaddr_in *)addr)->sin_port = htons(hp);
  256. return 0;
  257. }
  258. //=============================================================================
  259. int UDP_GetSocketAddr (int socket, struct qsockaddr *addr)
  260. {
  261. int addrlen = sizeof(struct qsockaddr);
  262. unsigned int a;
  263. Q_memset(addr, 0, sizeof(struct qsockaddr));
  264. getsockname(socket, (struct sockaddr *)addr, &addrlen);
  265. a = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  266. if (a == 0 || a == inet_addr("127.0.0.1"))
  267. ((struct sockaddr_in *)addr)->sin_addr.s_addr = myAddr;
  268. return 0;
  269. }
  270. //=============================================================================
  271. int UDP_GetNameFromAddr (struct qsockaddr *addr, char *name)
  272. {
  273. struct hostent *hostentry;
  274. hostentry = gethostbyaddr ((char *)&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), AF_INET);
  275. if (hostentry)
  276. {
  277. Q_strncpy (name, (char *)hostentry->h_name, NET_NAMELEN - 1);
  278. return 0;
  279. }
  280. Q_strcpy (name, UDP_AddrToString (addr));
  281. return 0;
  282. }
  283. //=============================================================================
  284. int UDP_GetAddrFromName(char *name, struct qsockaddr *addr)
  285. {
  286. struct hostent *hostentry;
  287. if (name[0] >= '0' && name[0] <= '9')
  288. return PartialIPAddress (name, addr);
  289. hostentry = gethostbyname (name);
  290. if (!hostentry)
  291. return -1;
  292. addr->sa_family = AF_INET;
  293. ((struct sockaddr_in *)addr)->sin_port = htons(net_hostport);
  294. ((struct sockaddr_in *)addr)->sin_addr.s_addr = *(int *)hostentry->h_addr_list[0];
  295. return 0;
  296. }
  297. //=============================================================================
  298. int UDP_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2)
  299. {
  300. if (addr1->sa_family != addr2->sa_family)
  301. return -1;
  302. if (((struct sockaddr_in *)addr1)->sin_addr.s_addr != ((struct sockaddr_in *)addr2)->sin_addr.s_addr)
  303. return -1;
  304. if (((struct sockaddr_in *)addr1)->sin_port != ((struct sockaddr_in *)addr2)->sin_port)
  305. return 1;
  306. return 0;
  307. }
  308. //=============================================================================
  309. int UDP_GetSocketPort (struct qsockaddr *addr)
  310. {
  311. return ntohs(((struct sockaddr_in *)addr)->sin_port);
  312. }
  313. int UDP_SetSocketPort (struct qsockaddr *addr, int port)
  314. {
  315. ((struct sockaddr_in *)addr)->sin_port = htons(port);
  316. return 0;
  317. }
  318. //=============================================================================