Mac Developer Library Developer


This manual page is for Mac OS X version 10.9

If you are running a different version of Mac OS X, view the documentation locally:

  • In Terminal, using the man(1) command

Reading manual pages

Manual pages are intended as a quick reference for people who already understand a technology.

  • To learn how the manual is organized or to learn about command syntax, read the manual page for manpages(5).

  • For more information about this technology, look for other documentation in the Apple Developer Library.

  • For general information about writing shell scripts, read Shell Scripting Primer.

ICMP(4)                  BSD Kernel Interfaces Manual                  ICMP(4)

     icmp -- Internet Control Message Protocol

     #include <sys/socket.h>
     #include <netinet/in.h>

     socket(AF_INET, SOCK_RAW, proto);

     ICMP is the error and control message protocol used by IP and the Internet protocol family.  It may be
     accessed through a ``raw socket'' for network monitoring and diagnostic functions.  The proto parameter
     to the socket call to create an ICMP socket is obtained from getprotobyname(3).  ICMP sockets are con-nectionless, connectionless,
     nectionless, and are normally used with the sendto and recvfrom calls, though the connect(2) call may
     also be used to fix the destination for future packets (in which case the read(2) or recv(2) and
     write(2) or send(2) system calls may be used).

     Outgoing packets automatically have an IP header prepended to them (based on the destination address).
     Incoming packets are received with the IP header and options intact.  Note that the ip_off and ip_len
     fields are in host byte order.  For more information about the IP header structure, see ip(4).

   Non-privileged ICMP
     ICMP sockets can be opened with the SOCK_DGRAM socket type without requiring root privileges. The syn-opsis synopsis
     opsis is the following:


     Datagram oriented ICMP sockets offer a subset of the functionality available to raw ICMP sockets. Only
     IMCP request messages of the following types can be sent: ICMP_ECHO, ICMP_TSTAMP or ICMP_MASKREQ.  The
     code field must be the value zero (0).  The minimal length of an ICMP message request is eight (8)

     The advantage of using datagram oriented ICMP sockets is that even a non-privileged process can use
     ICMP echo requests to gauge the quality of the connectivity to a host, or to receive ICMP destination
     unreachable message for path MTU discovery, or to receive time exceeded messages for traceroute.

     The following IP level option can be used with datagram oriented ICMP sockets:


     When the IP option IP_HDRINCL is used, the provided IP header must obey the following rules:

         ip_v       Must be IPVERSION (4);
         ip_hl      Between 5 and 10 (inclusive);
         ip_tos     Any value;
         ip_len     Must be the total length of IP datagram (IP header + ICMP message);
         ip_id      Must be zero, will be automatically set;
         ip_off     Must be zero, will be automatically set;
         ip_ttl     Any value;
         ip_p       Must be IPPROTO_IP;
         ip_sum     Value ignored, will be automatically set;
         ip_src     Must be an IP address currently assigned to one of the local interface or INADDR_ANY;
         ip_dst     Any address;
         ip_opts    Any option.

     The maximum length of a IMCP message that can be sent is controlled by the sysctl variable

     A socket operation may fail with one of the following errors returned:

     [EISCONN]        when trying to establish a connection on a socket which already has one, or when try-ing trying
                      ing to send a datagram with the destination address specified and the socket is
                      already connected;

     [ENOTCONN]       when trying to send a datagram, but no destination address is specified, and the
                      socket hasn't been connected;

     [ENOBUFS]        when the system runs out of memory for an internal data structure;

     [EADDRNOTAVAIL]  when an attempt is made to create a socket with a network address for which no network
                      interface exists;

     [EINVAL]         when an invalid value is used with IMCP datagram socket for a field of the IP or ICMP

     recv(2), send(2), inet(4), intro(4), ip(4)

     The icmp protocol appeared in 4.3BSD.

4.3 Berkeley Distribution        June 19, 2002       4.3 Berkeley Distribution

Reporting Problems

The way to report a problem with this manual page depends on the type of problem:

Content errors
Report errors in the content of this documentation with the feedback links below.
Bug reports
Report bugs in the functionality of the described tool or API through Bug Reporter.
Formatting problems
Report formatting mistakes in the online version of these pages with the feedback links below.