0

From a linux OS I am trying to write my own data usage monitor in C or python. I've searched and researched for a couple of days now. Currently I am trying to adapt sniffex.c to suit my needs. I've succeeded in verifying the total bytes sent and received during a few ftp sessions.

In sniffex.c the tcp packet size is calculated. My question is how do you calculate the UDP packet size? I've searched on this topic, but have not found anything. Does this question make sense?

Update:

The function where the packet sizes are computed looks like this:

got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
...
int size_payload;
...
case IPPROTO_UDP:
    printf("   Protocol: UDP\n");
    size_payload = header->len;
...
}

Do I still need to add 4 to size_payload?

The callback to this function looks like this:

/* now we can set our callback function */
pcap_loop(handle, num_packets, got_packet, NULL);
nomadicME
  • 1,389
  • 5
  • 15
  • 35
  • It seems like this line 'size_payload = header->len;' is exactly what you need. You have header structure, so you can get packet length directly from this structure. – fycth Oct 22 '12 at 10:21

1 Answers1

0

If you have an UDP datagram, you can get its size from its header.

For example if you have a pointer char *hdr to UDP datagram header, you can get its length by such a construction

int length = *(int *)(hdr + 4);

More about UDP http://en.wikipedia.org/wiki/User_Datagram_Protocol

fycth
  • 3,410
  • 25
  • 37
  • Thank you for the response. I implemented it like you showed, but I am getting: warning: assignment makes integer from pointer without a cast. I've been spending too much time in python, what is the best way to resolve this? I'll elaborate in my original question. Thanks. – nomadicME Oct 22 '12 at 08:35
  • I updated the answer - there was a typo in the code line, sorry – fycth Oct 22 '12 at 10:30