Out-of-Band Data
The stream socket abstraction includes out-of-band data. Out-of-band data is a logically independent transmission channel between a pair of connected stream sockets. Out-of-band data is delivered independent of normal data. The out-of-band data facilities must support the reliable delivery of at least one out-of-band message at a time. This message can contain at least one byte of data. At least one message can be pending delivery at any time.
With in-band signaling, urgent data is delivered in sequence with normal data, and the message is extracted from the normal data stream. The extracted message is stored separately. Users can choose between receiving the urgent data in order and receiving the data out of sequence, without having to buffer the intervening data.
Using MSG_PEEK
, you can peek at
out-of-band data. If the socket has a process group, a
SIGURG
signal is generated when the protocol is
notified of its existence. A process can set the process group or
process ID to deliver SIGURG
to with the appropriate
fcntl
call, as described in Interrupt-Driven Socket I/O for
SIGIO
. If multiple sockets have out-of-band data
waiting for delivery, a select
call for exceptional
conditions can determine which sockets have such data pending. For more
information, see the
fcntl
(2) and
select
(3C) man pages.
A logical mark is placed in the data stream at the point at which the out-of-band data was sent. The remote login and remote shell applications use this facility to propagate signals between client and server processes. When a signal is received, all data up to the mark in the data stream is discarded.
To send an out-of-band message, apply the
MSG_OOB
flag to send
()
or
sendto
(). To receive out-of-band data, specify
MSG_OOB
to recvfrom
()
or
recv
(). If out-of-band data is taken in line the
MSG_OOB
flag is not needed. For more information, see
the
send
(3C),
sendto
(3C),
recvfrom
(3C), and
recv
(3C) man pages.
The SIOCATMARK
ioctl
(2) indicates whether the read pointer currently points at
the mark in the data stream.
int yes; ioctl(s, SIOCATMARK, &yes);
If yes is 1
on return, the next read
returns data after the mark. Otherwise, assuming out-of-band data has arrived, the
next read provides data sent by the client before sending the out-of-band signal.
The routine in the remote login process that flushes output on receipt of an
interrupt or quit signal is shown in the following example. This code reads the
normal data up to the mark to discard the normal data, then reads the out-of-band
byte.
A process can also read or peek at the out-of-band data without first reading up
to the mark. Accessing this data when the underlying protocol delivers the urgent
data in-band with the normal data, and sends notification of its presence only ahead
of time, is more difficult. An example of this type of protocol is TCP, the protocol
used to provide socket streams in the Internet family. With such protocols, the
out-of-band byte might not yet have arrived when recv
()
is called
with the MSG_OOB
flag. In that case, the call returns the error
of EWOULDBLOCK
. Also, the amount of in-band data in the input
buffer might cause normal flow control to prevent the peer from sending the urgent
data until the buffer is cleared. The process must then read enough of the queued
data to clear the input buffer before the peer can send the urgent data.
Example 7-10 Flushing Terminal I/O on Receipt of Out-of-Band Data
#include <sys/ioctl.h> #include <sys/file.h> ... oob() { int out = FWRITE; char waste[BUFSIZ]; int mark = 0; /* flush local terminal output */ ioctl(1, TIOCFLUSH, (char *) &out); while(1) { if (ioctl(rem, SIOCATMARK, &mark) == -1) { perror("ioctl"); break; } if (mark) break; (void) read(rem, waste, sizeof waste); } if (recv(rem, &mark, 1, MSG_OOB) == -1) { perror("recv"); ... } ... }
A facility to retain the position of urgent in-line data in the socket stream is
available as a socket-level option, SO_OOBINLINE
. For more
information, see the
getsockopt
(3C) man page. With this socket-level option, the position of
urgent data remains. However, the urgent data immediately following the mark in the
normal data stream is returned without the MSG_OOB
flag.
Reception of multiple urgent indications moves the mark, but does not lose any
out-of-band data.