~jan-kneschke/mysql-proxy/packet-tracking-assertions

« back to all changes in this revision

Viewing changes to src/network-address-lua.c

  • Committer: Kay Roepke
  • Author(s): Jan Kneschke
  • Date: 2008-01-23 22:00:28 UTC
  • Revision ID: kay@mysql.com-20080123220028-hq2xqb69apa75fnx
first round on mysql-shell based on the proxy code

this is mostly a verification if the proxy-code is flexible enough to handle 
all three scenarios of: client, server and forwarding (proxy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $%BEGINLICENSE%$
2
 
 Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
3
 
 
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
7
 
 License.
8
 
 
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.
13
 
 
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
17
 
 02110-1301  USA
18
 
 
19
 
 $%ENDLICENSE%$ */
20
 
 
21
 
#include "config.h"
22
 
#include <lua.h>
23
 
 
24
 
#ifdef WIN32
25
 
#include <winsock2.h>
26
 
#include <ws2tcpip.h>
27
 
#else
28
 
#include <arpa/inet.h>
29
 
#endif
30
 
 
31
 
#include "lua-env.h"
32
 
#include "glib-ext.h"
33
 
 
34
 
#include "network-address.h"
35
 
#include "network-address-lua.h"
36
 
 
37
 
#define C(x) x, sizeof(x) - 1
38
 
#define S(x) x->str, x->len
39
 
 
40
 
static int proxy_address_get(lua_State *L) {
41
 
        network_address *addr = *(network_address **)luaL_checkself(L);
42
 
        gsize keysize = 0;
43
 
        const char *key = luaL_checklstring(L, 2, &keysize);
44
 
 
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"))) {
50
 
#ifdef HAVE_INET_NTOP
51
 
                char dst_addr[INET6_ADDRSTRLEN];
52
 
#endif
53
 
                const char *str = NULL;
54
 
 
55
 
                switch (addr->addr.common.sa_family) {
56
 
                case AF_INET:
57
 
                        str = inet_ntoa(addr->addr.ipv4.sin_addr);
58
 
                        if (!str) {
59
 
                                /* it shouldn't really fail, how about logging it ? */ 
60
 
                        }
61
 
                        break;
62
 
#ifdef HAVE_INET_NTOP
63
 
                case AF_INET6:
64
 
                        str = inet_ntop(addr->addr.common.sa_family, &addr->addr.ipv6.sin6_addr, dst_addr, sizeof(dst_addr));
65
 
                        if (!str) {
66
 
                                /* it shouldn't really fail, how about logging it ? */ 
67
 
                        }
68
 
                        break;
69
 
#endif
70
 
#ifndef WIN32
71
 
                case AF_UNIX:
72
 
                        str = addr->addr.un.sun_path;
73
 
                        break;
74
 
#endif
75
 
                default:
76
 
                        break;
77
 
                }
78
 
 
79
 
                if (NULL == str) {
80
 
                        lua_pushnil(L);
81
 
                } else {
82
 
                        lua_pushstring(L, str);
83
 
                }
84
 
        } else if (strleq(key, keysize, C("port"))) {
85
 
                switch (addr->addr.common.sa_family) {
86
 
                case AF_INET:
87
 
                        lua_pushinteger(L, ntohs(addr->addr.ipv4.sin_port));
88
 
                        break;
89
 
                case AF_INET6:
90
 
                        lua_pushinteger(L, ntohs(addr->addr.ipv6.sin6_port));
91
 
                        break;
92
 
                default:
93
 
                        lua_pushnil(L);
94
 
                        break;
95
 
                }
96
 
        } else {
97
 
                lua_pushnil(L);
98
 
        }
99
 
 
100
 
        return 1;
101
 
}
102
 
 
103
 
int network_address_lua_getmetatable(lua_State *L) {
104
 
        static const struct luaL_reg methods[] = {
105
 
                { "__index", proxy_address_get },
106
 
                { NULL, NULL },
107
 
        };
108
 
        return proxy_getmetatable(L, methods);
109
 
}
110
 
 
111
 
int network_address_lua_push(lua_State *L, network_address *addr) {
112
 
        network_address **address_p;
113
 
 
114
 
        if (!addr) {
115
 
                lua_pushnil(L);
116
 
                return 1;
117
 
        }
118
 
 
119
 
        address_p = lua_newuserdata(L, sizeof(network_address));
120
 
        *address_p = addr;
121
 
 
122
 
        network_address_lua_getmetatable(L);
123
 
        lua_setmetatable(L, -2); /* tie the metatable to the table   (sp -= 1) */
124
 
 
125
 
        return 1;
126
 
}
127