summaryrefslogtreecommitdiffstats
path: root/include/dnsjit/core/object.lua
blob: 7f5882949c9350de8e397a66fcb0cf116be6222d (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
-- Copyright (c) 2018-2021, OARC, Inc.
-- All rights reserved.
--
-- This file is part of dnsjit.
--
-- dnsjit 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, either version 3 of the License, or
-- (at your option) any later version.
--
-- dnsjit 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 dnsjit.  If not, see <http://www.gnu.org/licenses/>.

-- dnsjit.core.object
-- Base object that is passed between receiver and receivee
--   require("dnsjit.core.object")
--   print(object:type())
--   packet = object:cast()
--
-- This is the base object that can be casted to other objects that to
-- describe a DNS message, how it was captured or generated.
-- Objects can be chained together, for example a DNS message is created
-- ontop of a packet.
-- .SS Attributes
-- .TP
-- obj_type
-- The enum of the object type.
-- .TP
-- obj_prev
-- The previous object in the object chain.
module(...,package.seeall)

require("dnsjit.core.object_h")
require("dnsjit.core.object.pcap_h")
require("dnsjit.core.object.ether_h")
require("dnsjit.core.object.null_h")
require("dnsjit.core.object.loop_h")
require("dnsjit.core.object.linuxsll_h")
require("dnsjit.core.object.ieee802_h")
require("dnsjit.core.object.gre_h")
require("dnsjit.core.object.ip_h")
require("dnsjit.core.object.ip6_h")
require("dnsjit.core.object.icmp_h")
require("dnsjit.core.object.icmp6_h")
require("dnsjit.core.object.udp_h")
require("dnsjit.core.object.tcp_h")
require("dnsjit.core.object.payload_h")
require("dnsjit.core.object.dns_h")
local ffi = require("ffi")
local C = ffi.C

local t_name = "core_object_t"
local core_object_t
local Object = {
    NONE = 0,
    PCAP = 1,
    ETHER = 10,
    NULL = 11,
    LOOP = 12,
    LINUXSLL = 13,
    IEEE802 = 14,
    GRE = 15,
    IP = 20,
    IP6 = 21,
    ICMP = 22,
    ICMP6 = 23,
    UDP = 30,
    TCP = 31,
    PAYLOAD = 40,
    DNS = 50
}

local _type = {}
_type[Object.PCAP] = "pcap"
_type[Object.ETHER] = "ether"
_type[Object.NULL] = "null"
_type[Object.LOOP] = "loop"
_type[Object.LINUXSLL] = "linuxsll"
_type[Object.IEEE802] = "ieee802"
_type[Object.GRE] = "gre"
_type[Object.IP] = "ip"
_type[Object.IP6] = "ip6"
_type[Object.ICMP] = "icmp"
_type[Object.ICMP6] = "icmp6"
_type[Object.UDP] = "udp"
_type[Object.TCP] = "tcp"
_type[Object.PAYLOAD] = "payload"
_type[Object.DNS] = "dns"

_type[Object.NONE] = "none"

-- Return the textual type of the object.
function Object:type()
    return _type[self.obj_type]
end

-- Return the previous object.
function Object:prev()
    return self.obj_prev
end

local _cast = {}
_cast[Object.PCAP] = "core_object_pcap_t*"
_cast[Object.ETHER] = "core_object_ether_t*"
_cast[Object.NULL] = "core_object_null_t*"
_cast[Object.LOOP] = "core_object_loop_t*"
_cast[Object.LINUXSLL] = "core_object_linuxsll_t*"
_cast[Object.IEEE802] = "core_object_ieee802_t*"
_cast[Object.GRE] = "core_object_gre_t*"
_cast[Object.IP] = "core_object_ip_t*"
_cast[Object.IP6] = "core_object_ip6_t*"
_cast[Object.ICMP] = "core_object_icmp_t*"
_cast[Object.ICMP6] = "core_object_icmp6_t*"
_cast[Object.UDP] = "core_object_udp_t*"
_cast[Object.TCP] = "core_object_tcp_t*"
_cast[Object.PAYLOAD] = "core_object_payload_t*"
_cast[Object.DNS] = "core_object_dns_t*"

-- Cast the object to the underlining object module and return it.
function Object:cast()
    return ffi.cast(_cast[self.obj_type], self)
end

-- Cast the object to the specified object module and return it.
-- Returns nil if the object chain doesn't contained the specified object type.
function Object:cast_to(obj_type)
    if obj_type == nil then
        obj_type = self.obj_type
    end

    local obj = self
    while obj.obj_type ~= obj_type do
        obj = obj.obj_prev
        if obj == nil then return nil end
    end

    return ffi.cast(_cast[obj_type], obj)
end

-- Cast the object to the generic object module and return it.
function Object:uncast()
    return self
end

-- Make a copy of the object and return it.
function Object:copy()
    return C.core_object_copy(self)
end

-- Free the object, should only be used on copies or otherwise allocated.
function Object:free()
    C.core_object_free(self)
end

core_object_t = ffi.metatype(t_name, { __index = Object })

-- dnsjit.core.object.pcap (3),
-- dnsjit.core.object.ether (3),
-- dnsjit.core.object.null (3),
-- dnsjit.core.object.loop (3),
-- dnsjit.core.object.linuxsll (3),
-- dnsjit.core.object.ieee802 (3),
-- dnsjit.core.object.gre (3),
-- dnsjit.core.object.ip (3),
-- dnsjit.core.object.ip6 (3),
-- dnsjit.core.object.icmp (3),
-- dnsjit.core.object.icmp6 (3),
-- dnsjit.core.object.udp (3),
-- dnsjit.core.object.tcp (3),
-- dnsjit.core.object.payload (3),
-- dnsjit.core.object.dns (3)
return Object