socket.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef SOCKET_H
  2. #define SOCKET_H
  3. /*=========================================================================*\
  4. * Socket compatibilization module
  5. * LuaSocket toolkit
  6. *
  7. * BSD Sockets and WinSock are similar, but there are a few irritating
  8. * differences. Also, not all *nix platforms behave the same. This module
  9. * (and the associated usocket.h and wsocket.h) factor these differences and
  10. * creates a interface compatible with the io.h module.
  11. \*=========================================================================*/
  12. #include "io.h"
  13. /*=========================================================================*\
  14. * Platform specific compatibilization
  15. \*=========================================================================*/
  16. #ifdef _WIN32
  17. #include "wsocket.h"
  18. #define LUA_GAI_STRERROR gai_strerrorA
  19. #else
  20. #include "usocket.h"
  21. #define LUA_GAI_STRERROR gai_strerror
  22. #endif
  23. /*=========================================================================*\
  24. * The connect and accept functions accept a timeout and their
  25. * implementations are somewhat complicated. We chose to move
  26. * the timeout control into this module for these functions in
  27. * order to simplify the modules that use them.
  28. \*=========================================================================*/
  29. #include "timeout.h"
  30. /* convenient shorthand */
  31. typedef struct sockaddr SA;
  32. /*=========================================================================*\
  33. * Functions bellow implement a comfortable platform independent
  34. * interface to sockets
  35. \*=========================================================================*/
  36. #ifndef _WIN32
  37. #pragma GCC visibility push(hidden)
  38. #endif
  39. int socket_waitfd(p_socket ps, int sw, p_timeout tm);
  40. int socket_open(void);
  41. int socket_close(void);
  42. void socket_destroy(p_socket ps);
  43. int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, p_timeout tm);
  44. int socket_create(p_socket ps, int domain, int type, int protocol);
  45. int socket_bind(p_socket ps, SA *addr, socklen_t addr_len);
  46. int socket_listen(p_socket ps, int backlog);
  47. void socket_shutdown(p_socket ps, int how);
  48. int socket_connect(p_socket ps, SA *addr, socklen_t addr_len, p_timeout tm);
  49. int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *addr_len, p_timeout tm);
  50. int socket_send(p_socket ps, const char *data, size_t count, size_t *sent, p_timeout tm);
  51. int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, SA *addr, socklen_t addr_len, p_timeout tm);
  52. int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm);
  53. int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, SA *addr, socklen_t *addr_len, p_timeout tm);
  54. int socket_write(p_socket ps, const char *data, size_t count, size_t *sent, p_timeout tm);
  55. int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm);
  56. void socket_setblocking(p_socket ps);
  57. void socket_setnonblocking(p_socket ps);
  58. int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp);
  59. int socket_gethostbyname(const char *addr, struct hostent **hp);
  60. const char *socket_hoststrerror(int err);
  61. const char *socket_strerror(int err);
  62. const char *socket_ioerror(p_socket ps, int err);
  63. const char *socket_gaistrerror(int err);
  64. #ifndef _WIN32
  65. #pragma GCC visibility pop
  66. #endif
  67. #endif /* SOCKET_H */