Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ping using C sockets

  • Compiler: Code::Blocks(GNU GCC)
  • Platform: Windows(x86)
  • Includes: winsock.h winsock2.h (ws2_32 is linked as well)

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.

    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
    };

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.

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?

This is what I have so far, please note that I have purposely left out error checking.

    int socket_descriptor = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);

    struct sockaddr_in address; //Initialize address struct
    memset(&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 *)&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));
like image 284
John Vulconshinz Avatar asked Jan 13 '13 19:01

John Vulconshinz


People also ask

How do I ping a socket server?

In order to ping a specific port number, execute the “nc” command with the “v” option for “verbose”, “z” for “scanning” and specify the host as well as the port to be pinged. You can also specify a domain name instead of an IP address followed by the port that you want to ping.

Does ping use a socket?

Sockets are available in different flavours (each with its own purpose) and the type of socket that is relevant for understanding how ping works is the raw socket. A raw socket can be viewed as kind of an empty open-ended software object that is not even identified by an IP address or port number.

What is ping C command?

-c. Specifies the number of packets. This option is useful when you get an IP trace log. You can capture a minimum of ping packets.


1 Answers

If you don't have to implement the ping from scratch and you want only Windows solution, I'd second Anton's suggestion for IcmpSendEcho. If you have to implement ping, look at how POCO ICMP package is implemented. It is portable code and it runs fine on Windows.

In regards to the specific questions, here are the answers:

what header files should I include

#include <winsock2.h>

how do I create a ping packet

See ICMPv4PacketImpl::initPacket() for an example of IPv4 packet.

am I using the correct checksum generator function

Not for windows. See ICMPPacketImpl::checksum() for an example of checksum function.

should a ping be directed to port 80

No. There's no such thing as port when it comes to ICMP. See Does ICMP use a specific port?

should the socket I use be RAW or DGRAM

It should be RAW.

like image 97
Alex Avatar answered Sep 27 '22 21:09

Alex