title: unknown type name ‘sockaddr’
body: <p>When I run </p>
  
  <p><code>cc    -c -o rawudp.o rawudp.c</code></p>
  
  <p>I get </p>
  
  <p>In file included from rawudp.c:1:0: <br>
  rawudp.h:48:23: error: unknown type name ‘sockaddr' <br>
  make: <em>*</em> [rawudp.o] Error 1</p>
  
  <p>Here is the Header file that I use.</p>
  
  <h3>rawudp.h</h3>
  
  <pre><code>#ifndef RAWUDP_H_
  #define RAWUDP_H_ value
  
  #include &lt;stdlib.h&gt;
  #include &lt;strings.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/uio.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netinet/ip.h&gt;
  #include &lt;netinet/udp.h&gt;
  
  // The packet length
  #define PCKT_LEN 8192
  
  //IP Header Constants
  #define IP_HEADER_IHL       5;
  #define IP_HEADER_VERSION   4;
  #define IP_HEADER_TOS       16;
  #define IP_HEADER_TTL       64;
  #define IP_HEADER_PROTOCOL  17;
  
  typedef struct ipheader {
   unsigned char      iph_ihl:5, iph_ver:4;
   unsigned char      iph_tos;
   unsigned short int iph_len;
   unsigned short int iph_ident;
   unsigned char      iph_flag;
   unsigned short int iph_offset;
   unsigned char      iph_ttl;
   unsigned char      iph_protocol;
   unsigned short int iph_chksum;
   unsigned int       iph_sourceip;
   unsigned int       iph_destip;
  } ipheader;
  
  // UDP header's structure
  typedef struct udpheader {
   unsigned short int udph_srcport;
   unsigned short int udph_destport;
   unsigned short int udph_len;
   unsigned short int udph_chksum;
  } udpheader;
  #endif
  </code></pre>
  
  <h3>rawudp.c</h3>
  
  <pre><code>#include "rawudp.h"
  
  // Function for checksum calculation. From the RFC,
  // the checksum algorithm is:
  //  "The checksum field is the 16 bit one's complement of the one's
  //  complement sum of all 16 bit words in the header.  For purposes of
  //  computing the checksum, the value of the checksum field is zero."
  unsigned short csum(unsigned short *buf, int nwords)
  {       //
          unsigned long sum;
          for(sum=0; nwords&gt;0; nwords--)
                  sum += *buf++;
          sum = (sum &gt;&gt; 16) + (sum &amp;0xffff);
          sum += (sum &gt;&gt; 16);
          return (unsigned short)(~sum);
  }
  
  // Create a raw socket with UDP protocol
  int udp_socket_init(){
          int socket_descriptor;
          socket_descriptor = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
          if(socket_descriptor &lt; 0)
          {
                  perror("socket() error");
                  // If something wrong just exit
                  exit(-1);
          }
          else
                  return socket_descriptor;
                  //printf("socket() - Using SOCK_RAW socket and UDP protocol is OK.\n");
  }
  
  int ip_socket_init(sockaddr_in* udp_socket, char* port, char* ip_address){
          udp_socket-&gt;sin_family = AF_INET;
          //Convert and set port number
          if (!(udp_socket-&gt;sin_port = htons(atoi(port)))){
                  printf("Could not set port number\n");
                  exit(-1);
          }
          //Convert and set IP Address
          if (!(udp_socket-&gt;sin_addr.s_addr = inet_addr(ip_address)))
          {
                  printf("Could not set IP address\n");
                  exit(-1);
          }
          return 1;
  }
  
  int ip_header_init(ipheader* ip, char* source_ip, char* dest_ip, size_t size){
          // Fabricate the IP header or we can use the
          // standard header structures but assign our own values.
          ip-&gt;iph_ihl = IP_HEADER_IHL;
          ip-&gt;iph_ver = IP_HEADER_VERSION;
          ip-&gt;iph_tos = IP_HEADER_TOS; // Low delay
          ip-&gt;iph_len = size;
          ip-&gt;iph_ident = htons(54321);
          ip-&gt;iph_ttl = IP_HEADER_TTL; // hops
          ip-&gt;iph_protocol = IP_HEADER_PROTOCOL; // UDP
          // Source IP address, can use spoofed address here!!!
          ip-&gt;iph_sourceip = inet_addr(source_ip);
          // The destination IP address
          ip-&gt;iph_destip = inet_addr(dest_ip);
  }
  
  int udp_header_init(udpheader* udp, char* source_port, char* dest_port){
          // Fabricate the UDP header. Source port number, redundant
          udp-&gt;udph_srcport = htons(atoi(source_port));
          // Destination port number
          udp-&gt;udph_destport = htons(atoi(dest_port));
          udp-&gt;udph_len = htons(sizeof(struct udpheader));
  }
  
  // Source IP, source port, target IP, target port from the command line arguments
  int main(int argc, char *argv[])
  {
          int socket_descriptor;
          // No data/payload just datagram
          char buffer[PCKT_LEN];
  
          // Our own headers' structures
          struct ipheader *ip = (struct ipheader *) buffer;
          struct udpheader *udp = (struct udpheader *) (buffer + sizeof(struct ipheader));
  
          // Source and destination addresses: IP and port
          struct sockaddr_in sin;
          struct sockaddr_in din;
  
          socket_descriptor = udp_socket_init();
  
  
          int one = 1;
          const int *val = &amp;one;
  
          bzero(buffer, PCKT_LEN);
  
          if(argc != 5)
          {
                  printf("- Invalid parameters!!!\n");
                  printf("- Usage %s &lt;source hostname/IP&gt; &lt;source port&gt; &lt;target hostname/IP&gt; &lt;target port&gt;\n", argv[0]);
                  exit(-1);
          }
  
          ip_socket_init(&amp;sin, argv[2], argv[1]);
          ip_socket_init(&amp;din, argv[4], argv[3]);
  
          ip_header_init(ip, argv[1],  argv[3], sizeof(struct ipheader) + sizeof(struct udpheader));
  
          udp_header_init(udp, argv[2], argv[4]);
  
          // Calculate the checksum for integrity
          ip-&gt;iph_chksum = csum((unsigned short *)buffer, sizeof(struct ipheader) + sizeof(struct udpheader));
          // Inform the kernel do not fill up the packet structure. we will build our own...
          if(setsockopt(socket_descriptor, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) &lt; 0)
          {
                  perror("setsockopt() error");
                  exit(-1);
          }
          else
                  printf("setsockopt() is OK.\n");
  
          // Send loop, send for every 2 second for 100 count
          printf("Trying...\n");
          printf("Using raw socket and UDP protocol\n");
          printf("Using Source IP: %s port: %u, Target IP: %s port: %u.\n", argv[1], atoi(argv[2]), argv[3], atoi(argv[4]));
  
          int count;
          for(count = 1; count &lt;=20; count++)
          {
                  if(sendto(socket_descriptor, buffer, ip-&gt;iph_len, 0, (struct sockaddr *)&amp;sin, sizeof(sin)) &lt; 0)
                  // Verify
                  {
                          perror("sendto() error");
                          exit(-1);
                  }
                  else
                  {
                          printf("Count #%u - sendto() is OK.\n", count);
                          sleep(2);
                  }
          }
          close(socket_descriptor);
          return 0;
  }
  </code></pre>
  
  <p>I believe I have included the right headers but am unable to find the reason that sockaddr is not inclded in the header files. I have also tried sockaddr_in and still no dice.</p>