unix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /**
  2. @file unix.c
  3. @brief ENet Unix system specific functions
  4. */
  5. #ifndef _WIN32
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <sys/ioctl.h>
  9. #include <sys/time.h>
  10. #include <arpa/inet.h>
  11. #include <netinet/tcp.h>
  12. #include <netdb.h>
  13. #include <unistd.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <time.h>
  17. #define ENET_BUILDING_LIB 1
  18. #include "enet/enet.h"
  19. #ifdef __APPLE__
  20. #ifdef HAS_POLL
  21. #undef HAS_POLL
  22. #endif
  23. #ifndef HAS_FCNTL
  24. #define HAS_FCNTL 1
  25. #endif
  26. #ifndef HAS_INET_PTON
  27. #define HAS_INET_PTON 1
  28. #endif
  29. #ifndef HAS_INET_NTOP
  30. #define HAS_INET_NTOP 1
  31. #endif
  32. #ifndef HAS_MSGHDR_FLAGS
  33. #define HAS_MSGHDR_FLAGS 1
  34. #endif
  35. #ifndef HAS_SOCKLEN_T
  36. #define HAS_SOCKLEN_T 1
  37. #endif
  38. #endif
  39. #ifdef HAS_FCNTL
  40. #include <fcntl.h>
  41. #endif
  42. #ifdef HAS_POLL
  43. #include <sys/poll.h>
  44. #endif
  45. #ifndef HAS_SOCKLEN_T
  46. typedef int socklen_t;
  47. #endif
  48. #ifndef MSG_NOSIGNAL
  49. #define MSG_NOSIGNAL 0
  50. #endif
  51. static enet_uint32 timeBase = 0;
  52. int
  53. enet_initialize (void)
  54. {
  55. return 0;
  56. }
  57. void
  58. enet_deinitialize (void)
  59. {
  60. }
  61. enet_uint32
  62. enet_host_random_seed (void)
  63. {
  64. return (enet_uint32) time (NULL);
  65. }
  66. enet_uint32
  67. enet_time_get (void)
  68. {
  69. struct timeval timeVal;
  70. gettimeofday (& timeVal, NULL);
  71. return timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - timeBase;
  72. }
  73. void
  74. enet_time_set (enet_uint32 newTimeBase)
  75. {
  76. struct timeval timeVal;
  77. gettimeofday (& timeVal, NULL);
  78. timeBase = timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - newTimeBase;
  79. }
  80. int
  81. enet_address_set_host (ENetAddress * address, const char * name)
  82. {
  83. struct hostent * hostEntry = NULL;
  84. #ifdef HAS_GETHOSTBYNAME_R
  85. struct hostent hostData;
  86. char buffer [2048];
  87. int errnum;
  88. #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  89. gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
  90. #else
  91. hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum);
  92. #endif
  93. #else
  94. hostEntry = gethostbyname (name);
  95. #endif
  96. if (hostEntry == NULL ||
  97. hostEntry -> h_addrtype != AF_INET)
  98. {
  99. #ifdef HAS_INET_PTON
  100. if (! inet_pton (AF_INET, name, & address -> host))
  101. #else
  102. if (! inet_aton (name, (struct in_addr *) & address -> host))
  103. #endif
  104. return -1;
  105. return 0;
  106. }
  107. address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
  108. return 0;
  109. }
  110. int
  111. enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
  112. {
  113. #ifdef HAS_INET_NTOP
  114. if (inet_ntop (AF_INET, & address -> host, name, nameLength) == NULL)
  115. #else
  116. char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
  117. if (addr != NULL)
  118. strncpy (name, addr, nameLength);
  119. else
  120. #endif
  121. return -1;
  122. return 0;
  123. }
  124. int
  125. enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
  126. {
  127. struct in_addr in;
  128. struct hostent * hostEntry = NULL;
  129. #ifdef HAS_GETHOSTBYADDR_R
  130. struct hostent hostData;
  131. char buffer [2048];
  132. int errnum;
  133. in.s_addr = address -> host;
  134. #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  135. gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
  136. #else
  137. hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum);
  138. #endif
  139. #else
  140. in.s_addr = address -> host;
  141. hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
  142. #endif
  143. if (hostEntry == NULL)
  144. return enet_address_get_host_ip (address, name, nameLength);
  145. strncpy (name, hostEntry -> h_name, nameLength);
  146. return 0;
  147. }
  148. int
  149. enet_socket_bind (ENetSocket socket, const ENetAddress * address)
  150. {
  151. struct sockaddr_in sin;
  152. memset (& sin, 0, sizeof (struct sockaddr_in));
  153. sin.sin_family = AF_INET;
  154. if (address != NULL)
  155. {
  156. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  157. sin.sin_addr.s_addr = address -> host;
  158. }
  159. else
  160. {
  161. sin.sin_port = 0;
  162. sin.sin_addr.s_addr = INADDR_ANY;
  163. }
  164. return bind (socket,
  165. (struct sockaddr *) & sin,
  166. sizeof (struct sockaddr_in));
  167. }
  168. int
  169. enet_socket_get_address (ENetSocket socket, ENetAddress * address)
  170. {
  171. struct sockaddr_in sin;
  172. socklen_t sinLength = sizeof (struct sockaddr_in);
  173. if (getsockname (socket, (struct sockaddr *) & sin, & sinLength) == -1)
  174. return -1;
  175. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  176. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  177. return 0;
  178. }
  179. int
  180. enet_socket_listen (ENetSocket socket, int backlog)
  181. {
  182. return listen (socket, backlog < 0 ? SOMAXCONN : backlog);
  183. }
  184. ENetSocket
  185. enet_socket_create (ENetSocketType type)
  186. {
  187. return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
  188. }
  189. int
  190. enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
  191. {
  192. int result = -1;
  193. switch (option)
  194. {
  195. case ENET_SOCKOPT_NONBLOCK:
  196. #ifdef HAS_FCNTL
  197. result = fcntl (socket, F_SETFL, O_NONBLOCK | fcntl (socket, F_GETFL));
  198. #else
  199. result = ioctl (socket, FIONBIO, & value);
  200. #endif
  201. break;
  202. case ENET_SOCKOPT_BROADCAST:
  203. result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int));
  204. break;
  205. case ENET_SOCKOPT_REUSEADDR:
  206. result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int));
  207. break;
  208. case ENET_SOCKOPT_RCVBUF:
  209. result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int));
  210. break;
  211. case ENET_SOCKOPT_SNDBUF:
  212. result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int));
  213. break;
  214. case ENET_SOCKOPT_RCVTIMEO:
  215. {
  216. struct timeval timeVal;
  217. timeVal.tv_sec = value / 1000;
  218. timeVal.tv_usec = (value % 1000) * 1000;
  219. result = setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & timeVal, sizeof (struct timeval));
  220. break;
  221. }
  222. case ENET_SOCKOPT_SNDTIMEO:
  223. {
  224. struct timeval timeVal;
  225. timeVal.tv_sec = value / 1000;
  226. timeVal.tv_usec = (value % 1000) * 1000;
  227. result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & timeVal, sizeof (struct timeval));
  228. break;
  229. }
  230. case ENET_SOCKOPT_NODELAY:
  231. result = setsockopt (socket, IPPROTO_TCP, TCP_NODELAY, (char *) & value, sizeof (int));
  232. break;
  233. default:
  234. break;
  235. }
  236. return result == -1 ? -1 : 0;
  237. }
  238. int
  239. enet_socket_get_option (ENetSocket socket, ENetSocketOption option, int * value)
  240. {
  241. int result = -1;
  242. socklen_t len;
  243. switch (option)
  244. {
  245. case ENET_SOCKOPT_ERROR:
  246. len = sizeof (int);
  247. result = getsockopt (socket, SOL_SOCKET, SO_ERROR, value, & len);
  248. break;
  249. default:
  250. break;
  251. }
  252. return result == -1 ? -1 : 0;
  253. }
  254. int
  255. enet_socket_connect (ENetSocket socket, const ENetAddress * address)
  256. {
  257. struct sockaddr_in sin;
  258. int result;
  259. memset (& sin, 0, sizeof (struct sockaddr_in));
  260. sin.sin_family = AF_INET;
  261. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  262. sin.sin_addr.s_addr = address -> host;
  263. result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
  264. if (result == -1 && errno == EINPROGRESS)
  265. return 0;
  266. return result;
  267. }
  268. ENetSocket
  269. enet_socket_accept (ENetSocket socket, ENetAddress * address)
  270. {
  271. int result;
  272. struct sockaddr_in sin;
  273. socklen_t sinLength = sizeof (struct sockaddr_in);
  274. result = accept (socket,
  275. address != NULL ? (struct sockaddr *) & sin : NULL,
  276. address != NULL ? & sinLength : NULL);
  277. if (result == -1)
  278. return ENET_SOCKET_NULL;
  279. if (address != NULL)
  280. {
  281. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  282. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  283. }
  284. return result;
  285. }
  286. int
  287. enet_socket_shutdown (ENetSocket socket, ENetSocketShutdown how)
  288. {
  289. return shutdown (socket, (int) how);
  290. }
  291. void
  292. enet_socket_destroy (ENetSocket socket)
  293. {
  294. if (socket != -1)
  295. close (socket);
  296. }
  297. int
  298. enet_socket_send (ENetSocket socket,
  299. const ENetAddress * address,
  300. const ENetBuffer * buffers,
  301. size_t bufferCount)
  302. {
  303. struct msghdr msgHdr;
  304. struct sockaddr_in sin;
  305. int sentLength;
  306. memset (& msgHdr, 0, sizeof (struct msghdr));
  307. if (address != NULL)
  308. {
  309. memset (& sin, 0, sizeof (struct sockaddr_in));
  310. sin.sin_family = AF_INET;
  311. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  312. sin.sin_addr.s_addr = address -> host;
  313. msgHdr.msg_name = & sin;
  314. msgHdr.msg_namelen = sizeof (struct sockaddr_in);
  315. }
  316. msgHdr.msg_iov = (struct iovec *) buffers;
  317. msgHdr.msg_iovlen = bufferCount;
  318. sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL);
  319. if (sentLength == -1)
  320. {
  321. if (errno == EWOULDBLOCK)
  322. return 0;
  323. return -1;
  324. }
  325. return sentLength;
  326. }
  327. int
  328. enet_socket_receive (ENetSocket socket,
  329. ENetAddress * address,
  330. ENetBuffer * buffers,
  331. size_t bufferCount)
  332. {
  333. struct msghdr msgHdr;
  334. struct sockaddr_in sin;
  335. int recvLength;
  336. memset (& msgHdr, 0, sizeof (struct msghdr));
  337. if (address != NULL)
  338. {
  339. msgHdr.msg_name = & sin;
  340. msgHdr.msg_namelen = sizeof (struct sockaddr_in);
  341. }
  342. msgHdr.msg_iov = (struct iovec *) buffers;
  343. msgHdr.msg_iovlen = bufferCount;
  344. recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL);
  345. if (recvLength == -1)
  346. {
  347. if (errno == EWOULDBLOCK)
  348. return 0;
  349. return -1;
  350. }
  351. #ifdef HAS_MSGHDR_FLAGS
  352. if (msgHdr.msg_flags & MSG_TRUNC)
  353. return -1;
  354. #endif
  355. if (address != NULL)
  356. {
  357. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  358. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  359. }
  360. return recvLength;
  361. }
  362. int
  363. enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout)
  364. {
  365. struct timeval timeVal;
  366. timeVal.tv_sec = timeout / 1000;
  367. timeVal.tv_usec = (timeout % 1000) * 1000;
  368. return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal);
  369. }
  370. int
  371. enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
  372. {
  373. #ifdef HAS_POLL
  374. struct pollfd pollSocket;
  375. int pollCount;
  376. pollSocket.fd = socket;
  377. pollSocket.events = 0;
  378. if (* condition & ENET_SOCKET_WAIT_SEND)
  379. pollSocket.events |= POLLOUT;
  380. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  381. pollSocket.events |= POLLIN;
  382. pollCount = poll (& pollSocket, 1, timeout);
  383. if (pollCount < 0)
  384. {
  385. if (errno == EINTR && * condition & ENET_SOCKET_WAIT_INTERRUPT)
  386. {
  387. * condition = ENET_SOCKET_WAIT_INTERRUPT;
  388. return 0;
  389. }
  390. return -1;
  391. }
  392. * condition = ENET_SOCKET_WAIT_NONE;
  393. if (pollCount == 0)
  394. return 0;
  395. if (pollSocket.revents & POLLOUT)
  396. * condition |= ENET_SOCKET_WAIT_SEND;
  397. if (pollSocket.revents & POLLIN)
  398. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  399. return 0;
  400. #else
  401. fd_set readSet, writeSet;
  402. struct timeval timeVal;
  403. int selectCount;
  404. timeVal.tv_sec = timeout / 1000;
  405. timeVal.tv_usec = (timeout % 1000) * 1000;
  406. FD_ZERO (& readSet);
  407. FD_ZERO (& writeSet);
  408. if (* condition & ENET_SOCKET_WAIT_SEND)
  409. FD_SET (socket, & writeSet);
  410. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  411. FD_SET (socket, & readSet);
  412. selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
  413. if (selectCount < 0)
  414. {
  415. if (errno == EINTR && * condition & ENET_SOCKET_WAIT_INTERRUPT)
  416. {
  417. * condition = ENET_SOCKET_WAIT_INTERRUPT;
  418. return 0;
  419. }
  420. return -1;
  421. }
  422. * condition = ENET_SOCKET_WAIT_NONE;
  423. if (selectCount == 0)
  424. return 0;
  425. if (FD_ISSET (socket, & writeSet))
  426. * condition |= ENET_SOCKET_WAIT_SEND;
  427. if (FD_ISSET (socket, & readSet))
  428. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  429. return 0;
  430. #endif
  431. }
  432. #endif