lua_socket.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #ifndef LUA_SOCKET_H
  2. #define LUA_SOCKET_H
  3. /*=========================================================================*\
  4. * LuaSocket toolkit
  5. * Networking support for the Lua language
  6. * Diego Nehab
  7. * 26/11/1999
  8. *
  9. * This library is part of an effort to progressively increase the network
  10. * connectivity of the Lua language. The Lua interface to networking
  11. * functions follows the Sockets API closely, trying to simplify all tasks
  12. * involved in setting up both client and server connections. The provided
  13. * IO routines, however, follow the Lua style, being very similar to the
  14. * standard Lua read and write functions.
  15. *
  16. * Adapted to Squirrel by Domingo Alvarez Duarte on 04/01/2013
  17. *
  18. * RCS ID: $Id: luasocket.c,v 1.53 2005/10/07 04:40:59 diego Exp $
  19. \*=========================================================================*/
  20. #ifdef WIN32
  21. /*=========================================================================*\
  22. * Socket compatibilization module for Win32
  23. * LuaSocket toolkit
  24. *
  25. * RCS ID: $Id: wsocket.h,v 1.4 2005/10/07 04:40:59 diego Exp $
  26. \*=========================================================================*/
  27. /*=========================================================================*\
  28. * WinSock include files
  29. \*=========================================================================*/
  30. #include <winsock.h>
  31. typedef int socklen_t;
  32. typedef SOCKET t_socket;
  33. typedef t_socket *p_socket;
  34. #define SOCKET_INVALID (INVALID_SOCKET)
  35. #else
  36. /*=========================================================================*\
  37. * Socket compatibilization module for Unix
  38. * LuaSocket toolkit
  39. *
  40. * RCS ID: $Id: usocket.h,v 1.7 2005/10/07 04:40:59 diego Exp $
  41. \*=========================================================================*/
  42. /*=========================================================================*\
  43. * BSD include files
  44. \*=========================================================================*/
  45. /* error codes */
  46. #include <errno.h>
  47. #include <string.h>
  48. /* close function */
  49. #include <unistd.h>
  50. /* fnctnl function and associated constants */
  51. #include <fcntl.h>
  52. /* struct sockaddr */
  53. #include <sys/types.h>
  54. /* socket function */
  55. #include <sys/socket.h>
  56. /* struct timeval */
  57. #include <sys/time.h>
  58. /* gethostbyname and gethostbyaddr functions */
  59. #include <netdb.h>
  60. /* sigpipe handling */
  61. #include <signal.h>
  62. /* IP stuff*/
  63. #include <netinet/in.h>
  64. #include <arpa/inet.h>
  65. /* TCP options (nagle algorithm disable) */
  66. #include <netinet/tcp.h>
  67. typedef int t_socket;
  68. typedef t_socket *p_socket;
  69. #define SOCKET_INVALID (-1)
  70. #endif
  71. /* we are lazy... */
  72. typedef struct sockaddr SA;
  73. /* timeout control structure */
  74. typedef struct t_timeout_ {
  75. double block; /* maximum time for blocking calls */
  76. double total; /* total number of miliseconds for operation */
  77. double start; /* time of start of operation */
  78. } t_timeout;
  79. typedef t_timeout *p_timeout;
  80. void lua_timeout_init(p_timeout tm, double block, double total);
  81. double lua_timeout_get(p_timeout tm);
  82. double lua_timeout_getretry(p_timeout tm);
  83. p_timeout lua_timeout_markstart(p_timeout tm);
  84. double lua_timeout_getstart(p_timeout tm);
  85. double lua_timeout_gettime(void);
  86. #define timeout_iszero(tm) ((tm)->block == 0.0)
  87. /* IO error codes */
  88. enum {
  89. IO_DONE = 0, /* operation completed successfully */
  90. IO_TIMEOUT = -1, /* operation timed out */
  91. IO_CLOSED = -2, /* the connection has been closed */
  92. IO_UNKNOWN = -3, /* Unknown error */
  93. IO_SSL = -4 /* SSL error */
  94. };
  95. /* interface to error message function */
  96. typedef const char *(*p_error) (
  97. void *ctx, /* context needed by send */
  98. int err /* error code */
  99. );
  100. /* interface to send function */
  101. typedef int (*p_send) (
  102. void *ctx, /* context needed by send */
  103. const char *data, /* pointer to buffer with data to send */
  104. size_t count, /* number of bytes to send from buffer */
  105. size_t *sent, /* number of bytes sent uppon return */
  106. p_timeout tm /* timeout control */
  107. );
  108. /* interface to recv function */
  109. typedef int (*p_recv) (
  110. void *ctx, /* context needed by recv */
  111. char *data, /* pointer to buffer where data will be writen */
  112. size_t count, /* number of bytes to receive into buffer */
  113. size_t *got, /* number of bytes received uppon return */
  114. p_timeout tm /* timeout control */
  115. );
  116. /* IO driver definition */
  117. typedef struct t_io_ {
  118. void *ctx; /* context needed by send/recv */
  119. p_send send; /* send function pointer */
  120. p_recv recv; /* receive function pointer */
  121. p_error error; /* strerror function */
  122. } t_io;
  123. typedef t_io *p_io;
  124. void lua_io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx);
  125. const char *lua_io_strerror(int err);
  126. /*=========================================================================*\
  127. * Functions bellow implement a comfortable platform independent
  128. * interface to sockets
  129. \*=========================================================================*/
  130. int lua_socket_open(void);
  131. int lua_socket_close(void);
  132. void lua_socket_destroy(p_socket ps);
  133. void lua_socket_shutdown(p_socket ps, int how);
  134. int lua_socket_sendto(p_socket ps, const char *data, size_t count,
  135. size_t *sent, SA *addr, socklen_t addr_len, p_timeout tm);
  136. int lua_socket_recvfrom(p_socket ps, char *data, size_t count,
  137. size_t *got, SA *addr, socklen_t *addr_len, p_timeout tm);
  138. void lua_socket_setnonblocking(p_socket ps);
  139. void lua_socket_setblocking(p_socket ps);
  140. int lua_socket_waitfd(p_socket ps, int sw, p_timeout tm);
  141. int lua_socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds,
  142. p_timeout tm);
  143. int lua_socket_connect(p_socket ps, SA *addr, socklen_t addr_len, p_timeout tm);
  144. int lua_socket_create(p_socket ps, int domain, int type, int protocol);
  145. int lua_socket_bind(p_socket ps, SA *addr, socklen_t addr_len);
  146. int lua_socket_listen(p_socket ps, int backlog);
  147. int lua_socket_accept(p_socket ps, p_socket pa, SA *addr,
  148. socklen_t *addr_len, p_timeout tm);
  149. const char *lua_socket_hoststrerror(int err);
  150. const char *lua_socket_strerror(int err);
  151. /* these are perfect to use with the io abstraction module
  152. and the buffered input module */
  153. int lua_socket_send(p_socket ps, const char *data, size_t count,
  154. size_t *sent, p_timeout tm);
  155. int lua_socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm);
  156. const char *lua_socket_ioerror(p_socket ps, int err);
  157. int lua_socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp);
  158. int lua_socket_gethostbyname(const char *addr, struct hostent **hp);
  159. #endif //LUA_SOCKET_H