unix.c 12 KB

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