unix.c 12 KB

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