summaryrefslogtreecommitdiffstats
path: root/src/packet_handler.h
blob: 7e966d4b8b3ca0edf52bdf996bc122e223568652 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Copyright (c) 2017-2024 OARC, Inc.
 * Copyright (c) 2011-2017, IIS - The Internet Foundation in Sweden
 * All rights reserved.
 *
 * This file is part of PacketQ.
 *
 * PacketQ 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.
 *
 * PacketQ 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 PacketQ.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __packetq_packet_handler_h
#define __packetq_packet_handler_h

#include <assert.h>
#include <cctype>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

#include "sql.h"
#include "tcp.h"

namespace packetq {

class Table;
class Row;

inline int get_int_h(unsigned char* data)
{
    return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
}

inline int get_short_h(unsigned char* data)
{
    return data[0] | (data[1] << 8);
}

inline int get_int(unsigned char* data)
{
    return data[3] | (data[2] << 8) | (data[1] << 16) | (data[0] << 24);
}

inline int get_short(unsigned char* data)
{
    return data[1] | (data[0] << 8);
}

RefCountString* v4_addr2str(in6addr_t& addr);
RefCountString* v6_addr2str(in6addr_t& addr);

class Payload {
public:
    char m_p[0x10000];
    int  m_size;
    Payload()
    {
        m_size = sizeof(m_p);
    }
    inline char* alloc(int size)
    {
        if (size > m_size)
            return 0;
        return m_p;
    }
};

class IP_header {
public:
    IP_header()
        : s(0)
        , us(0)
        , ethertype(0)
        , src_port(0)
        , dst_port(0)
        , proto(0)
        , ip_ttl(0)
        , ip_version(0)
        , id(0)
        , length(0)
        , fragments(0)
        , ident(0)
        , offset(0)
    {
        memset(&src_ip, 0, sizeof(src_ip));
        memset(&dst_ip, 0, sizeof(dst_ip));
    }

    void           reset();
    int            decode(unsigned char* data, int ether_type, int id);
    unsigned int   s;
    unsigned int   us;
    unsigned short ethertype;
    in6addr_t      src_ip;
    in6addr_t      dst_ip;
    unsigned short src_port;
    unsigned short dst_port;
    unsigned short proto;
    unsigned short ip_ttl;
    unsigned short ip_version;
    unsigned int   id;
    unsigned int   length;
    unsigned int   fragments;
    unsigned int   ident;
    unsigned int   offset;
};

class Packet_handler;

class IP_header_to_table {
public:
    enum {
        COLUMN_ID,
        COLUMN_S,
        COLUMN_US,
        COLUMN_ETHER_TYPE,
        COLUMN_PROTOCOL,
        COLUMN_IP_TTL,
        COLUMN_IP_VERSION,
        COLUMN_SRC_PORT,
        COLUMN_DST_PORT,
        COLUMN_SRC_ADDR,
        COLUMN_DST_ADDR,
        COLUMN_FRAGMENTS
    };

    void add_packet_columns(Packet_handler& packet_handler);
    void on_table_created(Table* table, const std::vector<int>& columns);
    void assign(Row* row, IP_header* head, const std::vector<int>& columns);

private:
    Int_accessor  acc_id;
    Int_accessor  acc_s;
    Int_accessor  acc_us;
    Int_accessor  acc_ether_type;
    Int_accessor  acc_protocol;
    Int_accessor  acc_ip_ttl;
    Int_accessor  acc_ip_version;
    Int_accessor  acc_src_port;
    Int_accessor  acc_dst_port;
    Int_accessor  acc_fragments;
    Text_accessor acc_src_addr;
    Text_accessor acc_dst_addr;
};

class Packet {
public:
    enum ParseResult {
        ERROR,
        OK,
        NOT_SAMPLED
    };

    Packet(unsigned char* data, int len, int s, int us, int id, int link_layer_type)
    {
        m_s               = s;
        m_us              = us;
        m_data            = data;
        m_len             = len;
        m_id              = id;
        m_link_layer_type = link_layer_type;
    }

    ParseResult parse(Packet_handler* handler, const std::vector<int>& columns, Row& destination_row, bool sample);
    bool        parse_ethernet();
    bool        parse_sll();
    bool        parse_ip(unsigned char* data, int len, int ether_type);
    bool        parse_transport(unsigned char* data, int len);

    IP_header      m_ip_header;
    unsigned char* m_data;
    int            m_len;
    int            m_s;
    int            m_us;
    int            m_id;
    int            m_link_layer_type;
};

struct Packet_column {
    const char*   name;
    const char*   description;
    int           id;
    Coltype::Type type;
};

class Packet_handler {
public:
    Packet_handler()
        : table_name(0)
    {
    }
    virtual ~Packet_handler()
    {
    }

    Table* create_table(const std::vector<int>& columns);

    // for actual packet handlers to fill in
    virtual void                on_table_created(Table* table, const std::vector<int>& columns)                           = 0;
    virtual Packet::ParseResult parse(Packet& packet, const std::vector<int>& columns, Row& destination_row, bool sample) = 0;

    const char*                table_name;
    std::vector<Packet_column> packet_columns;

    void add_packet_column(const char* name, const char* description, Coltype::Type type, int id);
};

void            init_packet_handlers(bool escape_dnsnames);
void            destroy_packet_handlers();
Packet_handler* get_packet_handler(std::string table_name);

} // namespace packetq

#endif // __packetq_packet_handler_h