diff options
Diffstat (limited to 'src/udp.c')
-rw-r--r-- | src/udp.c | 98 |
1 files changed, 62 insertions, 36 deletions
@@ -10,43 +10,49 @@ | |||
10 | 10 | ||
11 | #include "luasocket.h" | 11 | #include "luasocket.h" |
12 | 12 | ||
13 | #include "aux.h" | 13 | #include "auxiliar.h" |
14 | #include "socket.h" | ||
14 | #include "inet.h" | 15 | #include "inet.h" |
16 | #include "error.h" | ||
15 | #include "udp.h" | 17 | #include "udp.h" |
16 | 18 | ||
17 | /*=========================================================================*\ | 19 | /*=========================================================================*\ |
18 | * Internal function prototypes | 20 | * Internal function prototypes |
19 | \*=========================================================================*/ | 21 | \*=========================================================================*/ |
20 | static int udp_global_create(lua_State *L); | 22 | static int global_create(lua_State *L); |
21 | static int udp_meth_send(lua_State *L); | 23 | static int meth_send(lua_State *L); |
22 | static int udp_meth_sendto(lua_State *L); | 24 | static int meth_sendto(lua_State *L); |
23 | static int udp_meth_receive(lua_State *L); | 25 | static int meth_receive(lua_State *L); |
24 | static int udp_meth_receivefrom(lua_State *L); | 26 | static int meth_receivefrom(lua_State *L); |
25 | static int udp_meth_getsockname(lua_State *L); | 27 | static int meth_getsockname(lua_State *L); |
26 | static int udp_meth_getpeername(lua_State *L); | 28 | static int meth_getpeername(lua_State *L); |
27 | static int udp_meth_setsockname(lua_State *L); | 29 | static int meth_setsockname(lua_State *L); |
28 | static int udp_meth_setpeername(lua_State *L); | 30 | static int meth_setpeername(lua_State *L); |
29 | static int udp_meth_close(lua_State *L); | 31 | static int meth_close(lua_State *L); |
30 | static int udp_meth_timeout(lua_State *L); | 32 | static int meth_timeout(lua_State *L); |
33 | static int meth_fd(lua_State *L); | ||
34 | static int meth_dirty(lua_State *L); | ||
31 | 35 | ||
32 | /* udp object methods */ | 36 | /* udp object methods */ |
33 | static luaL_reg udp[] = { | 37 | static luaL_reg udp[] = { |
34 | {"setpeername", udp_meth_setpeername}, | 38 | {"setpeername", meth_setpeername}, |
35 | {"setsockname", udp_meth_setsockname}, | 39 | {"setsockname", meth_setsockname}, |
36 | {"getsockname", udp_meth_getsockname}, | 40 | {"getsockname", meth_getsockname}, |
37 | {"getpeername", udp_meth_getpeername}, | 41 | {"getpeername", meth_getpeername}, |
38 | {"send", udp_meth_send}, | 42 | {"send", meth_send}, |
39 | {"sendto", udp_meth_sendto}, | 43 | {"sendto", meth_sendto}, |
40 | {"receive", udp_meth_receive}, | 44 | {"receive", meth_receive}, |
41 | {"receivefrom", udp_meth_receivefrom}, | 45 | {"receivefrom", meth_receivefrom}, |
42 | {"timeout", udp_meth_timeout}, | 46 | {"timeout", meth_timeout}, |
43 | {"close", udp_meth_close}, | 47 | {"close", meth_close}, |
48 | {"fd", meth_fd}, | ||
49 | {"dirty", meth_dirty}, | ||
44 | {NULL, NULL} | 50 | {NULL, NULL} |
45 | }; | 51 | }; |
46 | 52 | ||
47 | /* functions in library namespace */ | 53 | /* functions in library namespace */ |
48 | static luaL_reg func[] = { | 54 | static luaL_reg func[] = { |
49 | {"udp", udp_global_create}, | 55 | {"udp", global_create}, |
50 | {NULL, NULL} | 56 | {NULL, NULL} |
51 | }; | 57 | }; |
52 | 58 | ||
@@ -59,8 +65,10 @@ void udp_open(lua_State *L) | |||
59 | aux_newclass(L, "udp{connected}", udp); | 65 | aux_newclass(L, "udp{connected}", udp); |
60 | aux_newclass(L, "udp{unconnected}", udp); | 66 | aux_newclass(L, "udp{unconnected}", udp); |
61 | /* create class groups */ | 67 | /* create class groups */ |
62 | aux_add2group(L, "udp{connected}", "udp{any}"); | 68 | aux_add2group(L, "udp{connected}", "udp{any}"); |
63 | aux_add2group(L, "udp{unconnected}", "udp{any}"); | 69 | aux_add2group(L, "udp{unconnected}", "udp{any}"); |
70 | aux_add2group(L, "udp{connected}", "select{able}"); | ||
71 | aux_add2group(L, "udp{unconnected}", "select{able}"); | ||
64 | /* define library functions */ | 72 | /* define library functions */ |
65 | luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); | 73 | luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); |
66 | lua_pop(L, 1); | 74 | lua_pop(L, 1); |
@@ -72,7 +80,7 @@ void udp_open(lua_State *L) | |||
72 | /*-------------------------------------------------------------------------*\ | 80 | /*-------------------------------------------------------------------------*\ |
73 | * Send data through connected udp socket | 81 | * Send data through connected udp socket |
74 | \*-------------------------------------------------------------------------*/ | 82 | \*-------------------------------------------------------------------------*/ |
75 | static int udp_meth_send(lua_State *L) | 83 | static int meth_send(lua_State *L) |
76 | { | 84 | { |
77 | p_udp udp = (p_udp) aux_checkclass(L, "udp{connected}", 1); | 85 | p_udp udp = (p_udp) aux_checkclass(L, "udp{connected}", 1); |
78 | p_tm tm = &udp->tm; | 86 | p_tm tm = &udp->tm; |
@@ -90,7 +98,7 @@ static int udp_meth_send(lua_State *L) | |||
90 | /*-------------------------------------------------------------------------*\ | 98 | /*-------------------------------------------------------------------------*\ |
91 | * Send data through unconnected udp socket | 99 | * Send data through unconnected udp socket |
92 | \*-------------------------------------------------------------------------*/ | 100 | \*-------------------------------------------------------------------------*/ |
93 | static int udp_meth_sendto(lua_State *L) | 101 | static int meth_sendto(lua_State *L) |
94 | { | 102 | { |
95 | p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); | 103 | p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); |
96 | size_t count, sent = 0; | 104 | size_t count, sent = 0; |
@@ -117,7 +125,7 @@ static int udp_meth_sendto(lua_State *L) | |||
117 | /*-------------------------------------------------------------------------*\ | 125 | /*-------------------------------------------------------------------------*\ |
118 | * Receives data from a UDP socket | 126 | * Receives data from a UDP socket |
119 | \*-------------------------------------------------------------------------*/ | 127 | \*-------------------------------------------------------------------------*/ |
120 | static int udp_meth_receive(lua_State *L) | 128 | static int meth_receive(lua_State *L) |
121 | { | 129 | { |
122 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 130 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); |
123 | char buffer[UDP_DATAGRAMSIZE]; | 131 | char buffer[UDP_DATAGRAMSIZE]; |
@@ -136,11 +144,11 @@ static int udp_meth_receive(lua_State *L) | |||
136 | /*-------------------------------------------------------------------------*\ | 144 | /*-------------------------------------------------------------------------*\ |
137 | * Receives data and sender from a UDP socket | 145 | * Receives data and sender from a UDP socket |
138 | \*-------------------------------------------------------------------------*/ | 146 | \*-------------------------------------------------------------------------*/ |
139 | static int udp_meth_receivefrom(lua_State *L) | 147 | static int meth_receivefrom(lua_State *L) |
140 | { | 148 | { |
141 | p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); | 149 | p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); |
142 | struct sockaddr_in addr; | 150 | struct sockaddr_in addr; |
143 | size_t addr_len = sizeof(addr); | 151 | socklen_t addr_len = sizeof(addr); |
144 | char buffer[UDP_DATAGRAMSIZE]; | 152 | char buffer[UDP_DATAGRAMSIZE]; |
145 | size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer)); | 153 | size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer)); |
146 | int err; | 154 | int err; |
@@ -162,15 +170,33 @@ static int udp_meth_receivefrom(lua_State *L) | |||
162 | } | 170 | } |
163 | 171 | ||
164 | /*-------------------------------------------------------------------------*\ | 172 | /*-------------------------------------------------------------------------*\ |
173 | * Select support methods | ||
174 | \*-------------------------------------------------------------------------*/ | ||
175 | static int meth_fd(lua_State *L) | ||
176 | { | ||
177 | p_udp udp = (p_udp) aux_checkclass(L, "udp{any}", 1); | ||
178 | lua_pushnumber(L, udp->sock); | ||
179 | return 1; | ||
180 | } | ||
181 | |||
182 | static int meth_dirty(lua_State *L) | ||
183 | { | ||
184 | p_udp udp = (p_udp) aux_checkclass(L, "udp{any}", 1); | ||
185 | (void) udp; | ||
186 | lua_pushboolean(L, 0); | ||
187 | return 1; | ||
188 | } | ||
189 | |||
190 | /*-------------------------------------------------------------------------*\ | ||
165 | * Just call inet methods | 191 | * Just call inet methods |
166 | \*-------------------------------------------------------------------------*/ | 192 | \*-------------------------------------------------------------------------*/ |
167 | static int udp_meth_getpeername(lua_State *L) | 193 | static int meth_getpeername(lua_State *L) |
168 | { | 194 | { |
169 | p_udp udp = (p_udp) aux_checkclass(L, "udp{connected}", 1); | 195 | p_udp udp = (p_udp) aux_checkclass(L, "udp{connected}", 1); |
170 | return inet_meth_getpeername(L, &udp->sock); | 196 | return inet_meth_getpeername(L, &udp->sock); |
171 | } | 197 | } |
172 | 198 | ||
173 | static int udp_meth_getsockname(lua_State *L) | 199 | static int meth_getsockname(lua_State *L) |
174 | { | 200 | { |
175 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 201 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); |
176 | return inet_meth_getsockname(L, &udp->sock); | 202 | return inet_meth_getsockname(L, &udp->sock); |
@@ -179,7 +205,7 @@ static int udp_meth_getsockname(lua_State *L) | |||
179 | /*-------------------------------------------------------------------------*\ | 205 | /*-------------------------------------------------------------------------*\ |
180 | * Just call tm methods | 206 | * Just call tm methods |
181 | \*-------------------------------------------------------------------------*/ | 207 | \*-------------------------------------------------------------------------*/ |
182 | static int udp_meth_timeout(lua_State *L) | 208 | static int meth_timeout(lua_State *L) |
183 | { | 209 | { |
184 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 210 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); |
185 | return tm_meth_timeout(L, &udp->tm); | 211 | return tm_meth_timeout(L, &udp->tm); |
@@ -188,7 +214,7 @@ static int udp_meth_timeout(lua_State *L) | |||
188 | /*-------------------------------------------------------------------------*\ | 214 | /*-------------------------------------------------------------------------*\ |
189 | * Turns a master udp object into a client object. | 215 | * Turns a master udp object into a client object. |
190 | \*-------------------------------------------------------------------------*/ | 216 | \*-------------------------------------------------------------------------*/ |
191 | static int udp_meth_setpeername(lua_State *L) | 217 | static int meth_setpeername(lua_State *L) |
192 | { | 218 | { |
193 | p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); | 219 | p_udp udp = (p_udp) aux_checkclass(L, "udp{unconnected}", 1); |
194 | const char *address = luaL_checkstring(L, 2); | 220 | const char *address = luaL_checkstring(L, 2); |
@@ -211,7 +237,7 @@ static int udp_meth_setpeername(lua_State *L) | |||
211 | /*-------------------------------------------------------------------------*\ | 237 | /*-------------------------------------------------------------------------*\ |
212 | * Closes socket used by object | 238 | * Closes socket used by object |
213 | \*-------------------------------------------------------------------------*/ | 239 | \*-------------------------------------------------------------------------*/ |
214 | static int udp_meth_close(lua_State *L) | 240 | static int meth_close(lua_State *L) |
215 | { | 241 | { |
216 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); | 242 | p_udp udp = (p_udp) aux_checkgroup(L, "udp{any}", 1); |
217 | sock_destroy(&udp->sock); | 243 | sock_destroy(&udp->sock); |
@@ -221,7 +247,7 @@ static int udp_meth_close(lua_State *L) | |||
221 | /*-------------------------------------------------------------------------*\ | 247 | /*-------------------------------------------------------------------------*\ |
222 | * Turns a master object into a server object | 248 | * Turns a master object into a server object |
223 | \*-------------------------------------------------------------------------*/ | 249 | \*-------------------------------------------------------------------------*/ |
224 | static int udp_meth_setsockname(lua_State *L) | 250 | static int meth_setsockname(lua_State *L) |
225 | { | 251 | { |
226 | p_udp udp = (p_udp) aux_checkclass(L, "udp{master}", 1); | 252 | p_udp udp = (p_udp) aux_checkclass(L, "udp{master}", 1); |
227 | const char *address = luaL_checkstring(L, 2); | 253 | const char *address = luaL_checkstring(L, 2); |
@@ -242,7 +268,7 @@ static int udp_meth_setsockname(lua_State *L) | |||
242 | /*-------------------------------------------------------------------------*\ | 268 | /*-------------------------------------------------------------------------*\ |
243 | * Creates a master udp object | 269 | * Creates a master udp object |
244 | \*-------------------------------------------------------------------------*/ | 270 | \*-------------------------------------------------------------------------*/ |
245 | int udp_global_create(lua_State *L) | 271 | int global_create(lua_State *L) |
246 | { | 272 | { |
247 | /* allocate udp object */ | 273 | /* allocate udp object */ |
248 | p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); | 274 | p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); |