unix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. switch (option)
  229. {
  230. case ENET_SOCKOPT_ERROR:
  231. result = setsockopt (socket, SOL_SOCKET, SO_ERROR, (char *) value, sizeof (int));
  232. break;
  233. }
  234. return result == -1 ? -1 : 0;
  235. }
  236. int
  237. enet_socket_connect (ENetSocket socket, const ENetAddress * address)
  238. {
  239. struct sockaddr_in sin;
  240. int result;
  241. memset (& sin, 0, sizeof (struct sockaddr_in));
  242. sin.sin_family = AF_INET;
  243. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  244. sin.sin_addr.s_addr = address -> host;
  245. result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
  246. if (result == -1 && errno == EINPROGRESS)
  247. return 0;
  248. return result;
  249. }
  250. ENetSocket
  251. enet_socket_accept (ENetSocket socket, ENetAddress * address)
  252. {
  253. int result;
  254. struct sockaddr_in sin;
  255. socklen_t sinLength = sizeof (struct sockaddr_in);
  256. result = accept (socket,
  257. address != NULL ? (struct sockaddr *) & sin : NULL,
  258. address != NULL ? & sinLength : NULL);
  259. if (result == -1)
  260. return ENET_SOCKET_NULL;
  261. if (address != NULL)
  262. {
  263. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  264. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  265. }
  266. return result;
  267. }
  268. int
  269. enet_socket_shutdown (ENetSocket socket, ENetSocketShutdown how)
  270. {
  271. return shutdown (socket, (int) how);
  272. }
  273. void
  274. enet_socket_destroy (ENetSocket socket)
  275. {
  276. if (socket != -1)
  277. close (socket);
  278. }
  279. int
  280. enet_socket_send (ENetSocket socket,
  281. const ENetAddress * address,
  282. const ENetBuffer * buffers,
  283. size_t bufferCount)
  284. {
  285. struct msghdr msgHdr;
  286. struct sockaddr_in sin;
  287. int sentLength;
  288. memset (& msgHdr, 0, sizeof (struct msghdr));
  289. if (address != NULL)
  290. {
  291. memset (& sin, 0, sizeof (struct sockaddr_in));
  292. sin.sin_family = AF_INET;
  293. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  294. sin.sin_addr.s_addr = address -> host;
  295. msgHdr.msg_name = & sin;
  296. msgHdr.msg_namelen = sizeof (struct sockaddr_in);
  297. }
  298. msgHdr.msg_iov = (struct iovec *) buffers;
  299. msgHdr.msg_iovlen = bufferCount;
  300. sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL);
  301. if (sentLength == -1)
  302. {
  303. if (errno == EWOULDBLOCK)
  304. return 0;
  305. return -1;
  306. }
  307. return sentLength;
  308. }
  309. int
  310. enet_socket_receive (ENetSocket socket,
  311. ENetAddress * address,
  312. ENetBuffer * buffers,
  313. size_t bufferCount)
  314. {
  315. struct msghdr msgHdr;
  316. struct sockaddr_in sin;
  317. int recvLength;
  318. memset (& msgHdr, 0, sizeof (struct msghdr));
  319. if (address != NULL)
  320. {
  321. msgHdr.msg_name = & sin;
  322. msgHdr.msg_namelen = sizeof (struct sockaddr_in);
  323. }
  324. msgHdr.msg_iov = (struct iovec *) buffers;
  325. msgHdr.msg_iovlen = bufferCount;
  326. recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL);
  327. if (recvLength == -1)
  328. {
  329. if (errno == EWOULDBLOCK)
  330. return 0;
  331. return -1;
  332. }
  333. #ifdef HAS_MSGHDR_FLAGS
  334. if (msgHdr.msg_flags & MSG_TRUNC)
  335. return -1;
  336. #endif
  337. if (address != NULL)
  338. {
  339. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  340. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  341. }
  342. return recvLength;
  343. }
  344. int
  345. enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout)
  346. {
  347. struct timeval timeVal;
  348. timeVal.tv_sec = timeout / 1000;
  349. timeVal.tv_usec = (timeout % 1000) * 1000;
  350. return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal);
  351. }
  352. int
  353. enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
  354. {
  355. #ifdef HAS_POLL
  356. struct pollfd pollSocket;
  357. int pollCount;
  358. pollSocket.fd = socket;
  359. pollSocket.events = 0;
  360. if (* condition & ENET_SOCKET_WAIT_SEND)
  361. pollSocket.events |= POLLOUT;
  362. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  363. pollSocket.events |= POLLIN;
  364. pollCount = poll (& pollSocket, 1, timeout);
  365. if (pollCount < 0)
  366. {
  367. if (errno == EINTR && * condition & ENET_SOCKET_WAIT_INTERRUPT)
  368. {
  369. * condition = ENET_SOCKET_WAIT_INTERRUPT;
  370. return 0;
  371. }
  372. return -1;
  373. }
  374. * condition = ENET_SOCKET_WAIT_NONE;
  375. if (pollCount == 0)
  376. return 0;
  377. if (pollSocket.revents & POLLOUT)
  378. * condition |= ENET_SOCKET_WAIT_SEND;
  379. if (pollSocket.revents & POLLIN)
  380. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  381. return 0;
  382. #else
  383. fd_set readSet, writeSet;
  384. struct timeval timeVal;
  385. int selectCount;
  386. timeVal.tv_sec = timeout / 1000;
  387. timeVal.tv_usec = (timeout % 1000) * 1000;
  388. FD_ZERO (& readSet);
  389. FD_ZERO (& writeSet);
  390. if (* condition & ENET_SOCKET_WAIT_SEND)
  391. FD_SET (socket, & writeSet);
  392. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  393. FD_SET (socket, & readSet);
  394. selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
  395. if (selectCount < 0)
  396. {
  397. if (errno == EINTR && * condition & ENET_SOCKET_WAIT_INTERRUPT)
  398. {
  399. * condition = ENET_SOCKET_WAIT_INTERRUPT;
  400. return 0;
  401. }
  402. return -1;
  403. }
  404. * condition = ENET_SOCKET_WAIT_NONE;
  405. if (selectCount == 0)
  406. return 0;
  407. if (FD_ISSET (socket, & writeSet))
  408. * condition |= ENET_SOCKET_WAIT_SEND;
  409. if (FD_ISSET (socket, & readSet))
  410. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  411. return 0;
  412. #endif
  413. }
  414. #endif