1 //--------------------------------------------------------------------------
2 // Copyright (C) 2018-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 // http_context_data.cc author Bhagya Tholpady <bbantwal@cisco.com>
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "http_context_data.h"
25 
26 #include "http_msg_section.h"
27 #include "protocols/packet.h"
28 #include "service_inspectors/http2_inspect/http2_flow_data.h"
29 
30 using namespace snort;
31 
32 unsigned HttpContextData::ips_id = 0;
33 
get_snapshot(const Packet * p)34 HttpMsgSection* HttpContextData::get_snapshot(const Packet* p)
35 {
36     assert(p != nullptr);
37     return get_snapshot(p->flow, p->context);
38 }
39 
get_snapshot(const Flow * flow,IpsContext * context)40 HttpMsgSection* HttpContextData::get_snapshot(const Flow* flow, IpsContext* context)
41 {
42     assert(flow != nullptr);
43 
44     if (Http2FlowData::inspector_id != 0)
45     {
46         const Http2FlowData* const h2i_flow_data =
47             (Http2FlowData*)flow->get_flow_data(Http2FlowData::inspector_id);
48         if (h2i_flow_data != nullptr)
49             return h2i_flow_data->get_hi_msg_section();
50     }
51 
52     HttpContextData* hcd = (HttpContextData*)DetectionEngine::get_data(HttpContextData::ips_id,
53         context);
54 
55     if ( !hcd )
56         return nullptr;
57 
58     return hcd->current_section;
59 }
60 
save_snapshot(HttpMsgSection * section)61 void HttpContextData::save_snapshot(HttpMsgSection* section)
62 {
63     HttpContextData* hcd = IpsContextData::get<HttpContextData>(HttpContextData::ips_id);
64 
65     hcd->current_section = section;
66 }
67 
clear_snapshot(IpsContext * context)68 HttpMsgSection* HttpContextData::clear_snapshot(IpsContext* context)
69 {
70     HttpMsgSection* section = nullptr;
71     HttpContextData* hcd = (HttpContextData*)DetectionEngine::get_data(HttpContextData::ips_id,
72             context);
73 
74     if ( hcd )
75     {
76         section = hcd->current_section;
77         hcd->clear();
78     }
79 
80     return section;
81 }
82