title: How to ping using C sockets
body: <ul>
  <li>Compiler: Code::Blocks(GNU GCC)</li>
  <li>Platform: Windows(x86) </li>
  <li>Includes: winsock.h winsock2.h (ws2_32 is linked as well)</li>
  </ul>
  
  <p>I am currently trying to write a program that will read a text file containing a list of IP-Addresses and ping each IP-Addresses respectively. If the host responds to the ping then the host's IP-Address will be copied to a seconded file specified by the user. Unfourtantly this is the first time I have used C's socket library and I cannot find a good tutorial about how to ping using C. From what I understand of the couple tutorials I did find. I need to included a ICMP header, which is a struct containg the ICMP type, code, and checksum, in a IP datagram. But I have no idea how to go about doing so, should I declare the struct myself or is it declared in a header file? I am assuming that it is in a header but the tutorials contradicted each other about exactly where it is declared. I tired including icmp.h and netinet/icmp.h but my compiler complained that they don't exist so I created my own struct.</p>
  
  <pre><code>    struct echo_request
      {
          char type; // Type
          char code; // Code
          short checksum; // Checksum
          short id; // Identification
          short seq; // Sequence
          int time; // Time
          char data[16]; // Data
      };
  </code></pre>
  
  <p>I thought that I might be able to get away with it but I wasn't even able to compile my program because my compiler says that in_cksum()(checksum generator) is undefined.</p>
  
  <p>To sum up my questions, what header files should I include, how do I create a ping packet, am I using the correct checksum generator function, should a ping be directed to port 80, and should the socket I use be RAW or DGRAM?</p>
  
  <p>This is what I have so far, please note that I have purposely left out error checking.</p>
  
  <pre><code>    int socket_descriptor = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  
      struct sockaddr_in address; //Initialize address struct
      memset(&amp;address, 0, sizeof(address)); //Clear address struct
  
      //Declare address
      address.sin_family = AF_INET;
      address.sin_addr.s_addr = inet_addr(ipaddress);
      address.sin_port = htons(80);
  
      //Bind socket to address
      bind(socket_descriptor, (struct sockaddr *)&amp;address, sizeof(address));
  
      //Create packet
      struct echo_request packet; //See above for declaration of struct
      memset(packet.data, 1, 16);
      packet.type = 8; //ECHO_REQUEST
      packet.code = 0;
      packet.time = gettime();
      packet.checksum = 0;
      packet.checksum = in_cksum(packet, sizeof(packet));
  </code></pre>