unix.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 HAS_FCNTL
  19. #include <fcntl.h>
  20. #endif
  21. #ifdef __APPLE__
  22. #undef HAS_POLL
  23. #endif
  24. #ifdef HAS_POLL
  25. #include <sys/poll.h>
  26. #endif
  27. #ifndef HAS_SOCKLEN_T
  28. typedef int socklen_t;
  29. #endif
  30. #ifndef MSG_NOSIGNAL
  31. #define MSG_NOSIGNAL 0
  32. #endif
  33. static enet_uint32 timeBase = 0;
  34. int
  35. enet_initialize (void)
  36. {
  37. return 0;
  38. }
  39. void
  40. enet_deinitialize (void)
  41. {
  42. }
  43. enet_uint32
  44. enet_time_get (void)
  45. {
  46. struct timeval timeVal;
  47. gettimeofday (& timeVal, NULL);
  48. return timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - timeBase;
  49. }
  50. void
  51. enet_time_set (enet_uint32 newTimeBase)
  52. {
  53. struct timeval timeVal;
  54. gettimeofday (& timeVal, NULL);
  55. timeBase = timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - newTimeBase;
  56. }
  57. int
  58. enet_address_set_host (ENetAddress * address, const char * name)
  59. {
  60. struct hostent * hostEntry = NULL;
  61. #ifdef HAS_GETHOSTBYNAME_R
  62. struct hostent hostData;
  63. char buffer [2048];
  64. int errnum;
  65. #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  66. gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
  67. #else
  68. hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum);
  69. #endif
  70. #else
  71. hostEntry = gethostbyname (name);
  72. #endif
  73. if (hostEntry == NULL ||
  74. hostEntry -> h_addrtype != AF_INET)
  75. {
  76. #ifdef HAS_INET_PTON
  77. if (! inet_pton (AF_INET, name, & address -> host))
  78. #else
  79. if (! inet_aton (name, (struct in_addr *) & address -> host))
  80. #endif
  81. return -1;
  82. return 0;
  83. }
  84. address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
  85. return 0;
  86. }
  87. int
  88. enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
  89. {
  90. #ifdef HAS_INET_NTOP
  91. if (inet_ntop (AF_INET, & address -> host, name, nameLength) == NULL)
  92. #else
  93. char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
  94. if (addr != NULL)
  95. strncpy (name, addr, nameLength);
  96. else
  97. #endif
  98. return -1;
  99. return 0;
  100. }
  101. int
  102. enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
  103. {
  104. struct in_addr in;
  105. struct hostent * hostEntry = NULL;
  106. #ifdef HAS_GETHOSTBYADDR_R
  107. struct hostent hostData;
  108. char buffer [2048];
  109. int errnum;
  110. in.s_addr = address -> host;
  111. #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  112. gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
  113. #else
  114. hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum);
  115. #endif
  116. #else
  117. in.s_addr = address -> host;
  118. hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
  119. #endif
  120. if (hostEntry == NULL)
  121. return enet_address_get_host_ip (address, name, nameLength);
  122. strncpy (name, hostEntry -> h_name, nameLength);
  123. return 0;
  124. }
  125. int
  126. enet_socket_bind (ENetSocket socket, const ENetAddress * address)
  127. {
  128. struct sockaddr_in sin;
  129. memset (& sin, 0, sizeof (struct sockaddr_in));
  130. sin.sin_family = AF_INET;
  131. if (address != NULL)
  132. {
  133. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  134. sin.sin_addr.s_addr = address -> host;
  135. }
  136. else
  137. {
  138. sin.sin_port = 0;
  139. sin.sin_addr.s_addr = INADDR_ANY;
  140. }
  141. return bind (socket,
  142. (struct sockaddr *) & sin,
  143. sizeof (struct sockaddr_in));
  144. }
  145. int
  146. enet_socket_listen (ENetSocket socket, int backlog)
  147. {
  148. return listen (socket, backlog < 0 ? SOMAXCONN : backlog);
  149. }
  150. ENetSocket
  151. enet_socket_create (ENetSocketType type)
  152. {
  153. return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
  154. }
  155. int
  156. enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
  157. {
  158. int result = -1;
  159. switch (option)
  160. {
  161. case ENET_SOCKOPT_NONBLOCK:
  162. #ifdef HAS_FCNTL
  163. result = fcntl (socket, F_SETFL, O_NONBLOCK | fcntl (socket, F_GETFL));
  164. #else
  165. result = ioctl (socket, FIONBIO, & value);
  166. #endif
  167. break;
  168. case ENET_SOCKOPT_BROADCAST:
  169. result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int));
  170. break;
  171. case ENET_SOCKOPT_REUSEADDR:
  172. result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int));
  173. break;
  174. case ENET_SOCKOPT_RCVBUF:
  175. result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int));
  176. break;
  177. case ENET_SOCKOPT_SNDBUF:
  178. result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int));
  179. break;
  180. case ENET_SOCKOPT_RCVTIMEO:
  181. result = setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & value, sizeof (int));
  182. break;
  183. case ENET_SOCKOPT_SNDTIMEO:
  184. result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & value, sizeof (int));
  185. break;
  186. default:
  187. break;
  188. }
  189. return result == -1 ? -1 : 0;
  190. }
  191. int
  192. enet_socket_connect (ENetSocket socket, const ENetAddress * address)
  193. {
  194. struct sockaddr_in sin;
  195. memset (& sin, 0, sizeof (struct sockaddr_in));
  196. sin.sin_family = AF_INET;
  197. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  198. sin.sin_addr.s_addr = address -> host;
  199. return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
  200. }
  201. ENetSocket
  202. enet_socket_accept (ENetSocket socket, ENetAddress * address)
  203. {
  204. int result;
  205. struct sockaddr_in sin;
  206. socklen_t sinLength = sizeof (struct sockaddr_in);
  207. result = accept (socket,
  208. address != NULL ? (struct sockaddr *) & sin : NULL,
  209. address != NULL ? & sinLength : NULL);
  210. if (result == -1)
  211. return ENET_SOCKET_NULL;
  212. if (address != NULL)
  213. {
  214. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  215. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  216. }
  217. return result;
  218. }
  219. void
  220. enet_socket_destroy (ENetSocket socket)
  221. {
  222. if (socket != -1)
  223. close (socket);
  224. }
  225. int
  226. enet_socket_send (ENetSocket socket,
  227. const ENetAddress * address,
  228. const ENetBuffer * buffers,
  229. size_t bufferCount)
  230. {
  231. struct msghdr msgHdr;
  232. struct sockaddr_in sin;
  233. int sentLength;
  234. memset (& msgHdr, 0, sizeof (struct msghdr));
  235. if (address != NULL)
  236. {
  237. memset (& sin, 0, sizeof (struct sockaddr_in));
  238. sin.sin_family = AF_INET;
  239. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  240. sin.sin_addr.s_addr = address -> host;
  241. msgHdr.msg_name = & sin;
  242. msgHdr.msg_namelen = sizeof (struct sockaddr_in);
  243. }
  244. msgHdr.msg_iov = (struct iovec *) buffers;
  245. msgHdr.msg_iovlen = bufferCount;
  246. sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL);
  247. if (sentLength == -1)
  248. {
  249. if (errno == EWOULDBLOCK)
  250. return 0;
  251. return -1;
  252. }
  253. return sentLength;
  254. }
  255. int
  256. enet_socket_receive (ENetSocket socket,
  257. ENetAddress * address,
  258. ENetBuffer * buffers,
  259. size_t bufferCount)
  260. {
  261. struct msghdr msgHdr;
  262. struct sockaddr_in sin;
  263. int recvLength;
  264. memset (& msgHdr, 0, sizeof (struct msghdr));
  265. if (address != NULL)
  266. {
  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. recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL);
  273. if (recvLength == -1)
  274. {
  275. if (errno == EWOULDBLOCK)
  276. return 0;
  277. return -1;
  278. }
  279. #ifdef HAS_MSGHDR_FLAGS
  280. if (msgHdr.msg_flags & MSG_TRUNC)
  281. return -1;
  282. #endif
  283. if (address != NULL)
  284. {
  285. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  286. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  287. }
  288. return recvLength;
  289. }
  290. int
  291. enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout)
  292. {
  293. struct timeval timeVal;
  294. timeVal.tv_sec = timeout / 1000;
  295. timeVal.tv_usec = (timeout % 1000) * 1000;
  296. return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal);
  297. }
  298. int
  299. enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
  300. {
  301. #ifdef HAS_POLL
  302. struct pollfd pollSocket;
  303. int pollCount;
  304. pollSocket.fd = socket;
  305. pollSocket.events = 0;
  306. if (* condition & ENET_SOCKET_WAIT_SEND)
  307. pollSocket.events |= POLLOUT;
  308. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  309. pollSocket.events |= POLLIN;
  310. pollCount = poll (& pollSocket, 1, timeout);
  311. if (pollCount < 0)
  312. return -1;
  313. * condition = ENET_SOCKET_WAIT_NONE;
  314. if (pollCount == 0)
  315. return 0;
  316. if (pollSocket.revents & POLLOUT)
  317. * condition |= ENET_SOCKET_WAIT_SEND;
  318. if (pollSocket.revents & POLLIN)
  319. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  320. return 0;
  321. #else
  322. fd_set readSet, writeSet;
  323. struct timeval timeVal;
  324. int selectCount;
  325. timeVal.tv_sec = timeout / 1000;
  326. timeVal.tv_usec = (timeout % 1000) * 1000;
  327. FD_ZERO (& readSet);
  328. FD_ZERO (& writeSet);
  329. if (* condition & ENET_SOCKET_WAIT_SEND)
  330. FD_SET (socket, & writeSet);
  331. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  332. FD_SET (socket, & readSet);
  333. selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
  334. if (selectCount < 0)
  335. return -1;
  336. * condition = ENET_SOCKET_WAIT_NONE;
  337. if (selectCount == 0)
  338. return 0;
  339. if (FD_ISSET (socket, & writeSet))
  340. * condition |= ENET_SOCKET_WAIT_SEND;
  341. if (FD_ISSET (socket, & readSet))
  342. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  343. return 0;
  344. #endif
  345. }
  346. #endif