udpのヘルプ・マニュアル
日本語 英語
udp --help
man udp
UDP(7) Linux Programmer’s Manual UDP(7)
名前
udp - IPv4 の ユーザーデータグラムプロトコル
書式
#include
#include
udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
説明
こ れ は RFC 768 で記述されている User Datagram Protocol の実装である。
UDP はコネクションレスの、信頼性の低いデータパケットサービスである。 パ
ケットは到着前に並び替えられたり複製されたりする。 UDP は転送エラーを検
出するためにチェックサムを生成・チェックする。
UDP ソケットが生成されるとき、ローカルアドレスやリモートアドレスは指 定
されない。正しい行き先アドレスを引数として sendto(2) や sendmsg(2) を呼
べば、データグラムはただちに送信される。ソケットに対して connect(2) を
呼ぶと、デフォルトの行き先アドレスが設定され、 send(2) や write(2) を使
って、行き先アドレスの指定なしにデータグラムを送信できるようになる。 こ
の場合でも、行き先アドレスを sendto(2) や sendmsg(2) に渡せば、デフォル
ト以外のアドレスに送信可能である。パケットを受信するために、まずソケ ッ
トを bind(2) を用いてローカルなアドレスにバインドさせることもできる。そ
うでない場合は、ソケット層は自動的に net.ipv4.ip_local_port_range で 定
義 されている範囲の外で空いているローカルなポートを割り当て、ソケットを
INADDR_ANY にバインドする。
受信動作はパケットを一つだけ返す。渡したバッファよりもパケットが小さ け
れ ば、そのパケットの大きさのデータだけが返される。逆にバッファよりも大
きい場合はパケットは丸められ、 MSG_TRUNC フラグ が セ ッ ト さ れ る 。
MSG_WAITALL はサポートしていない。
IP オプションは、 ip(7) に記述されているソケットオプションを用いて読み
書きできる。これらは適切な /proc パラメータが有効な場合に限ってカーネル
によって処理される (しかし無効になっている場合でもユーザーには渡される)
。 ip(7) を参照のこと。
MSG_DONTROUTE フラグが送信時にセットされている場合には、行き先アドレ ス
は ローカルなインターフェースアドレスから参照できなければならない。パケ
ットはそのインターフェースにしか送られない。
デフォルトでは、Linux の UDP は Path MTU Discovery を行う。つまり、カー
ネルは特定の宛先 IP アドレスの MTU (Maximum Transmission Unit; 最大転送
単位) を記録し、UDP パケットの書き込みが MTU を超えた場合 EMSGSIZE を返
す 。 EMSGSIZE を返された場合、アプリケーションはパケットサイズを小さく
すべきであ る 。 ソ ケ ッ ト オ プ シ ョ ン IP_MTU_DISCOVER ま た は
/proc/sys/net/ipv4/ip_no_pmtu_disc ファイルを使って Path MTU Discovery
を無効にすることもできる (詳細は ip(7) を参照)。 Path MTU Discovery を
無 効 にした場合は、パケットサイズがインタフェースの MTU よりも大きいと
UDP はそのパケットをフラグメント化して送出する。しかしながら、性能と 信
頼性の理由から Path MTU Discovery を無効にするのは推奨できない。
アドレスのフォーマット
UDP は IPv4 の sockaddr_in アドレスフォーマットを用いる。これは ip(7)
に記述されている。
エラー処理
致命的なエラーは、たとえソケットが接続されていなくても、すべてエラー 戻
り 値としてユーザーに渡される。これにはネットワークから受け取る非同期エ
ラーも含まれる。同じソケットを使って送信した昔のパケットに関するエラ ー
を受け取るかもしれない。この振る舞いは他の BSD ソケットの実装の多くとは
異なる。これらではソケットが接続されていない場合はエラーを全く返さな い
。 Linux の振る舞いは RFC 1122 での指定に従ったものである。
Linux 2.0 と 2.2 では、古いコードとの互換性のために、 SO_BSDCOMPAT
SOL_SOCKET オプションを設定すれば、ソケットが接続されている場合に限って
リ モートのエラーを受信するようにできた (EPROTO と EMSGSIZE を除く)。ロ
ーカルで生成されたエラーは常に渡される。このソケットオプションのサポ ー
ト はそれ以降のバージョンの Linux で削除された。詳細は socket(7) を参照
。
IP_RECVERR オプションが有効になっていると、すべてのエラーはソケットのエ
ラ ー キ ュ ー に 保 存 さ れる。これは MSG_ERRQUEUE フラグをセットして
recvmsg(2) を呼べば受信できる。
/proc インタフェース
システム全体の UDP パラメータ設定には、 /proc/sys/net/ipv4/ ディレク ト
リ内のファイルの読み書きでアクセスできる。
udp_mem (Linux 2.6.25 以降)
これは 3 つの整数からなるベクトル値で、 UDP の全ソケットのキュー
で利用可能なページ数を制御する。
min このページ数より少なければ、UDP はそのメモリ使用に関し
て 干渉されない。 UDP に割り当てられたメモリ総量がこの
値を超過すると、 UDP はメモリ使用量を調整し始める。
pressure この値は tcp_mem の形式 (tcp(7) 参照) と合わせるために
導入された
max UDP の全ソケットのキューで利用可能なページ数。
こ れらの 3 つの値のデフォルト値は、ブート時に利用可能なメモリ総
量から計算される。
udp_rmem_min (integer; デフォルト値: PAGE_SIZE; Linux 2.6.25 以降)
メモリ使用量の調整中に、UDP ソケットが使用できる受信バッファの最
小 値 ( バ イ ト 単位)。 UDP の全ソケットのページ使用量の合計が
udp_mem pressure を超過している場合であっても、各 UDP ソケットは
データの受信にこのサイズ分だけは使用することができる。
udp_wmem_min (integer; デフォルト値: PAGE_SIZE; Linux 2.6.25 以降)
メモリ使用量の調整中に、UDP ソケットが使用できる送信バッファの最
小値 (バイト単位)。 UDP の全ソケットのページ使 用 量 の 合 計 が
udp_mem pressure を超過している場合であっても、各 UDP ソケットは
データの送信にこのサイズ分だけは使用することができる。
ソケットオプション
UDP ソケットオプションを設定または取得するには、取得には getsockopt(2)
を 、設定には setsockopt(2) をオプションレベル引数に IPPROTO_UDP を指定
して呼び出す。
UDP_CORK (Linux 2.5.44 以降)
このオプションが指定されると、このソケットの全てのデータ出力は一
つのデータグラムに蓄積され、このオプションが無効化された時に送信
される。このオプションは移植性を考慮したコードでは用いるべきでは
ない。
ioctl
以下に示す ioctl は ioctl(2) を使ってアクセスできる。正しい文法は以下の
通り。
int value;
error = ioctl(udp_socket, ioctl_type, &value);
FIONREAD (SIOCINQ)
integer のポインタを引数として取る。次に待機しているデータグラム
のサイズをバイト単位の整数で返す。待機しているデータグラムがない
場合は 0 を返す。
TIOCOUTQ (SIOCOUTQ)
ローカル送信キューにあるデータサイズをバイト単位で返す 。 Linux
2.4 以上でのみ対応している。
さらに、 ip(7) と socket(7) で述べられている全ての ioctl も対応している
。
エラー
socket(7) や ip(7) に記述されている全てのエラーが、 UDP ソケットの送 受
信で返される可能性がある。
ECONNREFUSED
行き先アドレスに関連づけられている受信者がいない。これは以前のパ
ケットがそのパケットを上書き送信してしまっているからであることが
多い。
バージョン
IP_RECVERR は Linux 2.2 の新しい機能である。
関連項目
ip(7), raw(7), socket(7), udplite(7)
RFC 768 : User Datagram Protocol
RFC 1122 : ホストの必要条件
RFC 1191 : path MTU discovery の記述
Linux 2008-11-21 UDP(7)
UDP(7) Linux Programmer’s Manual UDP(7)
NAME
udp - User Datagram Protocol for IPv4
SYNOPSIS
#include
#include
udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
DESCRIPTION
This is an implementation of the User Datagram Protocol described in
RFC 768. It implements a connectionless, unreliable datagram packet
service. Packets may be reordered or duplicated before they arrive.
UDP generates and checks checksums to catch transmission errors.
When a UDP socket is created, its local and remote addresses are
unspecified. Datagrams can be sent immediately using sendto(2) or
sendmsg(2) with a valid destination address as an argument. When con-
nect(2) is called on the socket, the default destination address is set
and datagrams can now be sent using send(2) or write(2) without speci-
fying a destination address. It is still possible to send to other
destinations by passing an address to sendto(2) or sendmsg(2). In
order to receive packets, the socket can be bound to a local address
first by using bind(2). Otherwise the socket layer will automatically
assign a free local port out of the range defined by
net.ipv4.ip_local_port_range and bind the socket to INADDR_ANY.
All receive operations return only one packet. When the packet is
smaller than the passed buffer, only that much data is returned; when
it is bigger, the packet is truncated and the MSG_TRUNC flag is set.
MSG_WAITALL is not supported.
IP options may be sent or received using the socket options described
in ip(7). They are only processed by the kernel when the appropriate
/proc parameter is enabled (but still passed to the user even when it
is turned off). See ip(7).
When the MSG_DONTROUTE flag is set on sending, the destination address
must refer to a local interface address and the packet is only sent to
that interface.
By default, Linux UDP does path MTU (Maximum Transmission Unit) discov-
ery. This means the kernel will keep track of the MTU to a specific
target IP address and return EMSGSIZE when a UDP packet write exceeds
it. When this happens, the application should decrease the packet
size. Path MTU discovery can be also turned off using the IP_MTU_DIS-
COVER socket option or the /proc/sys/net/ipv4/ip_no_pmtu_disc file; see
ip(7) for details. When turned off, UDP will fragment outgoing UDP
packets that exceed the interface MTU. However, disabling it is not
recommended for performance and reliability reasons.
Address Format
UDP uses the IPv4 sockaddr_in address format described in ip(7).
Error Handling
All fatal errors will be passed to the user as an error return even
when the socket is not connected. This includes asynchronous errors
received from the network. You may get an error for an earlier packet
that was sent on the same socket. This behavior differs from many
other BSD socket implementations which don’t pass any errors unless the
socket is connected. Linux’s behavior is mandated by RFC 1122.
For compatibility with legacy code, in Linux 2.0 and 2.2 it was possi-
ble to set the SO_BSDCOMPAT SOL_SOCKET option to receive remote errors
only when the socket has been connected (except for EPROTO and EMSG-
SIZE). Locally generated errors are always passed. Support for this
socket option was removed in later kernels; see socket(7) for further
information.
When the IP_RECVERR option is enabled, all errors are stored in the
socket error queue, and can be received by recvmsg(2) with the
MSG_ERRQUEUE flag set.
/proc interfaces
System-wide UDP parameter settings can be accessed by files in the
directory /proc/sys/net/ipv4/.
udp_mem (since Linux 2.6.25)
This is a vector of three integers governing the number of pages
allowed for queueing by all UDP sockets.
min Below this number of pages, UDP is not bothered about
its memory appetite. When the amount of memory allo-
cated by UDP exceeds this number, UDP starts to moder-
ate memory usage.
pressure This value was introduced to follow the format of
tcp_mem (see tcp(7)).
max Number of pages allowed for queueing by all UDP sock-
ets.
Defaults values for these three items are calculated at boot
time from the amount of available memory.
udp_rmem_min (integer; default value: PAGE_SIZE; since Linux 2.6.25)
Minimal size, in bites, of receive buffer used by UDP sockets in
moderation. Each UDP socket is able to use the size for receiv-
ing data, even if total pages of UDP sockets exceed udp_mem
pressure.
udp_wmem_min (integer; default value: PAGE_SIZE; since Linux 2.6.25)
Minimal size, in bytes, of send buffer used by UDP sockets in
moderation. Each UDP socket is able to use the size for sending
data, even if total pages of UDP sockets exceed udp_mem pres-
sure.
Socket Options
To set or get a UDP socket option, call getsockopt(2) to read or set-
sockopt(2) to write the option with the option level argument set to
IPPROTO_UDP.
UDP_CORK (since Linux 2.5.44)
If this option is enabled, then all data output on this socket
is accumulated into a single datagram that is transmitted when
the option is disabled. This option should not be used in code
intended to be portable.
Ioctls
These ioctls can be accessed using ioctl(2). The correct syntax is:
int value;
error = ioctl(udp_socket, ioctl_type, &value);
FIONREAD (SIOCINQ)
Gets a pointer to an integer as argument. Returns the size of
the next pending datagram in the integer in bytes, or 0 when no
datagram is pending.
TIOCOUTQ (SIOCOUTQ)
Returns the number of data bytes in the local send queue. Only
supported with Linux 2.4 and above.
In addition all ioctls documented in ip(7) and socket(7) are supported.
ERRORS
All errors documented for socket(7) or ip(7) may be returned by a send
or receive on a UDP socket.
ECONNREFUSED
No receiver was associated with the destination address. This
might be caused by a previous packet sent over the socket.
VERSIONS
IP_RECVERR is a new feature in Linux 2.2.
SEE ALSO
ip(7), raw(7), socket(7), udplite(7)
RFC 768 for the User Datagram Protocol.
RFC 1122 for the host requirements.
RFC 1191 for a description of path MTU discovery.
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2008-11-21 UDP(7)