4 * Wireshark's interface to the Lua Programming Language
6 * (c) 2006, Luis E. Garcia Ontanon <luis@ontanon.org>
7 * (c) 2008, Balint Reczey <balint.reczey@ericsson.com>
8 * (c) 2011, Stig Bjorlykke <stig@bjorlykke.org>
9 * (c) 2014, Hadriel Kaplan <hadrielk@yahoo.com>
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
13 * Copyright 1998 Gerald Combs
15 * SPDX-License-Identifier: GPL-2.0-or-later
22 #include <epan/addr_resolv.h>
24 /* WSLUA_CONTINUE_MODULE Pinfo */
27 WSLUA_CLASS_DEFINE(Address
,FAIL_ON_NULL("Address")); /* Represents an address. */
29 WSLUA_CONSTRUCTOR
Address_ip(lua_State
* L
) {
30 /* Creates an Address Object representing an IPv4 address. */
32 #define WSLUA_ARG_Address_ip_HOSTNAME 1 /* The address or name of the IP host. */
33 Address addr
= (Address
)g_malloc(sizeof(address
));
35 const char* name
= luaL_checkstring(L
,WSLUA_ARG_Address_ip_HOSTNAME
);
37 if (! get_host_ipaddr(name
, &ip_addr
)) {
41 alloc_address_wmem(NULL
, addr
, AT_IPv4
, 4, &ip_addr
);
43 WSLUA_RETURN(1); /* The Address object. */
46 WSLUA_CONSTRUCTOR
Address_ipv6(lua_State
* L
) {
47 /* Creates an Address Object representing an IPv6 address. */
49 #define WSLUA_ARG_Address_ipv6_HOSTNAME 1 /* The address or name of the IP host. */
50 Address addr
= (Address
)g_malloc(sizeof(address
));
52 const char* name
= luaL_checkstring(L
,WSLUA_ARG_Address_ipv6_HOSTNAME
);
54 if (!get_host_ipaddr6(name
, &ip_addr
)) {
55 memset(&ip_addr
, 0, sizeof(ip_addr
));
58 alloc_address_wmem(NULL
, addr
, AT_IPv6
, sizeof(ip_addr
.bytes
), &ip_addr
.bytes
);
60 WSLUA_RETURN(1); /* The Address object */
63 WSLUA_CONSTRUCTOR
Address_ether(lua_State
*L
) {
64 /* Creates an Address Object representing an Ethernet address. */
66 #define WSLUA_ARG_Address_ether_ETH 1 /* The Ethernet address. */
67 Address addr
= (Address
)g_malloc(sizeof(address
));
68 const char *name
= luaL_checkstring(L
, WSLUA_ARG_Address_ether_ETH
);
71 if(!str_to_eth(name
, eth_buf
))
72 memset(eth_buf
, 0, sizeof(eth_buf
));
74 alloc_address_wmem(NULL
, addr
, AT_ETHER
, sizeof(eth_buf
), eth_buf
);
76 WSLUA_RETURN(1); /* The Address object. */
81 static int Address_ss7(lua_State
* L
) {
82 Address addr
= g_malloc(sizeof(address
));
89 static int Address_sna(lua_State
* L
) {
90 Address addr
= g_malloc(sizeof(address
));
97 static int Address_atalk(lua_State
* L
) {
98 Address addr
= g_malloc(sizeof(address
));
100 /* alloc_address() */
105 static int Address_vines(lua_State
* L
) {
106 Address addr
= g_malloc(sizeof(address
));
108 /* alloc_address() */
113 static int Address_osi(lua_State
* L
) {
114 Address addr
= g_malloc(sizeof(address
));
116 /* alloc_address() */
121 static int Address_arcnet(lua_State
* L
) {
122 Address addr
= g_malloc(sizeof(address
));
124 /* alloc_address() */
129 static int Address_fc(lua_State
* L
) {
130 Address addr
= g_malloc(sizeof(address
));
132 /* alloc_address() */
137 static int Address_string(lua_State
* L
) {
138 Address addr
= g_malloc(sizeof(address
));
140 /* alloc_address() */
145 static int Address_eui64(lua_State
* L
) {
146 Address addr
= g_malloc(sizeof(address
));
148 /* alloc_address() */
153 static int Address_uri(lua_State
* L
) {
154 Address addr
= g_malloc(sizeof(address
));
156 /* alloc_address() */
161 static int Address_tipc(lua_State
* L
) {
162 Address addr
= g_malloc(sizeof(address
));
164 /* alloc_address() */
171 WSLUA_METHODS Address_methods
[] = {
172 WSLUA_CLASS_FNREG(Address
,ip
),
173 WSLUA_CLASS_FNREG_ALIAS(Address
,ipv4
,ip
),
174 WSLUA_CLASS_FNREG(Address
,ipv6
),
175 WSLUA_CLASS_FNREG(Address
,ether
),
177 WSLUA_CLASS_FNREG_ALIAS(Address
,ss7pc
,ss7
),
178 WSLUA_CLASS_FNREG(Address
,sna
},
179 WSLUA_CLASS_FNREG(Address
,atalk
),
180 WSLUA_CLASS_FNREG(Address
,vines
),
181 WSLUA_CLASS_FNREG(Address
,osi
),
182 WSLUA_CLASS_FNREG(Address
,arcnet
),
183 WSLUA_CLASS_FNREG(Address
,fc
),
184 WSLUA_CLASS_FNREG(Address
,string
),
185 WSLUA_CLASS_FNREG(Address
,eui64
),
186 WSLUA_CLASS_FNREG(Address
,uri
),
187 WSLUA_CLASS_FNREG(Address
,tipc
),
192 WSLUA_METAMETHOD
Address__tostring(lua_State
* L
) {
193 Address addr
= checkAddress(L
,1);
194 char *str
= address_to_display(NULL
, addr
);
196 lua_pushstring(L
, str
);
198 wmem_free(NULL
, str
);
200 WSLUA_RETURN(1); /* The string representing the address. */
203 /* Gets registered as metamethod automatically by WSLUA_REGISTER_CLASS/META */
204 static int Address__gc(lua_State
* L
) {
205 Address addr
= toAddress(L
,1);
215 WSLUA_METAMETHOD
Address__eq(lua_State
* L
) { /* Compares two Addresses. */
216 Address addr1
= checkAddress(L
,1);
217 Address addr2
= checkAddress(L
,2);
220 if (addresses_equal(addr1
, addr2
))
223 lua_pushboolean(L
,result
);
228 WSLUA_METAMETHOD
Address__le(lua_State
* L
) { /* Compares two Addresses. */
229 Address addr1
= checkAddress(L
,1);
230 Address addr2
= checkAddress(L
,2);
233 if (cmp_address(addr1
, addr2
) <= 0)
236 lua_pushboolean(L
,result
);
241 WSLUA_METAMETHOD
Address__lt(lua_State
* L
) { /* Compares two Addresses. */
242 Address addr1
= checkAddress(L
,1);
243 Address addr2
= checkAddress(L
,2);
246 if (cmp_address(addr1
, addr2
) < 0)
249 lua_pushboolean(L
,result
);
254 WSLUA_META Address_meta
[] = {
255 WSLUA_CLASS_MTREG(Address
,tostring
),
256 WSLUA_CLASS_MTREG(Address
,eq
),
257 WSLUA_CLASS_MTREG(Address
,le
),
258 WSLUA_CLASS_MTREG(Address
,lt
),
263 int Address_register(lua_State
*L
) {
264 WSLUA_REGISTER_CLASS(Address
);
270 * Editor modelines - https://www.wireshark.org/tools/modelines.html
275 * indent-tabs-mode: nil
278 * vi: set shiftwidth=4 tabstop=8 expandtab:
279 * :indentSize=4:tabSize=8:noTabs=true: