/src/suricata7/libhtp/htp/htp_connection.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * Copyright (c) 2009-2010 Open Information Security Foundation |
3 | | * Copyright (c) 2010-2013 Qualys, Inc. |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions are |
8 | | * met: |
9 | | * |
10 | | * - Redistributions of source code must retain the above copyright |
11 | | * notice, this list of conditions and the following disclaimer. |
12 | | |
13 | | * - Redistributions in binary form must reproduce the above copyright |
14 | | * notice, this list of conditions and the following disclaimer in the |
15 | | * documentation and/or other materials provided with the distribution. |
16 | | |
17 | | * - Neither the name of the Qualys, Inc. nor the names of its |
18 | | * contributors may be used to endorse or promote products derived from |
19 | | * this software without specific prior written permission. |
20 | | * |
21 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
22 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
23 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
24 | | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
25 | | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 | | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
31 | | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | ***************************************************************************/ |
33 | | |
34 | | /** |
35 | | * @file |
36 | | * @author Ivan Ristic <ivanr@webkreator.com> |
37 | | */ |
38 | | |
39 | | #include "htp_config_auto.h" |
40 | | |
41 | | #include "htp_private.h" |
42 | | |
43 | 16.4k | htp_conn_t *htp_conn_create(void) { |
44 | 16.4k | htp_conn_t *conn = calloc(1, sizeof (htp_conn_t)); |
45 | 16.4k | if (conn == NULL) return NULL; |
46 | | |
47 | 16.4k | conn->transactions = htp_list_create(16); |
48 | 16.4k | if (conn->transactions == NULL) { |
49 | 0 | free(conn); |
50 | 0 | return NULL; |
51 | 0 | } |
52 | | |
53 | 16.4k | conn->messages = htp_list_create(8); |
54 | 16.4k | if (conn->messages == NULL) { |
55 | 0 | htp_list_destroy(conn->transactions); |
56 | 0 | conn->transactions = NULL; |
57 | 0 | free(conn); |
58 | 0 | return NULL; |
59 | 0 | } |
60 | | |
61 | 16.4k | return conn; |
62 | 16.4k | } |
63 | | |
64 | 2.50k | void htp_conn_close(htp_conn_t *conn, const htp_time_t *timestamp) { |
65 | 2.50k | if (conn == NULL) return; |
66 | | |
67 | | // Update timestamp. |
68 | 2.50k | if (timestamp != NULL) { |
69 | 2.50k | memcpy(&(conn->close_timestamp), timestamp, sizeof(htp_time_t)); |
70 | 2.50k | } |
71 | 2.50k | } |
72 | | |
73 | 16.4k | void htp_conn_destroy(htp_conn_t *conn) { |
74 | 16.4k | if (conn == NULL) return; |
75 | | |
76 | 16.4k | if (conn->transactions != NULL) { |
77 | | // Destroy individual transactions. Do note that iterating |
78 | | // using the iterator does not work here because some of the |
79 | | // list element may be NULL (and with the iterator it is impossible |
80 | | // to distinguish a NULL element from the end of the list). |
81 | 232k | for (size_t i = 0, n = htp_list_size(conn->transactions); i < n; i++) { |
82 | 215k | htp_tx_t *tx = htp_list_get(conn->transactions, i); |
83 | 215k | if (tx != NULL) { |
84 | 177k | htp_tx_destroy_incomplete(tx); |
85 | 177k | } |
86 | 215k | } |
87 | | |
88 | 16.4k | htp_list_destroy(conn->transactions); |
89 | 16.4k | conn->transactions = NULL; |
90 | 16.4k | } |
91 | | |
92 | 16.4k | if (conn->messages != NULL) { |
93 | | // Destroy individual messages. |
94 | 2.16M | for (size_t i = 0, n = htp_list_size(conn->messages); i < n; i++) { |
95 | 2.15M | htp_log_t *l = htp_list_get(conn->messages, i); |
96 | 2.15M | free((void *) l->msg); |
97 | 2.15M | free(l); |
98 | 2.15M | } |
99 | | |
100 | 16.4k | htp_list_destroy(conn->messages); |
101 | 16.4k | conn->messages = NULL; |
102 | 16.4k | } |
103 | | |
104 | 16.4k | if (conn->server_addr != NULL) { |
105 | 0 | free(conn->server_addr); |
106 | 0 | } |
107 | | |
108 | 16.4k | if (conn->client_addr != NULL) { |
109 | 0 | free(conn->client_addr); |
110 | 0 | } |
111 | | |
112 | 16.4k | free(conn); |
113 | 16.4k | } |
114 | | |
115 | | htp_status_t htp_conn_open(htp_conn_t *conn, const char *client_addr, int client_port, |
116 | | const char *server_addr, int server_port, const htp_time_t *timestamp) |
117 | 16.4k | { |
118 | 16.4k | if (conn == NULL) return HTP_ERROR; |
119 | | |
120 | 16.4k | if (client_addr != NULL) { |
121 | 0 | conn->client_addr = strdup(client_addr); |
122 | 0 | if (conn->client_addr == NULL) return HTP_ERROR; |
123 | 0 | } |
124 | | |
125 | 16.4k | conn->client_port = client_port; |
126 | | |
127 | 16.4k | if (server_addr != NULL) { |
128 | 0 | conn->server_addr = strdup(server_addr); |
129 | 0 | if (conn->server_addr == NULL) { |
130 | 0 | if (conn->client_addr != NULL) { |
131 | 0 | free(conn->client_addr); |
132 | 0 | } |
133 | |
|
134 | 0 | return HTP_ERROR; |
135 | 0 | } |
136 | 0 | } |
137 | | |
138 | 16.4k | conn->server_port = server_port; |
139 | | |
140 | | // Remember when the connection was opened. |
141 | 16.4k | if (timestamp != NULL) { |
142 | 16.4k | memcpy(&(conn->open_timestamp), timestamp, sizeof(*timestamp)); |
143 | 16.4k | } |
144 | | |
145 | 16.4k | return HTP_OK; |
146 | 16.4k | } |
147 | | |
148 | 245k | htp_status_t htp_conn_remove_tx(htp_conn_t *conn, const htp_tx_t *tx) { |
149 | 245k | if ((tx == NULL) || (conn == NULL)) return HTP_ERROR; |
150 | 245k | if (conn->transactions == NULL) return HTP_ERROR; |
151 | 23.7M | for (size_t i = 0, n = htp_list_size(conn->transactions); i < n; i++) { |
152 | 23.7M | htp_tx_t *tx2 = htp_list_get(conn->transactions, i); |
153 | 23.7M | if (tx2 == tx) { |
154 | 245k | return htp_list_replace(conn->transactions, i, NULL); |
155 | 245k | } |
156 | 23.7M | } |
157 | 0 | return HTP_DECLINED; |
158 | 245k | } |
159 | | |
160 | 256k | void htp_conn_track_inbound_data(htp_conn_t *conn, size_t len, const htp_time_t *timestamp) { |
161 | 256k | if (conn == NULL) return; |
162 | 256k | conn->in_data_counter += len; |
163 | 256k | } |
164 | | |
165 | 298k | void htp_conn_track_outbound_data(htp_conn_t *conn, size_t len, const htp_time_t *timestamp) { |
166 | 298k | if (conn == NULL) return; |
167 | 298k | conn->out_data_counter += len; |
168 | 298k | } |