Skip to content

Commit

Permalink
Add tests for nonincreasing or decreasing timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
dmiller-nmap committed Apr 26, 2023
1 parent 2e7fc75 commit a43dd9b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
68 changes: 56 additions & 12 deletions Examples-pcap/readfile/readfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <pcap.h>

#define LINE_LEN 16
#define TIMEVAL_AFTER(a, b) (((a).tv_sec > (b).tv_sec) || ((a).tv_sec == (b).tv_sec && (a).tv_usec > (b).tv_usec))
#define TIMEVAL_BEFORE(a, b) (((a).tv_sec < (b).tv_sec) || ((a).tv_sec == (b).tv_sec && (a).tv_usec < (b).tv_usec))

#ifdef _WIN32
#include <tchar.h>
Expand All @@ -25,10 +27,22 @@ BOOL LoadNpcapDlls()

void dispatcher_handler(u_char *, const struct pcap_pkthdr *, const u_char *);

struct state {
int verify;
const struct pcap_pkthdr *first;
const struct pcap_pkthdr *prev;
pcap_t *p;
};

int main(int argc, char **argv)
{
pcap_t *fp;
char *filename = NULL;
char errbuf[PCAP_ERRBUF_SIZE];
struct state st;
st.verify = 0;
st.first = NULL;
st.prev = NULL;

#ifdef _WIN32
/* Load Npcap and its functions. */
Expand All @@ -39,25 +53,47 @@ int main(int argc, char **argv)
}
#endif

if(argc != 2)
if (argc == 3 && strcmp(argv[1], "-v") == 0) {
filename = argv[2];
st.verify = 1;
}
else if(argc != 2)
{
printf("usage: %s filename", argv[0]);
printf("usage: %s [-v] filename", argv[0]);
return -1;

}
else
filename = argv[1];

/* Open the capture file */
if ((fp = pcap_open_offline(argv[1], // name of the device
errbuf // error buffer
)) == NULL)
if ((fp = pcap_open_offline(filename, // name of the device
errbuf // error buffer
)) == NULL)
{
fprintf(stderr,"\nUnable to open the file %s.\n", argv[1]);
fprintf(stderr,"\nUnable to open the file %s: %s\n", filename, errbuf);
return -1;
}

/* read and dispatch packets until EOF is reached */
pcap_loop(fp, 0, dispatcher_handler, NULL);
st.p = fp;
if (0 != pcap_loop(fp, 0, dispatcher_handler, (u_char *) &st)) {
fprintf(stderr, "pcap_loop error: %s\n", pcap_geterr(fp));
pcap_close(fp);
return -1;
}

if (st.prev == NULL || st.first == NULL) {
fprintf(stderr, "No packets processed!\n");
pcap_close(fp);
return -1;
}

if (!TIMEVAL_AFTER(st.prev->ts, st.first->ts)) {
fprintf(stderr, "Timestamps do not increase: %lu.%06lu\n", st.prev->ts.tv_sec, st.prev->ts.tv_usec);
pcap_close(fp);
return -1;
}
pcap_close(fp);
return 0;
}
Expand All @@ -70,10 +106,18 @@ void dispatcher_handler(u_char *temp1,
{
u_int i=0;

/*
* unused variable
*/
(VOID*)temp1;
struct state *st = (struct state *) temp1;
if (st->first == NULL) {
st->first = header;
}

if (st->verify && st->prev != NULL) {
/* Default timestamp mode is monotonically increasing */
if (TIMEVAL_BEFORE(header->ts, st->prev->ts)) {
fprintf(stderr, "Backwards timestamp!\n");
pcap_breakloop(st->p);
}
}

/* print pkt timestamp and pkt len */
printf("%ld:%ld (%ld)\n", header->ts.tv_sec, header->ts.tv_usec, header->len);
Expand All @@ -86,5 +130,5 @@ void dispatcher_handler(u_char *temp1,
}

printf("\n\n");

st->prev = header;
}
4 changes: 2 additions & 2 deletions test/make_test.bat
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ taskkill /PID %SAVEPID% || goto :error
SET SAVEPID=0

echo Reading dump file...
.\%1\readfile.exe loopback-%1.pcap || goto :error
.\%1\readfile.exe -v loopback-%1.pcap || goto :error

echo Replaying dump file...
.\%1\sendcap.exe loopback-%1.pcap %devname% s || goto :error
Expand Down Expand Up @@ -94,7 +94,7 @@ taskkill /PID %SAVEPID% || goto :error
SET SAVEPID=0

echo Reading dump file...
.\%1\readfile.exe scanme-%1.pcap || goto :error
.\%1\readfile.exe -v scanme-%1.pcap || goto :error

echo Replaying dump file...
.\%1\sendcap.exe scanme-%1.pcap %devname% || goto :error
Expand Down

0 comments on commit a43dd9b

Please sign in to comment.