pcap: Reformat

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Reviewed-by: Norbert Pocs <npocs@redhat.com>
This commit is contained in:
Jakub Jelen
2023-06-27 14:54:18 +02:00
parent 19404bf509
commit 96faaeea03

View File

@@ -60,7 +60,7 @@ struct pcap_hdr_s {
uint32_t magic_number; /* magic number */ uint32_t magic_number; /* magic number */
uint16_t version_major; /* major version number */ uint16_t version_major; /* major version number */
uint16_t version_minor; /* minor version number */ uint16_t version_minor; /* minor version number */
int32_t thiszone; /* GMT to local correction */ int32_t thiszone; /* GMT to local correction */
uint32_t sigfigs; /* accuracy of timestamps */ uint32_t sigfigs; /* accuracy of timestamps */
uint32_t snaplen; /* max length of captured packets, in octets */ uint32_t snaplen; /* max length of captured packets, in octets */
uint32_t network; /* data link type */ uint32_t network; /* data link type */
@@ -122,10 +122,11 @@ struct ssh_pcap_file_struct {
/** /**
* @brief create a new ssh_pcap_file object * @brief create a new ssh_pcap_file object
*/ */
ssh_pcap_file ssh_pcap_file_new(void) { ssh_pcap_file ssh_pcap_file_new(void)
struct ssh_pcap_file_struct *pcap; {
struct ssh_pcap_file_struct *pcap = NULL;
pcap = (struct ssh_pcap_file_struct *) malloc(sizeof(struct ssh_pcap_file_struct)); pcap = malloc(sizeof(struct ssh_pcap_file_struct));
if (pcap == NULL) { if (pcap == NULL) {
return NULL; return NULL;
} }
@@ -137,162 +138,182 @@ ssh_pcap_file ssh_pcap_file_new(void) {
/** @internal /** @internal
* @brief writes a packet on file * @brief writes a packet on file
*/ */
static int ssh_pcap_file_write(ssh_pcap_file pcap, ssh_buffer packet){ static int ssh_pcap_file_write(ssh_pcap_file pcap, ssh_buffer packet)
int err; {
uint32_t len; int err;
if(pcap == NULL || pcap->output==NULL) uint32_t len;
return SSH_ERROR; if (pcap == NULL || pcap->output == NULL) {
len=ssh_buffer_get_len(packet); return SSH_ERROR;
err=fwrite(ssh_buffer_get(packet),len,1,pcap->output); }
if(err<0) len = ssh_buffer_get_len(packet);
return SSH_ERROR; err = fwrite(ssh_buffer_get(packet), len, 1, pcap->output);
else if (err < 0) {
return SSH_OK; return SSH_ERROR;
} else {
return SSH_OK;
}
} }
/** @internal /** @internal
* @brief prepends a packet with the pcap header and writes packet * @brief prepends a packet with the pcap header and writes packet
* on file * on file
*/ */
int ssh_pcap_file_write_packet(ssh_pcap_file pcap, ssh_buffer packet, uint32_t original_len){ int ssh_pcap_file_write_packet(ssh_pcap_file pcap, ssh_buffer packet, uint32_t original_len)
ssh_buffer header=ssh_buffer_new(); {
struct timeval now; ssh_buffer header = ssh_buffer_new();
int err; struct timeval now;
if(header == NULL) int err;
return SSH_ERROR;
gettimeofday(&now,NULL); if (header == NULL) {
return SSH_ERROR;
}
gettimeofday(&now, NULL);
err = ssh_buffer_allocate_size(header, err = ssh_buffer_allocate_size(header,
sizeof(uint32_t) * 4 + sizeof(uint32_t) * 4 +
ssh_buffer_get_len(packet)); ssh_buffer_get_len(packet));
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
err = ssh_buffer_add_u32(header,htonl(now.tv_sec)); err = ssh_buffer_add_u32(header, htonl(now.tv_sec));
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
err = ssh_buffer_add_u32(header,htonl(now.tv_usec)); err = ssh_buffer_add_u32(header, htonl(now.tv_usec));
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
err = ssh_buffer_add_u32(header,htonl(ssh_buffer_get_len(packet))); err = ssh_buffer_add_u32(header, htonl(ssh_buffer_get_len(packet)));
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
err = ssh_buffer_add_u32(header,htonl(original_len)); err = ssh_buffer_add_u32(header, htonl(original_len));
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
err = ssh_buffer_add_buffer(header,packet); err = ssh_buffer_add_buffer(header, packet);
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
err=ssh_pcap_file_write(pcap,header); err = ssh_pcap_file_write(pcap, header);
error: error:
SSH_BUFFER_FREE(header); SSH_BUFFER_FREE(header);
return err; return err;
} }
/** /**
* @brief opens a new pcap file and creates header * @brief opens a new pcap file and creates header
*/ */
int ssh_pcap_file_open(ssh_pcap_file pcap, const char *filename){ int ssh_pcap_file_open(ssh_pcap_file pcap, const char *filename)
ssh_buffer header; {
int err; ssh_buffer header = NULL;
if(pcap == NULL) int err;
return SSH_ERROR;
if(pcap->output){ if (pcap == NULL) {
fclose(pcap->output); return SSH_ERROR;
pcap->output=NULL; }
} if (pcap->output) {
pcap->output=fopen(filename,"wb"); fclose(pcap->output);
if(pcap->output==NULL) pcap->output = NULL;
return SSH_ERROR; }
header=ssh_buffer_new(); pcap->output = fopen(filename, "wb");
if(header==NULL) if (pcap->output == NULL) {
return SSH_ERROR; return SSH_ERROR;
}
header = ssh_buffer_new();
if (header == NULL) {
return SSH_ERROR;
}
err = ssh_buffer_allocate_size(header, err = ssh_buffer_allocate_size(header,
sizeof(uint32_t) * 5 + sizeof(uint32_t) * 5 +
sizeof(uint16_t) * 2); sizeof(uint16_t) * 2);
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
err = ssh_buffer_add_u32(header,htonl(PCAP_MAGIC)); err = ssh_buffer_add_u32(header, htonl(PCAP_MAGIC));
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
err = ssh_buffer_add_u16(header,htons(PCAP_VERSION_MAJOR)); err = ssh_buffer_add_u16(header, htons(PCAP_VERSION_MAJOR));
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
err = ssh_buffer_add_u16(header,htons(PCAP_VERSION_MINOR)); err = ssh_buffer_add_u16(header, htons(PCAP_VERSION_MINOR));
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
/* currently hardcode GMT to 0 */ /* currently hardcode GMT to 0 */
err = ssh_buffer_add_u32(header,htonl(0)); err = ssh_buffer_add_u32(header, htonl(0));
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
/* accuracy */ /* accuracy */
err = ssh_buffer_add_u32(header,htonl(0)); err = ssh_buffer_add_u32(header, htonl(0));
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
/* size of the biggest packet */ /* size of the biggest packet */
err = ssh_buffer_add_u32(header,htonl(MAX_PACKET_LEN)); err = ssh_buffer_add_u32(header, htonl(MAX_PACKET_LEN));
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
/* we will write sort-of IP */ /* we will write sort-of IP */
err = ssh_buffer_add_u32(header,htonl(DLT_RAW)); err = ssh_buffer_add_u32(header, htonl(DLT_RAW));
if (err < 0) { if (err < 0) {
goto error; goto error;
} }
err=ssh_pcap_file_write(pcap,header); err = ssh_pcap_file_write(pcap,header);
error: error:
SSH_BUFFER_FREE(header); SSH_BUFFER_FREE(header);
return err; return err;
} }
int ssh_pcap_file_close(ssh_pcap_file pcap){ int ssh_pcap_file_close(ssh_pcap_file pcap)
int err; {
if(pcap ==NULL || pcap->output==NULL) int err;
return SSH_ERROR;
err=fclose(pcap->output); if (pcap == NULL || pcap->output == NULL) {
pcap->output=NULL; return SSH_ERROR;
if(err != 0) }
return SSH_ERROR; err = fclose(pcap->output);
else pcap->output = NULL;
return SSH_OK; if (err != 0) {
return SSH_ERROR;
} else {
return SSH_OK;
}
} }
void ssh_pcap_file_free(ssh_pcap_file pcap){ void ssh_pcap_file_free(ssh_pcap_file pcap)
ssh_pcap_file_close(pcap); {
SAFE_FREE(pcap); ssh_pcap_file_close(pcap);
SAFE_FREE(pcap);
} }
/** @internal /** @internal
* @brief allocates a new ssh_pcap_context object * @brief allocates a new ssh_pcap_context object
*/ */
ssh_pcap_context ssh_pcap_context_new(ssh_session session){ ssh_pcap_context ssh_pcap_context_new(ssh_session session)
ssh_pcap_context ctx = (struct ssh_pcap_context_struct *) malloc(sizeof(struct ssh_pcap_context_struct)); {
if(ctx==NULL){ ssh_pcap_context ctx = (struct ssh_pcap_context_struct *)malloc(sizeof(struct ssh_pcap_context_struct));
ssh_set_error_oom(session); if (ctx == NULL) {
return NULL; ssh_set_error_oom(session);
} return NULL;
ZERO_STRUCTP(ctx); }
ctx->session=session; ZERO_STRUCTP(ctx);
return ctx; ctx->session = session;
return ctx;
} }
void ssh_pcap_context_free(ssh_pcap_context ctx){ void ssh_pcap_context_free(ssh_pcap_context ctx)
SAFE_FREE(ctx); {
SAFE_FREE(ctx);
} }
void ssh_pcap_context_set_file(ssh_pcap_context ctx, ssh_pcap_file pcap){ void ssh_pcap_context_set_file(ssh_pcap_context ctx, ssh_pcap_file pcap)
ctx->file=pcap; {
ctx->file = pcap;
} }
/** @internal /** @internal
@@ -379,7 +400,7 @@ static int ssh_pcap_context_connect(ssh_pcap_context ctx)
*/ */
int ssh_pcap_context_write(ssh_pcap_context ctx, int ssh_pcap_context_write(ssh_pcap_context ctx,
enum ssh_pcap_direction direction, enum ssh_pcap_direction direction,
void *data, void *data,
uint32_t len, uint32_t len,
uint32_t origlen) uint32_t origlen)
{ {
@@ -413,7 +434,7 @@ int ssh_pcap_context_write(ssh_pcap_context ctx,
0); /* checksum */ 0); /* checksum */
ctx->file->ipsequence++; ctx->file->ipsequence++;
if (rc != SSH_OK){ if (rc != SSH_OK) {
goto error; goto error;
} }
if (direction == SSH_PCAP_DIR_OUT) { if (direction == SSH_PCAP_DIR_OUT) {
@@ -506,17 +527,19 @@ error:
* sessions. * sessions.
* @returns SSH_ERROR in case of error, SSH_OK otherwise. * @returns SSH_ERROR in case of error, SSH_OK otherwise.
*/ */
int ssh_set_pcap_file(ssh_session session, ssh_pcap_file pcap){ int ssh_set_pcap_file(ssh_session session, ssh_pcap_file pcap)
ssh_pcap_context ctx=ssh_pcap_context_new(session); {
if(ctx==NULL){ ssh_pcap_context ctx = ssh_pcap_context_new(session);
ssh_set_error_oom(session); if (ctx == NULL) {
return SSH_ERROR; ssh_set_error_oom(session);
} return SSH_ERROR;
ctx->file=pcap; }
if(session->pcap_ctx) ctx->file = pcap;
ssh_pcap_context_free(session->pcap_ctx); if (session->pcap_ctx) {
session->pcap_ctx=ctx; ssh_pcap_context_free(session->pcap_ctx);
return SSH_OK; }
session->pcap_ctx = ctx;
return SSH_OK;
} }
/** @} */ /** @} */
@@ -527,28 +550,36 @@ int ssh_set_pcap_file(ssh_session session, ssh_pcap_file pcap){
#include "libssh/libssh.h" #include "libssh/libssh.h"
#include "libssh/priv.h" #include "libssh/priv.h"
int ssh_pcap_file_close(ssh_pcap_file pcap){ int ssh_pcap_file_close(ssh_pcap_file pcap)
(void) pcap; {
return SSH_ERROR; (void)pcap;
return SSH_ERROR;
} }
void ssh_pcap_file_free(ssh_pcap_file pcap){ void ssh_pcap_file_free(ssh_pcap_file pcap)
(void) pcap; {
(void)pcap;
} }
ssh_pcap_file ssh_pcap_file_new(void){ ssh_pcap_file ssh_pcap_file_new(void)
return NULL; {
return NULL;
} }
int ssh_pcap_file_open(ssh_pcap_file pcap, const char *filename){ int ssh_pcap_file_open(ssh_pcap_file pcap, const char *filename)
(void) pcap; {
(void) filename; (void)pcap;
return SSH_ERROR; (void)filename;
return SSH_ERROR;
} }
int ssh_set_pcap_file(ssh_session session, ssh_pcap_file pcapfile){ int ssh_set_pcap_file(ssh_session session, ssh_pcap_file pcapfile)
(void) pcapfile; {
ssh_set_error(session,SSH_REQUEST_DENIED,"Pcap support not compiled in"); (void)pcapfile;
return SSH_ERROR;
ssh_set_error(session, SSH_REQUEST_DENIED, "Pcap support not compiled in");
return SSH_ERROR;
} }
#endif #endif