Skip to content

Commit

Permalink
remove pcap_next_with_sf_offset from API
Browse files Browse the repository at this point in the history
fix indentation
  • Loading branch information
alexandretea authored and Alexandre Tea committed Apr 26, 2019
1 parent 4e3f877 commit 2cf27d7
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 84 deletions.
4 changes: 2 additions & 2 deletions pcap-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ struct pcap {
int activated; /* true if the capture is really started */
int oldstyle; /* if we're opening with pcap_open_live() */

long lastpkt_offset; /* last packet offset in save file */
long current_offset; /* current offset in save file */
long lastpkt_offset; /* last packet offset in save file */
long current_offset; /* current offset in save file */

struct pcap_opt opt;

Expand Down
10 changes: 5 additions & 5 deletions pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,19 +510,19 @@ pcap_next_with_file_offset(pcap_t *p, struct pcap_pkthdr *pkt_header,
const u_char *ret = pcap_next(p, pkt_header);

// sf_offset is set to -1 if live capture
*sf_offset = (p->rfile != NULL ? p->lastpkt_offset : -1);
*file_offset = (p->rfile != NULL ? p->lastpkt_offset : -1);
return ret;
}

int
pcap_next_ex_with_file_offset(pcap_t *p, struct pcap_pkthdr **pkt_header,
const u_char **pkt_data, long *file_offset)
{
int ret = pcap_next_ex(p, pkt_header, pkt_data);
int ret = pcap_next_ex(p, pkt_header, pkt_data);

// sf_offset is set to -1 if live capture
*sf_offset = (p->rfile != NULL ? p->lastpkt_offset : -1);
return ret;
// sf_offset is set to -1 if live capture
*file_offset = (p->rfile != NULL ? p->lastpkt_offset : -1);
return ret;
}

static struct capture_source_type {
Expand Down
4 changes: 1 addition & 3 deletions pcap/pcap.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,8 @@ PCAP_API int pcap_loop(pcap_t *, int, pcap_handler, u_char *);
PCAP_API int pcap_dispatch(pcap_t *, int, pcap_handler, u_char *);
PCAP_API const u_char *pcap_next(pcap_t *, struct pcap_pkthdr *);
PCAP_API int pcap_next_ex(pcap_t *, struct pcap_pkthdr **, const u_char **);
PCAP_API const u_char *pcap_next_with_sf_offset(pcap_t *, struct pcap_pkthdr *,
long *);
PCAP_API int pcap_next_ex_with_sf_offset(pcap_t *, struct pcap_pkthdr **,
const u_char **, long *);
const u_char **, long *);
PCAP_API void pcap_breakloop(pcap_t *);
PCAP_API int pcap_stats(pcap_t *, struct pcap_stat *);
PCAP_API int pcap_setfilter(pcap_t *, struct bpf_program *);
Expand Down
29 changes: 10 additions & 19 deletions pcap_next_ex.3pcap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
.\"
.TH PCAP_NEXT_EX 3PCAP "25 July 2018"
.SH NAME
pcap_next_ex, pcap_next, pcap_next_ex_with_sf_offset, pcap_next_with_sf_offset
pcap_next_ex, pcap_next_ex_with_sf_offset, pcap_next
\- read the next packet from a pcap_t
.SH SYNOPSIS
.nf
Expand All @@ -31,15 +31,14 @@ pcap_next_ex, pcap_next, pcap_next_ex_with_sf_offset, pcap_next_with_sf_offset
int pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
.ti +8
const u_char **pkt_data);
const u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h);
.LP
.ft B
int pcap_next_ex_with_sf_offset(pcap_t *p, struct pcap_pkthdr **pkt_header,
.ti +8
const u_char **pkt_data, long *sf_offset);
const u_char *pcap_next_with_sf_offset(pcap_t *p, struct pcap_pkthdr *h,
.ti +8
long *sf_offset);
.BR
.ft B
const u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h);
.LP
.ft
.fi
.SH DESCRIPTION
Expand All @@ -57,9 +56,8 @@ argument is set to point to the data in the packet. The
and the packet data are not to be freed by the caller, and are not
guaranteed to be valid after the next call to
.BR pcap_next_ex() ,
.BR pcap_next() ,
.BR pcap_next_ex_with_sf_offset() ,
.BR pcap_next_with_sf_offset() ,
.BR pcap_next() ,
.BR pcap_loop (3PCAP),
or
.BR pcap_dispatch (3PCAP);
Expand All @@ -76,9 +74,8 @@ pointer to the data in that packet. The
packet data is not to be freed by the caller, and is not
guaranteed to be valid after the next call to
.BR pcap_next_ex() ,
.BR pcap_next() ,
.BR pcap_next_ex_with_sf_offset() ,
.BR pcap_next_with_sf_offset() ,
.BR pcap_next() ,
.BR pcap_loop() ,
or
.BR pcap_dispatch() ;
Expand All @@ -89,14 +86,10 @@ structure pointed to by
.I h
is filled in with the appropriate values for the packet.
.PP
.B pcap_next_with_sf_offset()
and
.B pcap_next_ex_with_sf_offset()
are the same as
.B pcap_next()
and
is the same as
.B pcap_next_ex()
except that they take an additional
except that it takes an additional
.I sf_offset
pointer argument. The pointed variable will be set to the extracted
packet's ``savefile`` offset. If the packets are being read from a live capture, the
Expand Down Expand Up @@ -158,9 +151,7 @@ may be called with
as an argument to fetch or display the error text.
.PP
.B pcap_next()
and
.B pcap_next_with_sf_offset()
both return a pointer to the packet data on success, and returns
returns a pointer to the packet data on success, and returns
.B NULL
if an error occurred, or if no packets were read from a live capture
(if, for example, they were discarded because they didn't pass the
Expand Down
28 changes: 14 additions & 14 deletions sf-pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,10 @@ pcap_check_header(bpf_u_int32 magic, FILE *fp, u_int precision, char *errbuf,
}

p->cleanup_op = sf_cleanup;
/*
* If savefile, initialize current_offset to pcap file's body
*/
p->current_offset = sizeof(struct pcap_file_header);
/*
* If savefile, initialize current_offset to pcap file's body
*/
p->current_offset = sizeof(struct pcap_file_header);
return (p);
}

