c - UDP header length field is zero always while capturing sflow data from port 6343 -


i trying capture udp sflow data port 6343. trying capture udp header information provides source port, destination port, udp header length , checksum. able see ports capturing, udp , checksum fields 0 , 1 respectively means udp length not calculated , checksum not calculated. missing here udp header length , checksum calculation ?? following code use:

#include<stdio.h> //for standard things #include<stdlib.h>    //malloc #include<string.h>    //memset #include<netinet/ip_icmp.h>   //provides declarations icmp header #include<netinet/udp.h>   //provides declarations udp header #include<netinet/tcp.h>   //provides declarations tcp header #include<netinet/ip.h>    //provides declarations ip header #include<sys/socket.h> #include<arpa/inet.h> #define port 6343 #define pckt_len 65536  void handlepacket(unsigned char *, int); int sockt; int i,j; struct sockaddr_in source,dest;   int main() {     int saddr_size,data_size;     struct sockaddr_in daddr;     struct sockaddr_in saddr;     //struct in_addr in;     unsigned char *buffer = (unsigned char *)malloc(65536); // big !     malloc allocates block of size bytes of memory,returning pointer begining of block      struct udphdr *udph = (struct udphdr*)(buffer + sizeof(struct iphdr));       printf("starting...\n");     //create raw socket shall sniff     sockt = socket(af_inet ,sock_dgram ,0);     if(sockt < 0)     {         printf("socket error\n");         return 1;     }     memset((char *)&daddr,0,sizeof(daddr));      //prepare sockaddr_in structure     daddr.sin_family = af_inet;     daddr.sin_addr.s_addr = inaddr_any;     daddr.sin_port = htons(port);      //bind     if(bind(sockt,(struct sockaddr *)&daddr, sizeof(daddr))<0)     {       printf("bind failed");       return 1;     }     printf("bind done");      while(1)     {         saddr_size = sizeof saddr;         printf("waiting data...");          //receive packet         data_size = recvfrom(sockt , buffer ,65536 , 0 , (struct sockaddr*)  &saddr , (socklen_t*)&saddr_size);         if(data_size <0)         {             printf("packets not recieved \n");             return 1;         }         //now process packet         handlepacket(buffer , data_size);           printf("packets arrived %d \n",ntohs(daddr.sin_port));         printf("source port : %d , destination port : %d \n", ntohs(udph->source), ntohs(udph->dest));       }     close(sockt);     printf("finished");     return 0; }  void handlepacket(unsigned char *buffer, int data_size) {      //ip header length     struct iphdr *iph = (struct iphdr *)buffer;     unsigned short iphdrlen = iph->ihl*4;     // udp header length     struct udphdr *udph = (struct udphdr*)(buffer + iphdrlen);      memset(&source,0,sizeof(source));     source.sin_addr.s_addr = iph ->saddr;     memset(&dest,0,sizeof(dest));     dest.sin_addr.s_addr = iph->daddr;        printf("udp length : %d , udp checksum : %d \n",ntohs(udph->len), ntohs(udph->check));   } 

when use create socket of type af_inet / sock_dgram, operating system processes , ip , udp headers , strips them off before passing them you. see in buffer follows udp header.

you're passed source ip , port via fifth parameter recvfrom function, , payload length passed return value. if there problem udp checksum, os discard packet , application code never see it, it's not typically need worry on application level.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

wso2esb - How to concatenate JSON array values in WSO2 ESB? -