2
Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License as
6
published by the Free Software Foundation; version 2 of the
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28
#include <arpa/inet.h>
34
#include "network-address.h"
35
#include "network-address-lua.h"
37
#define C(x) x, sizeof(x) - 1
38
#define S(x) x->str, x->len
40
static int proxy_address_get(lua_State *L) {
41
network_address *addr = *(network_address **)luaL_checkself(L);
43
const char *key = luaL_checklstring(L, 2, &keysize);
45
if (strleq(key, keysize, C("type"))) {
46
lua_pushinteger(L, addr->addr.common.sa_family);
47
} else if (strleq(key, keysize, C("name"))) {
48
lua_pushlstring(L, S(addr->name));
49
} else if (strleq(key, keysize, C("address"))) {
51
char dst_addr[INET6_ADDRSTRLEN];
53
const char *str = NULL;
55
switch (addr->addr.common.sa_family) {
57
str = inet_ntoa(addr->addr.ipv4.sin_addr);
59
/* it shouldn't really fail, how about logging it ? */
64
str = inet_ntop(addr->addr.common.sa_family, &addr->addr.ipv6.sin6_addr, dst_addr, sizeof(dst_addr));
66
/* it shouldn't really fail, how about logging it ? */
72
str = addr->addr.un.sun_path;
82
lua_pushstring(L, str);
84
} else if (strleq(key, keysize, C("port"))) {
85
switch (addr->addr.common.sa_family) {
87
lua_pushinteger(L, ntohs(addr->addr.ipv4.sin_port));
90
lua_pushinteger(L, ntohs(addr->addr.ipv6.sin6_port));
103
int network_address_lua_getmetatable(lua_State *L) {
104
static const struct luaL_reg methods[] = {
105
{ "__index", proxy_address_get },
108
return proxy_getmetatable(L, methods);
111
int network_address_lua_push(lua_State *L, network_address *addr) {
112
network_address **address_p;
119
address_p = lua_newuserdata(L, sizeof(network_address));
122
network_address_lua_getmetatable(L);
123
lua_setmetatable(L, -2); /* tie the metatable to the table (sp -= 1) */