unix.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 (const ENetAddress * address, char * name, size_t nameLength)
  89. {
  90. struct in_addr in;
  91. struct hostent * hostEntry = NULL;
  92. #ifdef HAS_GETHOSTBYADDR_R
  93. struct hostent hostData;
  94. char buffer [2048];
  95. int errnum;
  96. in.s_addr = address -> host;
  97. #if defined(linux) || defined(FREEBSD)
  98. gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
  99. #else
  100. hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum);
  101. #endif
  102. #else
  103. in.s_addr = address -> host;
  104. hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
  105. #endif
  106. if (hostEntry == NULL)
  107. {
  108. #ifdef HAS_INET_NTOP
  109. if (inet_ntop (AF_INET, & address -> host, name, nameLength) == NULL)
  110. #else
  111. char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
  112. if (addr != NULL)
  113. strncpy (name, addr, nameLength);
  114. else
  115. #endif
  116. return -1;
  117. return 0;
  118. }
  119. strncpy (name, hostEntry -> h_name, nameLength);
  120. return 0;
  121. }
  122. ENetSocket
  123. enet_socket_create (ENetSocketType type, const ENetAddress * address)
  124. {
  125. ENetSocket newSocket = socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
  126. int receiveBufferSize = ENET_HOST_RECEIVE_BUFFER_SIZE,
  127. sendBufferSize = ENET_HOST_SEND_BUFFER_SIZE,
  128. allowBroadcasting = 1;
  129. #ifndef HAS_FCNTL
  130. int nonBlocking = 1;
  131. #endif
  132. struct sockaddr_in sin;
  133. if (newSocket == ENET_SOCKET_NULL)
  134. return ENET_SOCKET_NULL;
  135. if (type == ENET_SOCKET_TYPE_DATAGRAM)
  136. {
  137. #ifdef HAS_FCNTL
  138. fcntl (newSocket, F_SETFL, O_NONBLOCK | fcntl (newSocket, F_GETFL));
  139. #else
  140. ioctl (newSocket, FIONBIO, & nonBlocking);
  141. #endif
  142. setsockopt (newSocket, SOL_SOCKET, SO_RCVBUF, (char *) & receiveBufferSize, sizeof (int));
  143. setsockopt (newSocket, SOL_SOCKET, SO_SNDBUF, (char *) & sendBufferSize, sizeof (int));
  144. setsockopt (newSocket, SOL_SOCKET, SO_BROADCAST, (char *) & allowBroadcasting, sizeof (int));
  145. }
  146. if (address == NULL)
  147. return newSocket;
  148. memset (& sin, 0, sizeof (struct sockaddr_in));
  149. sin.sin_family = AF_INET;
  150. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  151. sin.sin_addr.s_addr = address -> host;
  152. if (bind (newSocket,
  153. (struct sockaddr *) & sin,
  154. sizeof (struct sockaddr_in)) == -1 ||
  155. (type == ENET_SOCKET_TYPE_STREAM &&
  156. listen (newSocket, SOMAXCONN) == -1))
  157. {
  158. close (newSocket);
  159. return ENET_SOCKET_NULL;
  160. }
  161. return newSocket;
  162. }
  163. int
  164. enet_socket_connect (ENetSocket socket, const ENetAddress * address)
  165. {
  166. struct sockaddr_in sin;
  167. memset (& sin, 0, sizeof (struct sockaddr_in));
  168. sin.sin_family = AF_INET;
  169. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  170. sin.sin_addr.s_addr = address -> host;
  171. return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
  172. }
  173. ENetSocket
  174. enet_socket_accept (ENetSocket socket, ENetAddress * address)
  175. {
  176. int result;
  177. struct sockaddr_in sin;
  178. socklen_t sinLength = sizeof (struct sockaddr_in);
  179. result = accept (socket,
  180. address != NULL ? (struct sockaddr *) & sin : NULL,
  181. address != NULL ? & sinLength : NULL);
  182. if (result == -1)
  183. return ENET_SOCKET_NULL;
  184. if (address != NULL)
  185. {
  186. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  187. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  188. }
  189. return result;
  190. }
  191. void
  192. enet_socket_destroy (ENetSocket socket)
  193. {
  194. close (socket);
  195. }
  196. int
  197. enet_socket_send (ENetSocket socket,
  198. const ENetAddress * address,
  199. const ENetBuffer * buffers,
  200. size_t bufferCount)
  201. {
  202. struct msghdr msgHdr;
  203. struct sockaddr_in sin;
  204. int sentLength;
  205. memset (& msgHdr, 0, sizeof (struct msghdr));
  206. if (address != NULL)
  207. {
  208. sin.sin_family = AF_INET;
  209. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  210. sin.sin_addr.s_addr = address -> host;
  211. msgHdr.msg_name = & sin;
  212. msgHdr.msg_namelen = sizeof (struct sockaddr_in);
  213. }
  214. msgHdr.msg_iov = (struct iovec *) buffers;
  215. msgHdr.msg_iovlen = bufferCount;
  216. sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL);
  217. if (sentLength == -1)
  218. {
  219. if (errno == EWOULDBLOCK)
  220. return 0;
  221. return -1;
  222. }
  223. return sentLength;
  224. }
  225. int
  226. enet_socket_receive (ENetSocket socket,
  227. ENetAddress * address,
  228. ENetBuffer * buffers,
  229. size_t bufferCount)
  230. {
  231. struct msghdr msgHdr;
  232. struct sockaddr_in sin;
  233. int recvLength;
  234. memset (& msgHdr, 0, sizeof (struct msghdr));
  235. if (address != NULL)
  236. {
  237. msgHdr.msg_name = & sin;
  238. msgHdr.msg_namelen = sizeof (struct sockaddr_in);
  239. }
  240. msgHdr.msg_iov = (struct iovec *) buffers;
  241. msgHdr.msg_iovlen = bufferCount;
  242. recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL);
  243. if (recvLength == -1)
  244. {
  245. if (errno == EWOULDBLOCK)
  246. return 0;
  247. return -1;
  248. }
  249. #ifdef HAS_MSGHDR_FLAGS
  250. if (msgHdr.msg_flags & MSG_TRUNC)
  251. return -1;
  252. #endif
  253. if (address != NULL)
  254. {
  255. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  256. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  257. }
  258. return recvLength;
  259. }
  260. int
  261. enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
  262. {
  263. #ifdef HAS_POLL
  264. struct pollfd pollSocket;
  265. int pollCount;
  266. pollSocket.fd = socket;
  267. pollSocket.events = 0;
  268. if (* condition & ENET_SOCKET_WAIT_SEND)
  269. pollSocket.events |= POLLOUT;
  270. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  271. pollSocket.events |= POLLIN;
  272. pollCount = poll (& pollSocket, 1, timeout);
  273. if (pollCount < 0)
  274. return -1;
  275. * condition = ENET_SOCKET_WAIT_NONE;
  276. if (pollCount == 0)
  277. return 0;
  278. if (pollSocket.revents & POLLOUT)
  279. * condition |= ENET_SOCKET_WAIT_SEND;
  280. if (pollSocket.revents & POLLIN)
  281. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  282. return 0;
  283. #else
  284. fd_set readSet, writeSet;
  285. struct timeval timeVal;
  286. int selectCount;
  287. timeVal.tv_sec = timeout / 1000;
  288. timeVal.tv_usec = (timeout % 1000) * 1000;
  289. FD_ZERO (& readSet);
  290. FD_ZERO (& writeSet);
  291. if (* condition & ENET_SOCKET_WAIT_SEND)
  292. FD_SET (socket, & writeSet);
  293. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  294. FD_SET (socket, & readSet);
  295. selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
  296. if (selectCount < 0)
  297. return -1;
  298. * condition = ENET_SOCKET_WAIT_NONE;
  299. if (selectCount == 0)
  300. return 0;
  301. if (FD_ISSET (socket, & writeSet))
  302. * condition |= ENET_SOCKET_WAIT_SEND;
  303. if (FD_ISSET (socket, & readSet))
  304. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  305. return 0;
  306. #endif
  307. }
  308. #endif