title: Receiving same address for Source and Destination IP address while capturing UDP data in Linux using C program
body: <p>I am trying to display the source and destination IP address while capturing UDP data from port 6343. What I observe is I am displaying a same IP address for both the fields. The following is the code I am running,</p>
  
  <pre><code>#include&lt;stdio.h&gt; //For standard things
  #include&lt;stdlib.h&gt;    //malloc
  #include&lt;string.h&gt;    //memset
  #include&lt;netinet/ip_icmp.h&gt;   //Provides declarations for icmp header
  #include&lt;netinet/udp.h&gt;   //Provides declarations for udp header
  #include&lt;netinet/tcp.h&gt;   //Provides declarations for tcp header
  #include&lt;netinet/ip.h&gt;    //Provides declarations for ip header
  #include&lt;sys/socket.h&gt;
  #include&lt;arpa/inet.h&gt;
  #define PORT 6343
  int sockt;
  int i,j;
  
  struct sockaddr_in source,dest; 
  
  int main()
  {
      int saddr_size,data_size;
      struct sockaddr_in saddr;
      struct sockaddr_in daddr;
  
      //struct in_addr in;
      unsigned char *buffer = (unsigned char *)malloc(65536); // Its Big ! Malloc allocates a block of size bytes of memory,returning a pointer to the begining of the block
  
      struct udphdr *udph = (struct udphdr*)(buffer + sizeof(struct iphdr));
      struct iphdr *iph = (struct iphdr *)buffer;
      memset(&amp;source,0,sizeof(source));
      source.sin_addr.s_addr = iph -&gt;saddr;
      memset(&amp;dest,0,sizeof(dest));
      dest.sin_addr.s_addr = iph-&gt;daddr;
      unsigned short iphdrlen = iph-&gt;ihl*4;
  
  
      printf("Starting...\n");
      //Create a socket
      sockt = socket(AF_INET ,SOCK_DGRAM ,0);
      if(sockt &lt; 0)
          {
              printf("Socket Error\n");
              return 1;
          }
      memset((char *)&amp;saddr,0,sizeof(saddr));
  
      //prepare the sockaddr_in structure
      daddr.sin_family = AF_INET;
      daddr.sin_addr.s_addr = htonl(INADDR_ANY);
      daddr.sin_port = htons(PORT);
  
      //Bind
      if(bind(sockt,(struct sockaddr *)&amp;daddr, sizeof(daddr))&lt;0)
          {
              printf("bind failed");
              return 1;
          }
      printf("bind done");
  
      while(1)
          {
              saddr_size = sizeof saddr;
              printf("waiting for data...");
  
              //Receive a packet
              data_size = recvfrom(sockt , buffer ,65536 , 0 , (struct sockaddr*)  &amp;saddr , (socklen_t*)&amp;saddr_size);
              if(data_size &lt;0)
                  {
                      printf("Packets not recieved \n");
                      return 1;
                  }
  
              printf("Packets arrived from %d \n",ntohs(daddr.sin_port));
              printf("\t Source Port : %d , Destination Port : %d, UDP Length : %d, Source IP  : %s, Destination IP  : %s, Protocol : %d, total length : %d \n", ntohs(udph-&gt;source), ntohs(udph-&gt;dest), ntohs(data_size), inet_ntoa(saddr.sin_addr),inet_ntoa(daddr.sin_addr), (unsigned int)iph-&gt;protocol, ntohs(iph-&gt;tot_len)); 
              ProcessPacket(buffer,data_size);
          }
      close(sockt);
      printf("Finished");
      return 0;
  }
  </code></pre>
  
  <p>The output I am receiving is :  </p>
  
  <blockquote>
    <p>Source Port : 55600 , Destination Port : 9162, UDP Length : 7173, Source IP  : 147.188.195.6, Destination IP  : 147.188.195.6, Protocol : 188, total length : 5 </p>
  </blockquote>
  
  <p>Am I going wrong while displaying the IP address or not pointing the right location to fetch the address?? </p>