2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
23 #include <netinet/in.h>
28 #define NI_MAXHOST 1025
33 * getaddrinfo(host, family, port)
35 static int nixio_getaddrinfo(lua_State *L) {
36 const char *host = NULL;
37 if (!lua_isnoneornil(L, 1)) {
38 host = luaL_checklstring(L, 1, NULL);
40 const char *family = luaL_optlstring(L, 2, "any", NULL);
41 const char *port = lua_tolstring(L, 3, NULL);
43 struct addrinfo hints, *result, *rp;
44 memset(&hints, 0, sizeof(hints));
46 if (!strcmp(family, "any")) {
47 hints.ai_family = AF_UNSPEC;
48 } else if (!strcmp(family, "inet")) {
49 hints.ai_family = AF_INET;
50 } else if (!strcmp(family, "inet6")) {
51 hints.ai_family = AF_INET6;
53 return luaL_argerror(L, 2, "supported values: any, inet, inet6");
56 hints.ai_socktype = 0;
57 hints.ai_protocol = 0;
59 int aistat = getaddrinfo(host, port, &hints, &result);
62 lua_pushinteger(L, aistat);
63 lua_pushstring(L, gai_strerror(aistat));
67 /* create socket object */
71 for (rp = result; rp != NULL; rp = rp->ai_next) {
72 /* avoid duplicate results */
73 if (!port && rp->ai_socktype != SOCK_STREAM) {
77 if (rp->ai_family == AF_INET || rp->ai_family == AF_INET6) {
78 lua_createtable(L, 0, port ? 4 : 2);
79 if (rp->ai_family == AF_INET) {
80 lua_pushliteral(L, "inet");
81 } else if (rp->ai_family == AF_INET6) {
82 lua_pushliteral(L, "inet6");
84 lua_setfield(L, -2, "family");
87 switch (rp->ai_socktype) {
89 lua_pushliteral(L, "stream");
92 lua_pushliteral(L, "dgram");
95 lua_pushliteral(L, "raw");
101 lua_setfield(L, -2, "socktype");
104 char ip[INET6_ADDRSTRLEN];
105 void *binaddr = NULL;
106 uint16_t binport = 0;
108 if (rp->ai_family == AF_INET) {
109 struct sockaddr_in *v4addr = (struct sockaddr_in*)rp->ai_addr;
110 binport = v4addr->sin_port;
111 binaddr = (void *)&v4addr->sin_addr;
112 } else if (rp->ai_family == AF_INET6) {
113 struct sockaddr_in6 *v6addr = (struct sockaddr_in6*)rp->ai_addr;
114 binport = v6addr->sin6_port;
115 binaddr = (void *)&v6addr->sin6_addr;
118 if (!inet_ntop(rp->ai_family, binaddr, ip, sizeof(ip))) {
119 freeaddrinfo(result);
120 return nixio__perror(L);
124 lua_pushinteger(L, ntohs(binport));
125 lua_setfield(L, -2, "port");
128 lua_pushstring(L, ip);
129 lua_setfield(L, -2, "address");
130 lua_rawseti(L, -2, i++);
134 freeaddrinfo(result);
140 * getnameinfo(address, family)
142 static int nixio_getnameinfo(lua_State *L) {
143 const char *ip = luaL_checklstring(L, 1, NULL);
144 const char *family = luaL_optlstring(L, 2, "inet", NULL);
145 char host[NI_MAXHOST];
147 struct sockaddr *addr = NULL;
151 if (!strcmp(family, "inet")) {
152 struct sockaddr_in inetaddr;
153 memset(&inetaddr, 0, sizeof(inetaddr));
154 inetaddr.sin_family = AF_INET;
155 if (inet_pton(AF_INET, ip, &inetaddr.sin_addr) < 1) {
156 return luaL_argerror(L, 1, "invalid address");
158 alen = sizeof(inetaddr);
159 addr = (struct sockaddr *)&inetaddr;
160 } else if (!strcmp(family, "inet6")) {
161 struct sockaddr_in6 inet6addr;
162 memset(&inet6addr, 0, sizeof(inet6addr));
163 inet6addr.sin6_family = AF_INET6;
164 if (inet_pton(AF_INET6, ip, &inet6addr.sin6_addr) < 1) {
165 return luaL_argerror(L, 1, "invalid address");
167 alen = sizeof(inet6addr);
168 addr = (struct sockaddr *)&inet6addr;
170 return luaL_argerror(L, 2, "supported values: inet, inet6");
173 res = getnameinfo(addr, alen, host, sizeof(host), NULL, 0, NI_NAMEREQD);
176 lua_pushinteger(L, res);
177 lua_pushstring(L, gai_strerror(res));
180 lua_pushstring(L, host);
186 * getsockname() / getpeername() helper
188 static int nixio_sock__getname(lua_State *L, int sock) {
189 int sockfd = nixio__checksockfd(L);
190 struct sockaddr_storage addr;
191 socklen_t addrlen = sizeof(addr);
192 char ipaddr[INET6_ADDRSTRLEN];
197 if (getsockname(sockfd, (struct sockaddr*)&addr, &addrlen)) {
198 return nixio__perror(L);
201 if (getpeername(sockfd, (struct sockaddr*)&addr, &addrlen)) {
202 return nixio__perror(L);
206 if (addr.ss_family == AF_INET) {
207 struct sockaddr_in *inetaddr = (struct sockaddr_in*)&addr;
208 port = inetaddr->sin_port;
209 binaddr = &inetaddr->sin_addr;
210 } else if (addr.ss_family == AF_INET6) {
211 struct sockaddr_in6 *inet6addr = (struct sockaddr_in6*)&addr;
212 port = inet6addr->sin6_port;
213 binaddr = &inet6addr->sin6_addr;
215 return luaL_error(L, "unknown address family");
218 if (!inet_ntop(addr.ss_family, binaddr, ipaddr, sizeof(ipaddr))) {
219 return nixio__perror(L);
222 lua_pushstring(L, ipaddr);
223 lua_pushinteger(L, ntohs(port));
230 static int nixio_sock_getsockname(lua_State *L) {
231 return nixio_sock__getname(L, 1);
237 static int nixio_sock_getpeername(lua_State *L) {
238 return nixio_sock__getname(L, 0);
243 static const luaL_reg R[] = {
244 {"getaddrinfo", nixio_getaddrinfo},
245 {"getnameinfo", nixio_getnameinfo},
250 static const luaL_reg M[] = {
251 {"getsockname", nixio_sock_getsockname},
252 {"getpeername", nixio_sock_getpeername},
256 void nixio_open_address(lua_State *L) {
257 luaL_register(L, NULL, R);
259 lua_pushvalue(L, -2);
260 luaL_register(L, NULL, M);