unix.c 13 KB

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