Scout Module API

ICMP Sockets

ICMP Sockets

ICMP packets can be sent and received through safe raw ICMP sockets. To protect users from interference, each ICMP socket is allowed to send and receive only packets of the registered type bound to the socket. In a similar way to the standard sockets the bind system call is used to specify the packets that are to be received and sent through a socket. To receive ICMP error messages associated with a specific local TCP/UDP port (e.g., Destination Unreachable, Source Quench, Redirect, Time Exceeded, Parameter Problem), the ICMP socket needs to be bound to the port.


    #include < planetlab.h >

    struct sockaddr_in sin;

    sock = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP_UDP);

    memset(& sin, 0, sizeof(sin));
    sin.sin_port = htons(9090);
  
    bind(sock, (struct sockaddr *)& sin, sizeof(sin));

For example, the above code fragment creates and binds the ICMP socket to local UDP port 9090. Only ICMP error messages associated with local UDP port 9090 can be received through this socket. This type of ICMP socket is read-only.

To send and receive ICMP messages that are not associated with a specific TCP/UDP port number (e.g., Echo, Echo Reply, Timestamp, Timestamp Reply, Information Request, Information Reply), the socket has to be bound to a specific ICMP identifier. The ICMP identifier is a 16-bit field present in bytes 5/6 in the header of these messages. Only messages containing the right identifier can be sent or received through a safe raw ICMP socket of this type.


    struct sockaddr_in sin;

    sock = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);

    memset(& sin, 0, sizeof(sin));
    sin.sin_port = htons(23456);
  
    bind(sock, (struct sockaddr *)& sin, sizeof(sin);

For example, the above code fragment creates and binds an ICMP socket to identifier 23456. Only ICMP messages with this identifier and of the proper type can be sent and received through this socket.

No two users are allowed to bind an ICMP socket to the same local UDP/TCP port, or the same identifier. See here for more information.