Note that there are some explanatory texts on larger screens.

plurals
  1. PONetwork receipt timer to ms resolution
    primarykey
    data
    text
    <p>My scenario, I'm collecting network packets and if packets match a network filter I want to record the time difference between consecutive packets, this last part is the part that doesn't work. My problem is that I cant get accurate sub-second measurements no matter what C timer function I use. I've tried: gettimeofday(), clock_gettime(), and clock().</p> <p>I'm looking for assistance to figure out why my timing code isn't working properly.</p> <p>I'm running on a cygwin environment. Compile Options: gcc -Wall capture.c -o capture -lwpcap -lrt</p> <p>Code snippet :</p> <pre><code>/*globals*/ int first_time = 0; struct timespec start, end; double sec_diff = 0; main() { pcap_t *adhandle; const struct pcap_pkthdr header; const u_char *packet; int sockfd = socket(PF_INET, SOCK_STREAM, 0); .... (previous I create socket/connect - works fine) save_attr = tty_set_raw(); while (1) { packet = pcap_next(adhandle, &amp;header); // Receive a packet? Process it if (packet != NULL) { got_packet(&amp;header, packet, adhandle); } if (linux_kbhit()) { // User types message to channel kb_char = linux_getch(); // Get user-supplied character if (kb_char == 0x03) // Stop loop (exit channel) if user hits Ctrl+C break; } } tty_restore(save_attr); close(sockfd); pcap_close(adhandle); printf("\nCapture complete.\n"); } </code></pre> <p>In got_packet:</p> <pre><code>got_packet(const struct pcap_pkthdr *header, const u_char *packet, pcap_t * p){ ... { ....do some packet filtering to only handle my packets, set match = 1 if (match == 1) { if (first_time == 0) { clock_gettime( CLOCK_MONOTONIC, &amp;start ); first_time++; } else { clock_gettime( CLOCK_MONOTONIC, &amp;end ); sec_diff = (end.tv_sec - start.tv_sec) + ((end.tv_nsec - start.tv_nsec)/1000000000.0); // Packet difference in seconds printf("sec_diff: %ld,\tstart_nsec: %ld,\tend_nsec: %ld\n", (end.tv_sec - start.tv_sec), start.tv_nsec, end.tv_nsec); printf("sec_diffcalc: %ld,\tstart_sec: %ld,\tend_sec: %ld\n", sec_diff, start.tv_sec, end.tv_sec); start = end; // Set the current to the start for next match } } } </code></pre> <p>I record all packets with Wireshark to compare, so I expect the difference in my timer to be the same as Wireshark's, however that is never the case. My output for tv_sec will be correct, however tv_nsec is not even close. Say there is a 0.5 second difference in wireshark, my timer will say there is a 1.999989728 second difference.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload