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/param.h>
31 /* setjmp() / longjmp() stuff */
32 static jmp_buf nixio__jump_alarm;
33 static void nixio__handle_alarm(int sig) { longjmp(nixio__jump_alarm, 1); }
35 #include <linux/netdevice.h>
37 /* struct net_device_stats is buggy on amd64, redefine it */
50 uint32_t rx_length_errors;
51 uint32_t rx_over_errors;
52 uint32_t rx_crc_errors;
53 uint32_t rx_frame_errors;
54 uint32_t rx_fifo_errors;
55 uint32_t rx_missed_errors;
57 uint32_t tx_aborted_errors;
58 uint32_t tx_carrier_errors;
59 uint32_t tx_fifo_errors;
60 uint32_t tx_heartbeat_errors;
61 uint32_t tx_window_errors;
63 uint32_t rx_compressed;
64 uint32_t tx_compressed;
69 #define NI_MAXHOST 1025
73 * address pushing helper
75 int nixio__addr_parse(nixio_addr *addr, struct sockaddr *saddr) {
78 addr->family = saddr->sa_family;
79 if (saddr->sa_family == AF_INET) {
80 struct sockaddr_in *inetaddr = (struct sockaddr_in*)saddr;
81 addr->port = ntohs(inetaddr->sin_port);
82 baddr = &inetaddr->sin_addr;
83 } else if (saddr->sa_family == AF_INET6) {
84 struct sockaddr_in6 *inet6addr = (struct sockaddr_in6*)saddr;
85 addr->port = ntohs(inet6addr->sin6_port);
86 baddr = &inet6addr->sin6_addr;
88 } else if (saddr->sa_family == AF_PACKET) {
89 struct sockaddr_ll *etheradddr = (struct sockaddr_ll*)saddr;
90 addr->prefix = etheradddr->sll_hatype;
91 addr->port = etheradddr->sll_ifindex;
93 for (size_t i = 0; i < etheradddr->sll_halen; i++) {
94 *c++ = nixio__bin2hex[(etheradddr->sll_addr[i] & 0xf0) >> 4];
95 *c++ = nixio__bin2hex[(etheradddr->sll_addr[i] & 0x0f)];
102 errno = EAFNOSUPPORT;
106 if (!inet_ntop(saddr->sa_family, baddr, addr->host, sizeof(addr->host))) {
114 * address pulling helper
116 int nixio__addr_write(nixio_addr *addr, struct sockaddr *saddr) {
117 if (addr->family == AF_UNSPEC) {
118 if (strchr(addr->host, ':')) {
119 addr->family = AF_INET6;
121 addr->family = AF_INET;
124 if (addr->family == AF_INET) {
125 struct sockaddr_in *inetaddr = (struct sockaddr_in *)saddr;
126 memset(inetaddr, 0, sizeof(struct sockaddr_in));
128 if (inet_pton(AF_INET, addr->host, &inetaddr->sin_addr) < 1) {
132 inetaddr->sin_family = AF_INET;
133 inetaddr->sin_port = htons((uint16_t)addr->port);
135 } else if (addr->family == AF_INET6) {
136 struct sockaddr_in6 *inet6addr = (struct sockaddr_in6 *)saddr;
137 memset(inet6addr, 0, sizeof(struct sockaddr_in6));
139 if (inet_pton(AF_INET6, addr->host, &inet6addr->sin6_addr) < 1) {
143 inet6addr->sin6_family = AF_INET6;
144 inet6addr->sin6_port = htons((uint16_t)addr->port);
147 errno = EAFNOSUPPORT;
153 * netmask to prefix helper
155 int nixio__addr_prefix(struct sockaddr *saddr) {
160 if (saddr->sa_family == AF_INET) {
161 addr = (uint8_t*)(&((struct sockaddr_in*)saddr)->sin_addr);
163 } else if (saddr->sa_family == AF_INET6) {
164 addr = (uint8_t*)(&((struct sockaddr_in6*)saddr)->sin6_addr);
167 errno = EAFNOSUPPORT;
171 for (size_t i = 0; i < len; i++) {
172 if (addr[i] == 0xff) {
174 } else if (addr[i] == 0x00) {
177 for (uint8_t c = addr[i]; c; c <<= 1) {
187 * getaddrinfo(host, family, port)
189 static int nixio_getaddrinfo(lua_State *L) {
190 const char *host = NULL;
191 if (!lua_isnoneornil(L, 1)) {
192 host = luaL_checklstring(L, 1, NULL);
194 const char *family = luaL_optlstring(L, 2, "any", NULL);
195 const char *port = lua_tolstring(L, 3, NULL);
197 struct addrinfo hints, *result, *rp;
198 memset(&hints, 0, sizeof(hints));
200 if (!strcmp(family, "any")) {
201 hints.ai_family = AF_UNSPEC;
202 } else if (!strcmp(family, "inet")) {
203 hints.ai_family = AF_INET;
204 } else if (!strcmp(family, "inet6")) {
205 hints.ai_family = AF_INET6;
207 return luaL_argerror(L, 2, "supported values: any, inet, inet6");
210 hints.ai_socktype = 0;
211 hints.ai_protocol = 0;
213 int aistat = getaddrinfo(host, port, &hints, &result);
216 lua_pushinteger(L, aistat);
217 lua_pushstring(L, gai_strerror(aistat));
221 /* create socket object */
225 for (rp = result; rp != NULL; rp = rp->ai_next) {
226 /* avoid duplicate results */
228 if (!port && rp->ai_socktype != SOCK_STREAM) {
233 if (rp->ai_family == AF_INET || rp->ai_family == AF_INET6) {
234 lua_createtable(L, 0, port ? 4 : 2);
235 if (rp->ai_family == AF_INET) {
236 lua_pushliteral(L, "inet");
237 } else if (rp->ai_family == AF_INET6) {
238 lua_pushliteral(L, "inet6");
240 lua_setfield(L, -2, "family");
243 switch (rp->ai_socktype) {
245 lua_pushliteral(L, "stream");
248 lua_pushliteral(L, "dgram");
251 lua_pushliteral(L, "raw");
257 lua_setfield(L, -2, "socktype");
261 if (nixio__addr_parse(&addr, rp->ai_addr)) {
262 freeaddrinfo(result);
263 return nixio__perror_s(L);
267 lua_pushinteger(L, addr.port);
268 lua_setfield(L, -2, "port");
271 lua_pushstring(L, addr.host);
272 lua_setfield(L, -2, "address");
273 lua_rawseti(L, -2, i++);
277 freeaddrinfo(result);
283 * getnameinfo(address, family[, timeout])
285 static int nixio_getnameinfo(lua_State *L) {
286 const char *ip = luaL_checkstring(L, 1);
287 const char *family = luaL_optstring(L, 2, NULL);
290 struct sigaction sa_new, sa_old;
291 int timeout = luaL_optnumber(L, 3, 0);
292 if (timeout > 0 && timeout < 1000)
294 sa_new.sa_handler = nixio__handle_alarm;
296 sigemptyset(&sa_new.sa_mask);
297 sigaction(SIGALRM, &sa_new, &sa_old);
299 /* user timeout exceeded */
300 if (setjmp(nixio__jump_alarm))
302 sigaction(SIGALRM, &sa_old, NULL);
305 lua_pushinteger(L, EAI_AGAIN);
306 lua_pushstring(L, gai_strerror(EAI_AGAIN));
311 ualarm(timeout * 1000, 0);
315 char host[NI_MAXHOST];
317 struct sockaddr_storage saddr;
319 memset(&addr, 0, sizeof(addr));
320 strncpy(addr.host, ip, sizeof(addr.host) - 1);
323 addr.family = AF_UNSPEC;
324 } else if (!strcmp(family, "inet")) {
325 addr.family = AF_INET;
326 } else if (!strcmp(family, "inet6")) {
327 addr.family = AF_INET6;
329 return luaL_argerror(L, 2, "supported values: inet, inet6");
332 nixio__addr_write(&addr, (struct sockaddr *)&saddr);
334 int res = getnameinfo((struct sockaddr *)&saddr, sizeof(saddr),
335 host, sizeof(host), NULL, 0, NI_NAMEREQD);
338 if (timeout > 0 && timeout < 1000)
341 sigaction(SIGALRM, &sa_old, NULL);
347 lua_pushinteger(L, res);
348 lua_pushstring(L, gai_strerror(res));
351 lua_pushstring(L, host);
359 static int nixio_sock_getsockname(lua_State *L) {
360 int sockfd = nixio__checksockfd(L);
361 struct sockaddr_storage saddr;
362 socklen_t addrlen = sizeof(saddr);
365 if (getsockname(sockfd, (struct sockaddr*)&saddr, &addrlen) ||
366 nixio__addr_parse(&addr, (struct sockaddr*)&saddr)) {
367 return nixio__perror_s(L);
370 lua_pushstring(L, addr.host);
371 lua_pushinteger(L, addr.port);
378 static int nixio_sock_getpeername(lua_State *L) {
379 int sockfd = nixio__checksockfd(L);
380 struct sockaddr_storage saddr;
381 socklen_t addrlen = sizeof(saddr);
384 if (getpeername(sockfd, (struct sockaddr*)&saddr, &addrlen) ||
385 nixio__addr_parse(&addr, (struct sockaddr*)&saddr)) {
386 return nixio__perror_s(L);
389 lua_pushstring(L, addr.host);
390 lua_pushinteger(L, addr.port);
394 #if defined(__linux__) || defined(BSD)
400 static int nixio_getifaddrs(lua_State *L) {
402 struct ifaddrs *ifaddr, *c;
403 if (getifaddrs(&ifaddr) == -1) {
404 return nixio__perror(L);
410 for (c = ifaddr; c; c = c->ifa_next) {
413 lua_pushstring(L, c->ifa_name);
414 lua_setfield(L, -2, "name");
416 lua_createtable(L, 0, 7);
417 lua_pushboolean(L, c->ifa_flags & IFF_UP);
418 lua_setfield(L, -2, "up");
420 lua_pushboolean(L, c->ifa_flags & IFF_BROADCAST);
421 lua_setfield(L, -2, "broadcast");
423 lua_pushboolean(L, c->ifa_flags & IFF_LOOPBACK);
424 lua_setfield(L, -2, "loopback");
426 lua_pushboolean(L, c->ifa_flags & IFF_POINTOPOINT);
427 lua_setfield(L, -2, "pointtopoint");
429 lua_pushboolean(L, c->ifa_flags & IFF_NOARP);
430 lua_setfield(L, -2, "noarp");
432 lua_pushboolean(L, c->ifa_flags & IFF_PROMISC);
433 lua_setfield(L, -2, "promisc");
435 lua_pushboolean(L, c->ifa_flags & IFF_MULTICAST);
436 lua_setfield(L, -2, "multicast");
437 lua_setfield(L, -2, "flags");
440 if (!nixio__addr_parse(&addr, c->ifa_addr)) {
441 lua_pushstring(L, addr.host);
442 lua_setfield(L, -2, "addr");
445 if (c->ifa_addr->sa_family == AF_INET) {
446 lua_pushliteral(L, "inet");
447 } else if (c->ifa_addr->sa_family == AF_INET6) {
448 lua_pushliteral(L, "inet6");
450 } else if (c->ifa_addr->sa_family == AF_PACKET) {
451 lua_pushliteral(L, "packet");
454 lua_pushliteral(L, "unknown");
456 lua_setfield(L, -2, "family");
459 if (c->ifa_addr->sa_family == AF_PACKET) {
460 lua_pushinteger(L, addr.port);
461 lua_setfield(L, -2, "ifindex");
463 lua_pushinteger(L, addr.prefix);
464 lua_setfield(L, -2, "hatype");
470 if (c->ifa_data && (!c->ifa_addr
471 || c->ifa_addr->sa_family == AF_PACKET)) {
473 lua_pushliteral(L, "packet");
474 lua_setfield(L, -2, "family");
477 lua_createtable(L, 0, 10);
478 struct nixio__nds *stats = c->ifa_data;
480 lua_pushnumber(L, stats->rx_packets);
481 lua_setfield(L, -2, "rx_packets");
483 lua_pushnumber(L, stats->tx_packets);
484 lua_setfield(L, -2, "tx_packets");
486 lua_pushnumber(L, stats->rx_bytes);
487 lua_setfield(L, -2, "rx_bytes");
489 lua_pushnumber(L, stats->tx_bytes);
490 lua_setfield(L, -2, "tx_bytes");
492 lua_pushnumber(L, stats->rx_errors);
493 lua_setfield(L, -2, "rx_errors");
495 lua_pushnumber(L, stats->tx_errors);
496 lua_setfield(L, -2, "tx_errors");
498 lua_pushnumber(L, stats->rx_dropped);
499 lua_setfield(L, -2, "rx_dropped");
501 lua_pushnumber(L, stats->tx_dropped);
502 lua_setfield(L, -2, "tx_dropped");
504 lua_pushnumber(L, stats->multicast);
505 lua_setfield(L, -2, "multicast");
507 lua_pushnumber(L, stats->collisions);
508 lua_setfield(L, -2, "collisions");
512 lua_setfield(L, -2, "data");
515 if (c->ifa_netmask && !nixio__addr_parse(&addr, c->ifa_netmask)) {
516 lua_pushstring(L, addr.host);
517 lua_setfield(L, -2, "netmask");
519 lua_pushinteger(L, nixio__addr_prefix(c->ifa_netmask));
520 lua_setfield(L, -2, "prefix");
523 if (c->ifa_broadaddr && !nixio__addr_parse(&addr, c->ifa_broadaddr)) {
524 lua_pushstring(L, addr.host);
525 lua_setfield(L, -2, "broadaddr");
528 if (c->ifa_dstaddr && !nixio__addr_parse(&addr, c->ifa_dstaddr)) {
529 lua_pushstring(L, addr.host);
530 lua_setfield(L, -2, "dstaddr");
533 lua_rawseti(L, -2, i++);
543 static const luaL_reg R[] = {
544 #if defined(__linux__) || defined(BSD)
545 {"getifaddrs", nixio_getifaddrs},
547 {"getaddrinfo", nixio_getaddrinfo},
548 {"getnameinfo", nixio_getnameinfo},
553 static const luaL_reg M[] = {
554 {"getsockname", nixio_sock_getsockname},
555 {"getpeername", nixio_sock_getpeername},
559 void nixio_open_address(lua_State *L) {
560 luaL_register(L, NULL, R);
562 lua_pushvalue(L, -2);
563 luaL_register(L, NULL, M);