Sortix volatile manual
This manual documents Sortix volatile, a development build that has not been officially released. You can instead view this document in the latest official manual.
PING(4) | Device Drivers Manual | PING(4) |
NAME
ping
— ping
protocol
SYNOPSIS
#include
<sys/socket.h>
#include <netinet/in.h>
#include <netinet/ping.h>
int
socket
(AF_INET,
SOCK_DGRAM,
IPPROTO_PING);
DESCRIPTION
The Ping Protocol uses the Echo Request and Echo Reply messages of the Internet Control Message Protocol (ICMP) to provide a connectionless best-effort echo of datagrams. A cooperating host will send back a Echo Reply message containing the same data as any Echo Request messages it receives. It is designed for packet-switched networks and provides multiplexing with a 16-bit port number (using the identifier field of the Echo Request and Echo Reply messages), and basic data integrity checks (16-bit ones' complement sum), and broadcasting. It does not provide a guarantee of delivery, avoidance of delivering multiple times, ordering, out of band data, nor flow control.
Ping sockets allow only sending Echo Request messages and receiving Echo Reply messages. The kernel will automatically send Echo Reply messages in response to any received Echo Request Messages.
The structure of ping datagrams is a 4 bytes sequence number (in big endian) followed by 0 or more bytes of an optional payload. Ping datagrams are sent inside a Echo Request message (with that sequence number) and received inside a Echo Reply message (also containing a sequence number).
Ping sockets are made with
socket(2) by passing an
appropriate domain (AF_INET
),
SOCK_DGRAM
as the type, and
IPPROTO_PING
as the protocol.
Initially a socket is not bound, it won't receive datagrams, and it does not
have a remote address and port set.
A Ping socket has the following state:
- The address family it belongs to.
- The network interface it is bound to (if any)
(
SO_BINDTODEVICE
andSO_BINDTOINDEX
) (initially none). - The local address and port (when bound) (initially none).
- The remote address and port (when connected) (initially none).
- A receive queue (initially empty).
- Whether the socket has been shutdown(2) for read and/or write (initially neither).
- A single pending asynchronous error (if any)
(
SO_ERROR
) (initially none). - Whether broadcast datagrams can be sent
(
SO_BROADCAST
) (initially no). - Whether binding to the any address and a port doesn't conflict with
binding to another address on the same port
(
SO_REUSEADDR
) (initially no). - Limits on the size of the receive and send queues
(
SO_RCVBUF
andSO_SNDBUF
).
Datagrams are sent as a packet with a header and the datagram itself. The header contains the port and the checksum. The header is 8 bytes.
Port numbers are 16-bit and range from 1 to 65535. Port 0 is not
valid. Binding to port 0 will assign an available port on the requested
address. Sending or connecting to port 0 will fail with
EADDRNOTAVAIL
. Received Echo Reply packets whose
port number is port 0 will be silently dropped. Ping ports are distinct from
ports in other transport layer protocols.
Packets contain a 16-bit ones' complement checksum. A received packet will be silently discarded if its checksum does not match its contents.
Sockets can be bound to a local address and port with bind(2) (if not already bound), or an local address and port will be automatically assigned on the first send or connect operation. The local address and port can be read with getsockname(2). If the socket hasn't been bound, the local address and port is reported as the any address on port 0. There are no ports that require superuser privileges.
Sockets can be bound to the any address, the broadcast address,
the address of a network interface, or the broadcast address of a network
interface. Binding to port 0 will automatically assign an available port on
the requested local address or fail with EAGAIN
if
no port is available. No two sockets can bind to the same local address and
port. No two sockets can be bound such that one is bound to the any address
and a port, and the other socket is bound to another address and the same
port; unless both sockets had the SO_REUSEADDR
socket option set when the second socket was bound, and the current user is
the same that bound the first socket or the current user has superuser
privileges.
A socket bound to a local address and port will receive an incoming datagram of the following conditions hold:
- The datagram belongs to the socket's address family and the protocol is Ping.
- The datagram's checksum matches the datagram.
- The datagram is an Echo Reply message.
- The datagram's port number is not port 0.
- The datagram is sent to the address or broadcast address of the network interface it is received on, or the datagram was sent to the broadcast address;
- The socket is either bound to the receiving network interface, or the socket is not bound to a network interface;
- The datagram is sent to the socket's local port;
- The datagram is sent to the socket's local address, or the socket's local address is the any address (and no other socket is bound to the datagram's address and that port);
- The socket is connected and the datagram was sent from the remote address and the remote port, or the socket is not connected; and
- The socket is not shut down for reading.
If so, the datagram is added to the socket's receive queue, otherwise it is discarded. The receive queue contains incoming packets waiting to be received. Incoming packets are dropped if the receive queue is full. Shrinking the receive queue limit drops packets as needed to stay below the limit.
The remote address and port can be set multiple times with
connect(2), after which the
socket is said to be connected, but Ping is connectionless and no handshake
is sent. The remote port must not be port 0 or the connection will fail with
EADDRNOTAVAIL
. If the socket is not bound,
connect(2) will determine
which network interface will be used to send to the remote address, and then
bind to the address of that network interface together with an available
port. connect(2) will fail
if there is no route from the local address to the requested remote address.
A connected socket only receive datagrams from the remote address and port.
connect(2) will drop
datagrams in the receive queue that don't originate from the requested
remote address. The send(2),
write(2), and
writev(2) functions can be
used on a connected socket and they send to the remote address and port by
default. If the socket is connected, the destination given to
sendto(2) and
sendmsg(2) must be
NULL
. The remote address and port can be read with
getpeername(2).
The socket can be disconnected by connecting to a socket address
with the family value set to AF_UNSPEC
, which resets
the remote address and port (if set), and otherwise has no effect.
Datagrams can be sent with
sendmsg(2) and
sendto(2). Sending on a
socket not bound to a local address and port will bind to the any address
and an available port, or fail with EAGAIN
if no
port is available. Datagrams can be received with
recvmsg(2),
recvfrom(2),
recv(2),
read(2), and
readv(2). If an asynchronous
error is pending, the next send and receive operation will fail with that
error and clear the asynchronous error, so the next operation can succeed.
Asynchronous errors can arise from network problems. There is no send queue
at the Ping level and datagrams are directly forwarded to the network layer.
It is an error to use any of the flags
MSG_CMSG_CLOEXEC
,
MSG_CMSG_CLOFORK
, MSG_EOR
,
MSG_OOB
, and
MSG_WAITALL
.
The condition of the socket can be tested with
poll(2) where
POLLIN
signifies a packet has been received (or the
socket is shut down for reading), POLLOUT
signifies
a packet can be sent now (and the socket is not shut down for writing),
POLLHUP
signifies the socket is shut down for
writing, and POLLERR
signifies an asynchronous error
is pending.
The socket can be shut down for receiving and/or sending with
shutdown(2). The receive
queue is emptied when shut down for receive (asynchronous errors are
preserved) and receive operations will succeed with an end of file
condition, but any pending asynchronous errors will take precedence and be
delivered instead. Sending when shut down for writing will raise
SIGPIPE
and fail with EPIPE
(regardless of a pending asynchronous error).
Socket options can be set with
setsockopt(2) and read
with getsockopt(2) and
exist on the IPPROTO_PING
level as well as
applicable underlying protocol levels.
Broadcast Echo Requests can be sent by setting the
SO_BROADCAST
socket option with
setsockopt(2) and sending
to a broadcast address of the network layer. RFC 1122 3.2.2.6 allows hosts
to ignore broadcast Echo Requests.
SOCKET OPTIONS
Ping sockets support these
setsockopt(2) /
getsockopt(2) options at
level SOL_SOCKET
:
SO_BINDTODEVICE
char[]- Bind to a network interface by its name. (Described in if(4))
SO_BINDTOINDEX
unsigned int- Bind to a network interface by its index number. (Described in if(4))
SO_BROADCAST
int- Whether sending to a broadcast address is allowed. (Described in if(4))
SO_DEBUG
int- Whether the socket is in debug mode. This option is not implemented and is
initially 0. Attempting to set it to non-zero will fail with
EPERM
. (Described in if(4)) SO_DOMAIN
sa_family_t- The socket domain (the address family). This option can only be read. (Described in if(4))
SO_DONTROUTE
int- Whether to bypass the routing table and only send on the local network.
This option is not implemented and is initially 0. Attempting to set it to
non-zero will fail with
EPERM
. (Described in if(4)) SO_ERROR
int- The asynchronous pending error (an errno(3) value). Cleared to 0 when read. This option can only be read. (Described in if(4))
SO_PROTOCOL
int- The socket protocol (
IPPROTO_PING
). This option can only be read. (Described in if(4)) SO_RCVBUF
int- How many bytes the receive queue can use (default is 64 pages, max 4096 pages). (Described in if(4))
SO_REUSEADDR
int- Whether binding to the any address on a port doesn't conflict with binding to another address and the same port, if both sockets have this option set and the user binding the second socket is the same that bound the first socket or the user binding the second socket has superuser privileges. (Described in if(4))
SO_SNDBUF
int- How many bytes the send queue can use (default is 64 pages, max 4096 pages). (Described in if(4))
SO_TYPE
int- The socket type (
SOCK_DGRAM
). This option can only be read. (Described in if(4))
IMPLEMENTATION NOTES
Received broadcast echo requests are ignored as permitted by RFC 1122 3.2.2.6.
Each packet currently use a page of memory, which counts towards the receive queue limit.
If no specific port is requested, one is randomly selected in the dynamic port range 32768 (inclusive) through 61000 (exclusive).
EXAMPLES
This example sends a Echo Request and blocks indefinitely until it receives a Echo Reply. remote is the remote socket address and remote_len is the size of remote. The remote and remote_len values should all be chosen according to the address family and network layer.
sa_family_t af = /* ... */; const struct sockaddr *remote = /* ... */; socklen_t remote_len = /* ... */; int fd = socket(af, SOCK_DGRAM, IPPROTO_PING); if (fd < 0) err(1, "socket"); if (connect(fd, remote, remote_len) < 0) err(1, "connect"); unsigned char request[56]; arc4random_buf(request, sizeof(request)); if (send(fd, request, sizeof(request), 0) < 0) err(1, "send"); unsigned char reply[56 + 1 /* detect too large reply */]; ssize_t amount = recv(fd, reply, sizeof(reply), 0); if (amount < 0 ) err(1, "recv"); if (amount == sizeof(request) && !memcmp(request, reply, sizeof(request))) printf("correct echo reply\n"); else printf("incorrect echo reply\n");
ERRORS
Socket operations can fail due to these error conditions, in addition to the error conditions of the network and link layer, and the error conditions of the invoked function.
- [
EACCES
] - A datagram was sent to a broadcast address, but
SO_BROADCAST
is turned off. - [
EADDRINUSE
] - The socket cannot be bound to the requested address and port because
another socket was already bound to 1) the same address and port 2) the
any address and the same port (and
SO_REUSEADDR
was not set on both sockets), or 3) some address and the same port but the requested address was the any address (andSO_REUSEADDR
was not set on both sockets). - [
EADDRNOTAVAIL
] - The socket cannot be bound to the requested address because no network interface had that address or broadcast address.
- [
EAGAIN
] - A port could not be assigned because each port in the dynamic port range had already been bound to a socket in a conflicting manner.
- [
ECONNREFUSED
] - The destination host of a datagram was not listening on the port. This error can happen asynchronously.
- [
EHOSTDOWN
] - The destination host of a datagram is not up. This error can happen asynchronously.
- [
EHOSTUNREACH
] - The destination host of a datagram was unreachable. This error can happen asynchronously.
- [
EISCONN
] - A destination address and port was specified when sending a datagram, but the socket has already been connected to a remote address and port.
- [
EMSGSIZE
] - The datagram was too large to be sent because it exceeded the maximum transmission unit (MTU) on the path between the local and remote address. This error can happen asynchronously.
- [
ENETDOWN
] - The network interface used to deliver a datagram isn't up. This error can happen asynchronously.
- [
ENETUNREACH
] - The destination network of a datagram was unreachable. This error can happen asynchronously.
- [
ENETUNREACH
] - The remote address could not be connected because there was no route from the local address to the remote address.
- [
ENOBUFS
] - There was not enough memory available for network packets.
- [
EPERM
] - One of the unimplemented
SO_DEBUG
andSO_DONTROUTE
socket options was attempted to be set to a non-zero value.
SEE ALSO
bind(2), connect(2), getpeername(2), getsockname(2), getsockopt(2), poll(2), recvfrom(2), recvmsg(2), sendmsg(2), sendto(2), setsockopt(2), shutdown(2), socket(2), icmp(4), if(4), inet(4), ip(4), kernel(7), ping(8)
STANDARDS
J. Postel (ed.), Internet Control Message Protocol - DARPA Internet Program Protocol Specification, STD 5, RFC 792, USC/Information Sciences Institute, September 1981.
Internet Engineering Task Force and R. Braden (ed.), Requirements for Internet Hosts -- Communication Layers, STD 3, RFC 1122, USC/Information Sciences Institute, October 1989.
HISTORY
Ping sockets originally appeared in Sortix 1.1.
BUGS
The handling of SO_REUSEADDR in
bind(2) does not yet enforce
the two sockets to be bound by the same user or the second socket to be
bound by a user with superuser privileges. The requirement that both sockets
have SO_REUSEADDR
set might be relaxed to only the
second socket having it set when this permission check is implemented.
The integration with the network layer is inadequate and the
asynchronous errors ECONNREFUSED
,
EHOSTDOWN
, EHOSTUNREACH
, and
ENETUNREACH
are never delivered asynchronously from
the network.
Ping sockets does not yet provide access to IP header values such as the Time To Live and does not yet report ICMP error messages.
The send(2) flag
MSG_DONTROUTE
and the
SO_DONTROUTE
socket option are not implemented
yet.
The SO_SNDBUF
socket option is currently
not used and the send queue is not limited at the socket level.
The automatic assignment of ports is random, but is statistically biased. A random port is picked, and if it is taken, the search sequentially iterates ports in ascending order until an available port is found or the search terminates.
June 4, 2017 | Sortix 1.1.0-dev |