~roger-booth/mysql-proxy/laminator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* $%BEGINLICENSE%$
 Copyright (C) 2008 MySQL AB, 2008 Sun Microsystems, Inc

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; version 2 of the License.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 $%ENDLICENSE%$ */

#include <lua.h>

#include <arpa/inet.h>

#include "lua-env.h"
#include "glib-ext.h"

#include "network-address.h"
#include "network-address-lua.h"

#define C(x) x, sizeof(x) - 1
#define S(x) x->str, x->len

static int proxy_address_get(lua_State *L) {
	network_address *addr = *(network_address **)luaL_checkself(L);
	gsize keysize = 0;
	const char *key = luaL_checklstring(L, 2, &keysize);

	if (strleq(key, keysize, C("type"))) {
		lua_pushinteger(L, addr->addr.common.sa_family);
	} else if (strleq(key, keysize, C("name"))) {
		lua_pushlstring(L, S(addr->name));
	} else if (strleq(key, keysize, C("address"))) {
		char dst_addr[INET6_ADDRSTRLEN];
		const char *str = NULL;

		switch (addr->addr.common.sa_family) {
		case AF_INET:
			str = inet_ntop(addr->addr.common.sa_family, &addr->addr.ipv4.sin_addr, dst_addr, sizeof(dst_addr));
			if (!str) {
				/* it shouldn't really fail, how about logging it ? */ 
			}
			break;
		case AF_INET6:
			str = inet_ntop(addr->addr.common.sa_family, &addr->addr.ipv6.sin6_addr, dst_addr, sizeof(dst_addr));
			if (!str) {
				/* it shouldn't really fail, how about logging it ? */ 
			}
			break;
		case AF_UNIX:
			str = addr->addr.un.sun_path;
			break;
		default:
			break;
		}

		if (NULL == str) {
			lua_pushnil(L);
		} else {
			lua_pushstring(L, str);
		}
	} else if (strleq(key, keysize, C("port"))) {
		switch (addr->addr.common.sa_family) {
		case AF_INET:
			lua_pushinteger(L, ntohs(addr->addr.ipv4.sin_port));
			break;
		case AF_INET6:
			lua_pushinteger(L, ntohs(addr->addr.ipv6.sin6_port));
			break;
		default:
			lua_pushnil(L);
			break;
		}
	} else {
		lua_pushnil(L);
	}

	return 1;
}

int network_address_lua_getmetatable(lua_State *L) {
	static const struct luaL_reg methods[] = {
		{ "__index", proxy_address_get },
		{ NULL, NULL },
	};
	return proxy_getmetatable(L, methods);
}

int network_address_lua_push(lua_State *L, network_address *addr) {
	network_address **address_p;

	if (!addr) {
		lua_pushnil(L);
		return 1;
	}

	address_p = lua_newuserdata(L, sizeof(network_address));
	*address_p = addr;

	network_address_lua_getmetatable(L);
	lua_setmetatable(L, -2); /* tie the metatable to the table   (sp -= 1) */

	return 1;
}