diff options
Diffstat (limited to 'src/udp.c')
-rw-r--r-- | src/udp.c | 37 |
1 files changed, 22 insertions, 15 deletions
@@ -1,10 +1,10 @@ | |||
1 | /*=========================================================================*\ | 1 | /*=========================================================================*\ |
2 | * UDP object | 2 | * UDP object |
3 | * LuaSocket toolkit | 3 | * LuaSocket toolkit |
4 | * | 4 | * |
5 | * RCS ID: $Id: udp.c,v 1.30 2009/05/27 09:31:35 diego Exp $ | 5 | * RCS ID: $Id: udp.c,v 1.30 2009/05/27 09:31:35 diego Exp $ |
6 | \*=========================================================================*/ | 6 | \*=========================================================================*/ |
7 | #include <string.h> | 7 | #include <string.h> |
8 | 8 | ||
9 | #include "lua.h" | 9 | #include "lua.h" |
10 | #include "lauxlib.h" | 10 | #include "lauxlib.h" |
@@ -18,10 +18,10 @@ | |||
18 | /* min and max macros */ | 18 | /* min and max macros */ |
19 | #ifndef MIN | 19 | #ifndef MIN |
20 | #define MIN(x, y) ((x) < (y) ? x : y) | 20 | #define MIN(x, y) ((x) < (y) ? x : y) |
21 | #endif | 21 | #endif |
22 | #ifndef MAX | 22 | #ifndef MAX |
23 | #define MAX(x, y) ((x) > (y) ? x : y) | 23 | #define MAX(x, y) ((x) > (y) ? x : y) |
24 | #endif | 24 | #endif |
25 | 25 | ||
26 | /*=========================================================================*\ | 26 | /*=========================================================================*\ |
27 | * Internal function prototypes | 27 | * Internal function prototypes |
@@ -109,7 +109,7 @@ int udp_open(lua_State *L) | |||
109 | auxiliar_add2group(L, "udp{connected}", "select{able}"); | 109 | auxiliar_add2group(L, "udp{connected}", "select{able}"); |
110 | auxiliar_add2group(L, "udp{unconnected}", "select{able}"); | 110 | auxiliar_add2group(L, "udp{unconnected}", "select{able}"); |
111 | /* define library functions */ | 111 | /* define library functions */ |
112 | luaL_openlib(L, NULL, func, 0); | 112 | luaL_openlib(L, NULL, func, 0); |
113 | return 0; | 113 | return 0; |
114 | } | 114 | } |
115 | 115 | ||
@@ -156,12 +156,12 @@ static int meth_sendto(lua_State *L) { | |||
156 | struct sockaddr_in addr; | 156 | struct sockaddr_in addr; |
157 | int err; | 157 | int err; |
158 | memset(&addr, 0, sizeof(addr)); | 158 | memset(&addr, 0, sizeof(addr)); |
159 | if (!inet_aton(ip, &addr.sin_addr)) | 159 | if (!inet_aton(ip, &addr.sin_addr)) |
160 | luaL_argerror(L, 3, "invalid ip address"); | 160 | luaL_argerror(L, 3, "invalid ip address"); |
161 | addr.sin_family = AF_INET; | 161 | addr.sin_family = AF_INET; |
162 | addr.sin_port = htons(port); | 162 | addr.sin_port = htons(port); |
163 | timeout_markstart(tm); | 163 | timeout_markstart(tm); |
164 | err = socket_sendto(&udp->sock, data, count, &sent, | 164 | err = socket_sendto(&udp->sock, data, count, &sent, |
165 | (SA *) &addr, sizeof(addr), tm); | 165 | (SA *) &addr, sizeof(addr), tm); |
166 | if (err != IO_DONE) { | 166 | if (err != IO_DONE) { |
167 | lua_pushnil(L); | 167 | lua_pushnil(L); |
@@ -206,7 +206,7 @@ static int meth_receivefrom(lua_State *L) { | |||
206 | p_timeout tm = &udp->tm; | 206 | p_timeout tm = &udp->tm; |
207 | timeout_markstart(tm); | 207 | timeout_markstart(tm); |
208 | count = MIN(count, sizeof(buffer)); | 208 | count = MIN(count, sizeof(buffer)); |
209 | err = socket_recvfrom(&udp->sock, buffer, count, &got, | 209 | err = socket_recvfrom(&udp->sock, buffer, count, &got, |
210 | (SA *) &addr, &addr_len, tm); | 210 | (SA *) &addr, &addr_len, tm); |
211 | if (err == IO_DONE) { | 211 | if (err == IO_DONE) { |
212 | lua_pushlstring(L, buffer, got); | 212 | lua_pushlstring(L, buffer, got); |
@@ -288,10 +288,17 @@ static int meth_setpeername(lua_State *L) { | |||
288 | p_timeout tm = &udp->tm; | 288 | p_timeout tm = &udp->tm; |
289 | const char *address = luaL_checkstring(L, 2); | 289 | const char *address = luaL_checkstring(L, 2); |
290 | int connecting = strcmp(address, "*"); | 290 | int connecting = strcmp(address, "*"); |
291 | unsigned short port = connecting ? | 291 | const char *port = connecting ? |
292 | (unsigned short) luaL_checknumber(L, 3) : | 292 | luaL_checkstring(L, 3) : |
293 | (unsigned short) luaL_optnumber(L, 3, 0); | 293 | luaL_optstring(L, 3, "0"); |
294 | const char *err = inet_tryconnect(&udp->sock, address, port, tm); | 294 | struct addrinfo connecthints; |
295 | const char *err; | ||
296 | memset(&connecthints, 0, sizeof(connecthints)); | ||
297 | connecthints.ai_socktype = SOCK_DGRAM; | ||
298 | /* make sure we try to connect only to the same family */ | ||
299 | connecthints.ai_family = udp->domain; | ||
300 | err = inet_tryconnect(&udp->sock, address, port, | ||
301 | tm, &connecthints); | ||
295 | if (err) { | 302 | if (err) { |
296 | lua_pushnil(L); | 303 | lua_pushnil(L); |
297 | lua_pushstring(L, err); | 304 | lua_pushstring(L, err); |
@@ -305,7 +312,7 @@ static int meth_setpeername(lua_State *L) { | |||
305 | } | 312 | } |
306 | 313 | ||
307 | /*-------------------------------------------------------------------------*\ | 314 | /*-------------------------------------------------------------------------*\ |
308 | * Closes socket used by object | 315 | * Closes socket used by object |
309 | \*-------------------------------------------------------------------------*/ | 316 | \*-------------------------------------------------------------------------*/ |
310 | static int meth_close(lua_State *L) { | 317 | static int meth_close(lua_State *L) { |
311 | p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); | 318 | p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); |
@@ -341,13 +348,13 @@ static int meth_setsockname(lua_State *L) { | |||
341 | * Library functions | 348 | * Library functions |
342 | \*=========================================================================*/ | 349 | \*=========================================================================*/ |
343 | /*-------------------------------------------------------------------------*\ | 350 | /*-------------------------------------------------------------------------*\ |
344 | * Creates a master udp object | 351 | * Creates a master udp object |
345 | \*-------------------------------------------------------------------------*/ | 352 | \*-------------------------------------------------------------------------*/ |
346 | static int udp_create(lua_State *L, int domain) { | 353 | static int udp_create(lua_State *L, int domain) { |
347 | t_socket sock; | 354 | t_socket sock; |
348 | const char *err = inet_trycreate(&sock, domain, SOCK_DGRAM); | 355 | const char *err = inet_trycreate(&sock, domain, SOCK_DGRAM); |
349 | /* try to allocate a system socket */ | 356 | /* try to allocate a system socket */ |
350 | if (!err) { | 357 | if (!err) { |
351 | /* allocate udp object */ | 358 | /* allocate udp object */ |
352 | p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); | 359 | p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); |
353 | auxiliar_setclass(L, "udp{unconnected}", -1); | 360 | auxiliar_setclass(L, "udp{unconnected}", -1); |