unix.c 11 KB

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