ip (7) - Linux Manuals
ip: Linux IPv4 protocol implementation
NAME
ip - Linux IPv4 protocol implementation
SYNOPSIS
#include <sys/socket.h>#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
raw_socket = socket(AF_INET, SOCK_RAW, protocol);
DESCRIPTION
Linux implements the Internet Protocol, version 4, described in RFC 791 and RFC 1122. ip contains a level 2 multicasting implementation conforming to RFC 1112. It also contains an IP router including a packet filter.The programming interface is BSD-sockets compatible. For more information on sockets, see socket(7).
An IP socket is created using socket(2):
Valid socket types include
SOCK_STREAM
to open a stream socket,
SOCK_DGRAM
to open a datagram socket, and
SOCK_RAW
to open a
raw(7)
socket to access the IP protocol directly.
protocol
is the IP protocol in the IP header to be received or sent.
Valid values for
protocol
include:
For
SOCK_RAW
you may specify a valid IANA IP protocol defined in
RFC 1700 assigned numbers.
When a process wants to receive new incoming packets or connections, it
should bind a socket to a local interface address using
bind(2).
In this case, only one IP socket may be bound to any given local
(address, port) pair.
When
INADDR_ANY
is specified in the bind call, the socket will be bound to
all
local interfaces.
When
listen(2)
is called on an unbound socket, the socket is automatically bound
to a random free port with the local address set to
INADDR_ANY.
When
connect(2)
is called on an unbound socket, the socket is automatically bound
to a random free port or to a usable shared port with the local address
set to
INADDR_ANY.
A TCP local socket address that has been bound is unavailable for
some time after closing, unless the
SO_REUSEADDR
flag has been set.
Care should be taken when using this flag as it makes TCP less reliable.
struct sockaddr_in {
/* Internet address. */
struct in_addr {
sin_family
is always set to
AF_INET.
This is required; in Linux 2.2 most networking functions return
EINVAL
when this setting is missing.
sin_port
contains the port in network byte order.
The port numbers below 1024 are called
privileged ports
(or sometimes:
reserved ports).
Only a privileged process
(on Linux: a process that has the
CAP_NET_BIND_SERVICE
capability in the user namespace governing its network namespace) may
bind(2)
to these sockets.
Note that the raw IPv4 protocol as such has no concept of a
port, they are implemented only by higher protocols like
tcp(7)
and
udp(7).
sin_addr
is the IP host address.
The
s_addr
member of
struct in_addr
contains the host interface address in network byte order.
in_addr
should be assigned one of the
INADDR_*
values
(e.g.,
INADDR_LOOPBACK)
using
htonl(3)
or set using the
inet_aton(3),
inet_addr(3),
inet_makeaddr(3)
library functions or directly with the name resolver (see
gethostbyname(3)).
IPv4 addresses are divided into unicast, broadcast,
and multicast addresses.
Unicast addresses specify a single interface of a host,
broadcast addresses specify all hosts on a network, and multicast
addresses address all hosts in a multicast group.
Datagrams to broadcast addresses can be sent or received only when the
SO_BROADCAST
socket flag is set.
In the current implementation, connection-oriented sockets are allowed
to use only unicast addresses.
Note that the address and the port are always stored in
network byte order.
In particular, this means that you need to call
htons(3)
on the number that is assigned to a port.
All address/port manipulation
functions in the standard library work in network byte order.
There are several special addresses:
INADDR_LOOPBACK
(127.0.0.1)
always refers to the local host via the loopback device;
INADDR_ANY
(0.0.0.0)
means any address for binding;
INADDR_BROADCAST
(255.255.255.255)
means any host and has the same effect on bind as
INADDR_ANY
for historical reasons.
When an invalid socket option is specified,
getsockopt(2)
and
setsockopt(2)
fail with the error
ENOPROTOOPT.
struct ip_mreqn {
imr_multiaddr
contains the address of the multicast group the application
wants to join or leave.
It must be a valid multicast address
(or
setsockopt(2)
fails with the error
EINVAL).
imr_address
is the address of the local interface with which the system
should join the multicast group; if it is equal to
INADDR_ANY,
an appropriate interface is chosen by the system.
imr_ifindex
is the interface index of the interface that should join/leave the
imr_multiaddr
group, or 0 to indicate any interface.
struct ip_mreq_source {
Address format
An IP socket address is defined as a combination of an IP interface
address and a 16-bit port number.
The basic IP protocol does not supply port numbers, they
are implemented by higher level protocols like
udp(7)
and
tcp(7).
On raw sockets
sin_port
is set to the IP protocol.
Socket options
IP supports some protocol-specific socket options that can be set with
setsockopt(2)
and read with
getsockopt(2).
The socket option level for IP is
IPPROTO_IP.
A boolean integer flag is zero when it is false, otherwise true.