unix.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. memset (& sin, 0, sizeof (struct sockaddr_in));
  213. sin.sin_family = AF_INET;
  214. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  215. sin.sin_addr.s_addr = address -> host;
  216. return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
  217. }
  218. ENetSocket
  219. enet_socket_accept (ENetSocket socket, ENetAddress * address)
  220. {
  221. int result;
  222. struct sockaddr_in sin;
  223. socklen_t sinLength = sizeof (struct sockaddr_in);
  224. result = accept (socket,
  225. address != NULL ? (struct sockaddr *) & sin : NULL,
  226. address != NULL ? & sinLength : NULL);
  227. if (result == -1)
  228. return ENET_SOCKET_NULL;
  229. if (address != NULL)
  230. {
  231. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  232. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  233. }
  234. return result;
  235. }
  236. void
  237. enet_socket_destroy (ENetSocket socket)
  238. {
  239. if (socket != -1)
  240. close (socket);
  241. }
  242. int
  243. enet_socket_send (ENetSocket socket,
  244. const ENetAddress * address,
  245. const ENetBuffer * buffers,
  246. size_t bufferCount)
  247. {
  248. struct msghdr msgHdr;
  249. struct sockaddr_in sin;
  250. int sentLength;
  251. memset (& msgHdr, 0, sizeof (struct msghdr));
  252. if (address != NULL)
  253. {
  254. memset (& sin, 0, sizeof (struct sockaddr_in));
  255. sin.sin_family = AF_INET;
  256. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  257. sin.sin_addr.s_addr = address -> host;
  258. msgHdr.msg_name = & sin;
  259. msgHdr.msg_namelen = sizeof (struct sockaddr_in);
  260. }
  261. msgHdr.msg_iov = (struct iovec *) buffers;
  262. msgHdr.msg_iovlen = bufferCount;
  263. sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL);
  264. if (sentLength == -1)
  265. {
  266. if (errno == EWOULDBLOCK)
  267. return 0;
  268. return -1;
  269. }
  270. return sentLength;
  271. }
  272. int
  273. enet_socket_receive (ENetSocket socket,
  274. ENetAddress * address,
  275. ENetBuffer * buffers,
  276. size_t bufferCount)
  277. {
  278. struct msghdr msgHdr;
  279. struct sockaddr_in sin;
  280. int recvLength;
  281. memset (& msgHdr, 0, sizeof (struct msghdr));
  282. if (address != NULL)
  283. {
  284. msgHdr.msg_name = & sin;
  285. msgHdr.msg_namelen = sizeof (struct sockaddr_in);
  286. }
  287. msgHdr.msg_iov = (struct iovec *) buffers;
  288. msgHdr.msg_iovlen = bufferCount;
  289. recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL);
  290. if (recvLength == -1)
  291. {
  292. if (errno == EWOULDBLOCK)
  293. return 0;
  294. return -1;
  295. }
  296. #ifdef HAS_MSGHDR_FLAGS
  297. if (msgHdr.msg_flags & MSG_TRUNC)
  298. return -1;
  299. #endif
  300. if (address != NULL)
  301. {
  302. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  303. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  304. }
  305. return recvLength;
  306. }
  307. int
  308. enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout)
  309. {
  310. struct timeval timeVal;
  311. timeVal.tv_sec = timeout / 1000;
  312. timeVal.tv_usec = (timeout % 1000) * 1000;
  313. return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal);
  314. }
  315. int
  316. enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
  317. {
  318. #ifdef HAS_POLL
  319. struct pollfd pollSocket;
  320. int pollCount;
  321. pollSocket.fd = socket;
  322. pollSocket.events = 0;
  323. if (* condition & ENET_SOCKET_WAIT_SEND)
  324. pollSocket.events |= POLLOUT;
  325. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  326. pollSocket.events |= POLLIN;
  327. pollCount = poll (& pollSocket, 1, timeout);
  328. if (pollCount < 0)
  329. return -1;
  330. * condition = ENET_SOCKET_WAIT_NONE;
  331. if (pollCount == 0)
  332. return 0;
  333. if (pollSocket.revents & POLLOUT)
  334. * condition |= ENET_SOCKET_WAIT_SEND;
  335. if (pollSocket.revents & POLLIN)
  336. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  337. return 0;
  338. #else
  339. fd_set readSet, writeSet;
  340. struct timeval timeVal;
  341. int selectCount;
  342. timeVal.tv_sec = timeout / 1000;
  343. timeVal.tv_usec = (timeout % 1000) * 1000;
  344. FD_ZERO (& readSet);
  345. FD_ZERO (& writeSet);
  346. if (* condition & ENET_SOCKET_WAIT_SEND)
  347. FD_SET (socket, & writeSet);
  348. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  349. FD_SET (socket, & readSet);
  350. selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
  351. if (selectCount < 0)
  352. return -1;
  353. * condition = ENET_SOCKET_WAIT_NONE;
  354. if (selectCount == 0)
  355. return 0;
  356. if (FD_ISSET (socket, & writeSet))
  357. * condition |= ENET_SOCKET_WAIT_SEND;
  358. if (FD_ISSET (socket, & readSet))
  359. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  360. return 0;
  361. #endif
  362. }
  363. #endif