1 //--------------------------------------------------------------------------
2 // Copyright (C) 2015-2021 Cisco and/or its affiliates. All rights reserved.
3 //
4 // This program is free software; you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License Version 2 as published
6 // by the Free Software Foundation.  You may not use, modify or distribute
7 // this program under any other version of the GNU General Public License.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 //--------------------------------------------------------------------------
18 // pp_codec_data_iface.cc author Joel Cornett <jocornet@cisco.com>
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "pp_codec_data_iface.h"
25 
26 #include "framework/codec.h"
27 #include "lua/lua_arg.h"
28 #include "main/snort_config.h"
29 
30 using namespace snort;
31 
set_fields(lua_State * L,int tindex,CodecData & self)32 static void set_fields(lua_State* L, int tindex, CodecData& self)
33 {
34     Lua::Table table(L, tindex);
35 
36     table.get_field("next_prot_id", reinterpret_cast<uint16_t&>(self.next_prot_id));
37     table.get_field("lyr_len", self.lyr_len);
38     table.get_field("invalid_bytes", self.invalid_bytes);
39     table.get_field("proto_bits", self.proto_bits);
40     table.get_field("codec_flags", self.codec_flags);
41     table.get_field("ip_layer_cnt", self.ip_layer_cnt);
42     table.get_field("ip6_extension_count", self.ip6_extension_count);
43     table.get_field("curr_ip6_extension", self.curr_ip6_extension);
44     table.get_field("ip6_csum_proto", reinterpret_cast<uint8_t&>(self.ip6_csum_proto));
45 }
46 
get_fields(lua_State * L,int tindex,CodecData & self)47 static void get_fields(lua_State* L, int tindex, CodecData& self)
48 {
49     Lua::Table table(L, tindex);
50 
51     table.set_field("next_prot_id", static_cast<uint16_t>(self.next_prot_id));
52     table.set_field("lyr_len", self.lyr_len);
53     table.set_field("invalid_bytes", self.invalid_bytes);
54     table.set_field("proto_bits", self.proto_bits);
55     table.set_field("codec_flags", self.codec_flags);
56     table.set_field("ip_layer_cnt", self.ip_layer_cnt);
57     table.set_field("ip6_extension_count", self.ip6_extension_count);
58     table.set_field("curr_ip6_extension", self.curr_ip6_extension);
59     table.set_field("ip6_csum_proto", static_cast<uint8_t>(self.ip6_csum_proto));
60 }
61 
62 static const luaL_Reg methods[] =
63 {
64     {
65         "new",
66         [](lua_State* L)
__anon454656030102() 67         {
68             Lua::Args args(L);
69 
70             auto& self = CodecDataIface.create(
71                 L, SnortConfig::get_conf(), ProtocolId::ETHERTYPE_NOT_SET);
72 
73             if ( args[1].is_table() )
74                 args[1].check_table(set_fields, self);
75             else if ( args[1].is_size() )
76             {
77                 //  FIXIT-L can check_size limit size to short?
78                 unsigned int tmp = args[1].check_size();
79                 if(tmp > UINT16_MAX)
80                     self.next_prot_id = ProtocolId::ETHERTYPE_NOT_SET;
81                 else
82                     self.next_prot_id = (ProtocolId)args[1].check_size();
83             }
84 
85             return 1;
86         }
87     },
88     {
89         "get",
90         [](lua_State* L)
__anon454656030202() 91         { return CodecDataIface.default_getter(L, get_fields); }
92     },
93     {
94         "set",
95         [](lua_State* L)
__anon454656030302() 96         { return CodecDataIface.default_setter(L, set_fields); }
97     },
98     { nullptr, nullptr }
99 };
100 
101 static const luaL_Reg metamethods[] =
102 {
103     {
104         "__tostring",
105         [](lua_State* L)
__anon454656030402() 106         { return CodecDataIface.default_tostring(L); }
107     },
108     {
109         "__gc",
110         [](lua_State* L)
__anon454656030502() 111         { return CodecDataIface.default_gc(L); }
112     },
113     { nullptr, nullptr }
114 };
115 
116 const struct Lua::TypeInterface<CodecData> CodecDataIface =
117 {
118     "CodecData",
119     methods,
120     metamethods
121 };
122