Skip to content

Commit

Permalink
pcap: Change method names and fix some issues
Browse files Browse the repository at this point in the history
Signed-off-by: Siddharth Chandrasekaran <[email protected]>
  • Loading branch information
sidcha committed Feb 17, 2024
1 parent 3bd228b commit a0a6635
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
6 changes: 3 additions & 3 deletions include/utils/pcap_gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ typedef struct {
void *cache;
} pcap_t;

pcap_t *pcap_create(char *path, uint32_t max_packet_size, uint32_t link_type);
int pcap_add_record(pcap_t *cap, uint8_t *capture_data, uint32_t length);
void pcap_dump(pcap_t *cap);
pcap_t *pcap_start(char *path, uint32_t max_packet_size, uint32_t link_type);
int pcap_add(pcap_t *cap, uint8_t *capture_data, uint32_t length);
int pcap_stop(pcap_t *cap);

#ifdef __cplusplus
}
Expand Down
29 changes: 15 additions & 14 deletions src/pcap_gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct pcap_record_header {
uint32_t orig_len;
} __packed;

pcap_t *pcap_create(char *path, uint32_t max_packet_size, uint32_t link_type)
pcap_t *pcap_start(char *path, uint32_t max_packet_size, uint32_t link_type)
{
pcap_t *cap;
struct pcap_header header;
Expand Down Expand Up @@ -73,22 +73,19 @@ pcap_t *pcap_create(char *path, uint32_t max_packet_size, uint32_t link_type)

static int pcap_flush(pcap_t *cap)
{
int ret;

ret = fwrite(cap->cache, cap->offset, 1, cap->file);
if (!ret)
return -1;
fflush(cap->file);
cap->offset = 0;
return 0;
if (fwrite(cap->cache, cap->offset, 1, cap->file)) {
cap->offset = 0;
return fflush(cap->file);
}
return -1;
}

int pcap_add_record(pcap_t *cap, uint8_t *capture_data, uint32_t length)
int pcap_add(pcap_t *cap, uint8_t *capture_data, uint32_t length)
{
struct pcap_record_header header;
uint32_t sec, usec;

if (sizeof(header) + length > PCAP_CACHE_SIZE) {
if (cap->offset + sizeof(header) + length > PCAP_CACHE_SIZE) {
if (pcap_flush(cap))
return -1;
}
Expand All @@ -107,10 +104,14 @@ int pcap_add_record(pcap_t *cap, uint8_t *capture_data, uint32_t length)
return 0;
}

void pcap_dump(pcap_t *cap)
int pcap_stop(pcap_t *cap)
{
pcap_flush(cap);
fclose(cap->file);
int ret = 0;

ret = pcap_flush(cap);
if (ret == 0)
ret = fclose(cap->file);
free(cap->cache);
free(cap);
return ret;
}

0 comments on commit a0a6635

Please sign in to comment.