unix.c 11 KB

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