Expand Down Expand Up @@ -718,16 +718,16 @@ pcap_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
if (p->swapped)
swap_pseudo_headers(p->linktype, hdr, *data);

if (p->rfile != NULL) {
/*
* If savefile:
* - we set lastpkt_offset to the offset of the packet we just read
* - we set the current_offset to the end of this packet
*/
p->current_offset += ps->hdrsize;
p->lastpkt_offset = p->current_offset;
p->current_offset += hdr->caplen;
}
if (p->rfile != NULL) {
/*
* If savefile:
* - we set lastpkt_offset to the offset of the packet we just read
* - we set the current_offset to the end of this packet
*/
p->current_offset += ps->hdrsize;
p->lastpkt_offset = p->current_offset;
p->current_offset += hdr->caplen;
}
return (0);
}

Expand Down
82 changes: 41 additions & 41 deletions sf-pcapng.c
Original file line number Diff line number Diff line change
Expand Up @@ -948,10 +948,10 @@ pcap_ng_check_header(bpf_u_int32 magic, FILE *fp, u_int precision, char *errbuf,
bhdrp->total_length = total_length;
shbp->byte_order_magic = byte_order_magic;

/*
* We initialize current_offset to the end of the SHB
*/
p->current_offset = bhdrp->total_length;
/*
* We initialize current_offset to the end of the SHB
*/
p->current_offset = bhdrp->total_length;

if (read_bytes(fp,
(u_char *)p->buffer + (sizeof(magic) + sizeof(total_length) + sizeof(byte_order_magic)),
Expand Down Expand Up @@ -1003,11 +1003,11 @@ pcap_ng_check_header(bpf_u_int32 magic, FILE *fp, u_int precision, char *errbuf,
if (status == -1)
goto fail; /* error */

/*
* Add to current_offset the length of the section we just read
*/
p->current_offset += cursor.data_remaining + sizeof(struct block_header)
+ sizeof(struct block_trailer);
/*
* Add to current_offset the length of the section we just read
*/
p->current_offset += cursor.data_remaining + sizeof(struct block_header)
+ sizeof(struct block_trailer);
switch (cursor.block_type) {

case BT_IDB:
Expand Down Expand Up @@ -1135,23 +1135,23 @@ pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
return (1); /* EOF */
if (status == -1)
return (-1); /* error */
if (p->rfile != NULL) {
/*
* We set lastpkt_offset to current offset and we add the length of
* the section we just read to the current_offset
*
* When we will read a packet block (from any kind), we will add to
* lastpkt_offset the length of a block header and the length of our
* packet block's header
* This way, the lastpkt_offset variable will be set to the
* packet savefile's offset
* offset in his original savefile.
*/
p->lastpkt_offset = p->current_offset;
p->current_offset += cursor.data_remaining
+ sizeof(struct block_header)
+ sizeof(struct block_trailer);
}
if (p->rfile != NULL) {
/*
* We set lastpkt_offset to current offset and we add the length of
* the section we just read to the current_offset
*
* When we will read a packet block (from any kind), we will add to
* lastpkt_offset the length of a block header and the length of our
* packet block's header
* This way, the lastpkt_offset variable will be set to the
* packet savefile's offset
* offset in his original savefile.
*/
p->lastpkt_offset = p->current_offset;
p->current_offset += cursor.data_remaining
+ sizeof(struct block_header)
+ sizeof(struct block_trailer);
}
switch (cursor.block_type) {

case BT_EPB:
Expand All @@ -1160,7 +1160,7 @@ pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
* EPB.
*/
epbp = get_from_block_data(&cursor, sizeof(*epbp),
p->errbuf);
p->errbuf);
if (epbp == NULL)
return (-1); /* error */

Expand All @@ -1181,10 +1181,10 @@ pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
t = ((uint64_t)epbp->timestamp_high) << 32 |
epbp->timestamp_low;
}
if (p->rfile != NULL) {
p->lastpkt_offset += sizeof(struct block_header)
+ sizeof(struct enhanced_packet_block);
}
if (p->rfile != NULL) {
p->lastpkt_offset += sizeof(struct block_header)
+ sizeof(struct enhanced_packet_block);
}
goto found;

case BT_SPB:
Expand All @@ -1193,7 +1193,7 @@ pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
* SPB.
*/
spbp = get_from_block_data(&cursor, sizeof(*spbp),
p->errbuf);
p->errbuf);
if (spbp == NULL)
return (-1); /* error */

Expand Down Expand Up @@ -1222,10 +1222,10 @@ pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
hdr->caplen = p->snapshot;
t = 0; /* no time stamps */

if (p->rfile != NULL) {
p->lastpkt_offset += sizeof(struct block_header)
+ sizeof(struct simple_packet_block);
}
if (p->rfile != NULL) {
p->lastpkt_offset += sizeof(struct block_header)
+ sizeof(struct simple_packet_block);
}
goto found;

case BT_PB:
Expand All @@ -1234,7 +1234,7 @@ pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
* PB.
*/
pbp = get_from_block_data(&cursor, sizeof(*pbp),
p->errbuf);
p->errbuf);
if (pbp == NULL)
return (-1); /* error */

Expand All @@ -1255,10 +1255,10 @@ pcap_ng_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char **data)
t = ((uint64_t)pbp->timestamp_high) << 32 |
pbp->timestamp_low;
}
if (p->rfile != NULL) {
p->lastpkt_offset += sizeof(struct block_header)
+ sizeof(struct packet_block);
}
if (p->rfile != NULL) {
p->lastpkt_offset += sizeof(struct block_header)
+ sizeof(struct packet_block);
}
goto found;

case BT_IDB:
Expand Down

0 comments on commit 2cf27d7

Please sign in to comment.