1 @node Sockets, Low-Level Terminal Interface, Pipes and FIFOs, Top
4 This chapter describes the GNU facilities for interprocess
5 communication using sockets.
8 @cindex interprocess communication, with sockets
9 A @dfn{socket} is a generalized interprocess communication channel.
10 Like a pipe, a socket is represented as a file descriptor. But,
11 unlike pipes, sockets support communication between unrelated
12 processes, and even between processes running on different machines
13 that communicate over a network. Sockets are the primary means of
14 communicating with other machines; @code{telnet}, @code{rlogin},
15 @code{ftp}, @code{talk}, and the other familiar network programs use
18 Not all operating systems support sockets. In the GNU library, the
19 header file @file{sys/socket.h} exists regardless of the operating
20 system, and the socket functions always exist, but if the system does
21 not really support sockets, these functions always fail.
23 @strong{Incomplete:} We do not currently document the facilities for
24 broadcast messages or for configuring Internet interfaces.
27 * Socket Concepts:: Basic concepts you need to know about.
28 * Communication Styles::Stream communication, datagrams, and other styles.
29 * Socket Addresses:: How socket names (``addresses'') work.
30 * File Namespace:: Details about the file namespace.
31 * Internet Namespace:: Details about the Internet namespace.
32 * Misc Namespaces:: Other namespaces not documented fully here.
33 * Open/Close Sockets:: Creating sockets and destroying them.
34 * Connections:: Operations on sockets with connection state.
35 * Datagrams:: Operations on datagram sockets.
36 * Inetd:: Inetd is a daemon that starts servers on request.
37 The most convenient way to write a server
38 is to make it work with Inetd.
39 * Socket Options:: Miscellaneous low-level socket options.
40 * Networks Database:: Accessing the database of network names.
44 @section Socket Concepts
46 @cindex communication style (of a socket)
47 @cindex style of communication (of a socket)
48 When you create a socket, you must specify the style of communication
49 you want to use and the type of protocol that should implement it.
50 The @dfn{communication style} of a socket defines the user-level
51 semantics of sending and receiving data on the socket. Choosing a
52 communication style specifies the answers to questions such as these:
58 @cindex stream (sockets)
59 @strong{What are the units of data transmission?} Some communication
60 styles regard the data as a sequence of bytes, with no larger
61 structure; others group the bytes into records (which are known in
62 this context as @dfn{packets}).
65 @cindex loss of data on sockets
66 @cindex data loss on sockets
67 @strong{Can data be lost during normal operation?} Some communication
68 styles guarantee that all the data sent arrives in the order it was
69 sent (barring system or network crashes); other styles occasionally
70 lose data as a normal part of operation, and may sometimes deliver
71 packets more than once or in the wrong order.
73 Designing a program to use unreliable communication styles usually
74 involves taking precautions to detect lost or misordered packets and
75 to retransmit data as needed.
78 @strong{Is communication entirely with one partner?} Some
79 communication styles are like a telephone call---you make a
80 @dfn{connection} with one remote socket, and then exchange data
81 freely. Other styles are like mailing letters---you specify a
82 destination address for each message you send.
85 @cindex namespace (of socket)
86 @cindex domain (of socket)
87 @cindex socket namespace
89 You must also choose a @dfn{namespace} for naming the socket. A socket
90 name (``address'') is meaningful only in the context of a particular
91 namespace. In fact, even the data type to use for a socket name may
92 depend on the namespace. Namespaces are also called ``domains'', but we
93 avoid that word as it can be confused with other usage of the same
94 term. Each namespace has a symbolic name that starts with @samp{PF_}.
95 A corresponding symbolic name starting with @samp{AF_} designates the
96 address format for that namespace.
98 @cindex network protocol
99 @cindex protocol (of socket)
100 @cindex socket protocol
101 @cindex protocol family
102 Finally you must choose the @dfn{protocol} to carry out the
103 communication. The protocol determines what low-level mechanism is used
104 to transmit and receive data. Each protocol is valid for a particular
105 namespace and communication style; a namespace is sometimes called a
106 @dfn{protocol family} because of this, which is why the namespace names
107 start with @samp{PF_}.
109 The rules of a protocol apply to the data passing between two programs,
110 perhaps on different computers; most of these rules are handled by the
111 operating system, and you need not know about them. What you do need to
112 know about protocols is this:
116 In order to have communication between two sockets, they must specify
117 the @emph{same} protocol.
120 Each protocol is meaningful with particular style/namespace
121 combinations and cannot be used with inappropriate combinations. For
122 example, the TCP protocol fits only the byte stream style of
123 communication and the Internet namespace.
126 For each combination of style and namespace, there is a @dfn{default
127 protocol} which you can request by specifying 0 as the protocol
128 number. And that's what you should normally do---use the default.
131 Throughout the following description at various places
132 variables/parameters to denote sizes are required. And here the trouble
133 starts. In the first implementations the type of these variables was
134 simply @code{int}. This type was on almost all machines of this time 32
135 bits wide and so a de-factor standard required 32 bit variables. This
136 is important since references to variables of this type are passed to
139 But now the POSIX people came and unified the interface with their words
140 "all size values are of type @code{size_t}". But on 64 bit machines
141 @code{size_t} is 64 bits wide and so variable references are not anymore
144 A solution provides the Unix98 specification which finally introduces a
145 type @code{socklen_t}. This type is used in all of the cases that were
146 previously changed to use @code{size_t}. The only requirement of this
147 type is that it is an unsigned type of at least 32 bits. Therefore,
148 implementations which require references to 32 bit variables be passed
149 can be as happy as implementations which use right from the start 64 bit
153 @node Communication Styles
154 @section Communication Styles
156 The GNU library includes support for several different kinds of sockets,
157 each with different characteristics. This section describes the
158 supported socket types. The symbolic constants listed here are
159 defined in @file{sys/socket.h}.
162 @comment sys/socket.h
164 @deftypevr Macro int SOCK_STREAM
165 The @code{SOCK_STREAM} style is like a pipe (@pxref{Pipes and FIFOs});
166 it operates over a connection with a particular remote socket, and
167 transmits data reliably as a stream of bytes.
169 Use of this style is covered in detail in @ref{Connections}.
172 @comment sys/socket.h
174 @deftypevr Macro int SOCK_DGRAM
175 The @code{SOCK_DGRAM} style is used for sending
176 individually-addressed packets, unreliably.
177 It is the diametrical opposite of @code{SOCK_STREAM}.
179 Each time you write data to a socket of this kind, that data becomes
180 one packet. Since @code{SOCK_DGRAM} sockets do not have connections,
181 you must specify the recipient address with each packet.
183 The only guarantee that the system makes about your requests to
184 transmit data is that it will try its best to deliver each packet you
185 send. It may succeed with the sixth packet after failing with the
186 fourth and fifth packets; the seventh packet may arrive before the
187 sixth, and may arrive a second time after the sixth.
189 The typical use for @code{SOCK_DGRAM} is in situations where it is
190 acceptable to simply resend a packet if no response is seen in a
191 reasonable amount of time.
193 @xref{Datagrams}, for detailed information about how to use datagram
198 @c This appears to be only for the NS domain, which we aren't
199 @c discussing and probably won't support either.
200 @comment sys/socket.h
202 @deftypevr Macro int SOCK_SEQPACKET
203 This style is like @code{SOCK_STREAM} except that the data is
204 structured into packets.
206 A program that receives data over a @code{SOCK_SEQPACKET} socket
207 should be prepared to read the entire message packet in a single call
208 to @code{read}; if it only reads part of the message, the remainder of
209 the message is simply discarded instead of being available for
210 subsequent calls to @code{read}.
212 Many protocols do not support this communication style.
217 @comment sys/socket.h
219 @deftypevr Macro int SOCK_RDM
220 This style is a reliable version of @code{SOCK_DGRAM}: it sends
221 individually addressed packets, but guarantees that each packet sent
222 arrives exactly once.
224 @strong{Warning:} It is not clear this is actually supported
225 by any operating system.
229 @comment sys/socket.h
231 @deftypevr Macro int SOCK_RAW
232 This style provides access to low-level network protocols and
233 interfaces. Ordinary user programs usually have no need to use this
237 @node Socket Addresses
238 @section Socket Addresses
240 @cindex address of socket
241 @cindex name of socket
242 @cindex binding a socket address
243 @cindex socket address (name) binding
244 The name of a socket is normally called an @dfn{address}. The
245 functions and symbols for dealing with socket addresses were named
246 inconsistently, sometimes using the term ``name'' and sometimes using
247 ``address''. You can regard these terms as synonymous where sockets
250 A socket newly created with the @code{socket} function has no
251 address. Other processes can find it for communication only if you
252 give it an address. We call this @dfn{binding} the address to the
253 socket, and the way to do it is with the @code{bind} function.
255 You need be concerned with the address of a socket if other processes
256 are to find it and start communicating with it. You can specify an
257 address for other sockets, but this is usually pointless; the first time
258 you send data from a socket, or use it to initiate a connection, the
259 system assigns an address automatically if you have not specified one.
261 Occasionally a client needs to specify an address because the server
262 discriminates based on addresses; for example, the rsh and rlogin
263 protocols look at the client's socket address and don't bypass password
264 checking unless it is less than @code{IPPORT_RESERVED} (@pxref{Ports}).
266 The details of socket addresses vary depending on what namespace you are
267 using. @xref{File Namespace}, or @ref{Internet Namespace}, for specific
270 Regardless of the namespace, you use the same functions @code{bind} and
271 @code{getsockname} to set and examine a socket's address. These
272 functions use a phony data type, @code{struct sockaddr *}, to accept the
273 address. In practice, the address lives in a structure of some other
274 data type appropriate to the address format you are using, but you cast
275 its address to @code{struct sockaddr *} when you pass it to
279 * Address Formats:: About @code{struct sockaddr}.
280 * Setting Address:: Binding an address to a socket.
281 * Reading Address:: Reading the address of a socket.
284 @node Address Formats
285 @subsection Address Formats
287 The functions @code{bind} and @code{getsockname} use the generic data
288 type @code{struct sockaddr *} to represent a pointer to a socket
289 address. You can't use this data type effectively to interpret an
290 address or construct one; for that, you must use the proper data type
291 for the socket's namespace.
293 Thus, the usual practice is to construct an address in the proper
294 namespace-specific type, then cast a pointer to @code{struct sockaddr *}
295 when you call @code{bind} or @code{getsockname}.
297 The one piece of information that you can get from the @code{struct
298 sockaddr} data type is the @dfn{address format} designator which tells
299 you which data type to use to understand the address fully.
302 The symbols in this section are defined in the header file
305 @comment sys/socket.h
307 @deftp {Date Type} {struct sockaddr}
308 The @code{struct sockaddr} type itself has the following members:
311 @item short int sa_family
312 This is the code for the address format of this address. It
313 identifies the format of the data which follows.
315 @item char sa_data[14]
316 This is the actual socket address data, which is format-dependent. Its
317 length also depends on the format, and may well be more than 14. The
318 length 14 of @code{sa_data} is essentially arbitrary.
322 Each address format has a symbolic name which starts with @samp{AF_}.
323 Each of them corresponds to a @samp{PF_} symbol which designates the
324 corresponding namespace. Here is a list of address format names:
327 @comment sys/socket.h
331 This designates the address format that goes with the file namespace.
332 (@code{PF_FILE} is the name of that namespace.) @xref{File Namespace
333 Details}, for information about this address format.
335 @comment sys/socket.h
339 This is a synonym for @code{AF_FILE}, for compatibility.
340 (@code{PF_UNIX} is likewise a synonym for @code{PF_FILE}.)
342 @comment sys/socket.h
346 This designates the address format that goes with the Internet
347 namespace. (@code{PF_INET} is the name of that namespace.)
348 @xref{Internet Address Formats}.
350 @comment sys/socket.h
351 @comment IPv6 Basic API
353 This is similar to @code{AF_INET}, but refers to the IPv6 protocol.
354 (@code{PF_INET6} is the name of the corresponding namespace.)
356 @comment sys/socket.h
360 This designates no particular address format. It is used only in rare
361 cases, such as to clear out the default destination address of a
362 ``connected'' datagram socket. @xref{Sending Datagrams}.
364 The corresponding namespace designator symbol @code{PF_UNSPEC} exists
365 for completeness, but there is no reason to use it in a program.
368 @file{sys/socket.h} defines symbols starting with @samp{AF_} for many
369 different kinds of networks, all or most of which are not actually
370 implemented. We will document those that really work, as we receive
371 information about how to use them.
373 @node Setting Address
374 @subsection Setting the Address of a Socket
377 Use the @code{bind} function to assign an address to a socket. The
378 prototype for @code{bind} is in the header file @file{sys/socket.h}.
379 For examples of use, see @ref{File Namespace}, or see @ref{Inet Example}.
381 @comment sys/socket.h
383 @deftypefun int bind (int @var{socket}, struct sockaddr *@var{addr}, socklen_t @var{length})
384 The @code{bind} function assigns an address to the socket
385 @var{socket}. The @var{addr} and @var{length} arguments specify the
386 address; the detailed format of the address depends on the namespace.
387 The first part of the address is always the format designator, which
388 specifies a namespace, and says that the address is in the format for
391 The return value is @code{0} on success and @code{-1} on failure. The
392 following @code{errno} error conditions are defined for this function:
396 The @var{socket} argument is not a valid file descriptor.
399 The descriptor @var{socket} is not a socket.
402 The specified address is not available on this machine.
405 Some other socket is already using the specified address.
408 The socket @var{socket} already has an address.
411 You do not have permission to access the requested address. (In the
412 Internet domain, only the super-user is allowed to specify a port number
413 in the range 0 through @code{IPPORT_RESERVED} minus one; see
417 Additional conditions may be possible depending on the particular namespace
421 @node Reading Address
422 @subsection Reading the Address of a Socket
425 Use the function @code{getsockname} to examine the address of an
426 Internet socket. The prototype for this function is in the header file
429 @comment sys/socket.h
431 @deftypefun int getsockname (int @var{socket}, struct sockaddr *@var{addr}, socklen_t *@var{length-ptr})
432 The @code{getsockname} function returns information about the
433 address of the socket @var{socket} in the locations specified by the
434 @var{addr} and @var{length-ptr} arguments. Note that the
435 @var{length-ptr} is a pointer; you should initialize it to be the
436 allocation size of @var{addr}, and on return it contains the actual
437 size of the address data.
439 The format of the address data depends on the socket namespace. The
440 length of the information is usually fixed for a given namespace, so
441 normally you can know exactly how much space is needed and can provide
442 that much. The usual practice is to allocate a place for the value
443 using the proper data type for the socket's namespace, then cast its
444 address to @code{struct sockaddr *} to pass it to @code{getsockname}.
446 The return value is @code{0} on success and @code{-1} on error. The
447 following @code{errno} error conditions are defined for this function:
451 The @var{socket} argument is not a valid file descriptor.
454 The descriptor @var{socket} is not a socket.
457 There are not enough internal buffers available for the operation.
461 You can't read the address of a socket in the file namespace. This is
462 consistent with the rest of the system; in general, there's no way to
463 find a file's name from a descriptor for that file.
466 @section The File Namespace
467 @cindex file namespace, for sockets
469 This section describes the details of the file namespace, whose
470 symbolic name (required when you create a socket) is @code{PF_FILE}.
473 * Concepts: File Namespace Concepts. What you need to understand.
474 * Details: File Namespace Details. Address format, symbolic names, etc.
475 * Example: File Socket Example. Example of creating a socket.
478 @node File Namespace Concepts
479 @subsection File Namespace Concepts
481 In the file namespace, socket addresses are file names. You can specify
482 any file name you want as the address of the socket, but you must have
483 write permission on the directory containing it. In order to connect to
484 a socket, you must have read permission for it. It's common to put
485 these files in the @file{/tmp} directory.
487 One peculiarity of the file namespace is that the name is only used when
488 opening the connection; once that is over with, the address is not
489 meaningful and may not exist.
491 Another peculiarity is that you cannot connect to such a socket from
492 another machine--not even if the other machine shares the file system
493 which contains the name of the socket. You can see the socket in a
494 directory listing, but connecting to it never succeeds. Some programs
495 take advantage of this, such as by asking the client to send its own
496 process ID, and using the process IDs to distinguish between clients.
497 However, we recommend you not use this method in protocols you design,
498 as we might someday permit connections from other machines that mount
499 the same file systems. Instead, send each new client an identifying
500 number if you want it to have one.
502 After you close a socket in the file namespace, you should delete the
503 file name from the file system. Use @code{unlink} or @code{remove} to
504 do this; see @ref{Deleting Files}.
506 The file namespace supports just one protocol for any communication
507 style; it is protocol number @code{0}.
509 @node File Namespace Details
510 @subsection Details of File Namespace
513 To create a socket in the file namespace, use the constant
514 @code{PF_FILE} as the @var{namespace} argument to @code{socket} or
515 @code{socketpair}. This constant is defined in @file{sys/socket.h}.
517 @comment sys/socket.h
519 @deftypevr Macro int PF_FILE
520 This designates the file namespace, in which socket addresses are file
521 names, and its associated family of protocols.
524 @comment sys/socket.h
526 @deftypevr Macro int PF_UNIX
527 This is a synonym for @code{PF_FILE}, for compatibility's sake.
530 The structure for specifying socket names in the file namespace is
531 defined in the header file @file{sys/un.h}:
536 @deftp {Data Type} {struct sockaddr_un}
537 This structure is used to specify file namespace socket addresses. It has
538 the following members:
541 @item short int sun_family
542 This identifies the address family or format of the socket address.
543 You should store the value @code{AF_FILE} to designate the file
544 namespace. @xref{Socket Addresses}.
546 @item char sun_path[108]
547 This is the file name to use.
549 @strong{Incomplete:} Why is 108 a magic number? RMS suggests making
550 this a zero-length array and tweaking the example following to use
551 @code{alloca} to allocate an appropriate amount of storage based on
552 the length of the filename.
556 You should compute the @var{length} parameter for a socket address in
557 the file namespace as the sum of the size of the @code{sun_family}
558 component and the string length (@emph{not} the allocation size!) of
559 the file name string.
561 @node File Socket Example
562 @subsection Example of File-Namespace Sockets
564 Here is an example showing how to create and name a socket in the file
568 @include mkfsock.c.texi
571 @node Internet Namespace
572 @section The Internet Namespace
573 @cindex Internet namespace, for sockets
575 This section describes the details the protocols and socket naming
576 conventions used in the Internet namespace.
578 To create a socket in the Internet namespace, use the symbolic name
579 @code{PF_INET} of this namespace as the @var{namespace} argument to
580 @code{socket} or @code{socketpair}. This macro is defined in
584 @comment sys/socket.h
586 @deftypevr Macro int PF_INET
587 This designates the Internet namespace and associated family of
591 A socket address for the Internet namespace includes the following components:
595 The address of the machine you want to connect to. Internet addresses
596 can be specified in several ways; these are discussed in @ref{Internet
597 Address Formats}, @ref{Host Addresses}, and @ref{Host Names}.
600 A port number for that machine. @xref{Ports}.
603 You must ensure that the address and port number are represented in a
604 canonical format called @dfn{network byte order}. @xref{Byte Order},
605 for information about this.
608 * Internet Address Formats:: How socket addresses are specified in the
610 * Host Addresses:: All about host addresses of internet host.
611 * Protocols Database:: Referring to protocols by name.
612 * Ports:: Internet port numbers.
613 * Services Database:: Ports may have symbolic names.
614 * Byte Order:: Different hosts may use different byte
615 ordering conventions; you need to
616 canonicalize host address and port number.
617 * Inet Example:: Putting it all together.
620 @node Internet Address Formats
621 @subsection Internet Socket Address Formats
623 In the Internet namespace, for both IPv4 (@code{AF_INET}) and IPv6
624 (@code{AF_INET6}), a socket address consists of a host address
625 and a port on that host. In addition, the protocol you choose serves
626 effectively as a part of the address because local port numbers are
627 meaningful only within a particular protocol.
629 The data types for representing socket addresses in the Internet namespace
630 are defined in the header file @file{netinet/in.h}.
633 @comment netinet/in.h
635 @deftp {Data Type} {struct sockaddr_in}
636 This is the data type used to represent socket addresses in the
637 Internet namespace. It has the following members:
640 @item short int sin_family
641 This identifies the address family or format of the socket address.
642 You should store the value of @code{AF_INET} in this member.
643 @xref{Socket Addresses}.
645 @item struct in_addr sin_addr
646 This is the Internet address of the host machine. @xref{Host
647 Addresses}, and @ref{Host Names}, for how to get a value to store
650 @item unsigned short int sin_port
651 This is the port number. @xref{Ports}.
655 When you call @code{bind} or @code{getsockname}, you should specify
656 @code{sizeof (struct sockaddr_in)} as the @var{length} parameter if
657 you are using an Internet namespace socket address.
659 @deftp {Data Type} {struct sockaddr_in6}
660 This is the data type used to represent socket addresses in the IPv6
661 namespace. It has the following members:
664 @item short int sin6_family
665 This identifies the address family or format of the socket address.
666 You should store the value of @code{AF_INET6} in this member.
667 @xref{Socket Addresses}.
669 @item struct in6_addr sin6_addr
670 This is the IPv6 address of the host machine. @xref{Host
671 Addresses}, and @ref{Host Names}, for how to get a value to store
674 @item uint32_t sin6_flowinfo
675 This is a currently unimplemented field.
677 @item uint16_t sin6_port
678 This is the port number. @xref{Ports}.
684 @subsection Host Addresses
686 Each computer on the Internet has one or more @dfn{Internet addresses},
687 numbers which identify that computer among all those on the Internet.
688 Users typically write IPv4 numeric host addresses as sequences of four
689 numbers, separated by periods, as in @samp{128.52.46.32}, and IPv6
690 numeric host addresses as sequences of up to eight numbers separated by
691 colons, as in @samp{5f03:1200:836f:c100::1}.
693 Each computer also has one or more @dfn{host names}, which are strings
694 of words separated by periods, as in @samp{churchy.gnu.ai.mit.edu}.
696 Programs that let the user specify a host typically accept both numeric
697 addresses and host names. But the program needs a numeric address to
698 open a connection; to use a host name, you must convert it to the
699 numeric address it stands for.
702 * Abstract Host Addresses:: What a host number consists of.
703 * Data type: Host Address Data Type. Data type for a host number.
704 * Functions: Host Address Functions. Functions to operate on them.
705 * Names: Host Names. Translating host names to host numbers.
708 @node Abstract Host Addresses
709 @subsubsection Internet Host Addresses
710 @cindex host address, Internet
711 @cindex Internet host address
714 Each computer on the Internet has one or more Internet addresses,
715 numbers which identify that computer among all those on the Internet.
718 @c I think this whole section could possibly be removed. It is slightly
719 @c misleading these days.
721 @cindex network number
722 @cindex local network address number
723 An Internet host address is a number containing four bytes of data.
724 These are divided into two parts, a @dfn{network number} and a
725 @dfn{local network address number} within that network. The network
726 number consists of the first one, two or three bytes; the rest of the
727 bytes are the local address.
729 Network numbers are registered with the Network Information Center
730 (NIC), and are divided into three classes---A, B, and C. The local
731 network address numbers of individual machines are registered with the
732 administrator of the particular network.
734 Class A networks have single-byte numbers in the range 0 to 127. There
735 are only a small number of Class A networks, but they can each support a
736 very large number of hosts. Medium-sized Class B networks have two-byte
737 network numbers, with the first byte in the range 128 to 191. Class C
738 networks are the smallest; they have three-byte network numbers, with
739 the first byte in the range 192-255. Thus, the first 1, 2, or 3 bytes
740 of an Internet address specifies a network. The remaining bytes of the
741 Internet address specify the address within that network.
743 The Class A network 0 is reserved for broadcast to all networks. In
744 addition, the host number 0 within each network is reserved for broadcast
745 to all hosts in that network.
747 The Class A network 127 is reserved for loopback; you can always use
748 the Internet address @samp{127.0.0.1} to refer to the host machine.
750 Since a single machine can be a member of multiple networks, it can
751 have multiple Internet host addresses. However, there is never
752 supposed to be more than one machine with the same host address.
754 @c !!! this section could document the IN_CLASS* macros in <netinet/in.h>.
756 @cindex standard dot notation, for Internet addresses
757 @cindex dot notation, for Internet addresses
758 There are four forms of the @dfn{standard numbers-and-dots notation}
759 for Internet addresses:
762 @item @var{a}.@var{b}.@var{c}.@var{d}
763 This specifies all four bytes of the address individually.
765 @item @var{a}.@var{b}.@var{c}
766 The last part of the address, @var{c}, is interpreted as a 2-byte quantity.
767 This is useful for specifying host addresses in a Class B network with
768 network address number @code{@var{a}.@var{b}}.
770 @item @var{a}.@var{b}
771 The last part of the address, @var{c}, is interpreted as a 3-byte quantity.
772 This is useful for specifying host addresses in a Class A network with
773 network address number @var{a}.
776 If only one part is given, this corresponds directly to the host address
780 Within each part of the address, the usual C conventions for specifying
781 the radix apply. In other words, a leading @samp{0x} or @samp{0X} implies
782 hexadecimal radix; a leading @samp{0} implies octal; and otherwise decimal
785 @node Host Address Data Type
786 @subsubsection Host Address Data Type
788 Internet host addresses are represented in some contexts as integers
789 (type @code{unsigned long int}). In other contexts, the integer is
790 packaged inside a structure of type @code{struct in_addr}. It would
791 be better if the usage were made consistent, but it is not hard to extract
792 the integer from the structure or put the integer into a structure.
794 The following basic definitions for Internet addresses appear in the
795 header file @file{netinet/in.h}:
798 @comment netinet/in.h
800 @deftp {Data Type} {struct in_addr}
801 This data type is used in certain contexts to contain an Internet host
802 address. It has just one field, named @code{s_addr}, which records the
803 host address number as an @code{unsigned long int}.
806 @comment netinet/in.h
808 @deftypevr Macro {unsigned int} INADDR_LOOPBACK
809 You can use this constant to stand for ``the address of this machine,''
810 instead of finding its actual address. It is the Internet address
811 @samp{127.0.0.1}, which is usually called @samp{localhost}. This
812 special constant saves you the trouble of looking up the address of your
813 own machine. Also, the system usually implements @code{INADDR_LOOPBACK}
814 specially, avoiding any network traffic for the case of one machine
818 @comment netinet/in.h
820 @deftypevr Macro {unsigned int} INADDR_ANY
821 You can use this constant to stand for ``any incoming address,'' when
822 binding to an address. @xref{Setting Address}. This is the usual
823 address to give in the @code{sin_addr} member of @w{@code{struct
824 sockaddr_in}} when you want to accept Internet connections.
827 @comment netinet/in.h
829 @deftypevr Macro {unsigned int} INADDR_BROADCAST
830 This constant is the address you use to send a broadcast message.
831 @c !!! broadcast needs further documented
834 @comment netinet/in.h
836 @deftypevr Macro {unsigned int} INADDR_NONE
837 This constant is returned by some functions to indicate an error.
840 @comment netinet/in.h
841 @comment IPv6 basic API
842 @deftp {Data Type} {struct in6_addr}
843 This data type is used to store an IPv6 address. It stores 128 bits of
844 data, which can be accessed (via a union) in a variety of ways.
847 @comment netinet/in.h
848 @comment IPv6 basic API
849 @deftypevr Constant {struct in6_addr} in6addr_loopback.
850 This constant is the IPv6 address @samp{::1}, the loopback address. See
851 above for a description of what this means. The macro
852 @code{IN6ADDR_LOOPBACK_INIT} is provided to allow you to initialise your
853 own variables to this value.
856 @comment netinet/in.h
857 @comment IPv6 basic API
858 @deftypevr Constant {struct in6_addr} in6addr_any
859 This constant is the IPv6 address @samp{::}, the unspecified address. See
860 above for a description of what this means. The macro
861 @code{IN6ADDR_ANY_INIT} is provided to allow you to initialise your
862 own variables to this value.
865 @node Host Address Functions
866 @subsubsection Host Address Functions
869 These additional functions for manipulating Internet addresses are
870 declared in @file{arpa/inet.h}. They represent Internet addresses in
871 network byte order; they represent network numbers and
872 local-address-within-network numbers in host byte order.
873 @xref{Byte Order}, for an explanation of network and host byte order.
877 @deftypefun int inet_aton (const char *@var{name}, struct in_addr *@var{addr})
878 This function converts the Internet host address @var{name}
879 from the standard numbers-and-dots notation into binary data and stores
880 it in the @code{struct in_addr} that @var{addr} points to.
881 @code{inet_aton} returns nonzero if the address is valid, zero if not.
886 @deftypefun {unsigned long int} inet_addr (const char *@var{name})
887 This function converts the Internet host address @var{name} from the
888 standard numbers-and-dots notation into binary data. If the input is
889 not valid, @code{inet_addr} returns @code{INADDR_NONE}. This is an
890 obsolete interface to @code{inet_aton}, described immediately above; it
891 is obsolete because @code{INADDR_NONE} is a valid address
892 (255.255.255.255), and @code{inet_aton} provides a cleaner way to
893 indicate error return.
898 @deftypefun {unsigned long int} inet_network (const char *@var{name})
899 This function extracts the network number from the address @var{name},
900 given in the standard numbers-and-dots notation. The returned address is
901 in host order. If the input is not valid, @code{inet_network} returns
907 @deftypefun {char *} inet_ntoa (struct in_addr @var{addr})
908 This function converts the Internet host address @var{addr} to a
909 string in the standard numbers-and-dots notation. The return value is
910 a pointer into a statically-allocated buffer. Subsequent calls will
911 overwrite the same buffer, so you should copy the string if you need
914 In multi-threaded programs each thread has an own statically-allocated
915 buffer. But still subsequent calls of @code{inet_ntoa} in the same
916 thread will overwrite the result of the last call.
921 @deftypefun {struct in_addr} inet_makeaddr (int @var{net}, int @var{local})
922 This function makes an Internet host address by combining the network
923 number @var{net} with the local-address-within-network number
929 @deftypefun int inet_lnaof (struct in_addr @var{addr})
930 This function returns the local-address-within-network part of the
931 Internet host address @var{addr}.
936 @deftypefun int inet_netof (struct in_addr @var{addr})
937 This function returns the network number part of the Internet host
942 @comment IPv6 basic API
943 @deftypefun int inet_pton (int @var{af}, const char *@var{cp}, void *@var{buf})
944 This function converts an Internet address (either IPv4 or IPv6) from
945 presentation (textual) to network (binary) format. @var{af} should be
946 either @code{AF_INET} or @code{AF_INET6}, as appropriate for the type of
947 address being converted. @var{cp} is a pointer to the input string, and
948 @var{buf} is a pointer to a buffer for the result. It is the caller's
949 responsibility to make sure the buffer is large enough.
953 @comment IPv6 basic API
954 @deftypefun {char *} inet_ntop (int @var{af}, const void *@var{cp}, char *@var{buf}, size_t @var{len})
955 This function converts an Internet address (either IPv4 or IPv6) from
956 network (binary) to presentation (textual) form. @var{af} should be
957 either @code{AF_INET} or @code{AF_INET6}, as appropriate. @var{cp} is a
958 pointer to the address to be converted. @var{buf} should be a pointer
959 to a buffer to hold the result, and @var{len} is the length of this
960 buffer. The return value from the function will be this buffer address.
964 @subsubsection Host Names
965 @cindex hosts database
966 @cindex converting host name to address
967 @cindex converting host address to name
969 Besides the standard numbers-and-dots notation for Internet addresses,
970 you can also refer to a host by a symbolic name. The advantage of a
971 symbolic name is that it is usually easier to remember. For example,
972 the machine with Internet address @samp{128.52.46.32} is also known as
973 @samp{churchy.gnu.ai.mit.edu}; and other machines in the @samp{gnu.ai.mit.edu}
974 domain can refer to it simply as @samp{churchy}.
978 Internally, the system uses a database to keep track of the mapping
979 between host names and host numbers. This database is usually either
980 the file @file{/etc/hosts} or an equivalent provided by a name server.
981 The functions and other symbols for accessing this database are declared
982 in @file{netdb.h}. They are BSD features, defined unconditionally if
983 you include @file{netdb.h}.
987 @deftp {Data Type} {struct hostent}
988 This data type is used to represent an entry in the hosts database. It
989 has the following members:
993 This is the ``official'' name of the host.
995 @item char **h_aliases
996 These are alternative names for the host, represented as a null-terminated
1000 This is the host address type; in practice, its value is always either
1001 @code{AF_INET} or @code{AF_INET6}, with the latter being used for IPv6
1002 hosts. In principle other kinds of addresses could be represented in
1003 the data base as well as Internet addresses; if this were done, you
1004 might find a value in this field other than @code{AF_INET} or
1005 @code{AF_INET6}. @xref{Socket Addresses}.
1008 This is the length, in bytes, of each address.
1010 @item char **h_addr_list
1011 This is the vector of addresses for the host. (Recall that the host
1012 might be connected to multiple networks and have different addresses on
1013 each one.) The vector is terminated by a null pointer.
1016 This is a synonym for @code{h_addr_list[0]}; in other words, it is the
1021 As far as the host database is concerned, each address is just a block
1022 of memory @code{h_length} bytes long. But in other contexts there is an
1023 implicit assumption that you can convert this to a @code{struct in_addr} or
1024 an @code{unsigned long int}. Host addresses in a @code{struct hostent}
1025 structure are always given in network byte order; see @ref{Byte Order}.
1027 You can use @code{gethostbyname}, @code{gethostbyname2} or
1028 @code{gethostbyaddr} to search the hosts database for information about
1029 a particular host. The information is returned in a
1030 statically-allocated structure; you must copy the information if you
1031 need to save it across calls. You can also use @code{getaddrinfo} and
1032 @code{getnameinfo} to obtain this information.
1036 @deftypefun {struct hostent *} gethostbyname (const char *@var{name})
1037 The @code{gethostbyname} function returns information about the host
1038 named @var{name}. If the lookup fails, it returns a null pointer.
1042 @comment IPv6 Basic API
1043 @deftypefun {struct hostent *} gethostbyname2 (const char *@var{name}, int @var{af})
1044 The @code{gethostbyname2} function is like @code{gethostbyname}, but
1045 allows the caller to specify the desired address family (e.g.@:
1046 @code{AF_INET} or @code{AF_INET6}) for the result.
1051 @deftypefun {struct hostent *} gethostbyaddr (const char *@var{addr}, int @var{length}, int @var{format})
1052 The @code{gethostbyaddr} function returns information about the host
1053 with Internet address @var{addr}. The @var{length} argument is the
1054 size (in bytes) of the address at @var{addr}. @var{format} specifies
1055 the address format; for an Internet address, specify a value of
1058 If the lookup fails, @code{gethostbyaddr} returns a null pointer.
1062 If the name lookup by @code{gethostbyname} or @code{gethostbyaddr}
1063 fails, you can find out the reason by looking at the value of the
1064 variable @code{h_errno}. (It would be cleaner design for these
1065 functions to set @code{errno}, but use of @code{h_errno} is compatible
1066 with other systems.) Before using @code{h_errno}, you must declare it
1073 Here are the error codes that you may find in @code{h_errno}:
1078 @item HOST_NOT_FOUND
1079 @vindex HOST_NOT_FOUND
1080 No such host is known in the data base.
1086 This condition happens when the name server could not be contacted. If
1087 you try again later, you may succeed then.
1093 A non-recoverable error occurred.
1099 The host database contains an entry for the name, but it doesn't have an
1100 associated Internet address.
1103 You can also scan the entire hosts database one entry at a time using
1104 @code{sethostent}, @code{gethostent}, and @code{endhostent}. Be careful
1105 in using these functions, because they are not reentrant.
1109 @deftypefun void sethostent (int @var{stayopen})
1110 This function opens the hosts database to begin scanning it. You can
1111 then call @code{gethostent} to read the entries.
1113 @c There was a rumor that this flag has different meaning if using the DNS,
1114 @c but it appears this description is accurate in that case also.
1115 If the @var{stayopen} argument is nonzero, this sets a flag so that
1116 subsequent calls to @code{gethostbyname} or @code{gethostbyaddr} will
1117 not close the database (as they usually would). This makes for more
1118 efficiency if you call those functions several times, by avoiding
1119 reopening the database for each call.
1124 @deftypefun {struct hostent *} gethostent ()
1125 This function returns the next entry in the hosts database. It
1126 returns a null pointer if there are no more entries.
1131 @deftypefun void endhostent ()
1132 This function closes the hosts database.
1136 @subsection Internet Ports
1139 A socket address in the Internet namespace consists of a machine's
1140 Internet address plus a @dfn{port number} which distinguishes the
1141 sockets on a given machine (for a given protocol). Port numbers range
1144 Port numbers less than @code{IPPORT_RESERVED} are reserved for standard
1145 servers, such as @code{finger} and @code{telnet}. There is a database
1146 that keeps track of these, and you can use the @code{getservbyname}
1147 function to map a service name onto a port number; see @ref{Services
1150 If you write a server that is not one of the standard ones defined in
1151 the database, you must choose a port number for it. Use a number
1152 greater than @code{IPPORT_USERRESERVED}; such numbers are reserved for
1153 servers and won't ever be generated automatically by the system.
1154 Avoiding conflicts with servers being run by other users is up to you.
1156 When you use a socket without specifying its address, the system
1157 generates a port number for it. This number is between
1158 @code{IPPORT_RESERVED} and @code{IPPORT_USERRESERVED}.
1160 On the Internet, it is actually legitimate to have two different
1161 sockets with the same port number, as long as they never both try to
1162 communicate with the same socket address (host address plus port
1163 number). You shouldn't duplicate a port number except in special
1164 circumstances where a higher-level protocol requires it. Normally,
1165 the system won't let you do it; @code{bind} normally insists on
1166 distinct port numbers. To reuse a port number, you must set the
1167 socket option @code{SO_REUSEADDR}. @xref{Socket-Level Options}.
1169 @pindex netinet/in.h
1170 These macros are defined in the header file @file{netinet/in.h}.
1172 @comment netinet/in.h
1174 @deftypevr Macro int IPPORT_RESERVED
1175 Port numbers less than @code{IPPORT_RESERVED} are reserved for
1179 @comment netinet/in.h
1181 @deftypevr Macro int IPPORT_USERRESERVED
1182 Port numbers greater than or equal to @code{IPPORT_USERRESERVED} are
1183 reserved for explicit use; they will never be allocated automatically.
1186 @node Services Database
1187 @subsection The Services Database
1188 @cindex services database
1189 @cindex converting service name to port number
1190 @cindex converting port number to service name
1192 @pindex /etc/services
1193 The database that keeps track of ``well-known'' services is usually
1194 either the file @file{/etc/services} or an equivalent from a name server.
1195 You can use these utilities, declared in @file{netdb.h}, to access
1196 the services database.
1201 @deftp {Data Type} {struct servent}
1202 This data type holds information about entries from the services database.
1203 It has the following members:
1207 This is the ``official'' name of the service.
1209 @item char **s_aliases
1210 These are alternate names for the service, represented as an array of
1211 strings. A null pointer terminates the array.
1214 This is the port number for the service. Port numbers are given in
1215 network byte order; see @ref{Byte Order}.
1218 This is the name of the protocol to use with this service.
1219 @xref{Protocols Database}.
1223 To get information about a particular service, use the
1224 @code{getservbyname} or @code{getservbyport} functions. The information
1225 is returned in a statically-allocated structure; you must copy the
1226 information if you need to save it across calls.
1230 @deftypefun {struct servent *} getservbyname (const char *@var{name}, const char *@var{proto})
1231 The @code{getservbyname} function returns information about the
1232 service named @var{name} using protocol @var{proto}. If it can't find
1233 such a service, it returns a null pointer.
1235 This function is useful for servers as well as for clients; servers
1236 use it to determine which port they should listen on (@pxref{Listening}).
1241 @deftypefun {struct servent *} getservbyport (int @var{port}, const char *@var{proto})
1242 The @code{getservbyport} function returns information about the
1243 service at port @var{port} using protocol @var{proto}. If it can't
1244 find such a service, it returns a null pointer.
1247 You can also scan the services database using @code{setservent},
1248 @code{getservent}, and @code{endservent}. Be careful in using these
1249 functions, because they are not reentrant.
1253 @deftypefun void setservent (int @var{stayopen})
1254 This function opens the services database to begin scanning it.
1256 If the @var{stayopen} argument is nonzero, this sets a flag so that
1257 subsequent calls to @code{getservbyname} or @code{getservbyport} will
1258 not close the database (as they usually would). This makes for more
1259 efficiency if you call those functions several times, by avoiding
1260 reopening the database for each call.
1265 @deftypefun {struct servent *} getservent (void)
1266 This function returns the next entry in the services database. If
1267 there are no more entries, it returns a null pointer.
1272 @deftypefun void endservent (void)
1273 This function closes the services database.
1277 @subsection Byte Order Conversion
1278 @cindex byte order conversion, for socket
1279 @cindex converting byte order
1282 @cindex little-endian
1283 Different kinds of computers use different conventions for the
1284 ordering of bytes within a word. Some computers put the most
1285 significant byte within a word first (this is called ``big-endian''
1286 order), and others put it last (``little-endian'' order).
1288 @cindex network byte order
1289 So that machines with different byte order conventions can
1290 communicate, the Internet protocols specify a canonical byte order
1291 convention for data transmitted over the network. This is known
1292 as the @dfn{network byte order}.
1294 When establishing an Internet socket connection, you must make sure that
1295 the data in the @code{sin_port} and @code{sin_addr} members of the
1296 @code{sockaddr_in} structure are represented in the network byte order.
1297 If you are encoding integer data in the messages sent through the
1298 socket, you should convert this to network byte order too. If you don't
1299 do this, your program may fail when running on or talking to other kinds
1302 If you use @code{getservbyname} and @code{gethostbyname} or
1303 @code{inet_addr} to get the port number and host address, the values are
1304 already in the network byte order, and you can copy them directly into
1305 the @code{sockaddr_in} structure.
1307 Otherwise, you have to convert the values explicitly. Use
1308 @code{htons} and @code{ntohs} to convert values for the @code{sin_port}
1309 member. Use @code{htonl} and @code{ntohl} to convert values for the
1310 @code{sin_addr} member. (Remember, @code{struct in_addr} is equivalent
1311 to @code{unsigned long int}.) These functions are declared in
1312 @file{netinet/in.h}.
1313 @pindex netinet/in.h
1315 @comment netinet/in.h
1317 @deftypefun {unsigned short int} htons (unsigned short int @var{hostshort})
1318 This function converts the @code{short} integer @var{hostshort} from
1319 host byte order to network byte order.
1322 @comment netinet/in.h
1324 @deftypefun {unsigned short int} ntohs (unsigned short int @var{netshort})
1325 This function converts the @code{short} integer @var{netshort} from
1326 network byte order to host byte order.
1329 @comment netinet/in.h
1331 @deftypefun {unsigned long int} htonl (unsigned long int @var{hostlong})
1332 This function converts the @code{long} integer @var{hostlong} from
1333 host byte order to network byte order.
1336 @comment netinet/in.h
1338 @deftypefun {unsigned long int} ntohl (unsigned long int @var{netlong})
1339 This function converts the @code{long} integer @var{netlong} from
1340 network byte order to host byte order.
1343 @node Protocols Database
1344 @subsection Protocols Database
1345 @cindex protocols database
1347 The communications protocol used with a socket controls low-level
1348 details of how data is exchanged. For example, the protocol implements
1349 things like checksums to detect errors in transmissions, and routing
1350 instructions for messages. Normal user programs have little reason to
1351 mess with these details directly.
1353 @cindex TCP (Internet protocol)
1354 The default communications protocol for the Internet namespace depends on
1355 the communication style. For stream communication, the default is TCP
1356 (``transmission control protocol''). For datagram communication, the
1357 default is UDP (``user datagram protocol''). For reliable datagram
1358 communication, the default is RDP (``reliable datagram protocol'').
1359 You should nearly always use the default.
1361 @pindex /etc/protocols
1362 Internet protocols are generally specified by a name instead of a
1363 number. The network protocols that a host knows about are stored in a
1364 database. This is usually either derived from the file
1365 @file{/etc/protocols}, or it may be an equivalent provided by a name
1366 server. You look up the protocol number associated with a named
1367 protocol in the database using the @code{getprotobyname} function.
1369 Here are detailed descriptions of the utilities for accessing the
1370 protocols database. These are declared in @file{netdb.h}.
1375 @deftp {Data Type} {struct protoent}
1376 This data type is used to represent entries in the network protocols
1377 database. It has the following members:
1381 This is the official name of the protocol.
1383 @item char **p_aliases
1384 These are alternate names for the protocol, specified as an array of
1385 strings. The last element of the array is a null pointer.
1388 This is the protocol number (in host byte order); use this member as the
1389 @var{protocol} argument to @code{socket}.
1393 You can use @code{getprotobyname} and @code{getprotobynumber} to search
1394 the protocols database for a specific protocol. The information is
1395 returned in a statically-allocated structure; you must copy the
1396 information if you need to save it across calls.
1400 @deftypefun {struct protoent *} getprotobyname (const char *@var{name})
1401 The @code{getprotobyname} function returns information about the
1402 network protocol named @var{name}. If there is no such protocol, it
1403 returns a null pointer.
1408 @deftypefun {struct protoent *} getprotobynumber (int @var{protocol})
1409 The @code{getprotobynumber} function returns information about the
1410 network protocol with number @var{protocol}. If there is no such
1411 protocol, it returns a null pointer.
1414 You can also scan the whole protocols database one protocol at a time by
1415 using @code{setprotoent}, @code{getprotoent}, and @code{endprotoent}.
1416 Be careful in using these functions, because they are not reentrant.
1420 @deftypefun void setprotoent (int @var{stayopen})
1421 This function opens the protocols database to begin scanning it.
1423 If the @var{stayopen} argument is nonzero, this sets a flag so that
1424 subsequent calls to @code{getprotobyname} or @code{getprotobynumber} will
1425 not close the database (as they usually would). This makes for more
1426 efficiency if you call those functions several times, by avoiding
1427 reopening the database for each call.
1432 @deftypefun {struct protoent *} getprotoent (void)
1433 This function returns the next entry in the protocols database. It
1434 returns a null pointer if there are no more entries.
1439 @deftypefun void endprotoent (void)
1440 This function closes the protocols database.
1444 @subsection Internet Socket Example
1446 Here is an example showing how to create and name a socket in the
1447 Internet namespace. The newly created socket exists on the machine that
1448 the program is running on. Rather than finding and using the machine's
1449 Internet address, this example specifies @code{INADDR_ANY} as the host
1450 address; the system replaces that with the machine's actual address.
1453 @include mkisock.c.texi
1456 Here is another example, showing how you can fill in a @code{sockaddr_in}
1457 structure, given a host name string and a port number:
1460 @include isockad.c.texi
1463 @node Misc Namespaces
1464 @section Other Namespaces
1471 Certain other namespaces and associated protocol families are supported
1472 but not documented yet because they are not often used. @code{PF_NS}
1473 refers to the Xerox Network Software protocols. @code{PF_ISO} stands
1474 for Open Systems Interconnect. @code{PF_CCITT} refers to protocols from
1475 CCITT. @file{socket.h} defines these symbols and others naming protocols
1476 not actually implemented.
1478 @code{PF_IMPLINK} is used for communicating between hosts and Internet
1479 Message Processors. For information on this, and on @code{PF_ROUTE}, an
1480 occasionally-used local area routing protocol, see the GNU Hurd Manual
1481 (to appear in the future).
1483 @node Open/Close Sockets
1484 @section Opening and Closing Sockets
1486 This section describes the actual library functions for opening and
1487 closing sockets. The same functions work for all namespaces and
1491 * Creating a Socket:: How to open a socket.
1492 * Closing a Socket:: How to close a socket.
1493 * Socket Pairs:: These are created like pipes.
1496 @node Creating a Socket
1497 @subsection Creating a Socket
1498 @cindex creating a socket
1499 @cindex socket, creating
1500 @cindex opening a socket
1502 The primitive for creating a socket is the @code{socket} function,
1503 declared in @file{sys/socket.h}.
1504 @pindex sys/socket.h
1506 @comment sys/socket.h
1508 @deftypefun int socket (int @var{namespace}, int @var{style}, int @var{protocol})
1509 This function creates a socket and specifies communication style
1510 @var{style}, which should be one of the socket styles listed in
1511 @ref{Communication Styles}. The @var{namespace} argument specifies
1512 the namespace; it must be @code{PF_FILE} (@pxref{File Namespace}) or
1513 @code{PF_INET} (@pxref{Internet Namespace}). @var{protocol}
1514 designates the specific protocol (@pxref{Socket Concepts}); zero is
1515 usually right for @var{protocol}.
1517 The return value from @code{socket} is the file descriptor for the new
1518 socket, or @code{-1} in case of error. The following @code{errno} error
1519 conditions are defined for this function:
1522 @item EPROTONOSUPPORT
1523 The @var{protocol} or @var{style} is not supported by the
1524 @var{namespace} specified.
1527 The process already has too many file descriptors open.
1530 The system already has too many file descriptors open.
1533 The process does not have privilege to create a socket of the specified
1534 @var{style} or @var{protocol}.
1537 The system ran out of internal buffer space.
1540 The file descriptor returned by the @code{socket} function supports both
1541 read and write operations. But, like pipes, sockets do not support file
1542 positioning operations.
1545 For examples of how to call the @code{socket} function,
1546 see @ref{File Namespace}, or @ref{Inet Example}.
1549 @node Closing a Socket
1550 @subsection Closing a Socket
1551 @cindex socket, closing
1552 @cindex closing a socket
1553 @cindex shutting down a socket
1554 @cindex socket shutdown
1556 When you are finished using a socket, you can simply close its
1557 file descriptor with @code{close}; see @ref{Opening and Closing Files}.
1558 If there is still data waiting to be transmitted over the connection,
1559 normally @code{close} tries to complete this transmission. You
1560 can control this behavior using the @code{SO_LINGER} socket option to
1561 specify a timeout period; see @ref{Socket Options}.
1563 @pindex sys/socket.h
1564 You can also shut down only reception or only transmission on a
1565 connection by calling @code{shutdown}, which is declared in
1566 @file{sys/socket.h}.
1568 @comment sys/socket.h
1570 @deftypefun int shutdown (int @var{socket}, int @var{how})
1571 The @code{shutdown} function shuts down the connection of socket
1572 @var{socket}. The argument @var{how} specifies what action to
1577 Stop receiving data for this socket. If further data arrives,
1581 Stop trying to transmit data from this socket. Discard any data
1582 waiting to be sent. Stop looking for acknowledgement of data already
1583 sent; don't retransmit it if it is lost.
1586 Stop both reception and transmission.
1589 The return value is @code{0} on success and @code{-1} on failure. The
1590 following @code{errno} error conditions are defined for this function:
1594 @var{socket} is not a valid file descriptor.
1597 @var{socket} is not a socket.
1600 @var{socket} is not connected.
1605 @subsection Socket Pairs
1606 @cindex creating a socket pair
1608 @cindex opening a socket pair
1610 @pindex sys/socket.h
1611 A @dfn{socket pair} consists of a pair of connected (but unnamed)
1612 sockets. It is very similar to a pipe and is used in much the same
1613 way. Socket pairs are created with the @code{socketpair} function,
1614 declared in @file{sys/socket.h}. A socket pair is much like a pipe; the
1615 main difference is that the socket pair is bidirectional, whereas the
1616 pipe has one input-only end and one output-only end (@pxref{Pipes and
1619 @comment sys/socket.h
1621 @deftypefun int socketpair (int @var{namespace}, int @var{style}, int @var{protocol}, int @var{filedes}@t{[2]})
1622 This function creates a socket pair, returning the file descriptors in
1623 @code{@var{filedes}[0]} and @code{@var{filedes}[1]}. The socket pair
1624 is a full-duplex communications channel, so that both reading and writing
1625 may be performed at either end.
1627 The @var{namespace}, @var{style}, and @var{protocol} arguments are
1628 interpreted as for the @code{socket} function. @var{style} should be
1629 one of the communication styles listed in @ref{Communication Styles}.
1630 The @var{namespace} argument specifies the namespace, which must be
1631 @code{AF_FILE} (@pxref{File Namespace}); @var{protocol} specifies the
1632 communications protocol, but zero is the only meaningful value.
1634 If @var{style} specifies a connectionless communication style, then
1635 the two sockets you get are not @emph{connected}, strictly speaking,
1636 but each of them knows the other as the default destination address,
1637 so they can send packets to each other.
1639 The @code{socketpair} function returns @code{0} on success and @code{-1}
1640 on failure. The following @code{errno} error conditions are defined
1645 The process has too many file descriptors open.
1648 The specified namespace is not supported.
1650 @item EPROTONOSUPPORT
1651 The specified protocol is not supported.
1654 The specified protocol does not support the creation of socket pairs.
1659 @section Using Sockets with Connections
1664 The most common communication styles involve making a connection to a
1665 particular other socket, and then exchanging data with that socket
1666 over and over. Making a connection is asymmetric; one side (the
1667 @dfn{client}) acts to request a connection, while the other side (the
1668 @dfn{server}) makes a socket and waits for the connection request.
1673 @ref{Connecting}, describes what the client program must do to
1674 initiate a connection with a server.
1677 @ref{Listening}, and @ref{Accepting Connections}, describe what the
1678 server program must do to wait for and act upon connection requests
1682 @ref{Transferring Data}, describes how data is transferred through the
1688 * Connecting:: What the client program must do.
1689 * Listening:: How a server program waits for requests.
1690 * Accepting Connections:: What the server does when it gets a request.
1691 * Who is Connected:: Getting the address of the
1692 other side of a connection.
1693 * Transferring Data:: How to send and receive data.
1694 * Byte Stream Example:: An example program: a client for communicating
1695 over a byte stream socket in the Internet namespace.
1696 * Server Example:: A corresponding server program.
1697 * Out-of-Band Data:: This is an advanced feature.
1701 @subsection Making a Connection
1702 @cindex connecting a socket
1703 @cindex socket, connecting
1704 @cindex socket, initiating a connection
1705 @cindex socket, client actions
1707 In making a connection, the client makes a connection while the server
1708 waits for and accepts the connection. Here we discuss what the client
1709 program must do, using the @code{connect} function, which is declared in
1710 @file{sys/socket.h}.
1712 @comment sys/socket.h
1714 @deftypefun int connect (int @var{socket}, struct sockaddr *@var{addr}, socklen_t @var{length})
1715 The @code{connect} function initiates a connection from the socket
1716 with file descriptor @var{socket} to the socket whose address is
1717 specified by the @var{addr} and @var{length} arguments. (This socket
1718 is typically on another machine, and it must be already set up as a
1719 server.) @xref{Socket Addresses}, for information about how these
1720 arguments are interpreted.
1722 Normally, @code{connect} waits until the server responds to the request
1723 before it returns. You can set nonblocking mode on the socket
1724 @var{socket} to make @code{connect} return immediately without waiting
1725 for the response. @xref{File Status Flags}, for information about
1727 @c !!! how do you tell when it has finished connecting? I suspect the
1728 @c way you do it is select for writing.
1730 The normal return value from @code{connect} is @code{0}. If an error
1731 occurs, @code{connect} returns @code{-1}. The following @code{errno}
1732 error conditions are defined for this function:
1736 The socket @var{socket} is not a valid file descriptor.
1739 File descriptor @var{socket} is not a socket.
1742 The specified address is not available on the remote machine.
1745 The namespace of the @var{addr} is not supported by this socket.
1748 The socket @var{socket} is already connected.
1751 The attempt to establish the connection timed out.
1754 The server has actively refused to establish the connection.
1757 The network of the given @var{addr} isn't reachable from this host.
1760 The socket address of the given @var{addr} is already in use.
1763 The socket @var{socket} is non-blocking and the connection could not be
1764 established immediately. You can determine when the connection is
1765 completely established with @code{select}; @pxref{Waiting for I/O}.
1766 Another @code{connect} call on the same socket, before the connection is
1767 completely established, will fail with @code{EALREADY}.
1770 The socket @var{socket} is non-blocking and already has a pending
1771 connection in progress (see @code{EINPROGRESS} above).
1774 This function is defined as a cancelation point in multi-threaded
1775 programs. So one has to be prepared for this and make sure that
1776 possibly allocated resources (like memory, files descriptors,
1777 semaphores or whatever) are freed even if the thread is cancel.
1778 @c @xref{pthread_cleanup_push}, for a method how to do this.
1782 @subsection Listening for Connections
1783 @cindex listening (sockets)
1784 @cindex sockets, server actions
1785 @cindex sockets, listening
1787 Now let us consider what the server process must do to accept
1788 connections on a socket. First it must use the @code{listen} function
1789 to enable connection requests on the socket, and then accept each
1790 incoming connection with a call to @code{accept} (@pxref{Accepting
1791 Connections}). Once connection requests are enabled on a server socket,
1792 the @code{select} function reports when the socket has a connection
1793 ready to be accepted (@pxref{Waiting for I/O}).
1795 The @code{listen} function is not allowed for sockets using
1796 connectionless communication styles.
1798 You can write a network server that does not even start running until a
1799 connection to it is requested. @xref{Inetd Servers}.
1801 In the Internet namespace, there are no special protection mechanisms
1802 for controlling access to connect to a port; any process on any machine
1803 can make a connection to your server. If you want to restrict access to
1804 your server, make it examine the addresses associated with connection
1805 requests or implement some other handshaking or identification
1808 In the File namespace, the ordinary file protection bits control who has
1809 access to connect to the socket.
1811 @comment sys/socket.h
1813 @deftypefun int listen (int @var{socket}, unsigned int @var{n})
1814 The @code{listen} function enables the socket @var{socket} to accept
1815 connections, thus making it a server socket.
1817 The argument @var{n} specifies the length of the queue for pending
1818 connections. When the queue fills, new clients attempting to connect
1819 fail with @code{ECONNREFUSED} until the server calls @code{accept} to
1820 accept a connection from the queue.
1822 The @code{listen} function returns @code{0} on success and @code{-1}
1823 on failure. The following @code{errno} error conditions are defined
1828 The argument @var{socket} is not a valid file descriptor.
1831 The argument @var{socket} is not a socket.
1834 The socket @var{socket} does not support this operation.
1838 @node Accepting Connections
1839 @subsection Accepting Connections
1840 @cindex sockets, accepting connections
1841 @cindex accepting connections
1843 When a server receives a connection request, it can complete the
1844 connection by accepting the request. Use the function @code{accept}
1847 A socket that has been established as a server can accept connection
1848 requests from multiple clients. The server's original socket
1849 @emph{does not become part} of the connection; instead, @code{accept}
1850 makes a new socket which participates in the connection.
1851 @code{accept} returns the descriptor for this socket. The server's
1852 original socket remains available for listening for further connection
1855 The number of pending connection requests on a server socket is finite.
1856 If connection requests arrive from clients faster than the server can
1857 act upon them, the queue can fill up and additional requests are refused
1858 with a @code{ECONNREFUSED} error. You can specify the maximum length of
1859 this queue as an argument to the @code{listen} function, although the
1860 system may also impose its own internal limit on the length of this
1863 @comment sys/socket.h
1865 @deftypefun int accept (int @var{socket}, struct sockaddr *@var{addr}, socklen_t *@var{length-ptr})
1866 This function is used to accept a connection request on the server
1867 socket @var{socket}.
1869 The @code{accept} function waits if there are no connections pending,
1870 unless the socket @var{socket} has nonblocking mode set. (You can use
1871 @code{select} to wait for a pending connection, with a nonblocking
1872 socket.) @xref{File Status Flags}, for information about nonblocking
1875 The @var{addr} and @var{length-ptr} arguments are used to return
1876 information about the name of the client socket that initiated the
1877 connection. @xref{Socket Addresses}, for information about the format
1880 Accepting a connection does not make @var{socket} part of the
1881 connection. Instead, it creates a new socket which becomes
1882 connected. The normal return value of @code{accept} is the file
1883 descriptor for the new socket.
1885 After @code{accept}, the original socket @var{socket} remains open and
1886 unconnected, and continues listening until you close it. You can
1887 accept further connections with @var{socket} by calling @code{accept}
1890 If an error occurs, @code{accept} returns @code{-1}. The following
1891 @code{errno} error conditions are defined for this function:
1895 The @var{socket} argument is not a valid file descriptor.
1898 The descriptor @var{socket} argument is not a socket.
1901 The descriptor @var{socket} does not support this operation.
1904 @var{socket} has nonblocking mode set, and there are no pending
1905 connections immediately available.
1908 This function is defined as a cancelation point in multi-threaded
1909 programs. So one has to be prepared for this and make sure that
1910 possibly allocated resources (like memory, files descriptors,
1911 semaphores or whatever) are freed even if the thread is cancel.
1912 @c @xref{pthread_cleanup_push}, for a method how to do this.
1915 The @code{accept} function is not allowed for sockets using
1916 connectionless communication styles.
1918 @node Who is Connected
1919 @subsection Who is Connected to Me?
1921 @comment sys/socket.h
1923 @deftypefun int getpeername (int @var{socket}, struct sockaddr *@var{addr}, size_t *@var{length-ptr})
1924 The @code{getpeername} function returns the address of the socket that
1925 @var{socket} is connected to; it stores the address in the memory space
1926 specified by @var{addr} and @var{length-ptr}. It stores the length of
1927 the address in @code{*@var{length-ptr}}.
1929 @xref{Socket Addresses}, for information about the format of the
1930 address. In some operating systems, @code{getpeername} works only for
1931 sockets in the Internet domain.
1933 The return value is @code{0} on success and @code{-1} on error. The
1934 following @code{errno} error conditions are defined for this function:
1938 The argument @var{socket} is not a valid file descriptor.
1941 The descriptor @var{socket} is not a socket.
1944 The socket @var{socket} is not connected.
1947 There are not enough internal buffers available.
1952 @node Transferring Data
1953 @subsection Transferring Data
1954 @cindex reading from a socket
1955 @cindex writing to a socket
1957 Once a socket has been connected to a peer, you can use the ordinary
1958 @code{read} and @code{write} operations (@pxref{I/O Primitives}) to
1959 transfer data. A socket is a two-way communications channel, so read
1960 and write operations can be performed at either end.
1962 There are also some I/O modes that are specific to socket operations.
1963 In order to specify these modes, you must use the @code{recv} and
1964 @code{send} functions instead of the more generic @code{read} and
1965 @code{write} functions. The @code{recv} and @code{send} functions take
1966 an additional argument which you can use to specify various flags to
1967 control the special I/O modes. For example, you can specify the
1968 @code{MSG_OOB} flag to read or write out-of-band data, the
1969 @code{MSG_PEEK} flag to peek at input, or the @code{MSG_DONTROUTE} flag
1970 to control inclusion of routing information on output.
1973 * Sending Data:: Sending data with @code{send}.
1974 * Receiving Data:: Reading data with @code{recv}.
1975 * Socket Data Options:: Using @code{send} and @code{recv}.
1979 @subsubsection Sending Data
1981 @pindex sys/socket.h
1982 The @code{send} function is declared in the header file
1983 @file{sys/socket.h}. If your @var{flags} argument is zero, you can just
1984 as well use @code{write} instead of @code{send}; see @ref{I/O
1985 Primitives}. If the socket was connected but the connection has broken,
1986 you get a @code{SIGPIPE} signal for any use of @code{send} or
1987 @code{write} (@pxref{Miscellaneous Signals}).
1989 @comment sys/socket.h
1991 @deftypefun int send (int @var{socket}, void *@var{buffer}, size_t @var{size}, int @var{flags})
1992 The @code{send} function is like @code{write}, but with the additional
1993 flags @var{flags}. The possible values of @var{flags} are described
1994 in @ref{Socket Data Options}.
1996 This function returns the number of bytes transmitted, or @code{-1} on
1997 failure. If the socket is nonblocking, then @code{send} (like
1998 @code{write}) can return after sending just part of the data.
1999 @xref{File Status Flags}, for information about nonblocking mode.
2001 Note, however, that a successful return value merely indicates that
2002 the message has been sent without error, not necessarily that it has
2003 been received without error.
2005 The following @code{errno} error conditions are defined for this function:
2009 The @var{socket} argument is not a valid file descriptor.
2012 The operation was interrupted by a signal before any data was sent.
2013 @xref{Interrupted Primitives}.
2016 The descriptor @var{socket} is not a socket.
2019 The socket type requires that the message be sent atomically, but the
2020 message is too large for this to be possible.
2023 Nonblocking mode has been set on the socket, and the write operation
2024 would block. (Normally @code{send} blocks until the operation can be
2028 There is not enough internal buffer space available.
2031 You never connected this socket.
2034 This socket was connected but the connection is now broken. In this
2035 case, @code{send} generates a @code{SIGPIPE} signal first; if that
2036 signal is ignored or blocked, or if its handler returns, then
2037 @code{send} fails with @code{EPIPE}.
2040 This function is defined as a cancelation point in multi-threaded
2041 programs. So one has to be prepared for this and make sure that
2042 possibly allocated resources (like memory, files descriptors,
2043 semaphores or whatever) are freed even if the thread is cancel.
2044 @c @xref{pthread_cleanup_push}, for a method how to do this.
2047 @node Receiving Data
2048 @subsubsection Receiving Data
2050 @pindex sys/socket.h
2051 The @code{recv} function is declared in the header file
2052 @file{sys/socket.h}. If your @var{flags} argument is zero, you can
2053 just as well use @code{read} instead of @code{recv}; see @ref{I/O
2056 @comment sys/socket.h
2058 @deftypefun int recv (int @var{socket}, void *@var{buffer}, size_t @var{size}, int @var{flags})
2059 The @code{recv} function is like @code{read}, but with the additional
2060 flags @var{flags}. The possible values of @var{flags} are described
2061 In @ref{Socket Data Options}.
2063 If nonblocking mode is set for @var{socket}, and no data is available to
2064 be read, @code{recv} fails immediately rather than waiting. @xref{File
2065 Status Flags}, for information about nonblocking mode.
2067 This function returns the number of bytes received, or @code{-1} on failure.
2068 The following @code{errno} error conditions are defined for this function:
2072 The @var{socket} argument is not a valid file descriptor.
2075 The descriptor @var{socket} is not a socket.
2078 Nonblocking mode has been set on the socket, and the read operation
2079 would block. (Normally, @code{recv} blocks until there is input
2080 available to be read.)
2083 The operation was interrupted by a signal before any data was read.
2084 @xref{Interrupted Primitives}.
2087 You never connected this socket.
2090 This function is defined as a cancelation point in multi-threaded
2091 programs. So one has to be prepared for this and make sure that
2092 possibly allocated resources (like memory, files descriptors,
2093 semaphores or whatever) are freed even if the thread is cancel.
2094 @c @xref{pthread_cleanup_push}, for a method how to do this.
2097 @node Socket Data Options
2098 @subsubsection Socket Data Options
2100 @pindex sys/socket.h
2101 The @var{flags} argument to @code{send} and @code{recv} is a bit
2102 mask. You can bitwise-OR the values of the following macros together
2103 to obtain a value for this argument. All are defined in the header
2104 file @file{sys/socket.h}.
2106 @comment sys/socket.h
2108 @deftypevr Macro int MSG_OOB
2109 Send or receive out-of-band data. @xref{Out-of-Band Data}.
2112 @comment sys/socket.h
2114 @deftypevr Macro int MSG_PEEK
2115 Look at the data but don't remove it from the input queue. This is
2116 only meaningful with input functions such as @code{recv}, not with
2120 @comment sys/socket.h
2122 @deftypevr Macro int MSG_DONTROUTE
2123 Don't include routing information in the message. This is only
2124 meaningful with output operations, and is usually only of interest for
2125 diagnostic or routing programs. We don't try to explain it here.
2128 @node Byte Stream Example
2129 @subsection Byte Stream Socket Example
2131 Here is an example client program that makes a connection for a byte
2132 stream socket in the Internet namespace. It doesn't do anything
2133 particularly interesting once it has connected to the server; it just
2134 sends a text string to the server and exits.
2137 @include inetcli.c.texi
2140 @node Server Example
2141 @subsection Byte Stream Connection Server Example
2143 The server end is much more complicated. Since we want to allow
2144 multiple clients to be connected to the server at the same time, it
2145 would be incorrect to wait for input from a single client by simply
2146 calling @code{read} or @code{recv}. Instead, the right thing to do is
2147 to use @code{select} (@pxref{Waiting for I/O}) to wait for input on
2148 all of the open sockets. This also allows the server to deal with
2149 additional connection requests.
2151 This particular server doesn't do anything interesting once it has
2152 gotten a message from a client. It does close the socket for that
2153 client when it detects an end-of-file condition (resulting from the
2154 client shutting down its end of the connection).
2156 This program uses @code{make_socket} and @code{init_sockaddr} to set
2157 up the socket address; see @ref{Inet Example}.
2160 @include inetsrv.c.texi
2163 @node Out-of-Band Data
2164 @subsection Out-of-Band Data
2166 @cindex out-of-band data
2167 @cindex high-priority data
2168 Streams with connections permit @dfn{out-of-band} data that is
2169 delivered with higher priority than ordinary data. Typically the
2170 reason for sending out-of-band data is to send notice of an
2171 exceptional condition. The way to send out-of-band data is using
2172 @code{send}, specifying the flag @code{MSG_OOB} (@pxref{Sending
2175 Out-of-band data is received with higher priority because the
2176 receiving process need not read it in sequence; to read the next
2177 available out-of-band data, use @code{recv} with the @code{MSG_OOB}
2178 flag (@pxref{Receiving Data}). Ordinary read operations do not read
2179 out-of-band data; they read only the ordinary data.
2181 @cindex urgent socket condition
2182 When a socket finds that out-of-band data is on its way, it sends a
2183 @code{SIGURG} signal to the owner process or process group of the
2184 socket. You can specify the owner using the @code{F_SETOWN} command
2185 to the @code{fcntl} function; see @ref{Interrupt Input}. You must
2186 also establish a handler for this signal, as described in @ref{Signal
2187 Handling}, in order to take appropriate action such as reading the
2190 Alternatively, you can test for pending out-of-band data, or wait
2191 until there is out-of-band data, using the @code{select} function; it
2192 can wait for an exceptional condition on the socket. @xref{Waiting
2193 for I/O}, for more information about @code{select}.
2195 Notification of out-of-band data (whether with @code{SIGURG} or with
2196 @code{select}) indicates that out-of-band data is on the way; the data
2197 may not actually arrive until later. If you try to read the
2198 out-of-band data before it arrives, @code{recv} fails with an
2199 @code{EWOULDBLOCK} error.
2201 Sending out-of-band data automatically places a ``mark'' in the stream
2202 of ordinary data, showing where in the sequence the out-of-band data
2203 ``would have been''. This is useful when the meaning of out-of-band
2204 data is ``cancel everything sent so far''. Here is how you can test,
2205 in the receiving process, whether any ordinary data was sent before
2209 success = ioctl (socket, SIOCATMARK, &result);
2212 Here's a function to discard any ordinary data preceding the
2217 discard_until_mark (int socket)
2221 /* @r{This is not an arbitrary limit; any size will do.} */
2223 int result, success;
2225 /* @r{If we have reached the mark, return.} */
2226 success = ioctl (socket, SIOCATMARK, &result);
2232 /* @r{Otherwise, read a bunch of ordinary data and discard it.}
2233 @r{This is guaranteed not to read past the mark}
2234 @r{if it starts before the mark.} */
2235 success = read (socket, buffer, sizeof buffer);
2242 If you don't want to discard the ordinary data preceding the mark, you
2243 may need to read some of it anyway, to make room in internal system
2244 buffers for the out-of-band data. If you try to read out-of-band data
2245 and get an @code{EWOULDBLOCK} error, try reading some ordinary data
2246 (saving it so that you can use it when you want it) and see if that
2247 makes room. Here is an example:
2254 struct buffer *next;
2257 /* @r{Read the out-of-band data from SOCKET and return it}
2258 @r{as a `struct buffer', which records the address of the data}
2261 @r{It may be necessary to read some ordinary data}
2262 @r{in order to make room for the out-of-band data.}
2263 @r{If so, the ordinary data is saved as a chain of buffers}
2264 @r{found in the `next' field of the value.} */
2267 read_oob (int socket)
2269 struct buffer *tail = 0;
2270 struct buffer *list = 0;
2274 /* @r{This is an arbitrary limit.}
2275 @r{Does anyone know how to do this without a limit?} */
2276 char *buffer = (char *) xmalloc (1024);
2277 struct buffer *link;
2281 /* @r{Try again to read the out-of-band data.} */
2282 success = recv (socket, buffer, sizeof buffer, MSG_OOB);
2285 /* @r{We got it, so return it.} */
2287 = (struct buffer *) xmalloc (sizeof (struct buffer));
2288 link->buffer = buffer;
2289 link->size = success;
2294 /* @r{If we fail, see if we are at the mark.} */
2295 success = ioctl (socket, SIOCATMARK, &result);
2300 /* @r{At the mark; skipping past more ordinary data cannot help.}
2301 @r{So just wait a while.} */
2306 /* @r{Otherwise, read a bunch of ordinary data and save it.}
2307 @r{This is guaranteed not to read past the mark}
2308 @r{if it starts before the mark.} */
2309 success = read (socket, buffer, sizeof buffer);
2313 /* @r{Save this data in the buffer list.} */
2316 = (struct buffer *) xmalloc (sizeof (struct buffer));
2317 link->buffer = buffer;
2318 link->size = success;
2320 /* @r{Add the new link to the end of the list.} */
2332 @section Datagram Socket Operations
2334 @cindex datagram socket
2335 This section describes how to use communication styles that don't use
2336 connections (styles @code{SOCK_DGRAM} and @code{SOCK_RDM}). Using
2337 these styles, you group data into packets and each packet is an
2338 independent communication. You specify the destination for each
2339 packet individually.
2341 Datagram packets are like letters: you send each one independently,
2342 with its own destination address, and they may arrive in the wrong
2343 order or not at all.
2345 The @code{listen} and @code{accept} functions are not allowed for
2346 sockets using connectionless communication styles.
2349 * Sending Datagrams:: Sending packets on a datagram socket.
2350 * Receiving Datagrams:: Receiving packets on a datagram socket.
2351 * Datagram Example:: An example program: packets sent over a
2352 datagram socket in the file namespace.
2353 * Example Receiver:: Another program, that receives those packets.
2356 @node Sending Datagrams
2357 @subsection Sending Datagrams
2358 @cindex sending a datagram
2359 @cindex transmitting datagrams
2360 @cindex datagrams, transmitting
2362 @pindex sys/socket.h
2363 The normal way of sending data on a datagram socket is by using the
2364 @code{sendto} function, declared in @file{sys/socket.h}.
2366 You can call @code{connect} on a datagram socket, but this only
2367 specifies a default destination for further data transmission on the
2368 socket. When a socket has a default destination, then you can use
2369 @code{send} (@pxref{Sending Data}) or even @code{write} (@pxref{I/O
2370 Primitives}) to send a packet there. You can cancel the default
2371 destination by calling @code{connect} using an address format of
2372 @code{AF_UNSPEC} in the @var{addr} argument. @xref{Connecting}, for
2373 more information about the @code{connect} function.
2375 @comment sys/socket.h
2377 @deftypefun int sendto (int @var{socket}, void *@var{buffer}. size_t @var{size}, int @var{flags}, struct sockaddr *@var{addr}, socklen_t @var{length})
2378 The @code{sendto} function transmits the data in the @var{buffer}
2379 through the socket @var{socket} to the destination address specified
2380 by the @var{addr} and @var{length} arguments. The @var{size} argument
2381 specifies the number of bytes to be transmitted.
2383 The @var{flags} are interpreted the same way as for @code{send}; see
2384 @ref{Socket Data Options}.
2386 The return value and error conditions are also the same as for
2387 @code{send}, but you cannot rely on the system to detect errors and
2388 report them; the most common error is that the packet is lost or there
2389 is no one at the specified address to receive it, and the operating
2390 system on your machine usually does not know this.
2392 It is also possible for one call to @code{sendto} to report an error
2393 due to a problem related to a previous call.
2395 This function is defined as a cancelation point in multi-threaded
2396 programs. So one has to be prepared for this and make sure that
2397 possibly allocated resources (like memory, files descriptors,
2398 semaphores or whatever) are freed even if the thread is cancel.
2399 @c @xref{pthread_cleanup_push}, for a method how to do this.
2402 @node Receiving Datagrams
2403 @subsection Receiving Datagrams
2404 @cindex receiving datagrams
2406 The @code{recvfrom} function reads a packet from a datagram socket and
2407 also tells you where it was sent from. This function is declared in
2408 @file{sys/socket.h}.
2410 @comment sys/socket.h
2412 @deftypefun int recvfrom (int @var{socket}, void *@var{buffer}, size_t @var{size}, int @var{flags}, struct sockaddr *@var{addr}, socklen_t *@var{length-ptr})
2413 The @code{recvfrom} function reads one packet from the socket
2414 @var{socket} into the buffer @var{buffer}. The @var{size} argument
2415 specifies the maximum number of bytes to be read.
2417 If the packet is longer than @var{size} bytes, then you get the first
2418 @var{size} bytes of the packet, and the rest of the packet is lost.
2419 There's no way to read the rest of the packet. Thus, when you use a
2420 packet protocol, you must always know how long a packet to expect.
2422 The @var{addr} and @var{length-ptr} arguments are used to return the
2423 address where the packet came from. @xref{Socket Addresses}. For a
2424 socket in the file domain, the address information won't be meaningful,
2425 since you can't read the address of such a socket (@pxref{File
2426 Namespace}). You can specify a null pointer as the @var{addr} argument
2427 if you are not interested in this information.
2429 The @var{flags} are interpreted the same way as for @code{recv}
2430 (@pxref{Socket Data Options}). The return value and error conditions
2431 are also the same as for @code{recv}.
2433 This function is defined as a cancelation point in multi-threaded
2434 programs. So one has to be prepared for this and make sure that
2435 possibly allocated resources (like memory, files descriptors,
2436 semaphores or whatever) are freed even if the thread is cancel.
2437 @c @xref{pthread_cleanup_push}, for a method how to do this.
2440 You can use plain @code{recv} (@pxref{Receiving Data}) instead of
2441 @code{recvfrom} if you know don't need to find out who sent the packet
2442 (either because you know where it should come from or because you
2443 treat all possible senders alike). Even @code{read} can be used if
2444 you don't want to specify @var{flags} (@pxref{I/O Primitives}).
2447 @c sendmsg and recvmsg are like readv and writev in that they
2448 @c use a series of buffers. It's not clear this is worth
2449 @c supporting or that we support them.
2450 @c !!! they can do more; it is hairy
2452 @comment sys/socket.h
2454 @deftp {Data Type} {struct msghdr}
2457 @comment sys/socket.h
2459 @deftypefun int sendmsg (int @var{socket}, const struct msghdr *@var{message}, int @var{flags})
2461 This function is defined as a cancelation point in multi-threaded
2462 programs. So one has to be prepared for this and make sure that
2463 possibly allocated resources (like memory, files descriptors,
2464 semaphores or whatever) are freed even if the thread is cancel.
2465 @c @xref{pthread_cleanup_push}, for a method how to do this.
2468 @comment sys/socket.h
2470 @deftypefun int recvmsg (int @var{socket}, struct msghdr *@var{message}, int @var{flags})
2472 This function is defined as a cancelation point in multi-threaded
2473 programs. So one has to be prepared for this and make sure that
2474 possibly allocated resources (like memory, files descriptors,
2475 semaphores or whatever) are freed even if the thread is cancel.
2476 @c @xref{pthread_cleanup_push}, for a method how to do this.
2480 @node Datagram Example
2481 @subsection Datagram Socket Example
2483 Here is a set of example programs that send messages over a datagram
2484 stream in the file namespace. Both the client and server programs use the
2485 @code{make_named_socket} function that was presented in @ref{File
2486 Namespace}, to create and name their sockets.
2488 First, here is the server program. It sits in a loop waiting for
2489 messages to arrive, bouncing each message back to the sender.
2490 Obviously, this isn't a particularly useful program, but it does show
2491 the general ideas involved.
2494 @include filesrv.c.texi
2497 @node Example Receiver
2498 @subsection Example of Reading Datagrams
2500 Here is the client program corresponding to the server above.
2502 It sends a datagram to the server and then waits for a reply. Notice
2503 that the socket for the client (as well as for the server) in this
2504 example has to be given a name. This is so that the server can direct
2505 a message back to the client. Since the socket has no associated
2506 connection state, the only way the server can do this is by
2507 referencing the name of the client.
2510 @include filecli.c.texi
2513 Keep in mind that datagram socket communications are unreliable. In
2514 this example, the client program waits indefinitely if the message
2515 never reaches the server or if the server's response never comes
2516 back. It's up to the user running the program to kill it and restart
2517 it, if desired. A more automatic solution could be to use
2518 @code{select} (@pxref{Waiting for I/O}) to establish a timeout period
2519 for the reply, and in case of timeout either resend the message or
2520 shut down the socket and exit.
2523 @section The @code{inetd} Daemon
2525 We've explained above how to write a server program that does its own
2526 listening. Such a server must already be running in order for anyone
2529 Another way to provide service for an Internet port is to let the daemon
2530 program @code{inetd} do the listening. @code{inetd} is a program that
2531 runs all the time and waits (using @code{select}) for messages on a
2532 specified set of ports. When it receives a message, it accepts the
2533 connection (if the socket style calls for connections) and then forks a
2534 child process to run the corresponding server program. You specify the
2535 ports and their programs in the file @file{/etc/inetd.conf}.
2539 * Configuring Inetd::
2543 @subsection @code{inetd} Servers
2545 Writing a server program to be run by @code{inetd} is very simple. Each time
2546 someone requests a connection to the appropriate port, a new server
2547 process starts. The connection already exists at this time; the
2548 socket is available as the standard input descriptor and as the
2549 standard output descriptor (descriptors 0 and 1) in the server
2550 process. So the server program can begin reading and writing data
2551 right away. Often the program needs only the ordinary I/O facilities;
2552 in fact, a general-purpose filter program that knows nothing about
2553 sockets can work as a byte stream server run by @code{inetd}.
2555 You can also use @code{inetd} for servers that use connectionless
2556 communication styles. For these servers, @code{inetd} does not try to accept
2557 a connection, since no connection is possible. It just starts the
2558 server program, which can read the incoming datagram packet from
2559 descriptor 0. The server program can handle one request and then
2560 exit, or you can choose to write it to keep reading more requests
2561 until no more arrive, and then exit. You must specify which of these
2562 two techniques the server uses, when you configure @code{inetd}.
2564 @node Configuring Inetd
2565 @subsection Configuring @code{inetd}
2567 The file @file{/etc/inetd.conf} tells @code{inetd} which ports to listen to
2568 and what server programs to run for them. Normally each entry in the
2569 file is one line, but you can split it onto multiple lines provided
2570 all but the first line of the entry start with whitespace. Lines that
2571 start with @samp{#} are comments.
2573 Here are two standard entries in @file{/etc/inetd.conf}:
2576 ftp stream tcp nowait root /libexec/ftpd ftpd
2577 talk dgram udp wait root /libexec/talkd talkd
2580 An entry has this format:
2583 @var{service} @var{style} @var{protocol} @var{wait} @var{username} @var{program} @var{arguments}
2586 The @var{service} field says which service this program provides. It
2587 should be the name of a service defined in @file{/etc/services}.
2588 @code{inetd} uses @var{service} to decide which port to listen on for
2591 The fields @var{style} and @var{protocol} specify the communication
2592 style and the protocol to use for the listening socket. The style
2593 should be the name of a communication style, converted to lower case
2594 and with @samp{SOCK_} deleted---for example, @samp{stream} or
2595 @samp{dgram}. @var{protocol} should be one of the protocols listed in
2596 @file{/etc/protocols}. The typical protocol names are @samp{tcp} for
2597 byte stream connections and @samp{udp} for unreliable datagrams.
2599 The @var{wait} field should be either @samp{wait} or @samp{nowait}.
2600 Use @samp{wait} if @var{style} is a connectionless style and the
2601 server, once started, handles multiple requests, as many as come in.
2602 Use @samp{nowait} if @code{inetd} should start a new process for each message
2603 or request that comes in. If @var{style} uses connections, then
2604 @var{wait} @strong{must} be @samp{nowait}.
2606 @var{user} is the user name that the server should run as. @code{inetd} runs
2607 as root, so it can set the user ID of its children arbitrarily. It's
2608 best to avoid using @samp{root} for @var{user} if you can; but some
2609 servers, such as Telnet and FTP, read a username and password
2610 themselves. These servers need to be root initially so they can log
2611 in as commanded by the data coming over the network.
2613 @var{program} together with @var{arguments} specifies the command to
2614 run to start the server. @var{program} should be an absolute file
2615 name specifying the executable file to run. @var{arguments} consists
2616 of any number of whitespace-separated words, which become the
2617 command-line arguments of @var{program}. The first word in
2618 @var{arguments} is argument zero, which should by convention be the
2619 program name itself (sans directories).
2621 If you edit @file{/etc/inetd.conf}, you can tell @code{inetd} to reread the
2622 file and obey its new contents by sending the @code{inetd} process the
2623 @code{SIGHUP} signal. You'll have to use @code{ps} to determine the
2624 process ID of the @code{inetd} process, as it is not fixed.
2626 @c !!! could document /etc/inetd.sec
2628 @node Socket Options
2629 @section Socket Options
2630 @cindex socket options
2632 This section describes how to read or set various options that modify
2633 the behavior of sockets and their underlying communications protocols.
2635 @cindex level, for socket options
2636 @cindex socket option level
2637 When you are manipulating a socket option, you must specify which
2638 @dfn{level} the option pertains to. This describes whether the option
2639 applies to the socket interface, or to a lower-level communications
2643 * Socket Option Functions:: The basic functions for setting and getting
2645 * Socket-Level Options:: Details of the options at the socket level.
2648 @node Socket Option Functions
2649 @subsection Socket Option Functions
2651 @pindex sys/socket.h
2652 Here are the functions for examining and modifying socket options.
2653 They are declared in @file{sys/socket.h}.
2655 @comment sys/socket.h
2657 @deftypefun int getsockopt (int @var{socket}, int @var{level}, int @var{optname}, void *@var{optval}, socklen_t *@var{optlen-ptr})
2658 The @code{getsockopt} function gets information about the value of
2659 option @var{optname} at level @var{level} for socket @var{socket}.
2661 The option value is stored in a buffer that @var{optval} points to.
2662 Before the call, you should supply in @code{*@var{optlen-ptr}} the
2663 size of this buffer; on return, it contains the number of bytes of
2664 information actually stored in the buffer.
2666 Most options interpret the @var{optval} buffer as a single @code{int}
2669 The actual return value of @code{getsockopt} is @code{0} on success
2670 and @code{-1} on failure. The following @code{errno} error conditions
2675 The @var{socket} argument is not a valid file descriptor.
2678 The descriptor @var{socket} is not a socket.
2681 The @var{optname} doesn't make sense for the given @var{level}.
2685 @comment sys/socket.h
2687 @deftypefun int setsockopt (int @var{socket}, int @var{level}, int @var{optname}, void *@var{optval}, socklen_t @var{optlen})
2688 This function is used to set the socket option @var{optname} at level
2689 @var{level} for socket @var{socket}. The value of the option is passed
2690 in the buffer @var{optval}, which has size @var{optlen}.
2692 The return value and error codes for @code{setsockopt} are the same as
2693 for @code{getsockopt}.
2696 @node Socket-Level Options
2697 @subsection Socket-Level Options
2699 @comment sys/socket.h
2701 @deftypevr Constant int SOL_SOCKET
2702 Use this constant as the @var{level} argument to @code{getsockopt} or
2703 @code{setsockopt} to manipulate the socket-level options described in
2707 @pindex sys/socket.h
2708 Here is a table of socket-level option names; all are defined in the
2709 header file @file{sys/socket.h}.
2712 @comment sys/socket.h
2715 @c Extra blank line here makes the table look better.
2717 This option toggles recording of debugging information in the underlying
2718 protocol modules. The value has type @code{int}; a nonzero value means
2720 @c !!! should say how this is used
2721 @c Ok, anyone who knows, please explain.
2723 @comment sys/socket.h
2726 This option controls whether @code{bind} (@pxref{Setting Address})
2727 should permit reuse of local addresses for this socket. If you enable
2728 this option, you can actually have two sockets with the same Internet
2729 port number; but the system won't allow you to use the two
2730 identically-named sockets in a way that would confuse the Internet. The
2731 reason for this option is that some higher-level Internet protocols,
2732 including FTP, require you to keep reusing the same socket number.
2734 The value has type @code{int}; a nonzero value means ``yes''.
2736 @comment sys/socket.h
2739 This option controls whether the underlying protocol should
2740 periodically transmit messages on a connected socket. If the peer
2741 fails to respond to these messages, the connection is considered
2742 broken. The value has type @code{int}; a nonzero value means
2745 @comment sys/socket.h
2748 This option controls whether outgoing messages bypass the normal
2749 message routing facilities. If set, messages are sent directly to the
2750 network interface instead. The value has type @code{int}; a nonzero
2751 value means ``yes''.
2753 @comment sys/socket.h
2756 This option specifies what should happen when the socket of a type
2757 that promises reliable delivery still has untransmitted messages when
2758 it is closed; see @ref{Closing a Socket}. The value has type
2759 @code{struct linger}.
2761 @comment sys/socket.h
2763 @deftp {Data Type} {struct linger}
2764 This structure type has the following members:
2768 This field is interpreted as a boolean. If nonzero, @code{close}
2769 blocks until the data is transmitted or the timeout period has expired.
2772 This specifies the timeout period, in seconds.
2776 @comment sys/socket.h
2779 This option controls whether datagrams may be broadcast from the socket.
2780 The value has type @code{int}; a nonzero value means ``yes''.
2782 @comment sys/socket.h
2785 If this option is set, out-of-band data received on the socket is
2786 placed in the normal input queue. This permits it to be read using
2787 @code{read} or @code{recv} without specifying the @code{MSG_OOB}
2788 flag. @xref{Out-of-Band Data}. The value has type @code{int}; a
2789 nonzero value means ``yes''.
2791 @comment sys/socket.h
2794 This option gets or sets the size of the output buffer. The value is a
2795 @code{size_t}, which is the size in bytes.
2797 @comment sys/socket.h
2800 This option gets or sets the size of the input buffer. The value is a
2801 @code{size_t}, which is the size in bytes.
2803 @comment sys/socket.h
2806 @comment sys/socket.h
2809 This option can be used with @code{getsockopt} only. It is used to
2810 get the socket's communication style. @code{SO_TYPE} is the
2811 historical name, and @code{SO_STYLE} is the preferred name in GNU.
2812 The value has type @code{int} and its value designates a communication
2813 style; see @ref{Communication Styles}.
2815 @comment sys/socket.h
2818 @c Extra blank line here makes the table look better.
2820 This option can be used with @code{getsockopt} only. It is used to reset
2821 the error status of the socket. The value is an @code{int}, which represents
2822 the previous error status.
2823 @c !!! what is "socket error status"? this is never defined.
2826 @node Networks Database
2827 @section Networks Database
2828 @cindex networks database
2829 @cindex converting network number to network name
2830 @cindex converting network name to network number
2832 @pindex /etc/networks
2834 Many systems come with a database that records a list of networks known
2835 to the system developer. This is usually kept either in the file
2836 @file{/etc/networks} or in an equivalent from a name server. This data
2837 base is useful for routing programs such as @code{route}, but it is not
2838 useful for programs that simply communicate over the network. We
2839 provide functions to access this data base, which are declared in
2844 @deftp {Data Type} {struct netent}
2845 This data type is used to represent information about entries in the
2846 networks database. It has the following members:
2850 This is the ``official'' name of the network.
2852 @item char **n_aliases
2853 These are alternative names for the network, represented as a vector
2854 of strings. A null pointer terminates the array.
2856 @item int n_addrtype
2857 This is the type of the network number; this is always equal to
2858 @code{AF_INET} for Internet networks.
2860 @item unsigned long int n_net
2861 This is the network number. Network numbers are returned in host
2862 byte order; see @ref{Byte Order}.
2866 Use the @code{getnetbyname} or @code{getnetbyaddr} functions to search
2867 the networks database for information about a specific network. The
2868 information is returned in a statically-allocated structure; you must
2869 copy the information if you need to save it.
2873 @deftypefun {struct netent *} getnetbyname (const char *@var{name})
2874 The @code{getnetbyname} function returns information about the network
2875 named @var{name}. It returns a null pointer if there is no such
2881 @deftypefun {struct netent *} getnetbyaddr (long @var{net}, int @var{type})
2882 The @code{getnetbyaddr} function returns information about the network
2883 of type @var{type} with number @var{net}. You should specify a value of
2884 @code{AF_INET} for the @var{type} argument for Internet networks.
2886 @code{getnetbyaddr} returns a null pointer if there is no such
2890 You can also scan the networks database using @code{setnetent},
2891 @code{getnetent}, and @code{endnetent}. Be careful in using these
2892 functions, because they are not reentrant.
2896 @deftypefun void setnetent (int @var{stayopen})
2897 This function opens and rewinds the networks database.
2899 If the @var{stayopen} argument is nonzero, this sets a flag so that
2900 subsequent calls to @code{getnetbyname} or @code{getnetbyaddr} will
2901 not close the database (as they usually would). This makes for more
2902 efficiency if you call those functions several times, by avoiding
2903 reopening the database for each call.
2908 @deftypefun {struct netent *} getnetent (void)
2909 This function returns the next entry in the networks database. It
2910 returns a null pointer if there are no more entries.
2915 @deftypefun void endnetent (void)
2916 This function closes the networks database.