unix.c 11 KB

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