mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-11 02:38:09 +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:
89
src/pcap.c
89
src/pcap.c
@@ -122,10 +122,11 @@ struct ssh_pcap_file_struct {
|
||||
/**
|
||||
* @brief create a new ssh_pcap_file object
|
||||
*/
|
||||
ssh_pcap_file ssh_pcap_file_new(void) {
|
||||
struct ssh_pcap_file_struct *pcap;
|
||||
ssh_pcap_file ssh_pcap_file_new(void)
|
||||
{
|
||||
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) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -137,29 +138,36 @@ ssh_pcap_file ssh_pcap_file_new(void) {
|
||||
/** @internal
|
||||
* @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;
|
||||
if(pcap == NULL || pcap->output==NULL)
|
||||
if (pcap == NULL || pcap->output == NULL) {
|
||||
return SSH_ERROR;
|
||||
}
|
||||
len = ssh_buffer_get_len(packet);
|
||||
err = fwrite(ssh_buffer_get(packet), len, 1, pcap->output);
|
||||
if(err<0)
|
||||
if (err < 0) {
|
||||
return SSH_ERROR;
|
||||
else
|
||||
} else {
|
||||
return SSH_OK;
|
||||
}
|
||||
}
|
||||
|
||||
/** @internal
|
||||
* @brief prepends a packet with the pcap header and writes packet
|
||||
* 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;
|
||||
int err;
|
||||
if(header == NULL)
|
||||
|
||||
if (header == NULL) {
|
||||
return SSH_ERROR;
|
||||
}
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
err = ssh_buffer_allocate_size(header,
|
||||
sizeof(uint32_t) * 4 +
|
||||
@@ -196,21 +204,26 @@ error:
|
||||
/**
|
||||
* @brief opens a new pcap file and creates header
|
||||
*/
|
||||
int ssh_pcap_file_open(ssh_pcap_file pcap, const char *filename){
|
||||
ssh_buffer header;
|
||||
int ssh_pcap_file_open(ssh_pcap_file pcap, const char *filename)
|
||||
{
|
||||
ssh_buffer header = NULL;
|
||||
int err;
|
||||
if(pcap == NULL)
|
||||
|
||||
if (pcap == NULL) {
|
||||
return SSH_ERROR;
|
||||
}
|
||||
if (pcap->output) {
|
||||
fclose(pcap->output);
|
||||
pcap->output = NULL;
|
||||
}
|
||||
pcap->output = fopen(filename, "wb");
|
||||
if(pcap->output==NULL)
|
||||
if (pcap->output == NULL) {
|
||||
return SSH_ERROR;
|
||||
}
|
||||
header = ssh_buffer_new();
|
||||
if(header==NULL)
|
||||
if (header == NULL) {
|
||||
return SSH_ERROR;
|
||||
}
|
||||
err = ssh_buffer_allocate_size(header,
|
||||
sizeof(uint32_t) * 5 +
|
||||
sizeof(uint16_t) * 2);
|
||||
@@ -255,19 +268,24 @@ error:
|
||||
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)
|
||||
|
||||
if (pcap == NULL || pcap->output == NULL) {
|
||||
return SSH_ERROR;
|
||||
}
|
||||
err = fclose(pcap->output);
|
||||
pcap->output = NULL;
|
||||
if(err != 0)
|
||||
if (err != 0) {
|
||||
return SSH_ERROR;
|
||||
else
|
||||
} 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);
|
||||
}
|
||||
@@ -276,7 +294,8 @@ void ssh_pcap_file_free(ssh_pcap_file pcap){
|
||||
/** @internal
|
||||
* @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_set_error_oom(session);
|
||||
@@ -287,11 +306,13 @@ ssh_pcap_context ssh_pcap_context_new(ssh_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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -506,15 +527,17 @@ error:
|
||||
* sessions.
|
||||
* @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_set_error_oom(session);
|
||||
return SSH_ERROR;
|
||||
}
|
||||
ctx->file = pcap;
|
||||
if(session->pcap_ctx)
|
||||
if (session->pcap_ctx) {
|
||||
ssh_pcap_context_free(session->pcap_ctx);
|
||||
}
|
||||
session->pcap_ctx = ctx;
|
||||
return SSH_OK;
|
||||
}
|
||||
@@ -527,26 +550,34 @@ int ssh_set_pcap_file(ssh_session session, ssh_pcap_file pcap){
|
||||
#include "libssh/libssh.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 ssh_pcap_file_free(ssh_pcap_file pcap){
|
||||
void ssh_pcap_file_free(ssh_pcap_file pcap)
|
||||
{
|
||||
(void)pcap;
|
||||
}
|
||||
|
||||
ssh_pcap_file ssh_pcap_file_new(void){
|
||||
ssh_pcap_file ssh_pcap_file_new(void)
|
||||
{
|
||||
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;
|
||||
|
||||
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");
|
||||
return SSH_ERROR;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user