mirror of
				https://github.com/lunarmodules/luasocket.git
				synced 2025-10-31 02:15:38 +01:00 
			
		
		
		
	New agnostic IPv4 IPv6 functions.
Also dealing with EPROTOTYPE Yosemite seems to be throwing at us for no reason.
This commit is contained in:
		| @@ -160,9 +160,11 @@ Support, Manual"> | ||||
| <a href="socket.html#setsize">_SETSIZE</a>, | ||||
| <a href="socket.html#source">source</a>, | ||||
| <a href="tcp.html#socket.tcp">tcp</a>, | ||||
| <a href="tcp.html#socket.tcp4">tcp4</a>, | ||||
| <a href="tcp.html#socket.tcp6">tcp6</a>, | ||||
| <a href="socket.html#try">try</a>, | ||||
| <a href="udp.html#socket.udp">udp</a>, | ||||
| <a href="udp.html#socket.udp4">udp4</a>, | ||||
| <a href="udp.html#socket.udp6">udp6</a>, | ||||
| <a href="socket.html#version">_VERSION</a>. | ||||
| </blockquote> | ||||
|   | ||||
							
								
								
									
										27
									
								
								doc/tcp.html
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								doc/tcp.html
									
									
									
									
									
								
							| @@ -44,6 +44,33 @@ | ||||
| socket.<b>tcp()</b> | ||||
| </p> | ||||
|  | ||||
| <p class=description> | ||||
| Creates and returns an TCP master object. A master object can | ||||
| be transformed into a server object with the method | ||||
| <a href=#listen><tt>listen</tt></a> (after a call to <a | ||||
| href=#bind><tt>bind</tt></a>) or into a client object with | ||||
| the method <a href=#connect><tt>connect</tt></a>. The only other | ||||
| method supported by a master object is the | ||||
| <a href=#close><tt>close</tt></a> method.</p> | ||||
|  | ||||
| <p class=return> | ||||
| In case of success, a new master object is returned. In case of error, | ||||
| <b><tt>nil</tt></b> is returned, followed by an error message. | ||||
| </p> | ||||
|  | ||||
| <p class=note> | ||||
| Note: The choice between IPv4 and IPv6 happens during a call to | ||||
| <a href=#bind><tt>bind</tt></a> or <a | ||||
| href=#bind><tt>connect</tt></a>, depending on the address | ||||
| family obtained from the resolver. | ||||
| </p> | ||||
|  | ||||
| <!-- socket.tcp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||||
|  | ||||
| <p class=name id="socket.tcp4"> | ||||
| socket.<b>tcp4()</b> | ||||
| </p> | ||||
|  | ||||
| <p class=description> | ||||
| Creates and returns an IPv4 TCP master object. A master object can | ||||
| be transformed into a server object with the method | ||||
|   | ||||
							
								
								
									
										41
									
								
								doc/udp.html
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								doc/udp.html
									
									
									
									
									
								
							| @@ -45,6 +45,43 @@ | ||||
