unix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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_listen (ENetSocket socket, int backlog)
  164. {
  165. return listen (socket, backlog < 0 ? SOMAXCONN : backlog);
  166. }
  167. ENetSocket
  168. enet_socket_create (ENetSocketType type)
  169. {
  170. return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
  171. }
  172. int
  173. enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
  174. {
  175. int result = -1;
  176. switch (option)
  177. {
  178. case ENET_SOCKOPT_NONBLOCK:
  179. #ifdef HAS_FCNTL
  180. result = fcntl (socket, F_SETFL, O_NONBLOCK | fcntl (socket, F_GETFL));
  181. #else
  182. result = ioctl (socket, FIONBIO, & value);
  183. #endif
  184. break;
  185. case ENET_SOCKOPT_BROADCAST:
  186. result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int));
  187. break;
  188. case ENET_SOCKOPT_REUSEADDR:
  189. result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int));
  190. break;
  191. case ENET_SOCKOPT_RCVBUF:
  192. result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int));
  193. break;
  194. case ENET_SOCKOPT_SNDBUF:
  195. result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int));
  196. break;
  197. case ENET_SOCKOPT_RCVTIMEO:
  198. result = setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & value, sizeof (int));
  199. break;
  200. case ENET_SOCKOPT_SNDTIMEO:
  201. result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & value, sizeof (int));
  202. break;
  203. default:
  204. break;
  205. }
  206. return result == -1 ? -1 : 0;
  207. }
  208. int
  209. enet_socket_connect (ENetSocket socket, const ENetAddress * address)
  210. {
  211. struct sockaddr_in sin;
  212. int result;
  213. memset (& sin, 0, sizeof (struct sockaddr_in));
  214. sin.sin_family = AF_INET;
  215. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  216. sin.sin_addr.s_addr = address -> host;
  217. result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
  218. if (result == -1 && errno == EINPROGRESS)
  219. return 0;
  220. return result;
  221. }
  222. ENetSocket
  223. enet_socket_accept (ENetSocket socket, ENetAddress * address)
  224. {
  225. int result;
  226. struct sockaddr_in sin;
  227. socklen_t sinLength = sizeof (struct sockaddr_in);
  228. result = accept (socket,
  229. address != NULL ? (struct sockaddr *) & sin : NULL,
  230. address != NULL ? & sinLength : NULL);
  231. if (result == -1)
  232. return ENET_SOCKET_NULL;
  233. if (address != NULL)
  234. {
  235. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  236. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  237. }
  238. return result;
  239. }
  240. int
  241. enet_socket_shutdown (ENetSocket socket, ENetSocketShutdown how)
  242. {
  243. return shutdown (socket, (int) how);
  244. }
  245. void
  246. enet_socket_destroy (ENetSocket socket)
  247. {
  248. if (socket != -1)
  249. close (socket);
  250. }
  251. int
  252. enet_socket_send (ENetSocket socket,
  253. const ENetAddress * address,
  254. const ENetBuffer * buffers,
  255. size_t bufferCount)
  256. {
  257. struct msghdr msgHdr;
  258. struct sockaddr_in sin;
  259. int sentLength;
  260. memset (& msgHdr, 0, sizeof (struct msghdr));
  261. if (address != NULL)
  262. {
  263. memset (& sin, 0, sizeof (struct sockaddr_in));
  264. sin.sin_family = AF_INET;
  265. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  266. sin.sin_addr.s_addr = address -> host;
  267. msgHdr.msg_name = & sin;
  268. msgHdr.msg_namelen = sizeof (struct sockaddr_in);
  269. }
  270. msgHdr.msg_iov = (struct iovec *) buffers;
  271. msgHdr.msg_iovlen = bufferCount;
  272. sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL);
  273. if (sentLength == -1)
  274. {
  275. if (errno == EWOULDBLOCK)
  276. return 0;
  277. return -1;
  278. }
  279. return sentLength;
  280. }
  281. int
  282. enet_socket_receive (ENetSocket socket,
  283. ENetAddress * address,
  284. ENetBuffer * buffers,
  285. size_t bufferCount)
  286. {
  287. struct msghdr msgHdr;
  288. struct sockaddr_in sin;
  289. int recvLength;
  290. memset (& msgHdr, 0, sizeof (struct msghdr));
  291. if (address != NULL)
  292. {
  293. msgHdr.msg_name = & sin;
  294. msgHdr.msg_namelen = sizeof (struct sockaddr_in);
  295. }
  296. msgHdr.msg_iov = (struct iovec *) buffers;
  297. msgHdr.msg_iovlen = bufferCount;
  298. recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL);
  299. if (recvLength == -1)
  300. {
  301. if (errno == EWOULDBLOCK)
  302. return 0;
  303. return -1;
  304. }
  305. #ifdef HAS_MSGHDR_FLAGS
  306. if (msgHdr.msg_flags & MSG_TRUNC)
  307. return -1;
  308. #endif
  309. if (address != NULL)
  310. {
  311. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  312. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  313. }
  314. return recvLength;
  315. }
  316. int
  317. enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout)
  318. {
  319. struct timeval timeVal;
  320. timeVal.tv_sec = timeout / 1000;
  321. timeVal.tv_usec = (timeout % 1000) * 1000;
  322. return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal);
  323. }
  324. int
  325. enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
  326. {
  327. #ifdef HAS_POLL
  328. struct pollfd pollSocket;
  329. int pollCount;
  330. pollSocket.fd = socket;
  331. pollSocket.events = 0;
  332. if (* condition & ENET_SOCKET_WAIT_SEND)
  333. pollSocket.events |= POLLOUT;
  334. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  335. pollSocket.events |= POLLIN;
  336. pollCount = poll (& pollSocket, 1, timeout);
  337. if (pollCount < 0)
  338. return -1;
  339. * condition = ENET_SOCKET_WAIT_NONE;
  340. if (pollCount == 0)
  341. return 0;
  342. if (pollSocket.revents & POLLOUT)
  343. * condition |= ENET_SOCKET_WAIT_SEND;
  344. if (pollSocket.revents & POLLIN)
  345. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  346. return 0;
  347. #else
  348. fd_set readSet, writeSet;
  349. struct timeval timeVal;
  350. int selectCount;
  351. timeVal.tv_sec = timeout / 1000;
  352. timeVal.tv_usec = (timeout % 1000) * 1000;
  353. FD_ZERO (& readSet);
  354. FD_ZERO (& writeSet);
  355. if (* condition & ENET_SOCKET_WAIT_SEND)
  356. FD_SET (socket, & writeSet);
  357. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  358. FD_SET (socket, & readSet);
  359. selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
  360. if (selectCount < 0)
  361. return -1;
  362. * condition = ENET_SOCKET_WAIT_NONE;
  363. if (selectCount == 0)
  364. return 0;
  365. if (FD_ISSET (socket, & writeSet))
  366. * condition |= ENET_SOCKET_WAIT_SEND;
  367. if (FD_ISSET (socket, & readSet))
  368. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  369. return 0;
  370. #endif
  371. }
  372. #endif