unix.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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(__FreeBSD__)
  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(__FreeBSD__)
  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. ENetSocket
  126. enet_socket_create (ENetSocketType type, const ENetAddress * address)
  127. {
  128. ENetSocket newSocket = socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
  129. struct sockaddr_in sin;
  130. if (newSocket == ENET_SOCKET_NULL)
  131. return ENET_SOCKET_NULL;
  132. if (address == NULL)
  133. return newSocket;
  134. memset (& sin, 0, sizeof (struct sockaddr_in));
  135. sin.sin_family = AF_INET;
  136. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  137. sin.sin_addr.s_addr = address -> host;
  138. if (bind (newSocket,
  139. (struct sockaddr *) & sin,
  140. sizeof (struct sockaddr_in)) == -1 ||
  141. (type == ENET_SOCKET_TYPE_STREAM &&
  142. address -> port != ENET_PORT_ANY &&
  143. listen (newSocket, SOMAXCONN) == -1))
  144. {
  145. close (newSocket);
  146. return ENET_SOCKET_NULL;
  147. }
  148. return newSocket;
  149. }
  150. int
  151. enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
  152. {
  153. int result = -1;
  154. switch (option)
  155. {
  156. case ENET_SOCKOPT_NONBLOCK:
  157. #ifdef HAS_FCNTL
  158. result = fcntl (socket, F_SETFL, O_NONBLOCK | fcntl (socket, F_GETFL));
  159. #else
  160. result = ioctl (socket, FIONBIO, & value);
  161. #endif
  162. break;
  163. case ENET_SOCKOPT_BROADCAST:
  164. result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int));
  165. break;
  166. case ENET_SOCKOPT_RCVBUF:
  167. result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int));
  168. break;
  169. case ENET_SOCKOPT_SNDBUF:
  170. result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int));
  171. break;
  172. default:
  173. break;
  174. }
  175. return result == -1 ? -1 : 0;
  176. }
  177. int
  178. enet_socket_connect (ENetSocket socket, const ENetAddress * address)
  179. {
  180. struct sockaddr_in sin;
  181. memset (& sin, 0, sizeof (struct sockaddr_in));
  182. sin.sin_family = AF_INET;
  183. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  184. sin.sin_addr.s_addr = address -> host;
  185. return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
  186. }
  187. ENetSocket
  188. enet_socket_accept (ENetSocket socket, ENetAddress * address)
  189. {
  190. int result;
  191. struct sockaddr_in sin;
  192. socklen_t sinLength = sizeof (struct sockaddr_in);
  193. result = accept (socket,
  194. address != NULL ? (struct sockaddr *) & sin : NULL,
  195. address != NULL ? & sinLength : NULL);
  196. if (result == -1)
  197. return ENET_SOCKET_NULL;
  198. if (address != NULL)
  199. {
  200. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  201. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  202. }
  203. return result;
  204. }
  205. void
  206. enet_socket_destroy (ENetSocket socket)
  207. {
  208. close (socket);
  209. }
  210. int
  211. enet_socket_send (ENetSocket socket,
  212. const ENetAddress * address,
  213. const ENetBuffer * buffers,
  214. size_t bufferCount)
  215. {
  216. struct msghdr msgHdr;
  217. struct sockaddr_in sin;
  218. int sentLength;
  219. memset (& msgHdr, 0, sizeof (struct msghdr));
  220. if (address != NULL)
  221. {
  222. memset (& sin, 0, sizeof (struct sockaddr_in));
  223. sin.sin_family = AF_INET;
  224. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  225. sin.sin_addr.s_addr = address -> host;
  226. msgHdr.msg_name = & sin;
  227. msgHdr.msg_namelen = sizeof (struct sockaddr_in);
  228. }
  229. msgHdr.msg_iov = (struct iovec *) buffers;
  230. msgHdr.msg_iovlen = bufferCount;
  231. sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL);
  232. if (sentLength == -1)
  233. {
  234. if (errno == EWOULDBLOCK)
  235. return 0;
  236. return -1;
  237. }
  238. return sentLength;
  239. }
  240. int
  241. enet_socket_receive (ENetSocket socket,
  242. ENetAddress * address,
  243. ENetBuffer * buffers,
  244. size_t bufferCount)
  245. {
  246. struct msghdr msgHdr;
  247. struct sockaddr_in sin;
  248. int recvLength;
  249. memset (& msgHdr, 0, sizeof (struct msghdr));
  250. if (address != NULL)
  251. {
  252. msgHdr.msg_name = & sin;
  253. msgHdr.msg_namelen = sizeof (struct sockaddr_in);
  254. }
  255. msgHdr.msg_iov = (struct iovec *) buffers;
  256. msgHdr.msg_iovlen = bufferCount;
  257. recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL);
  258. if (recvLength == -1)
  259. {
  260. if (errno == EWOULDBLOCK)
  261. return 0;
  262. return -1;
  263. }
  264. #ifdef HAS_MSGHDR_FLAGS
  265. if (msgHdr.msg_flags & MSG_TRUNC)
  266. return -1;
  267. #endif
  268. if (address != NULL)
  269. {
  270. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  271. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  272. }
  273. return recvLength;
  274. }
  275. int
  276. enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
  277. {
  278. #ifdef HAS_POLL
  279. struct pollfd pollSocket;
  280. int pollCount;
  281. pollSocket.fd = socket;
  282. pollSocket.events = 0;
  283. if (* condition & ENET_SOCKET_WAIT_SEND)
  284. pollSocket.events |= POLLOUT;
  285. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  286. pollSocket.events |= POLLIN;
  287. pollCount = poll (& pollSocket, 1, timeout);
  288. if (pollCount < 0)
  289. return -1;
  290. * condition = ENET_SOCKET_WAIT_NONE;
  291. if (pollCount == 0)
  292. return 0;
  293. if (pollSocket.revents & POLLOUT)
  294. * condition |= ENET_SOCKET_WAIT_SEND;
  295. if (pollSocket.revents & POLLIN)
  296. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  297. return 0;
  298. #else
  299. fd_set readSet, writeSet;
  300. struct timeval timeVal;
  301. int selectCount;
  302. timeVal.tv_sec = timeout / 1000;
  303. timeVal.tv_usec = (timeout % 1000) * 1000;
  304. FD_ZERO (& readSet);
  305. FD_ZERO (& writeSet);
  306. if (* condition & ENET_SOCKET_WAIT_SEND)
  307. FD_SET (socket, & writeSet);
  308. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  309. FD_SET (socket, & readSet);
  310. selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
  311. if (selectCount < 0)
  312. return -1;
  313. * condition = ENET_SOCKET_WAIT_NONE;
  314. if (selectCount == 0)
  315. return 0;
  316. if (FD_ISSET (socket, & writeSet))
  317. * condition |= ENET_SOCKET_WAIT_SEND;
  318. if (FD_ISSET (socket, & readSet))
  319. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  320. return 0;
  321. #endif
  322. }
  323. #endif