mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-11 10:40:27 +09:00
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:
169
src/pcap.c
169
src/pcap.c
@@ -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,57 +138,64 @@ 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;
|
int err;
|
||||||
uint32_t len;
|
uint32_t len;
|
||||||
if(pcap == NULL || pcap->output==NULL)
|
if (pcap == NULL || pcap->output == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
len=ssh_buffer_get_len(packet);
|
}
|
||||||
err=fwrite(ssh_buffer_get(packet),len,1,pcap->output);
|
len = ssh_buffer_get_len(packet);
|
||||||
if(err<0)
|
err = fwrite(ssh_buffer_get(packet), len, 1, pcap->output);
|
||||||
|
if (err < 0) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
else
|
} else {
|
||||||
return SSH_OK;
|
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();
|
{
|
||||||
|
ssh_buffer header = ssh_buffer_new();
|
||||||
struct timeval now;
|
struct timeval now;
|
||||||
int err;
|
int err;
|
||||||
if(header == NULL)
|
|
||||||
|
if (header == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
gettimeofday(&now,NULL);
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
@@ -196,78 +204,88 @@ error:
|
|||||||
/**
|
/**
|
||||||
* @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;
|
{
|
||||||
|
ssh_buffer header = NULL;
|
||||||
int err;
|
int err;
|
||||||
if(pcap == NULL)
|
|
||||||
|
if (pcap == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
if(pcap->output){
|
|
||||||
fclose(pcap->output);
|
|
||||||
pcap->output=NULL;
|
|
||||||
}
|
}
|
||||||
pcap->output=fopen(filename,"wb");
|
if (pcap->output) {
|
||||||
if(pcap->output==NULL)
|
fclose(pcap->output);
|
||||||
|
pcap->output = NULL;
|
||||||
|
}
|
||||||
|
pcap->output = fopen(filename, "wb");
|
||||||
|
if (pcap->output == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
header=ssh_buffer_new();
|
}
|
||||||
if(header==NULL)
|
header = ssh_buffer_new();
|
||||||
|
if (header == NULL) {
|
||||||
return SSH_ERROR;
|
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;
|
int err;
|
||||||
if(pcap ==NULL || pcap->output==NULL)
|
|
||||||
|
if (pcap == NULL || pcap->output == NULL) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
err=fclose(pcap->output);
|
}
|
||||||
pcap->output=NULL;
|
err = fclose(pcap->output);
|
||||||
if(err != 0)
|
pcap->output = NULL;
|
||||||
|
if (err != 0) {
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
else
|
} else {
|
||||||
return SSH_OK;
|
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);
|
ssh_pcap_file_close(pcap);
|
||||||
SAFE_FREE(pcap);
|
SAFE_FREE(pcap);
|
||||||
}
|
}
|
||||||
@@ -276,23 +294,26 @@ void ssh_pcap_file_free(ssh_pcap_file 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));
|
||||||
|
if (ctx == NULL) {
|
||||||
ssh_set_error_oom(session);
|
ssh_set_error_oom(session);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ZERO_STRUCTP(ctx);
|
ZERO_STRUCTP(ctx);
|
||||||
ctx->session=session;
|
ctx->session = session;
|
||||||
return ctx;
|
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
|
||||||
@@ -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,16 +527,18 @@ 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);
|
||||||
|
if (ctx == NULL) {
|
||||||
ssh_set_error_oom(session);
|
ssh_set_error_oom(session);
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
ctx->file=pcap;
|
ctx->file = pcap;
|
||||||
if(session->pcap_ctx)
|
if (session->pcap_ctx) {
|
||||||
ssh_pcap_context_free(session->pcap_ctx);
|
ssh_pcap_context_free(session->pcap_ctx);
|
||||||
session->pcap_ctx=ctx;
|
}
|
||||||
|
session->pcap_ctx = ctx;
|
||||||
return SSH_OK;
|
return SSH_OK;
|
||||||
}
|
}
|
||||||
/** @} */
|
/** @} */
|
||||||
@@ -527,27 +550,35 @@ 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;
|
{
|
||||||
|
(void)pcap;
|
||||||
|
|
||||||
return SSH_ERROR;
|
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;
|
||||||
|
(void)filename;
|
||||||
|
|
||||||
return SSH_ERROR;
|
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;
|
||||||
|
|
||||||
|
ssh_set_error(session, SSH_REQUEST_DENIED, "Pcap support not compiled in");
|
||||||
return SSH_ERROR;
|
return SSH_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user