moonbridge
changeset 107:06d965df8a0c
Moved local/remote address extraction/formatting to I/O library
author | jbe |
---|---|
date | Wed Apr 08 23:13:09 2015 +0200 (2015-04-08) |
parents | 8fce76ef321f |
children | 110493d29f90 |
files | moonbridge.c moonbridge_io.c moonbridge_io.h |
line diff
1.1 --- a/moonbridge.c Wed Apr 08 20:38:18 2015 +0200 1.2 +++ b/moonbridge.c Wed Apr 08 23:13:09 2015 +0200 1.3 @@ -33,7 +33,6 @@ 1.4 #include <sys/socket.h> 1.5 #include <sys/un.h> 1.6 #include <netinet/in.h> 1.7 -#include <arpa/inet.h> 1.8 #include <poll.h> 1.9 #include <signal.h> 1.10 #include <sys/wait.h> 1.11 @@ -843,7 +842,7 @@ 1.12 } 1.13 if (controlmsg == MOONBR_COMMAND_TERMINATE) break; 1.14 listener = moonbr_child_receive_pointer(MOONBR_FD_CONTROL); 1.15 - if (fd) moonbr_io_pushhandle(L, fd, controlmsg == MOONBR_SOCKETTYPE_NETWORK); 1.16 + if (fd) moonbr_io_pushhandle(L, fd); 1.17 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool)); 1.18 if (!fd) { 1.19 lua_newtable(L); 1.20 @@ -854,69 +853,6 @@ 1.21 lua_setfield(L, -2, "interval"); 1.22 } else { 1.23 lua_pushvalue(L, -2); 1.24 - if (listener->proto == MOONBR_PROTO_TCP6) { 1.25 - struct sockaddr_in6 addr; 1.26 - char addrstrbuf[INET6_ADDRSTRLEN]; 1.27 - const char *addrstr; 1.28 - socklen_t addr_len = sizeof(struct sockaddr_in6); 1.29 - if (getsockname(fd, (struct sockaddr *)&addr, &addr_len)) { 1.30 - moonbr_child_log_errno("Could not get local IP address/port"); 1.31 - } else { 1.32 - addrstr = inet_ntop(AF_INET6, addr.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf)); 1.33 - if (!addrstr) { 1.34 - moonbr_child_log_errno("Could not format local IP address"); 1.35 - } else { 1.36 - lua_pushstring(L, addrstr); 1.37 - lua_setfield(L, -2, "local_ip6"); 1.38 - } 1.39 - lua_pushinteger(L, ntohs(addr.sin6_port)); 1.40 - lua_setfield(L, -2, "local_tcpport"); 1.41 - } 1.42 - if (getpeername(fd, (struct sockaddr *)&addr, &addr_len)) { 1.43 - moonbr_child_log_errno("Could not get remote IP address/port"); 1.44 - } else { 1.45 - addrstr = inet_ntop(AF_INET6, addr.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf)); 1.46 - if (!addrstr) { 1.47 - moonbr_child_log_errno("Could not format remote IP address"); 1.48 - } else { 1.49 - lua_pushstring(L, addrstr); 1.50 - lua_setfield(L, -2, "remote_ip6"); 1.51 - } 1.52 - lua_pushinteger(L, ntohs(addr.sin6_port)); 1.53 - lua_setfield(L, -2, "remote_tcpport"); 1.54 - } 1.55 - } else if (listener->proto == MOONBR_PROTO_TCP4) { 1.56 - struct sockaddr_in addr; 1.57 - char addrstrbuf[INET_ADDRSTRLEN]; 1.58 - const char *addrstr; 1.59 - socklen_t addr_len = sizeof(struct sockaddr_in); 1.60 - if (getsockname(fd, (struct sockaddr *)&addr, &addr_len)) { 1.61 - moonbr_child_log_errno("Could not get local IP address/port"); 1.62 - } else { 1.63 - addrstr = inet_ntop(AF_INET, &addr.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf)); 1.64 - if (!addrstr) { 1.65 - moonbr_child_log_errno("Could not format local IP address"); 1.66 - } else { 1.67 - lua_pushstring(L, addrstr); 1.68 - lua_setfield(L, -2, "local_ip4"); 1.69 - } 1.70 - lua_pushinteger(L, ntohs(addr.sin_port)); 1.71 - lua_setfield(L, -2, "local_tcpport"); 1.72 - } 1.73 - if (getpeername(fd, (struct sockaddr *)&addr, &addr_len)) { 1.74 - moonbr_child_log_errno("Could not get remote IP address/port"); 1.75 - } else { 1.76 - addrstr = inet_ntop(AF_INET, &addr.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf)); 1.77 - if (!addrstr) { 1.78 - moonbr_child_log_errno("Could not format remote IP address"); 1.79 - } else { 1.80 - lua_pushstring(L, addrstr); 1.81 - lua_setfield(L, -2, "remote_ip4"); 1.82 - } 1.83 - lua_pushinteger(L, ntohs(addr.sin_port)); 1.84 - lua_setfield(L, -2, "remote_tcpport"); 1.85 - } 1.86 - } 1.87 } 1.88 if (lua_pcall(L, 1, 1, 1)) { 1.89 fprintf(stderr, "Error in \"connect\" function: %s\n", lua_tostring(L, -1));
2.1 --- a/moonbridge_io.c Wed Apr 08 20:38:18 2015 +0200 2.2 +++ b/moonbridge_io.c Wed Apr 08 23:13:09 2015 +0200 2.3 @@ -9,6 +9,7 @@ 2.4 #include <fcntl.h> 2.5 #include <netinet/in.h> 2.6 #include <netinet/tcp.h> 2.7 +#include <arpa/inet.h> 2.8 #include <sys/types.h> 2.9 #include <netdb.h> 2.10 2.11 @@ -29,7 +30,8 @@ 2.12 2.13 typedef struct { 2.14 int fd; 2.15 - int isnetwork; 2.16 + int issock; 2.17 + sa_family_t addrfam; 2.18 int finished; 2.19 int closed; 2.20 int nonblocking; 2.21 @@ -78,7 +80,7 @@ 2.22 2.23 static void moonbr_io_handle_set_linger(lua_State *L, moonbr_io_handle_t *handle, int timeout) { 2.24 struct linger lingerval = { 0, }; 2.25 - if (!handle->isnetwork) return; 2.26 + if (!handle->issock) return; 2.27 if (timeout >= 0) { 2.28 lingerval.l_onoff = 1; 2.29 lingerval.l_linger = timeout; 2.30 @@ -94,7 +96,10 @@ 2.31 2.32 static void moonbr_io_handle_set_nopush(lua_State *L, moonbr_io_handle_t *handle, int nopush) { 2.33 #if defined(TCP_NOPUSH) || defined(TCP_CORK) 2.34 - if (!handle->isnetwork || handle->nopush == nopush) return; 2.35 + if ( 2.36 + !(handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) || 2.37 + handle->nopush == nopush 2.38 + ) return; 2.39 #if defined(TCP_NOPUSH) 2.40 if (setsockopt(handle->fd, IPPROTO_TCP, TCP_NOPUSH, &nopush, sizeof(nopush))) { 2.41 #elif defined(TCP_CORK) 2.42 @@ -387,7 +392,7 @@ 2.43 } 2.44 } 2.45 handle->finished = 1; 2.46 - if (handle->isnetwork) { 2.47 + if (handle->addrfam == AF_INET6 || handle->addrfam == AF_INET) { 2.48 if (shutdown(handle->fd, SHUT_WR)) { 2.49 moonbr_io_errmsg(); 2.50 lua_pushnil(L); 2.51 @@ -470,11 +475,23 @@ 2.52 } 2.53 } 2.54 2.55 -void moonbr_io_pushhandle(lua_State *L, int fd, int isnetwork) { 2.56 +void moonbr_io_pushhandle(lua_State *L, int fd) { 2.57 moonbr_io_handle_t *handle; 2.58 + struct sockaddr addr; 2.59 + socklen_t addrlen; 2.60 handle = lua_newuserdata(L, sizeof(moonbr_io_handle_t)); 2.61 handle->fd = fd; 2.62 - handle->isnetwork = isnetwork; 2.63 + addrlen = sizeof(addr); 2.64 + if (getsockname(fd, &addr, &addrlen)) { 2.65 + if (errno != ENOTSOCK) { 2.66 + moonbr_io_errmsg(); 2.67 + luaL_error(L, "Unexpected error when examining socket: %s", errmsg); 2.68 + } 2.69 + handle->issock = 0; 2.70 + } else { 2.71 + handle->issock = 1; 2.72 + handle->addrfam = addr.sa_family; 2.73 + } 2.74 handle->finished = 0; 2.75 handle->closed = 0; 2.76 handle->nonblocking = -1; 2.77 @@ -496,6 +513,85 @@ 2.78 lua_newtable(L); 2.79 lua_setfield(L, -2, "writequeue"); 2.80 lua_newtable(L); // public 2.81 + if (handle->addrfam == AF_INET6) { 2.82 + struct sockaddr_in6 addr_in6; 2.83 + char addrstrbuf[INET6_ADDRSTRLEN]; 2.84 + const char *addrstr; 2.85 + addrlen = sizeof(addr_in6); 2.86 + if (getsockname(fd, (struct sockaddr *)&addr_in6, &addrlen)) { 2.87 + moonbr_io_errmsg(); 2.88 + luaL_error(L, "Could not determine local IP address/port: %s", errmsg); 2.89 + } 2.90 + if (addrlen > sizeof(addr_in6)) { 2.91 + luaL_error(L, "Could not determine local IP address/port: buffer size exceeded"); 2.92 + } 2.93 + addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf)); 2.94 + if (!addrstr) { 2.95 + moonbr_io_errmsg(); 2.96 + luaL_error(L, "Could not format local IP address: %s", errmsg); 2.97 + } else { 2.98 + lua_pushstring(L, addrstr); 2.99 + lua_setfield(L, -2, "local_ip6"); 2.100 + } 2.101 + lua_pushinteger(L, ntohs(addr_in6.sin6_port)); 2.102 + lua_setfield(L, -2, "local_tcpport"); 2.103 + if (getpeername(fd, (struct sockaddr *)&addr_in6, &addrlen)) { 2.104 + moonbr_io_errmsg(); 2.105 + luaL_error(L, "Could not determine remote IP address/port: %s", errmsg); 2.106 + } 2.107 + if (addrlen > sizeof(addr_in6)) { 2.108 + luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded"); 2.109 + } 2.110 + addrstr = inet_ntop(AF_INET6, addr_in6.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf)); 2.111 + if (!addrstr) { 2.112 + moonbr_io_errmsg(); 2.113 + luaL_error(L, "Could not format remote IP address: %s", errmsg); 2.114 + } else { 2.115 + lua_pushstring(L, addrstr); 2.116 + lua_setfield(L, -2, "remote_ip6"); 2.117 + } 2.118 + lua_pushinteger(L, ntohs(addr_in6.sin6_port)); 2.119 + lua_setfield(L, -2, "remote_tcpport"); 2.120 + } else if (handle->addrfam == AF_INET) { 2.121 + struct sockaddr_in addr_in; 2.122 + char addrstrbuf[INET_ADDRSTRLEN]; 2.123 + const char *addrstr; 2.124 + addrlen = sizeof(addr_in); 2.125 + if (getsockname(fd, (struct sockaddr *)&addr_in, &addrlen)) { 2.126 + moonbr_io_errmsg(); 2.127 + luaL_error(L, "Could not determine local IP address/port: %s", errmsg); 2.128 + } 2.129 + if (addrlen > sizeof(addr_in)) { 2.130 + luaL_error(L, "Could not determine local IP address/port: buffer size exceeded"); 2.131 + } 2.132 + addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf)); 2.133 + if (!addrstr) { 2.134 + moonbr_io_errmsg(); 2.135 + luaL_error(L, "Could not format local IP address: %s", errmsg); 2.136 + } else { 2.137 + lua_pushstring(L, addrstr); 2.138 + lua_setfield(L, -2, "local_ip4"); 2.139 + } 2.140 + lua_pushinteger(L, ntohs(addr_in.sin_port)); 2.141 + lua_setfield(L, -2, "local_tcpport"); 2.142 + if (getpeername(fd, (struct sockaddr *)&addr_in, &addrlen)) { 2.143 + moonbr_io_errmsg(); 2.144 + luaL_error(L, "Could not determine remote IP address/port: %s", errmsg); 2.145 + } 2.146 + if (addrlen > sizeof(addr_in)) { 2.147 + luaL_error(L, "Could not determine remote IP address/port: buffer size exceeded"); 2.148 + } 2.149 + addrstr = inet_ntop(AF_INET, &addr_in.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf)); 2.150 + if (!addrstr) { 2.151 + moonbr_io_errmsg(); 2.152 + luaL_error(L, "Could not format remote IP address: %s", errmsg); 2.153 + } else { 2.154 + lua_pushstring(L, addrstr); 2.155 + lua_setfield(L, -2, "remote_ip4"); 2.156 + } 2.157 + lua_pushinteger(L, ntohs(addr_in.sin_port)); 2.158 + lua_setfield(L, -2, "remote_tcpport"); 2.159 + } 2.160 luaL_getmetatable(L, MOONBR_IO_HANDLE_PUBLIC_MT_REGKEY); 2.161 lua_setmetatable(L, -2); 2.162 lua_setfield(L, -2, "public"); 2.163 @@ -579,7 +675,7 @@ 2.164 return 2; 2.165 } 2.166 } 2.167 - moonbr_io_pushhandle(L, sock, 1); 2.168 + moonbr_io_pushhandle(L, sock); 2.169 return 1; 2.170 } 2.171
3.1 --- a/moonbridge_io.h Wed Apr 08 20:38:18 2015 +0200 3.2 +++ b/moonbridge_io.h Wed Apr 08 23:13:09 2015 +0200 3.3 @@ -1,5 +1,5 @@ 3.4 3.5 -void moonbr_io_pushhandle(lua_State *L, int fd, int useshutdown); 3.6 +void moonbr_io_pushhandle(lua_State *L, int fd); 3.7 void moonbr_io_closehandle(lua_State *L, int idx, int reset); 3.8 int moonbr_io_tcpconnect(lua_State *L); 3.9 int moonbr_io_tcpconnect_nb(lua_State *L);