| socket.<b>udp()</b> | ||||
| </p> | ||||
|  | ||||
| <p class="description"> | ||||
| Creates and returns an unconnected UDP object. | ||||
| Unconnected objects support the | ||||
| <a href="#sendto"><tt>sendto</tt></a>, | ||||
| <a href="#receive"><tt>receive</tt></a>, | ||||
| <a href="#receivefrom"><tt>receivefrom</tt></a>, | ||||
| <a href="#getoption"><tt>getoption</tt></a>, | ||||
| <a href="#getsockname"><tt>getsockname</tt></a>, | ||||
| <a href="#setoption"><tt>setoption</tt></a>, | ||||
| <a href="#settimeout"><tt>settimeout</tt></a>, | ||||
| <a href="#setpeername"><tt>setpeername</tt></a>, | ||||
| <a href="#setsockname"><tt>setsockname</tt></a>, and | ||||
| <a href="#close"><tt>close</tt></a>. | ||||
| The <a href="#setpeername"><tt>setpeername</tt></a> | ||||
| is used to connect the object. | ||||
| </p> | ||||
|  | ||||
| <p class="return"> | ||||
| In case of success, a new unconnected UDP object | ||||
| returned. In case of error, <b><tt>nil</tt></b> is returned, followed by | ||||
| an error message. | ||||
| </p> | ||||
|  | ||||
| <p class=note> | ||||
| Note: The choice between IPv4 and IPv6 happens during a call to | ||||
| <a href=#sendto><tt>sendto</tt></a>, <a | ||||
| href=#setpeername><tt>setpeername</tt></a>, or <a | ||||
| href=#setsockname><tt>sockname</tt></a>, depending on the address | ||||
| family obtained from the resolver. | ||||
| </p> | ||||
|  | ||||
| <!-- socket.udp4 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||||
|  | ||||
| <p class="name" id="socket.udp"> | ||||
| socket.<b>udp4()</b> | ||||
| </p> | ||||
|  | ||||
| <p class="description"> | ||||
| Creates and returns an unconnected IPv4 UDP object. | ||||
| Unconnected objects support the | ||||
| @@ -102,10 +139,6 @@ Note: The TCP object returned will have the option | ||||
| "<tt>ipv6-v6only</tt>" set to <tt><b>true</b></tt>. | ||||
| </p> | ||||
|  | ||||
|  | ||||
|  | ||||
| <!-- close +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||||
|  | ||||
| <!-- close +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||||
|  | ||||
| <p class="name" id="close"> | ||||
|   | ||||
							
								
								
									
										104
									
								
								src/inet.c
									
									
									
									
									
								
							
							
						
						
									
										104
									
								
								src/inet.c
									
									
									
									
									
								
							| @@ -94,7 +94,7 @@ static int inet_global_getnameinfo(lua_State *L) { | ||||
|  | ||||
|     memset(&hints, 0, sizeof(hints)); | ||||
|     hints.ai_socktype = SOCK_STREAM; | ||||
|     hints.ai_family = PF_UNSPEC; | ||||
|     hints.ai_family = AF_UNSPEC; | ||||
|  | ||||
|     ret = getaddrinfo(host, serv, &hints, &resolved); | ||||
|     if (ret != 0) { | ||||
| @@ -146,7 +146,7 @@ static int inet_global_toip(lua_State *L) | ||||
| int inet_optfamily(lua_State* L, int narg, const char* def) | ||||
| { | ||||
|     static const char* optname[] = { "unspec", "inet", "inet6", NULL }; | ||||
|     static int optvalue[] = { PF_UNSPEC, PF_INET, PF_INET6, 0 }; | ||||
|     static int optvalue[] = { AF_UNSPEC, AF_INET, AF_INET6, 0 }; | ||||
|  | ||||
|     return optvalue[luaL_checkoption(L, narg, def, optname)]; | ||||
| } | ||||
| @@ -167,7 +167,7 @@ static int inet_global_getaddrinfo(lua_State *L) | ||||
|     int i = 1, ret = 0; | ||||
|     memset(&hints, 0, sizeof(hints)); | ||||
|     hints.ai_socktype = SOCK_STREAM; | ||||
|     hints.ai_family = PF_UNSPEC; | ||||
|     hints.ai_family = AF_UNSPEC; | ||||
|     ret = getaddrinfo(hostname, NULL, &hints, &resolved); | ||||
|     if (ret != 0) { | ||||
|         lua_pushnil(L); | ||||
| @@ -198,6 +198,16 @@ static int inet_global_getaddrinfo(lua_State *L) | ||||
|                 lua_pushliteral(L, "inet6"); | ||||
|                 lua_settable(L, -3); | ||||
|                 break; | ||||
|             case AF_UNSPEC: | ||||
|                 lua_pushliteral(L, "family"); | ||||
|                 lua_pushliteral(L, "unspec"); | ||||
|                 lua_settable(L, -3); | ||||
|                 break; | ||||
|             default: | ||||
|                 lua_pushliteral(L, "family"); | ||||
|                 lua_pushliteral(L, "unknown"); | ||||
|                 lua_settable(L, -3); | ||||
|                 break; | ||||
|         } | ||||
|         lua_pushliteral(L, "addr"); | ||||
|         lua_pushstring(L, hbuf); | ||||
| @@ -254,12 +264,11 @@ int inet_meth_getpeername(lua_State *L, p_socket ps, int family) | ||||
|     } | ||||
|     lua_pushstring(L, name); | ||||
|     lua_pushinteger(L, (int) strtol(port, (char **) NULL, 10)); | ||||
|     if (family == PF_INET) { | ||||
|         lua_pushliteral(L, "inet"); | ||||
|     } else if (family == PF_INET6) { | ||||
|         lua_pushliteral(L, "inet6"); | ||||
|     } else { | ||||
|         lua_pushliteral(L, "uknown family"); | ||||
|     switch (family) { | ||||
|         case AF_INET: lua_pushliteral(L, "inet"); break; | ||||
|         case AF_INET6: lua_pushliteral(L, "inet6"); break; | ||||
|         case AF_UNSPEC: lua_pushliteral(L, "unspec"); break; | ||||
|         default: lua_pushliteral(L, "unknown"); break; | ||||
|     } | ||||
|     return 3; | ||||
| } | ||||
| @@ -288,12 +297,11 @@ int inet_meth_getsockname(lua_State *L, p_socket ps, int family) | ||||
|     } | ||||
|     lua_pushstring(L, name); | ||||
|     lua_pushstring(L, port); | ||||
|     if (family == PF_INET) { | ||||
|         lua_pushliteral(L, "inet"); | ||||
|     } else if (family == PF_INET6) { | ||||
|         lua_pushliteral(L, "inet6"); | ||||
|     } else { | ||||
|         lua_pushliteral(L, "uknown family"); | ||||
|     switch (family) { | ||||
|         case AF_INET: lua_pushliteral(L, "inet"); break; | ||||
|         case AF_INET6: lua_pushliteral(L, "inet6"); break; | ||||
|         case AF_UNSPEC: lua_pushliteral(L, "unspec"); break; | ||||
|         default: lua_pushliteral(L, "unknown"); break; | ||||
|     } | ||||
|     return 3; | ||||
| } | ||||
| @@ -354,7 +362,7 @@ const char *inet_trycreate(p_socket ps, int family, int type) { | ||||
| const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm) | ||||
| { | ||||
|     switch (family) { | ||||
|         case PF_INET: { | ||||
|         case AF_INET: { | ||||
|             struct sockaddr_in sin; | ||||
|             memset((char *) &sin, 0, sizeof(sin)); | ||||
|             sin.sin_family = AF_UNSPEC; | ||||
| @@ -362,7 +370,7 @@ const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm) | ||||
|             return socket_strerror(socket_connect(ps, (SA *) &sin, | ||||
|                 sizeof(sin), tm)); | ||||
|         } | ||||
|         case PF_INET6: { | ||||
|         case AF_INET6: { | ||||
|             struct sockaddr_in6 sin6; | ||||
|             struct in6_addr addrany = IN6ADDR_ANY_INIT; | ||||
|             memset((char *) &sin6, 0, sizeof(sin6)); | ||||
| @@ -383,6 +391,7 @@ const char *inet_tryconnect(p_socket ps, int *family, const char *address, | ||||
| { | ||||
|     struct addrinfo *iterator = NULL, *resolved = NULL; | ||||
|     const char *err = NULL; | ||||
|     int current_family = *family; | ||||
|     /* try resolving */ | ||||
|     err = socket_gaistrerror(getaddrinfo(address, serv, | ||||
|                 connecthints, &resolved)); | ||||
| @@ -397,23 +406,23 @@ const char *inet_tryconnect(p_socket ps, int *family, const char *address, | ||||
|          * that shows up while iterating. if there was a | ||||
|          * bind, all families will be the same and we will | ||||
|          * not enter this branch. */ | ||||
|         if (*family != iterator->ai_family) { | ||||
|         if (current_family != iterator->ai_family || *ps == SOCKET_INVALID) { | ||||
|             socket_destroy(ps); | ||||
|             err = socket_strerror(socket_create(ps, iterator->ai_family, | ||||
|                 iterator->ai_socktype, iterator->ai_protocol)); | ||||
|             if (err != NULL) { | ||||
|                 freeaddrinfo(resolved); | ||||
|                 return err; | ||||
|             } | ||||
|             *family = iterator->ai_family; | ||||
|             /* all sockets initially non-blocking */ | ||||
|             if (err) continue; | ||||
|             current_family = iterator->ai_family; | ||||
|             /* set non-blocking before connect */ | ||||
|             socket_setnonblocking(ps); | ||||
|         } | ||||
|         /* try connecting to remote address */ | ||||
|         err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr, | ||||
|             (socklen_t) iterator->ai_addrlen, tm)); | ||||
|         /* if success, break out of loop */ | ||||
|         if (err == NULL) break; | ||||
|         if (err == NULL) { | ||||
|             *family = current_family; | ||||
|             break; | ||||
|         } | ||||
|     } | ||||
|     freeaddrinfo(resolved); | ||||
|     /* here, if err is set, we failed */ | ||||
| @@ -424,15 +433,14 @@ const char *inet_tryconnect(p_socket ps, int *family, const char *address, | ||||
| * Tries to accept a socket | ||||
| \*-------------------------------------------------------------------------*/ | ||||
| const char *inet_tryaccept(p_socket server, int family, p_socket client, | ||||
|     p_timeout tm) | ||||
| { | ||||
|     p_timeout tm) { | ||||
| 	socklen_t len; | ||||
| 	t_sockaddr_storage addr; | ||||
| 	if (family == PF_INET6) { | ||||
| 		len = sizeof(struct sockaddr_in6); | ||||
| 	} else { | ||||
| 		len = sizeof(struct sockaddr_in); | ||||
| 	} | ||||
|     switch (family) { | ||||
|         case AF_INET6: len = sizeof(struct sockaddr_in6); break; | ||||
|         case AF_INET: len = sizeof(struct sockaddr_in); break; | ||||
|         default: len = sizeof(addr); break; | ||||
|     } | ||||
| 	return socket_strerror(socket_accept(server, client, (SA *) &addr, | ||||
|         &len, tm)); | ||||
| } | ||||
| @@ -440,12 +448,11 @@ const char *inet_tryaccept(p_socket server, int family, p_socket client, | ||||
| /*-------------------------------------------------------------------------*\ | ||||
| * Tries to bind socket to (address, port) | ||||
| \*-------------------------------------------------------------------------*/ | ||||
| const char *inet_trybind(p_socket ps, const char *address, const char *serv, | ||||
|         struct addrinfo *bindhints) | ||||
| { | ||||
| const char *inet_trybind(p_socket ps, int *family, const char *address, | ||||
|     const char *serv, struct addrinfo *bindhints) { | ||||
|     struct addrinfo *iterator = NULL, *resolved = NULL; | ||||
|     const char *err = NULL; | ||||
|     t_socket sock = *ps; | ||||
|     int current_family = *family; | ||||
|     /* translate luasocket special values to C */ | ||||
|     if (strcmp(address, "*") == 0) address = NULL; | ||||
|     if (!serv) serv = "0"; | ||||
| @@ -457,30 +464,27 @@ const char *inet_trybind(p_socket ps, const char *address, const char *serv, | ||||
|     } | ||||
|     /* iterate over resolved addresses until one is good */ | ||||
|     for (iterator = resolved; iterator; iterator = iterator->ai_next) { | ||||
|         if(sock == SOCKET_INVALID) { | ||||
|             err = socket_strerror(socket_create(&sock, iterator->ai_family, | ||||
|         if (current_family != iterator->ai_family || *ps == SOCKET_INVALID) { | ||||
|             socket_destroy(ps); | ||||
|             err = socket_strerror(socket_create(ps, iterator->ai_family, | ||||
|                         iterator->ai_socktype, iterator->ai_protocol)); | ||||
|             if(err) | ||||
|                 continue; | ||||
|             if (err) continue; | ||||
|             current_family = iterator->ai_family; | ||||
|         } | ||||
|         /* try binding to local address */ | ||||
|         err = socket_strerror(socket_bind(&sock, | ||||
|             (SA *) iterator->ai_addr, | ||||
|         err = socket_strerror(socket_bind(ps, (SA *) iterator->ai_addr, | ||||
|             (socklen_t) iterator->ai_addrlen)); | ||||
|  | ||||
|         /* keep trying unless bind succeeded */ | ||||
|         if (err) { | ||||
|             if(sock != *ps) | ||||
|                 socket_destroy(&sock); | ||||
|         } else { | ||||
|             /* remember what we connected to, particularly the family */ | ||||
|             *bindhints = *iterator; | ||||
|         if (err == NULL) { | ||||
|             *family = current_family; | ||||
|             /* set to non-blocking after bind */ | ||||
|             socket_setnonblocking(ps); | ||||
|             break; | ||||
|         } | ||||
|     } | ||||
|     /* cleanup and return error */ | ||||
|     freeaddrinfo(resolved); | ||||
|     *ps = sock; | ||||
|     /* here, if err is set, we failed */ | ||||
|     return err; | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -27,8 +27,8 @@ int inet_open(lua_State *L); | ||||
| const char *inet_trycreate(p_socket ps, int family, int type); | ||||
| const char *inet_tryconnect(p_socket ps, int *family, const char *address, | ||||
|         const char *serv, p_timeout tm, struct addrinfo *connecthints); | ||||
| const char *inet_trybind(p_socket ps, const char *address, const char *serv, | ||||
|         struct addrinfo *bindhints); | ||||
| const char *inet_trybind(p_socket ps, int *family, const char *address, | ||||
|         const char *serv, struct addrinfo *bindhints); | ||||
| const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm); | ||||
| const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm); | ||||
|  | ||||
|   | ||||
| @@ -32,7 +32,7 @@ function _M.bind(host, port, backlog) | ||||
|     err = "no info on address" | ||||
|     for i, alt in base.ipairs(addrinfo) do | ||||
|         if alt.family == "inet" then | ||||
|             sock, err = socket.tcp() | ||||
|             sock, err = socket.tcp4() | ||||
|         else | ||||
|             sock, err = socket.tcp6() | ||||
|         end | ||||
|   | ||||
							
								
								
									
										83
									
								
								src/tcp.c
									
									
									
									
									
								
							
							
						
						
									
										83
									
								
								src/tcp.c
									
									
									
									
									
								
							| @@ -18,6 +18,7 @@ | ||||
| * Internal function prototypes | ||||
| \*=========================================================================*/ | ||||
| static int global_create(lua_State *L); | ||||
| static int global_create4(lua_State *L); | ||||
| static int global_create6(lua_State *L); | ||||
| static int global_connect(lua_State *L); | ||||
| static int meth_connect(lua_State *L); | ||||
| @@ -90,6 +91,7 @@ static t_opt optset[] = { | ||||
| /* functions in library namespace */ | ||||
| static luaL_Reg func[] = { | ||||
|     {"tcp", global_create}, | ||||
|     {"tcp4", global_create4}, | ||||
|     {"tcp6", global_create6}, | ||||
|     {"connect", global_connect}, | ||||
|     {NULL, NULL} | ||||
| @@ -213,8 +215,7 @@ static int meth_accept(lua_State *L) | ||||
| /*-------------------------------------------------------------------------*\ | ||||
| * Binds an object to an address | ||||
| \*-------------------------------------------------------------------------*/ | ||||
| static int meth_bind(lua_State *L) | ||||
| { | ||||
| static int meth_bind(lua_State *L) { | ||||
|     p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{master}", 1); | ||||
|     const char *address =  luaL_checkstring(L, 2); | ||||
|     const char *port = luaL_checkstring(L, 3); | ||||
| @@ -224,7 +225,7 @@ static int meth_bind(lua_State *L) | ||||
|     bindhints.ai_socktype = SOCK_STREAM; | ||||
|     bindhints.ai_family = tcp->family; | ||||
|     bindhints.ai_flags = AI_PASSIVE; | ||||
|     err = inet_trybind(&tcp->sock, address, port, &bindhints); | ||||
|     err = inet_trybind(&tcp->sock, &tcp->family, address, port, &bindhints); | ||||
|     if (err) { | ||||
|         lua_pushnil(L); | ||||
|         lua_pushstring(L, err); | ||||
| @@ -237,8 +238,7 @@ static int meth_bind(lua_State *L) | ||||
| /*-------------------------------------------------------------------------*\ | ||||
| * Turns a master tcp object into a client object. | ||||
| \*-------------------------------------------------------------------------*/ | ||||
| static int meth_connect(lua_State *L) | ||||
| { | ||||
| static int meth_connect(lua_State *L) { | ||||
|     p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); | ||||
|     const char *address =  luaL_checkstring(L, 2); | ||||
|     const char *port = luaL_checkstring(L, 3); | ||||
| @@ -279,9 +279,12 @@ static int meth_close(lua_State *L) | ||||
| static int meth_getfamily(lua_State *L) | ||||
| { | ||||
|     p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); | ||||
|     if (tcp->family == PF_INET6) { | ||||
|     if (tcp->family == AF_INET6) { | ||||
|         lua_pushliteral(L, "inet6"); | ||||
|         return 1; | ||||
|     } else if (tcp->family == AF_INET) { | ||||
|         lua_pushliteral(L, "inet4"); | ||||
|         return 1; | ||||
|     } else { | ||||
|         lua_pushliteral(L, "inet4"); | ||||
|         return 1; | ||||
| @@ -353,7 +356,12 @@ static int meth_settimeout(lua_State *L) | ||||
| \*-------------------------------------------------------------------------*/ | ||||
| static int tcp_create(lua_State *L, int family) { | ||||
|     t_socket sock; | ||||
|     const char *err = inet_trycreate(&sock, family, SOCK_STREAM); | ||||
|     /* if family is AF_UNSPEC, we create an AF_INET socket | ||||
|      * but store AF_UNSPEC into tcp-family. This will allow it | ||||
|      * later be replaced with an AF_INET6 socket if | ||||
|      * trybind or tryconnect prefer it instead. */ | ||||
|     const char *err = inet_trycreate(&sock, family == AF_UNSPEC? | ||||
|         AF_INET: family, SOCK_STREAM); | ||||
|     /* try to allocate a system socket */ | ||||
|     if (!err) { | ||||
|         /* allocate tcp object */ | ||||
| @@ -363,7 +371,7 @@ static int tcp_create(lua_State *L, int family) { | ||||
|         auxiliar_setclass(L, "tcp{master}", -1); | ||||
|         /* initialize remaining structure fields */ | ||||
|         socket_setnonblocking(&sock); | ||||
|         if (family == PF_INET6) { | ||||
|         if (family == AF_INET6) { | ||||
|             int yes = 1; | ||||
|             setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, | ||||
|                 (void *)&yes, sizeof(yes)); | ||||
| @@ -383,6 +391,10 @@ static int tcp_create(lua_State *L, int family) { | ||||
| } | ||||
|  | ||||
| static int global_create(lua_State *L) { | ||||
|     return tcp_create(L, AF_UNSPEC); | ||||
| } | ||||
|  | ||||
| static int global_create4(lua_State *L) { | ||||
|     return tcp_create(L, AF_INET); | ||||
| } | ||||
|  | ||||
| @@ -390,53 +402,6 @@ static int global_create6(lua_State *L) { | ||||
|     return tcp_create(L, AF_INET6); | ||||
| } | ||||
|  | ||||
| #if 0 | ||||
| static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, | ||||
|     struct addrinfo *connecthints, p_tcp tcp) { | ||||
|     struct addrinfo *iterator = NULL, *resolved = NULL; | ||||
|     const char *err = NULL; | ||||
|     /* try resolving */ | ||||
|     err = socket_gaistrerror(getaddrinfo(remoteaddr, remoteserv, | ||||
|                 connecthints, &resolved)); | ||||
|     if (err != NULL) { | ||||
|         if (resolved) freeaddrinfo(resolved); | ||||
|         return err; | ||||
|     } | ||||
|     /* iterate over all returned addresses trying to connect */ | ||||
|     for (iterator = resolved; iterator; iterator = iterator->ai_next) { | ||||
|         p_timeout tm = timeout_markstart(&tcp->tm); | ||||
|         /* create new socket if necessary. if there was no | ||||
|          * bind, we need to create one for every new family | ||||
|          * that shows up while iterating. if there was a | ||||
|          * bind, all families will be the same and we will | ||||
|          * not enter this branch. */ | ||||
|         if (tcp->family != iterator->ai_family) { | ||||
|             socket_destroy(&tcp->sock); | ||||
|             err = socket_strerror(socket_create(&tcp->sock, | ||||
|                 iterator->ai_family, iterator->ai_socktype, | ||||
|                 iterator->ai_protocol)); | ||||
|             if (err != NULL) { | ||||
|                 freeaddrinfo(resolved); | ||||
|                 return err; | ||||
|             } | ||||
|             tcp->family = iterator->ai_family; | ||||
|             /* all sockets initially non-blocking */ | ||||
|             socket_setnonblocking(&tcp->sock); | ||||
|         } | ||||
|         /* finally try connecting to remote address */ | ||||
|         err = socket_strerror(socket_connect(&tcp->sock, | ||||
|             (SA *) iterator->ai_addr, | ||||
|             (socklen_t) iterator->ai_addrlen, tm)); | ||||
|         /* if success, break out of loop */ | ||||
|         if (err == NULL) break; | ||||
|     } | ||||
|  | ||||
|     freeaddrinfo(resolved); | ||||
|     /* here, if err is set, we failed */ | ||||
|     return err; | ||||
| } | ||||
| #endif | ||||
|  | ||||
| static int global_connect(lua_State *L) { | ||||
|     const char *remoteaddr = luaL_checkstring(L, 1); | ||||
|     const char *remoteserv = luaL_checkstring(L, 2); | ||||
| @@ -453,26 +418,26 @@ static int global_connect(lua_State *L) { | ||||
|     timeout_init(&tcp->tm, -1, -1); | ||||
|     buffer_init(&tcp->buf, &tcp->io, &tcp->tm); | ||||
|     tcp->sock = SOCKET_INVALID; | ||||
|     tcp->family = PF_UNSPEC; | ||||
|     tcp->family = AF_UNSPEC; | ||||
|     /* allow user to pick local address and port */ | ||||
|     memset(&bindhints, 0, sizeof(bindhints)); | ||||
|     bindhints.ai_socktype = SOCK_STREAM; | ||||
|     bindhints.ai_family = family; | ||||
|     bindhints.ai_flags = AI_PASSIVE; | ||||
|     if (localaddr) { | ||||
|         err = inet_trybind(&tcp->sock, localaddr, localserv, &bindhints); | ||||
|         err = inet_trybind(&tcp->sock, &tcp->family, localaddr,  | ||||
|             localserv, &bindhints); | ||||
|         if (err) { | ||||
|             lua_pushnil(L); | ||||
|             lua_pushstring(L, err); | ||||
|             return 2; | ||||
|         } | ||||
|         tcp->family = bindhints.ai_family; | ||||
|     } | ||||
|     /* try to connect to remote address and port */ | ||||
|     memset(&connecthints, 0, sizeof(connecthints)); | ||||
|     connecthints.ai_socktype = SOCK_STREAM; | ||||
|     /* make sure we try to connect only to the same family */ | ||||
|     connecthints.ai_family = bindhints.ai_family; | ||||
|     connecthints.ai_family = tcp->family;  | ||||
|     err = inet_tryconnect(&tcp->sock, &tcp->family, remoteaddr, remoteserv, | ||||
|          &tcp->tm, &connecthints); | ||||
|     if (err) { | ||||
|   | ||||
							
								
								
									
										19
									
								
								src/udp.c
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								src/udp.c
									
									
									
									
									
								
							| @@ -27,6 +27,7 @@ | ||||
| * Internal function prototypes | ||||
| \*=========================================================================*/ | ||||
| static int global_create(lua_State *L); | ||||
| static int global_create4(lua_State *L); | ||||
| static int global_create6(lua_State *L); | ||||
| static int meth_send(lua_State *L); | ||||
| static int meth_sendto(lua_State *L); | ||||
| @@ -107,6 +108,7 @@ static t_opt optget[] = { | ||||
| /* functions in library namespace */ | ||||
| static luaL_Reg func[] = { | ||||
|     {"udp", global_create}, | ||||
|     {"udp4", global_create4}, | ||||
|     {"udp6", global_create6}, | ||||
|     {NULL, NULL} | ||||
| }; | ||||
| @@ -264,7 +266,7 @@ static int meth_receivefrom(lua_State *L) | ||||
| static int meth_getfamily(lua_State *L) | ||||
| { | ||||
|     p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); | ||||
|     if (udp->family == PF_INET6) { | ||||
|     if (udp->family == AF_INET6) { | ||||
|         lua_pushliteral(L, "inet6"); | ||||
|         return 1; | ||||
|     } else { | ||||
| @@ -391,7 +393,7 @@ static int meth_setsockname(lua_State *L) { | ||||
|     bindhints.ai_socktype = SOCK_DGRAM; | ||||
|     bindhints.ai_family = udp->family; | ||||
|     bindhints.ai_flags = AI_PASSIVE; | ||||
|     err = inet_trybind(&udp->sock, address, port, &bindhints); | ||||
|     err = inet_trybind(&udp->sock, &udp->family, address, port, &bindhints); | ||||
|     if (err) { | ||||
|         lua_pushnil(L); | ||||
|         lua_pushstring(L, err); | ||||
| @@ -409,7 +411,12 @@ static int meth_setsockname(lua_State *L) { | ||||
| \*-------------------------------------------------------------------------*/ | ||||
| static int udp_create(lua_State *L, int family) { | ||||
|     t_socket sock; | ||||
|     const char *err = inet_trycreate(&sock, family, SOCK_DGRAM); | ||||
|     /* if family is AF_UNSPEC, we create an AF_INET socket | ||||
|      * but store AF_UNSPEC into tcp-family. This will allow it | ||||
|      * later be replaced with an AF_INET6 socket if | ||||
|      * trybind or tryconnect prefer it instead. */ | ||||
|     const char *err = inet_trycreate(&sock, family == AF_UNSPEC? | ||||
|         AF_INET: family, SOCK_DGRAM); | ||||
|     /* try to allocate a system socket */ | ||||
|     if (!err) { | ||||
|         /* allocate udp object */ | ||||
| @@ -417,7 +424,7 @@ static int udp_create(lua_State *L, int family) { | ||||
|         auxiliar_setclass(L, "udp{unconnected}", -1); | ||||
|         /* initialize remaining structure fields */ | ||||
|         socket_setnonblocking(&sock); | ||||
|         if (family == PF_INET6) { | ||||
|         if (family == AF_INET6) { | ||||
|             int yes = 1; | ||||
|             setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, | ||||
|                 (void *)&yes, sizeof(yes)); | ||||
| @@ -434,6 +441,10 @@ static int udp_create(lua_State *L, int family) { | ||||
| } | ||||
|  | ||||
| static int global_create(lua_State *L) { | ||||
|     return udp_create(L, AF_UNSPEC); | ||||
| } | ||||
|  | ||||
| static int global_create4(lua_State *L) { | ||||
|     return udp_create(L, AF_INET); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -211,6 +211,8 @@ int socket_send(p_socket ps, const char *data, size_t count, | ||||
|         err = errno; | ||||
|         /* EPIPE means the connection was closed */ | ||||
|         if (err == EPIPE) return IO_CLOSED; | ||||
|         /* EPROTOTYPE means the connection is being closed (on Yosemite!)*/ | ||||
|         if (err == EPROTOTYPE) continue; | ||||
|         /* we call was interrupted, just try again */ | ||||
|         if (err == EINTR) continue; | ||||
|         /* if failed fatal reason, report error */ | ||||
| @@ -239,6 +241,7 @@ int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, | ||||
|         } | ||||
|         err = errno; | ||||
|         if (err == EPIPE) return IO_CLOSED; | ||||
|         if (err == EPROTOTYPE) continue; | ||||
|         if (err == EINTR) continue; | ||||
|         if (err != EAGAIN) return err; | ||||
|         if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; | ||||
| @@ -317,6 +320,8 @@ int socket_write(p_socket ps, const char *data, size_t count, | ||||
|         err = errno; | ||||
|         /* EPIPE means the connection was closed */ | ||||
|         if (err == EPIPE) return IO_CLOSED; | ||||
|         /* EPROTOTYPE means the connection is being closed (on Yosemite!)*/ | ||||
|         if (err == EPROTOTYPE) continue; | ||||
|         /* we call was interrupted, just try again */ | ||||
|         if (err == EINTR) continue; | ||||
|         /* if failed fatal reason, report error */ | ||||
| @@ -410,7 +415,9 @@ const char *socket_strerror(int err) { | ||||
|         case ECONNABORTED: return PIE_CONNABORTED; | ||||
|         case ECONNRESET: return PIE_CONNRESET; | ||||
|         case ETIMEDOUT: return PIE_TIMEDOUT; | ||||
|         default: return strerror(err); | ||||
|         default: { | ||||
|             return strerror(err); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -447,16 +447,14 @@ end | ||||
|  | ||||
| ------------------------------------------------------------------------ | ||||
| function rebind_test() | ||||
|     --local c ,c1 = socket.bind("localhost", 0) | ||||
|    local c ,c1 = socket.bind("127.0.0.1", 0) | ||||
|     if not c then pass ("failed to bind! " .. tostring(c) .. ' ' .. tostring(c1))  return end | ||||
| 	assert(c,c1) | ||||
|   | ||||
|     local i, p = c:getsockname() | ||||
|     local s, e = socket.tcp() | ||||
|     assert(s, e) | ||||
|     s:setoption("reuseaddr", false) | ||||
|     r, e = s:bind("localhost", p) | ||||
|     r, e = s:bind(i, p) | ||||
|     assert(not r, "managed to rebind!") | ||||
|     assert(e) | ||||
|     pass("ok") | ||||
| @@ -593,7 +591,7 @@ function test_writeafterclose() | ||||
|     while not err do | ||||
|         sent, err, errsent, time = data:send(str) | ||||
|     end | ||||
|     assert(err == "closed", "should have returned 'closed'") | ||||
|     assert(err == "closed", "got " .. err .. " instead of 'closed'") | ||||
|     pass("ok") | ||||
| end | ||||
|  | ||||
| @@ -674,6 +672,9 @@ if sock then test_methods(socket.udp6(), udp_methods) | ||||
| else io.stderr:write("Warning! IPv6 does not support!\n") end | ||||
| end | ||||
|  | ||||
| test("closed connection detection: ") | ||||
| test_closed() | ||||
|  | ||||
| test("partial receive") | ||||
| test_partialrecv() | ||||
|  | ||||
| @@ -697,9 +698,6 @@ rebind_test() | ||||
| test("active close: ") | ||||
| active_close() | ||||
|  | ||||
| test("closed connection detection: ") | ||||
| test_closed() | ||||
|  | ||||
| test("accept function: ") | ||||
| accept_timeout() | ||||
| accept_errors() | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| local socket = require"socket" | ||||
| local udp = socket.udp | ||||
| local localhost = "127.0.0.1" | ||||
| local port = arg[1] | ||||
| local port = assert(arg[1], "missing port argument") | ||||
|  | ||||
| se = udp(); se:setoption("reuseaddr", true) | ||||
| se:setsockname(localhost, 5062) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user