unix.c 14 KB

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