diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2002-07-03 19:06:54 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2002-07-03 19:06:54 +0000 |
| commit | 88026bef8a5ca586e354965a79134646fb566c72 (patch) | |
| tree | 7594d9748c9dfd5db4addc0756b0cf86a0d9206a | |
| parent | 9b8bce6465d2c7de84782f9e12f529a020a16444 (diff) | |
| download | luasocket-88026bef8a5ca586e354965a79134646fb566c72.tar.gz luasocket-88026bef8a5ca586e354965a79134646fb566c72.tar.bz2 luasocket-88026bef8a5ca586e354965a79134646fb566c72.zip | |
Initial revision
| -rw-r--r-- | src/inet.h | 36 | ||||
| -rw-r--r-- | src/select.h | 7 | ||||
| -rw-r--r-- | src/socket.h | 18 | ||||
| -rw-r--r-- | src/timeout.c | 160 | ||||
| -rw-r--r-- | src/timeout.h | 20 | ||||
| -rw-r--r-- | src/udp.c | 287 | ||||
| -rw-r--r-- | src/udp.h | 24 |
7 files changed, 552 insertions, 0 deletions
diff --git a/src/inet.h b/src/inet.h new file mode 100644 index 0000000..3b0453e --- /dev/null +++ b/src/inet.h | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | /*=========================================================================*\ | ||
| 2 | * Internet domain class | ||
| 3 | * RCS ID: $Id$ | ||
| 4 | \*=========================================================================*/ | ||
| 5 | #ifndef INET_H_ | ||
| 6 | #define INET_H_ | ||
| 7 | |||
| 8 | #include <lua.h> | ||
| 9 | #include "lssock.h" | ||
| 10 | |||
| 11 | /* class name */ | ||
| 12 | #define INET_CLASS "luasocket(inet)" | ||
| 13 | |||
| 14 | /*-------------------------------------------------------------------------*\ | ||
| 15 | * Socket fields | ||
| 16 | \*-------------------------------------------------------------------------*/ | ||
| 17 | #define INET_FIELDS SOCK_FIELDS | ||
| 18 | |||
| 19 | /*-------------------------------------------------------------------------*\ | ||
| 20 | * Socket structure | ||
| 21 | \*-------------------------------------------------------------------------*/ | ||
| 22 | typedef t_sock t_inet; | ||
| 23 | typedef t_inet *p_inet; | ||
| 24 | |||
| 25 | /*-------------------------------------------------------------------------*\ | ||
| 26 | * Exported functions | ||
| 27 | \*-------------------------------------------------------------------------*/ | ||
| 28 | void inet_open(lua_State *L); | ||
| 29 | void inet_construct(lua_State *L, p_inet inet); | ||
| 30 | void inet_inherit(lua_State *L, cchar *lsclass); | ||
| 31 | |||
| 32 | cchar *inet_tryconnect(p_sock sock, cchar *address, ushort); | ||
| 33 | cchar *inet_trybind(p_sock sock, cchar *address, ushort); | ||
| 34 | cchar *inet_trysocket(p_inet inet, int type); | ||
| 35 | |||
| 36 | #endif /* INET_H_ */ | ||
diff --git a/src/select.h b/src/select.h new file mode 100644 index 0000000..c3267ad --- /dev/null +++ b/src/select.h | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | #ifndef SLCT_H_ | ||
| 2 | #define SLCT_H_ | ||
| 3 | |||
| 4 | void select_addclass(lua_State *L, cchar *lsclass); | ||
| 5 | void select_open(lua_State *L); | ||
| 6 | |||
| 7 | #endif | ||
diff --git a/src/socket.h b/src/socket.h new file mode 100644 index 0000000..c9dee20 --- /dev/null +++ b/src/socket.h | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | #ifndef SOCK_H_ | ||
| 2 | #define SOCK_H_ | ||
| 3 | |||
| 4 | #include <lua.h> | ||
| 5 | #include "lsfd.h" | ||
| 6 | |||
| 7 | #define SOCK_CLASS "luasocket(sock)" | ||
| 8 | |||
| 9 | #define SOCK_FIELDS FD_FIELDS | ||
| 10 | |||
| 11 | typedef t_fd t_sock; | ||
| 12 | typedef t_sock *p_sock; | ||
| 13 | |||
| 14 | void sock_open(lua_State *L); | ||
| 15 | void sock_construct(lua_State *L, p_sock sock); | ||
| 16 | void sock_inherit(lua_State *L, cchar *lsclass); | ||
| 17 | |||
| 18 | #endif /* SOCK_H_ */ | ||
diff --git a/src/timeout.c b/src/timeout.c new file mode 100644 index 0000000..266a86e --- /dev/null +++ b/src/timeout.c | |||
| @@ -0,0 +1,160 @@ | |||
| 1 | /*=========================================================================*\ | ||
| 2 | * Timeout management functions | ||
| 3 | \*=========================================================================*/ | ||
| 4 | #include <lua.h> | ||
| 5 | #include <lauxlib.h> | ||
| 6 | |||
| 7 | #include "lspriv.h" | ||
| 8 | #include "lstm.h" | ||
| 9 | |||
| 10 | #include <stdio.h> | ||
| 11 | |||
| 12 | #ifdef WIN32 | ||
| 13 | #include <windows.h> | ||
| 14 | #else | ||
| 15 | #include <sys/times.h> | ||
| 16 | #include <time.h> | ||
| 17 | #include <unistd.h> | ||
| 18 | #endif | ||
| 19 | |||
| 20 | /*=========================================================================*\ | ||
| 21 | * Internal function prototypes | ||
| 22 | \*=========================================================================*/ | ||
| 23 | #ifdef _DEBUG | ||
| 24 | static int tm_lua_time(lua_State *L); | ||
| 25 | static int tm_lua_sleep(lua_State *L); | ||
| 26 | #endif | ||
| 27 | |||
| 28 | /*=========================================================================*\ | ||
| 29 | * Exported functions. | ||
| 30 | \*=========================================================================*/ | ||
| 31 | /*-------------------------------------------------------------------------*\ | ||
| 32 | * Sets timeout limits | ||
| 33 | * Input | ||
| 34 | * tm: timeout control structure | ||
| 35 | * mode: block or return timeout | ||
| 36 | * value: timeout value in miliseconds | ||
| 37 | \*-------------------------------------------------------------------------*/ | ||
| 38 | void tm_set(p_tm tm, int tm_block, int tm_return) | ||
| 39 | { | ||
| 40 | tm->tm_block = tm_block; | ||
| 41 | tm->tm_return = tm_return; | ||
| 42 | } | ||
| 43 | |||
| 44 | /*-------------------------------------------------------------------------*\ | ||
| 45 | * Returns timeout limits | ||
| 46 | * Input | ||
| 47 | * tm: timeout control structure | ||
| 48 | * mode: block or return timeout | ||
| 49 | * value: timeout value in miliseconds | ||
| 50 | \*-------------------------------------------------------------------------*/ | ||
| 51 | void tm_get(p_tm tm, int *tm_block, int *tm_return) | ||
| 52 | { | ||
| 53 | if (tm_block) *tm_block = tm->tm_block; | ||
| 54 | if (tm_return) *tm_return = tm->tm_return; | ||
| 55 | } | ||
| 56 | |||
| 57 | /*-------------------------------------------------------------------------*\ | ||
| 58 | * Determines how much time we have left for the current io operation | ||
| 59 | * an IO write operation. | ||
| 60 | * Input | ||
| 61 | * tm: timeout control structure | ||
| 62 | * Returns | ||
| 63 | * the number of ms left or -1 if there is no time limit | ||
| 64 | \*-------------------------------------------------------------------------*/ | ||
| 65 | int tm_getremaining(p_tm tm) | ||
| 66 | { | ||
| 67 | /* no timeout */ | ||
| 68 | if (tm->tm_block < 0 && tm->tm_return < 0) | ||
| 69 | return -1; | ||
| 70 | /* there is no block timeout, we use the return timeout */ | ||
| 71 | else if (tm->tm_block < 0) | ||
| 72 | return MAX(tm->tm_return - tm_gettime() + tm->tm_start, 0); | ||
| 73 | /* there is no return timeout, we use the block timeout */ | ||
| 74 | else if (tm->tm_return < 0) | ||
| 75 | return tm->tm_block; | ||
| 76 | /* both timeouts are specified */ | ||
| 77 | else return MIN(tm->tm_block, | ||
| 78 | MAX(tm->tm_return - tm_gettime() + tm->tm_start, 0)); | ||
| 79 | } | ||
| 80 | |||
| 81 | /*-------------------------------------------------------------------------*\ | ||
| 82 | * Marks the operation start time in sock structure | ||
| 83 | * Input | ||
| 84 | * tm: timeout control structure | ||
| 85 | \*-------------------------------------------------------------------------*/ | ||
| 86 | void tm_markstart(p_tm tm) | ||
| 87 | { | ||
| 88 | tm->tm_start = tm_gettime(); | ||
| 89 | tm->tm_end = tm->tm_start; | ||
| 90 | } | ||
| 91 | |||
| 92 | /*-------------------------------------------------------------------------*\ | ||
| 93 | * Returns the length of the operation in ms | ||
| 94 | * Input | ||
| 95 | * tm: timeout control structure | ||
| 96 | \*-------------------------------------------------------------------------*/ | ||
| 97 | int tm_getelapsed(p_tm tm) | ||
| 98 | { | ||
| 99 | return tm->tm_end - tm->tm_start; | ||
| 100 | } | ||
| 101 | |||
| 102 | /*-------------------------------------------------------------------------*\ | ||
| 103 | * Gets time in ms, relative to system startup. | ||
| 104 | * Returns | ||
| 105 | * time in ms. | ||
| 106 | \*-------------------------------------------------------------------------*/ | ||
| 107 | #ifdef WIN32 | ||
| 108 | int tm_gettime(void) | ||
| 109 | { | ||
| 110 | return GetTickCount(); | ||
| 111 | } | ||
| 112 | #else | ||
| 113 | int tm_gettime(void) | ||
| 114 | { | ||
| 115 | struct tms t; | ||
| 116 | return (times(&t)*1000)/CLK_TCK; | ||
| 117 | } | ||
| 118 | #endif | ||
| 119 | |||
| 120 | /*-------------------------------------------------------------------------*\ | ||
| 121 | * Initializes module | ||
| 122 | \*-------------------------------------------------------------------------*/ | ||
| 123 | void tm_open(lua_State *L) | ||
| 124 | { | ||
| 125 | (void) L; | ||
| 126 | #ifdef _DEBUG | ||
| 127 | lua_pushcfunction(L, tm_lua_time); | ||
| 128 | lua_setglobal(L, "_time"); | ||
| 129 | lua_pushcfunction(L, tm_lua_sleep); | ||
| 130 | lua_setglobal(L, "_sleep"); | ||
| 131 | #endif | ||
| 132 | } | ||
| 133 | |||
| 134 | /*=========================================================================*\ | ||
| 135 | * Test support functions | ||
| 136 | \*=========================================================================*/ | ||
| 137 | /*-------------------------------------------------------------------------*\ | ||
| 138 | * Returns the time the system has been up, in secconds. | ||
| 139 | \*-------------------------------------------------------------------------*/ | ||
| 140 | #ifdef _DEBUG | ||
| 141 | static int tm_lua_time(lua_State *L) | ||
| 142 | { | ||
| 143 | lua_pushnumber(L, tm_gettime()/1000.0); | ||
| 144 | return 1; | ||
| 145 | } | ||
| 146 | |||
| 147 | /*-------------------------------------------------------------------------*\ | ||
| 148 | * Sleep for n seconds. | ||
| 149 | \*-------------------------------------------------------------------------*/ | ||
| 150 | int tm_lua_sleep(lua_State *L) | ||
| 151 | { | ||
| 152 | double n = luaL_check_number(L, 1); | ||
| 153 | #ifdef WIN32 | ||
| 154 | Sleep(n*1000); | ||
| 155 | #else | ||
| 156 | sleep(n); | ||
| 157 | #endif | ||
| 158 | return 0; | ||
| 159 | } | ||
| 160 | #endif | ||
diff --git a/src/timeout.h b/src/timeout.h new file mode 100644 index 0000000..af7e591 --- /dev/null +++ b/src/timeout.h | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | #ifndef _TM_H | ||
| 2 | #define _TM_H | ||
| 3 | |||
| 4 | typedef struct t_tm_tag { | ||
| 5 | int tm_return; | ||
| 6 | int tm_block; | ||
| 7 | int tm_start; | ||
| 8 | int tm_end; | ||
| 9 | } t_tm; | ||
| 10 | typedef t_tm *p_tm; | ||
| 11 | |||
| 12 | void tm_set(p_tm tm, int tm_block, int tm_return); | ||
| 13 | int tm_getremaining(p_tm tm); | ||
| 14 | int tm_getelapsed(p_tm tm); | ||
| 15 | int tm_gettime(void); | ||
| 16 | void tm_get(p_tm tm, int *tm_block, int *tm_return); | ||
| 17 | void tm_markstart(p_tm tm); | ||
| 18 | void tm_open(lua_State *L); | ||
| 19 | |||
| 20 | #endif | ||
diff --git a/src/udp.c b/src/udp.c new file mode 100644 index 0000000..0dc0df8 --- /dev/null +++ b/src/udp.c | |||
| @@ -0,0 +1,287 @@ | |||
| 1 | /*=========================================================================*\ | ||
| 2 | * UDP socket object implementation (inherits from sock and inet) | ||
| 3 | \*=========================================================================*/ | ||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | #include <lua.h> | ||
| 7 | #include <lauxlib.h> | ||
| 8 | |||
| 9 | #include "lsinet.h" | ||
| 10 | #include "lsudp.h" | ||
| 11 | #include "lscompat.h" | ||
| 12 | #include "lsselect.h" | ||
| 13 | |||
| 14 | /*=========================================================================*\ | ||
| 15 | * Internal function prototypes. | ||
| 16 | \*=========================================================================*/ | ||
| 17 | static int udp_lua_send(lua_State *L); | ||
| 18 | static int udp_lua_sendto(lua_State *L); | ||
| 19 | static int udp_lua_receive(lua_State *L); | ||
| 20 | static int udp_lua_receivefrom(lua_State *L); | ||
| 21 | static int udp_lua_setpeername(lua_State *L); | ||
| 22 | static int udp_lua_setsockname(lua_State *L); | ||
| 23 | |||
| 24 | static int udp_global_udpsocket(lua_State *L); | ||
| 25 | |||
| 26 | static struct luaL_reg funcs[] = { | ||
| 27 | {"send", udp_lua_send}, | ||
| 28 | {"sendto", udp_lua_sendto}, | ||
| 29 | {"receive", udp_lua_receive}, | ||
| 30 | {"receivefrom", udp_lua_receivefrom}, | ||
| 31 | {"setpeername", udp_lua_setpeername}, | ||
| 32 | {"setsockname", udp_lua_setsockname}, | ||
| 33 | }; | ||
| 34 | |||
| 35 | /*=========================================================================*\ | ||
| 36 | * Exported functions | ||
| 37 | \*=========================================================================*/ | ||
| 38 | /*-------------------------------------------------------------------------*\ | ||
| 39 | * Initializes module | ||
| 40 | \*-------------------------------------------------------------------------*/ | ||
| 41 | void udp_open(lua_State *L) | ||
| 42 | { | ||
| 43 | unsigned int i; | ||
| 44 | priv_newclass(L, UDP_CLASS); | ||
| 45 | udp_inherit(L, UDP_CLASS); | ||
| 46 | /* declare global functions */ | ||
| 47 | lua_pushcfunction(L, udp_global_udpsocket); | ||
| 48 | lua_setglobal(L, "udpsocket"); | ||
| 49 | for (i = 0; i < sizeof(funcs)/sizeof(funcs[0]); i++) | ||
| 50 | priv_newglobalmethod(L, funcs[i].name); | ||
| 51 | /* make class selectable */ | ||
| 52 | select_addclass(L, UDP_CLASS); | ||
| 53 | } | ||
| 54 | |||
| 55 | /*-------------------------------------------------------------------------*\ | ||
| 56 | * Hook object methods to methods table. | ||
| 57 | \*-------------------------------------------------------------------------*/ | ||
| 58 | void udp_inherit(lua_State *L, cchar *lsclass) | ||
| 59 | { | ||
| 60 | unsigned int i; | ||
| 61 | inet_inherit(L, lsclass); | ||
| 62 | for (i = 0; i < sizeof(funcs)/sizeof(funcs[0]); i++) { | ||
| 63 | lua_pushcfunction(L, funcs[i].func); | ||
| 64 | priv_setmethod(L, lsclass, funcs[i].name); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | /*-------------------------------------------------------------------------*\ | ||
| 69 | * Initializes socket structure | ||
| 70 | \*-------------------------------------------------------------------------*/ | ||
| 71 | void udp_construct(lua_State *L, p_udp udp) | ||
| 72 | { | ||
| 73 | inet_construct(L, (p_inet) udp); | ||
| 74 | udp->udp_connected = 0; | ||
| 75 | } | ||
| 76 | |||
| 77 | /*-------------------------------------------------------------------------*\ | ||
| 78 | * Creates a socket structure and initializes it. A socket object is | ||
| 79 | * left in the Lua stack. | ||
| 80 | * Returns | ||
| 81 | * pointer to allocated structure | ||
| 82 | \*-------------------------------------------------------------------------*/ | ||
| 83 | p_udp udp_push(lua_State *L) | ||
| 84 | { | ||
| 85 | p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); | ||
| 86 | priv_setclass(L, UDP_CLASS); | ||
| 87 | udp_construct(L, udp); | ||
| 88 | return udp; | ||
| 89 | } | ||
| 90 | |||
| 91 | /*=========================================================================*\ | ||
| 92 | * Socket table constructors | ||
| 93 | \*=========================================================================*/ | ||
| 94 | /*-------------------------------------------------------------------------*\ | ||
| 95 | * Creates a udp socket object and returns it to the Lua script. | ||
| 96 | * Lua Input: [options] | ||
| 97 | * options: socket options table | ||
| 98 | * Lua Returns | ||
| 99 | * On success: udp socket | ||
| 100 | * On error: nil, followed by an error message | ||
| 101 | \*-------------------------------------------------------------------------*/ | ||
| 102 | static int udp_global_udpsocket(lua_State *L) | ||
| 103 | { | ||
| 104 | int oldtop = lua_gettop(L); | ||
| 105 | p_udp udp = udp_push(L); | ||
| 106 | cchar *err = inet_trysocket((p_inet) udp, SOCK_DGRAM); | ||
| 107 | if (err) { | ||
| 108 | lua_pushnil(L); | ||
| 109 | lua_pushstring(L, err); | ||
| 110 | return 2; | ||
| 111 | } | ||
| 112 | if (oldtop < 1) return 1; | ||
| 113 | err = compat_trysetoptions(L, udp->fd); | ||
| 114 | if (err) { | ||
| 115 | lua_pushnil(L); | ||
| 116 | lua_pushstring(L, err); | ||
| 117 | return 2; | ||
| 118 | } | ||
| 119 | return 1; | ||
| 120 | } | ||
| 121 | |||
| 122 | /*=========================================================================*\ | ||
| 123 | * Socket table methods | ||
| 124 | \*=========================================================================*/ | ||
| 125 | /*-------------------------------------------------------------------------*\ | ||
| 126 | * Receives data from a UDP socket | ||
| 127 | * Lua Input: sock [, wanted] | ||
| 128 | * sock: client socket created by the connect function | ||
| 129 | * wanted: the number of bytes expected (default: LUASOCKET_UDPBUFFERSIZE) | ||
| 130 | * Lua Returns | ||
| 131 | * On success: datagram received | ||
| 132 | * On error: nil, followed by an error message | ||
| 133 | \*-------------------------------------------------------------------------*/ | ||
| 134 | static int udp_lua_receive(lua_State *L) | ||
| 135 | { | ||
| 136 | p_udp udp = (p_udp) lua_touserdata(L, 1); | ||
| 137 | unsigned char buffer[UDP_DATAGRAMSIZE]; | ||
| 138 | size_t got, wanted = (size_t) luaL_opt_number(L, 2, sizeof(buffer)); | ||
| 139 | int err; | ||
| 140 | p_tm tm = &udp->base_tm; | ||
| 141 | wanted = MIN(wanted, sizeof(buffer)); | ||
| 142 | tm_markstart(tm); | ||
| 143 | err = compat_recv(udp->fd, buffer, wanted, &got, tm_getremaining(tm)); | ||
| 144 | if (err == PRIV_CLOSED) err = PRIV_REFUSED; | ||
| 145 | if (err != PRIV_DONE) lua_pushnil(L); | ||
| 146 | else lua_pushlstring(L, buffer, got); | ||
| 147 | priv_pusherror(L, err); | ||
| 148 | return 2; | ||
| 149 | } | ||
| 150 | |||
| 151 | /*-------------------------------------------------------------------------*\ | ||
| 152 | * Receives a datagram from a UDP socket | ||
| 153 | * Lua Input: sock [, wanted] | ||
| 154 | * sock: client socket created by the connect function | ||
| 155 | * wanted: the number of bytes expected (default: LUASOCKET_UDPBUFFERSIZE) | ||
| 156 | * Lua Returns | ||
| 157 | * On success: datagram received, ip and port of sender | ||
| 158 | * On error: nil, followed by an error message | ||
| 159 | \*-------------------------------------------------------------------------*/ | ||
| 160 | static int udp_lua_receivefrom(lua_State *L) | ||
| 161 | { | ||
| 162 | p_udp udp = (p_udp) lua_touserdata(L, 1); | ||
| 163 | p_tm tm = &udp->base_tm; | ||
| 164 | struct sockaddr_in peer; | ||
| 165 | size_t peer_len = sizeof(peer); | ||
| 166 | unsigned char buffer[UDP_DATAGRAMSIZE]; | ||
| 167 | size_t wanted = (size_t) luaL_opt_number(L, 2, sizeof(buffer)); | ||
| 168 | size_t got; | ||
| 169 | int err; | ||
| 170 | if (udp->udp_connected) lua_error(L, "receivefrom on connected socket"); | ||
| 171 | tm_markstart(tm); | ||
| 172 | wanted = MIN(wanted, sizeof(buffer)); | ||
| 173 | err = compat_recvfrom(udp->fd, buffer, wanted, &got, tm_getremaining(tm), | ||
| 174 | (SA *) &peer, &peer_len); | ||
| 175 | if (err == PRIV_CLOSED) err = PRIV_REFUSED; | ||
| 176 | if (err == PRIV_DONE) { | ||
| 177 | lua_pushlstring(L, buffer, got); | ||
| 178 | lua_pushstring(L, inet_ntoa(peer.sin_addr)); | ||
| 179 | lua_pushnumber(L, ntohs(peer.sin_port)); | ||
| 180 | return 3; | ||
| 181 | } else { | ||
| 182 | lua_pushnil(L); | ||
| 183 | priv_pusherror(L, err); | ||
| 184 | return 2; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | /*-------------------------------------------------------------------------*\ | ||
| 189 | * Send data through a connected UDP socket | ||
| 190 | * Lua Input: sock, data | ||
| 191 | * sock: udp socket | ||
| 192 | * data: data to be sent | ||
| 193 | * Lua Returns | ||
| 194 | * On success: nil, followed by the total number of bytes sent | ||
| 195 | * On error: error message | ||
| 196 | \*-------------------------------------------------------------------------*/ | ||
| 197 | static int udp_lua_send(lua_State *L) | ||
| 198 | { | ||
| 199 | p_udp udp = (p_udp) lua_touserdata(L, 1); | ||
| 200 | p_tm tm = &udp->base_tm; | ||
| 201 | size_t wanted, sent = 0; | ||
| 202 | int err; | ||
| 203 | cchar *data = luaL_check_lstr(L, 2, &wanted); | ||
| 204 | if (!udp->udp_connected) lua_error(L, "send on unconnected socket"); | ||
| 205 | tm_markstart(tm); | ||
| 206 | err = compat_send(udp->fd, data, wanted, &sent, tm_getremaining(tm)); | ||
| 207 | priv_pusherror(L, err == PRIV_CLOSED ? PRIV_REFUSED : err); | ||
| 208 | lua_pushnumber(L, sent); | ||
| 209 | return 2; | ||
| 210 | } | ||
| 211 | |||
| 212 | /*-------------------------------------------------------------------------*\ | ||
| 213 | * Send data through a unconnected UDP socket | ||
| 214 | * Lua Input: sock, data, ip, port | ||
| 215 | * sock: udp socket | ||
| 216 | * data: data to be sent | ||
| 217 | * ip: ip address of target | ||
| 218 | * port: port in target | ||
| 219 | * Lua Returns | ||
| 220 | * On success: nil, followed by the total number of bytes sent | ||
| 221 | * On error: error message | ||
| 222 | \*-------------------------------------------------------------------------*/ | ||
| 223 | static int udp_lua_sendto(lua_State *L) | ||
| 224 | { | ||
| 225 | p_udp udp = (p_udp) lua_touserdata(L, 1); | ||
| 226 | size_t wanted, sent = 0; | ||
| 227 | cchar *data = luaL_check_lstr(L, 2, &wanted); | ||
| 228 | cchar *ip = luaL_check_string(L, 3); | ||
| 229 | ushort port = (ushort) luaL_check_number(L, 4); | ||
| 230 | p_tm tm = &udp->base_tm; | ||
| 231 | struct sockaddr_in peer; | ||
| 232 | int err; | ||
| 233 | if (udp->udp_connected) lua_error(L, "sendto on connected socket"); | ||
| 234 | memset(&peer, 0, sizeof(peer)); | ||
| 235 | if (!inet_aton(ip, &peer.sin_addr)) lua_error(L, "invalid ip address"); | ||
| 236 | peer.sin_family = AF_INET; | ||
| 237 | peer.sin_port = htons(port); | ||
| 238 | tm_markstart(tm); | ||
| 239 | err = compat_sendto(udp->fd, data, wanted, &sent, tm_getremaining(tm), | ||
| 240 | (SA *) &peer, sizeof(peer)); | ||
| 241 | priv_pusherror(L, err == PRIV_CLOSED ? PRIV_REFUSED : err); | ||
| 242 | lua_pushnumber(L, sent); | ||
| 243 | return 2; | ||
| 244 | } | ||
| 245 | |||
| 246 | /*-------------------------------------------------------------------------*\ | ||
| 247 | * Associates a local address to an UDP socket | ||
| 248 | * Lua Input: address, port | ||
| 249 | * address: host name or ip address to bind to | ||
| 250 | * port: port to bind to | ||
| 251 | * Lua Returns | ||
| 252 | * On success: nil | ||
| 253 | * On error: error message | ||
| 254 | \*-------------------------------------------------------------------------*/ | ||
| 255 | static int udp_lua_setsockname(lua_State * L) | ||
| 256 | { | ||
| 257 | p_udp udp = (p_udp) lua_touserdata(L, 1); | ||
| 258 | cchar *address = luaL_check_string(L, 2); | ||
| 259 | ushort port = (ushort) luaL_check_number(L, 3); | ||
| 260 | cchar *err = inet_trybind((p_inet) udp, address, port); | ||
| 261 | if (err) lua_pushstring(L, err); | ||
| 262 | else lua_pushnil(L); | ||
| 263 | return 1; | ||
| 264 | } | ||
| 265 | |||
| 266 | /*-------------------------------------------------------------------------*\ | ||
| 267 | * Sets a peer for a UDP socket | ||
| 268 | * Lua Input: address, port | ||
| 269 | * address: remote host name | ||
| 270 | * port: remote host port | ||
| 271 | * Lua Returns | ||
| 272 | * On success: nil | ||
| 273 | * On error: error message | ||
| 274 | \*-------------------------------------------------------------------------*/ | ||
| 275 | static int udp_lua_setpeername(lua_State *L) | ||
| 276 | { | ||
| 277 | p_udp udp = (p_udp) lua_touserdata(L, 1); | ||
| 278 | cchar *address = luaL_check_string(L, 2); | ||
| 279 | ushort port = (ushort) luaL_check_number(L, 3); | ||
| 280 | cchar *err = inet_tryconnect((p_inet) udp, address, port); | ||
| 281 | if (!err) { | ||
| 282 | udp->udp_connected = 1; | ||
| 283 | lua_pushnil(L); | ||
| 284 | } else lua_pushstring(L, err); | ||
| 285 | return 1; | ||
| 286 | } | ||
| 287 | |||
diff --git a/src/udp.h b/src/udp.h new file mode 100644 index 0000000..3c82c29 --- /dev/null +++ b/src/udp.h | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | #ifndef UDP_H_ | ||
| 2 | #define UDP_H_ | ||
| 3 | |||
| 4 | #include "lsinet.h" | ||
| 5 | |||
| 6 | #define UDP_CLASS "luasocket(UDP socket)" | ||
| 7 | |||
| 8 | #define UDP_DATAGRAMSIZE 576 | ||
| 9 | |||
| 10 | #define UDP_FIELDS \ | ||
| 11 | INET_FIELDS; \ | ||
| 12 | int udp_connected | ||
| 13 | |||
| 14 | typedef struct t_udp_tag { | ||
| 15 | UDP_FIELDS; | ||
| 16 | } t_udp; | ||
| 17 | typedef t_udp *p_udp; | ||
| 18 | |||
| 19 | void udp_inherit(lua_State *L, cchar *lsclass); | ||
| 20 | void udp_construct(lua_State *L, p_udp udp); | ||
| 21 | void udp_open(lua_State *L); | ||
| 22 | p_udp udp_push(lua_State *L); | ||
| 23 | |||
| 24 | #endif | ||
