Send an UDP packet and receive an ICMP response from router in C

d.hill picture d.hill · Feb 3, 2011 · Viewed 9.2k times · Source

I'm trying write a C program that sends an UDP packet to a given IP adress and waits for an ICMP response of a router telling that the time to live expired. It's kept very simple because I just want to understand the mechanism at first. What I need is someone checking my code giving feedback on what's wrong and what's missing. I'm very unexperienced at C-programming but I have to do it as an assignment - giving my best to understand it...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/ip.h> 

// The packet length
#define PCKT_LEN 8192 

// Source IP, source port, target IP, target port from the command line arguments
int main(int argc, char *argv[])
{
    int send, recv, resp;
    int ttl = 1; // Time to live
    char buffer[PCKT_LEN];

    // Destination address
    struct sockaddr_in dst;

    // ICMP Response
    struct msghdr icmp;

    memset(buffer, 0, PCKT_LEN);

    // Create a raw socket with UDP protocol
    if ((send = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
    {
        printf("Could not process socket() [send].\n");
        return EXIT_FAILURE;
    }

    // ICMP Socket
    if ((recv = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
    {
        printf("Could not process socket() [recv].\n");
        return EXIT_FAILURE;
    }

    dst.sin_family      = AF_INET;
    dst.sin_addr.s_addr = inet_addr("74.125.39.106");
    dst.sin_port        = htons(60001);

    // Define time to life
    if(setsockopt(send, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0)
    {
        printf("Could not process setsockopt().\n");
        return EXIT_FAILURE;
    }

    if(sendto(send, buffer, sizeof(buffer), 0, (struct sockaddr *) &dst, sizeof(dst)) < 0)
    {
        printf("Could not process sendto().\n");
        return EXIT_FAILURE;
    }


    if((resp = recvmsg(recv, (struct msghdr *) &icmp, IP_RECVERR|MSG_ERRQUEUE)) < 0 )
    {
        printf("Could not process recvmsg().\n");
        return EXIT_FAILURE;
    }

    close(send);
    close(recv);

    return 0;
}

I keep receiving "Could not process recvmsg()." and I have no clue anymore what else to try. I'd like to receive an ICMP answer and read its senders IP-Address.

Looking forward to helpfull hints.

Best regards!

Answer

ayush picture ayush · Feb 3, 2011

I normally use

recvfrom(serversock, buf, 100, 0, (struct sockaddr *)&rcv,&size);

printf("Received packet from %s:%d\nData: %s\n\n", inet_ntoa(rcv.sin_addr), ntohs(rcv.sin_port), buf);