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.
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <arpa/inet.h>
32 * connect()/bind() shortcut
34 static int nixio__bind_connect(lua_State *L, int do_bind) {
35 const char *host = NULL;
36 if (!lua_isnoneornil(L, 1)) {
37 host = luaL_checklstring(L, 1, NULL);
39 const char *port = luaL_checklstring(L, 2, NULL);
40 const char *family = luaL_optlstring(L, 3, "any", NULL);
41 const char *socktype = luaL_optlstring(L, 4, "stream", 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, 3, "supported values: any, inet, inet6");
56 if (!strcmp(socktype, "any")) {
57 hints.ai_socktype = 0;
58 } else if (!strcmp(socktype, "stream")) {
59 hints.ai_socktype = SOCK_STREAM;
60 } else if (!strcmp(socktype, "dgram")) {
61 hints.ai_socktype = SOCK_DGRAM;
63 return luaL_argerror(L, 4, "supported values: any, stream, dgram");
67 hints.ai_flags |= AI_PASSIVE;
70 hints.ai_protocol = 0;
72 int aistat = getaddrinfo(host, port, &hints, &result);
75 lua_pushinteger(L, aistat);
76 lua_pushstring(L, gai_strerror(aistat));
80 /* create socket object */
81 nixio_sock *sock = lua_newuserdata(L, sizeof(nixio_sock));
84 for (rp = result; rp != NULL; rp = rp->ai_next) {
85 sock->fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
91 status = bind(sock->fd, rp->ai_addr, rp->ai_addrlen);
93 status = connect(sock->fd, rp->ai_addr, rp->ai_addrlen);
98 sock->domain = rp->ai_family;
99 sock->type = rp->ai_socktype;
100 sock->protocol = rp->ai_protocol;
107 freeaddrinfo(result);
111 return nixio__perror(L);
114 luaL_getmetatable(L, NIXIO_META);
115 lua_setmetatable(L, -2);
121 * bind(host, port, [family=any], [type=any]) shortcut
123 static int nixio_bind(lua_State *L) {
124 return nixio__bind_connect(L, 1);
128 * connect(host, port, [family=any], [type=any]) shortcut
130 static int nixio_connect(lua_State *L) {
131 return nixio__bind_connect(L, 0);
135 * bind()/connect() helper
137 static int nixio_sock__bind_connect(lua_State *L, int do_bind) {
138 nixio_sock *sock = nixio__checksock(L);
141 if (sock->domain == AF_INET || sock->domain == AF_INET6) {
142 const char *host = NULL;
143 if (!lua_isnoneornil(L, 2)) {
144 host = luaL_checklstring(L, 2, NULL);
146 const char *port = luaL_checklstring(L, 3, NULL);
148 struct addrinfo hints, *result, *rp;
150 memset(&hints, 0, sizeof(hints));
151 hints.ai_family = sock->domain;
152 hints.ai_socktype = sock->type;
153 hints.ai_protocol = sock->protocol;
156 hints.ai_flags |= AI_PASSIVE;
159 int aistat = getaddrinfo(host, port, &hints, &result);
162 lua_pushinteger(L, aistat);
163 lua_pushstring(L, gai_strerror(aistat));
167 for (rp = result; rp != NULL; rp = rp->ai_next) {
169 status = bind(sock->fd, rp->ai_addr, rp->ai_addrlen);
171 status = connect(sock->fd, rp->ai_addr, rp->ai_addrlen);
180 freeaddrinfo(result);
181 } else if (sock->domain == AF_UNIX) {
183 const char *path = luaL_checklstring(L, 2, &pathlen);
185 struct sockaddr_un addr;
186 addr.sun_family = AF_UNIX;
187 luaL_argcheck(L, pathlen < sizeof(addr.sun_path), 2, "out of range");
188 strncpy(addr.sun_path, path, sizeof(addr.sun_path));
191 status = bind(sock->fd, (struct sockaddr*)&addr, sizeof(addr));
193 status = connect(sock->fd, (struct sockaddr*)&addr, sizeof(addr));
196 return luaL_error(L, "not supported");
198 return nixio__pstatus(L, !status);
204 static int nixio_sock_bind(lua_State *L) {
205 return nixio_sock__bind_connect(L, 1);
211 static int nixio_sock_connect(lua_State *L) {
212 return nixio_sock__bind_connect(L, 0);
218 static int nixio_sock_listen(lua_State *L) {
219 int sockfd = nixio__checksockfd(L);
220 lua_Integer backlog = luaL_checkinteger(L, 2);
221 return nixio__pstatus(L, !listen(sockfd, backlog));
227 static int nixio_sock_accept(lua_State *L) {
228 nixio_sock *sock = nixio__checksock(L);
229 struct sockaddr_storage addr;
230 socklen_t addrlen = sizeof(addr);
231 char ipaddr[INET6_ADDRSTRLEN];
235 int newfd = accept(sock->fd, (struct sockaddr *)&addr, &addrlen);
237 return nixio__perror(L);
240 /* create userdata */
241 nixio_sock *clsock = lua_newuserdata(L, sizeof(nixio_sock));
242 luaL_getmetatable(L, NIXIO_META);
243 lua_setmetatable(L, -2);
245 memcpy(clsock, sock, sizeof(clsock));
248 if (addr.ss_family == AF_INET) {
249 struct sockaddr_in *inetaddr = (struct sockaddr_in*)&addr;
250 port = inetaddr->sin_port;
251 binaddr = &inetaddr->sin_addr;
252 } else if (addr.ss_family == AF_INET6) {
253 struct sockaddr_in6 *inet6addr = (struct sockaddr_in6*)&addr;
254 port = inet6addr->sin6_port;
255 binaddr = &inet6addr->sin6_addr;
257 return luaL_error(L, "unknown address family");
260 if (!inet_ntop(addr.ss_family, binaddr, ipaddr, sizeof(ipaddr))) {
261 return nixio__perror(L);
264 lua_pushstring(L, ipaddr);
265 lua_pushinteger(L, ntohs(port));
270 static const luaL_reg R[] = {
271 {"bind", nixio_bind},
272 {"connect", nixio_connect},
277 static const luaL_reg M[] = {
278 {"bind", nixio_sock_bind},
279 {"connect", nixio_sock_connect},
280 {"listen", nixio_sock_listen},
281 {"accept", nixio_sock_accept},
285 void nixio_open_bind(lua_State *L) {
286 luaL_register(L, NULL, R);
288 lua_pushvalue(L, -2);
289 luaL_register(L, NULL, M);