aboutsummaryrefslogtreecommitdiff
path: root/src/udp.c
diff options
context:
space:
mode:
authorDiego Nehab <diego@impa.br>2015-08-22 19:52:01 -0300
committerDiego Nehab <diego@impa.br>2015-08-22 19:52:01 -0300
commit96965b179c7311f850f72a8629b9ba6d3a31d117 (patch)
tree05c93629654f686a99ee8923ef2361b7d2244ca8 /src/udp.c
parentb211838648c1cb092e96e3ae721932a212808d96 (diff)
downloadluasocket-96965b179c7311f850f72a8629b9ba6d3a31d117.tar.gz
luasocket-96965b179c7311f850f72a8629b9ba6d3a31d117.tar.bz2
luasocket-96965b179c7311f850f72a8629b9ba6d3a31d117.zip
New agnostic IPv4 IPv6 functions.
Also dealing with EPROTOTYPE Yosemite seems to be throwing at us for no reason.
Diffstat (limited to 'src/udp.c')
-rw-r--r--src/udp.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/udp.c b/src/udp.c
index 7ff00f5..6600859 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -27,6 +27,7 @@
27* Internal function prototypes 27* Internal function prototypes
28\*=========================================================================*/ 28\*=========================================================================*/
29static int global_create(lua_State *L); 29static int global_create(lua_State *L);
30static int global_create4(lua_State *L);
30static int global_create6(lua_State *L); 31static int global_create6(lua_State *L);
31static int meth_send(lua_State *L); 32static int meth_send(lua_State *L);
32static int meth_sendto(lua_State *L); 33static int meth_sendto(lua_State *L);
@@ -107,6 +108,7 @@ static t_opt optget[] = {
107/* functions in library namespace */ 108/* functions in library namespace */
108static luaL_Reg func[] = { 109static luaL_Reg func[] = {
109 {"udp", global_create}, 110 {"udp", global_create},
111 {"udp4", global_create4},
110 {"udp6", global_create6}, 112 {"udp6", global_create6},
111 {NULL, NULL} 113 {NULL, NULL}
112}; 114};
@@ -264,7 +266,7 @@ static int meth_receivefrom(lua_State *L)
264static int meth_getfamily(lua_State *L) 266static int meth_getfamily(lua_State *L)
265{ 267{
266 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); 268 p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1);
267 if (udp->family == PF_INET6) { 269 if (udp->family == AF_INET6) {
268 lua_pushliteral(L, "inet6"); 270 lua_pushliteral(L, "inet6");
269 return 1; 271 return 1;
270 } else { 272 } else {
@@ -391,7 +393,7 @@ static int meth_setsockname(lua_State *L) {
391 bindhints.ai_socktype = SOCK_DGRAM; 393 bindhints.ai_socktype = SOCK_DGRAM;
392 bindhints.ai_family = udp->family; 394 bindhints.ai_family = udp->family;
393 bindhints.ai_flags = AI_PASSIVE; 395 bindhints.ai_flags = AI_PASSIVE;
394 err = inet_trybind(&udp->sock, address, port, &bindhints); 396 err = inet_trybind(&udp->sock, &udp->family, address, port, &bindhints);
395 if (err) { 397 if (err) {
396 lua_pushnil(L); 398 lua_pushnil(L);
397 lua_pushstring(L, err); 399 lua_pushstring(L, err);
@@ -409,7 +411,12 @@ static int meth_setsockname(lua_State *L) {
409\*-------------------------------------------------------------------------*/ 411\*-------------------------------------------------------------------------*/
410static int udp_create(lua_State *L, int family) { 412static int udp_create(lua_State *L, int family) {
411 t_socket sock; 413 t_socket sock;
412 const char *err = inet_trycreate(&sock, family, SOCK_DGRAM); 414 /* if family is AF_UNSPEC, we create an AF_INET socket
415 * but store AF_UNSPEC into tcp-family. This will allow it
416 * later be replaced with an AF_INET6 socket if
417 * trybind or tryconnect prefer it instead. */
418 const char *err = inet_trycreate(&sock, family == AF_UNSPEC?
419 AF_INET: family, SOCK_DGRAM);
413 /* try to allocate a system socket */ 420 /* try to allocate a system socket */
414 if (!err) { 421 if (!err) {
415 /* allocate udp object */ 422 /* allocate udp object */
@@ -417,7 +424,7 @@ static int udp_create(lua_State *L, int family) {
417 auxiliar_setclass(L, "udp{unconnected}", -1); 424 auxiliar_setclass(L, "udp{unconnected}", -1);
418 /* initialize remaining structure fields */ 425 /* initialize remaining structure fields */
419 socket_setnonblocking(&sock); 426 socket_setnonblocking(&sock);
420 if (family == PF_INET6) { 427 if (family == AF_INET6) {
421 int yes = 1; 428 int yes = 1;
422 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, 429 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
423 (void *)&yes, sizeof(yes)); 430 (void *)&yes, sizeof(yes));
@@ -434,6 +441,10 @@ static int udp_create(lua_State *L, int family) {
434} 441}
435 442
436static int global_create(lua_State *L) { 443static int global_create(lua_State *L) {
444 return udp_create(L, AF_UNSPEC);
445}
446
447static int global_create4(lua_State *L) {
437 return udp_create(L, AF_INET); 448 return udp_create(L, AF_INET);
438} 449}
439 450