Skip to content

Commit

Permalink
http: aliases for htp opaque htp_tx_t
Browse files Browse the repository at this point in the history
In preparation of libhtp rust
  • Loading branch information
catenacyber committed Jan 17, 2025
1 parent 8f6795d commit 23050d7
Show file tree
Hide file tree
Showing 23 changed files with 345 additions and 319 deletions.
41 changes: 21 additions & 20 deletions src/app-layer-htp-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "suricata-common.h"
#include "app-layer-htp-file.h"
#include "app-layer-htp-libhtp.h"
#include "app-layer-htp-range.h"
#include "app-layer-events.h"
#include "util-validate.h"
Expand Down Expand Up @@ -181,17 +182,17 @@ int HTPFileOpenWithRange(HtpState *s, HtpTxUserData *txud, const uint8_t *filena
// Then, we will try to handle reassembly of different ranges of the same file
uint8_t *keyurl;
uint32_t keylen;
if (tx->request_hostname != NULL) {
uint32_t hlen = (uint32_t)bstr_len(tx->request_hostname);
if (bstr_len(tx->request_hostname) > UINT16_MAX) {
if (htp_tx_request_hostname(tx) != NULL) {
uint32_t hlen = (uint32_t)bstr_len(htp_tx_request_hostname(tx));
if (bstr_len(htp_tx_request_hostname(tx)) > UINT16_MAX) {
hlen = UINT16_MAX;
}
keylen = hlen + filename_len;
keyurl = SCMalloc(keylen);
if (keyurl == NULL) {
SCReturnInt(-1);
}
memcpy(keyurl, bstr_ptr(tx->request_hostname), hlen);
memcpy(keyurl, bstr_ptr(htp_tx_request_hostname(tx)), hlen);
memcpy(keyurl + hlen, filename, filename_len);
} else {
// do not reassemble file without host info
Expand Down Expand Up @@ -402,9 +403,9 @@ static int HTPFileParserTest01(void)

htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, http_state, 0);
FAIL_IF_NULL(tx);
FAIL_IF_NULL(tx->request_method);
FAIL_IF_NULL(htp_tx_request_method(tx));

FAIL_IF(memcmp(bstr_util_strdup_to_c(tx->request_method), "POST", 4) != 0);
FAIL_IF(memcmp(bstr_util_strdup_to_c(htp_tx_request_method(tx)), "POST", 4) != 0);

AppLayerParserThreadCtxFree(alp_tctx);
StreamTcpFreeConfig(true);
Expand Down Expand Up @@ -476,8 +477,8 @@ static int HTPFileParserTest02(void)

htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, http_state, 0);
FAIL_IF_NULL(tx);
FAIL_IF_NULL(tx->request_method);
FAIL_IF(memcmp(bstr_util_strdup_to_c(tx->request_method), "POST", 4) != 0);
FAIL_IF_NULL(htp_tx_request_method(tx));
FAIL_IF(memcmp(bstr_util_strdup_to_c(htp_tx_request_method(tx)), "POST", 4) != 0);
HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);
FAIL_IF_NULL(tx_ud->files_ts.tail);
Expand Down Expand Up @@ -568,9 +569,9 @@ static int HTPFileParserTest03(void)

htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, http_state, 0);
FAIL_IF_NULL(tx);
FAIL_IF_NULL(tx->request_method);
FAIL_IF_NULL(htp_tx_request_method(tx));

FAIL_IF(memcmp(bstr_util_strdup_to_c(tx->request_method), "POST", 4) != 0);
FAIL_IF(memcmp(bstr_util_strdup_to_c(htp_tx_request_method(tx)), "POST", 4) != 0);

HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);
Expand Down Expand Up @@ -664,9 +665,9 @@ static int HTPFileParserTest04(void)

htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, http_state, 0);
FAIL_IF_NULL(tx);
FAIL_IF_NULL(tx->request_method);
FAIL_IF_NULL(htp_tx_request_method(tx));

FAIL_IF(memcmp(bstr_util_strdup_to_c(tx->request_method), "POST", 4) != 0);
FAIL_IF(memcmp(bstr_util_strdup_to_c(htp_tx_request_method(tx)), "POST", 4) != 0);

HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);
Expand Down Expand Up @@ -730,9 +731,9 @@ static int HTPFileParserTest05(void)

htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, http_state, 0);
FAIL_IF_NULL(tx);
FAIL_IF_NULL(tx->request_method);
FAIL_IF_NULL(htp_tx_request_method(tx));

FAIL_IF(memcmp(bstr_util_strdup_to_c(tx->request_method), "POST", 4) != 0);
FAIL_IF(memcmp(bstr_util_strdup_to_c(htp_tx_request_method(tx)), "POST", 4) != 0);

HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);
Expand Down Expand Up @@ -805,9 +806,9 @@ static int HTPFileParserTest06(void)

htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, http_state, 0);
FAIL_IF_NULL(tx);
FAIL_IF_NULL(tx->request_method);
FAIL_IF_NULL(htp_tx_request_method(tx));

FAIL_IF(memcmp(bstr_util_strdup_to_c(tx->request_method), "POST", 4) != 0);
FAIL_IF(memcmp(bstr_util_strdup_to_c(htp_tx_request_method(tx)), "POST", 4) != 0);

HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);
Expand Down Expand Up @@ -870,8 +871,8 @@ static int HTPFileParserTest07(void)

htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, http_state, 0);
FAIL_IF_NULL(tx);
FAIL_IF_NULL(tx->request_method);
FAIL_IF(memcmp(bstr_util_strdup_to_c(tx->request_method), "POST", 4) != 0);
FAIL_IF_NULL(htp_tx_request_method(tx));
FAIL_IF(memcmp(bstr_util_strdup_to_c(htp_tx_request_method(tx)), "POST", 4) != 0);

HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);
Expand Down Expand Up @@ -1192,9 +1193,9 @@ static int HTPFileParserTest11(void)

htp_tx_t *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, http_state, 0);
FAIL_IF_NULL(tx);
FAIL_IF_NULL(tx->request_method);
FAIL_IF_NULL(htp_tx_request_method(tx));

FAIL_IF(memcmp(bstr_util_strdup_to_c(tx->request_method), "POST", 4) != 0);
FAIL_IF(memcmp(bstr_util_strdup_to_c(htp_tx_request_method(tx)), "POST", 4) != 0);

HtpTxUserData *tx_ud = htp_tx_get_user_data(tx);
FAIL_IF_NULL(tx_ud);
Expand Down
25 changes: 25 additions & 0 deletions src/app-layer-htp-libhtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,31 @@
#define HTP_RESPONSE_PROGRESS_TRAILER HTP_RESPONSE_TRAILER
#define HTP_RESPONSE_PROGRESS_COMPLETE HTP_RESPONSE_COMPLETE

// Functions introduced to handle opaque htp_tx_t
#define htp_tx_flags(tx) tx->flags
#define htp_tx_is_protocol_0_9(tx) tx->is_protocol_0_9
#define htp_tx_request_auth_type(tx) tx->request_auth_type
#define htp_tx_request_hostname(tx) tx->request_hostname
#define htp_tx_request_line(tx) tx->request_line
#define htp_tx_request_message_len(tx) tx->request_message_len
#define htp_tx_request_method(tx) tx->request_method
#define htp_tx_request_method_number(tx) tx->request_method_number
#define htp_tx_request_port_number(tx) tx->request_port_number
#define htp_tx_request_progress(tx) tx->request_progress
#define htp_tx_request_protocol(tx) tx->request_protocol
#define htp_tx_request_protocol_number(tx) tx->request_protocol_number
#define htp_tx_request_uri(tx) tx->request_uri
#define htp_tx_request_headers(tx) tx->request_headers
#define htp_tx_response_headers(tx) tx->response_headers
#define htp_tx_response_protocol(tx) tx->response_protocol
#define htp_tx_response_line(tx) tx->response_line
#define htp_tx_response_message(tx) tx->response_message
#define htp_tx_response_message_len(tx) tx->response_message_len
#define htp_tx_response_status(tx) tx->response_status
#define htp_tx_response_status_number(tx) tx->response_status_number
#define htp_tx_response_progress(tx) tx->response_progress
#define htp_tx_response_protocol_number(tx) tx->response_protocol_number

bstr *SCHTPGenerateNormalizedUri(htp_tx_t *tx, htp_uri_t *uri, bool uri_include_all);

#endif /* SURICATA_APP_LAYER_HTP_LIBHTP__H */
5 changes: 3 additions & 2 deletions src/app-layer-htp-xff.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "app-layer-parser.h"
#include "app-layer-htp.h"
#include "app-layer-htp-libhtp.h"
#include "app-layer-htp-xff.h"

#ifndef HAVE_MEMRCHR
Expand Down Expand Up @@ -140,8 +141,8 @@ int HttpXFFGetIPFromTx(const Flow *f, uint64_t tx_id, HttpXFFCfg *xff_cfg,
}

htp_header_t *h_xff = NULL;
if (tx->request_headers != NULL) {
h_xff = htp_table_get_c(tx->request_headers, xff_cfg->header);
if (htp_tx_request_headers(tx) != NULL) {
h_xff = htp_table_get_c(htp_tx_request_headers(tx), xff_cfg->header);
}

if (h_xff != NULL && bstr_len(h_xff->value) >= XFF_CHAIN_MINLEN &&
Expand Down
Loading

0 comments on commit 23050d7

Please sign in to comment.