]> git.ipfire.org Git - thirdparty/glibc.git/blame - manual/socket.texi
Don't build libnsl for new ABIs
[thirdparty/glibc.git] / manual / socket.texi
CommitLineData
28f540f4 1@node Sockets, Low-Level Terminal Interface, Pipes and FIFOs, Top
7a68c94a 2@c %MENU% A more complicated IPC mechanism, with networking support
28f540f4
RM
3@chapter Sockets
4
5This chapter describes the GNU facilities for interprocess
6communication using sockets.
7
8@cindex socket
9@cindex interprocess communication, with sockets
10A @dfn{socket} is a generalized interprocess communication channel.
04b9968b
UD
11Like a pipe, a socket is represented as a file descriptor. Unlike pipes
12sockets support communication between unrelated processes, and even
13between processes running on different machines that communicate over a
14network. Sockets are the primary means of communicating with other
15machines; @code{telnet}, @code{rlogin}, @code{ftp}, @code{talk} and the
16other familiar network programs use sockets.
28f540f4 17
1f77f049 18Not all operating systems support sockets. In @theglibc{}, the
28f540f4
RM
19header file @file{sys/socket.h} exists regardless of the operating
20system, and the socket functions always exist, but if the system does
04b9968b 21not really support sockets these functions always fail.
28f540f4
RM
22
23@strong{Incomplete:} We do not currently document the facilities for
4bca4c17
UD
24broadcast messages or for configuring Internet interfaces. The
25reentrant functions and some newer functions that are related to IPv6
26aren't documented either so far.
28f540f4
RM
27
28@menu
29* Socket Concepts:: Basic concepts you need to know about.
04b9968b 30* Communication Styles::Stream communication, datagrams and other styles.
28f540f4 31* Socket Addresses:: How socket names (``addresses'') work.
4bca4c17
UD
32* Interface Naming:: Identifying specific network interfaces.
33* Local Namespace:: Details about the local namespace.
28f540f4
RM
34* Internet Namespace:: Details about the Internet namespace.
35* Misc Namespaces:: Other namespaces not documented fully here.
36* Open/Close Sockets:: Creating sockets and destroying them.
37* Connections:: Operations on sockets with connection state.
38* Datagrams:: Operations on datagram sockets.
39* Inetd:: Inetd is a daemon that starts servers on request.
40 The most convenient way to write a server
41 is to make it work with Inetd.
42* Socket Options:: Miscellaneous low-level socket options.
43* Networks Database:: Accessing the database of network names.
44@end menu
45
46@node Socket Concepts
47@section Socket Concepts
48
49@cindex communication style (of a socket)
50@cindex style of communication (of a socket)
51When you create a socket, you must specify the style of communication
52you want to use and the type of protocol that should implement it.
53The @dfn{communication style} of a socket defines the user-level
54semantics of sending and receiving data on the socket. Choosing a
55communication style specifies the answers to questions such as these:
56
57@itemize @bullet
58@item
59@cindex packet
60@cindex byte stream
61@cindex stream (sockets)
62@strong{What are the units of data transmission?} Some communication
04b9968b 63styles regard the data as a sequence of bytes with no larger
28f540f4
RM
64structure; others group the bytes into records (which are known in
65this context as @dfn{packets}).
66
67@item
68@cindex loss of data on sockets
69@cindex data loss on sockets
70@strong{Can data be lost during normal operation?} Some communication
71styles guarantee that all the data sent arrives in the order it was
72sent (barring system or network crashes); other styles occasionally
73lose data as a normal part of operation, and may sometimes deliver
74packets more than once or in the wrong order.
75
76Designing a program to use unreliable communication styles usually
77involves taking precautions to detect lost or misordered packets and
78to retransmit data as needed.
79
80@item
81@strong{Is communication entirely with one partner?} Some
82communication styles are like a telephone call---you make a
04b9968b 83@dfn{connection} with one remote socket and then exchange data
28f540f4
RM
84freely. Other styles are like mailing letters---you specify a
85destination address for each message you send.
86@end itemize
87
88@cindex namespace (of socket)
89@cindex domain (of socket)
90@cindex socket namespace
91@cindex socket domain
92You must also choose a @dfn{namespace} for naming the socket. A socket
93name (``address'') is meaningful only in the context of a particular
94namespace. In fact, even the data type to use for a socket name may
95depend on the namespace. Namespaces are also called ``domains'', but we
96avoid that word as it can be confused with other usage of the same
97term. Each namespace has a symbolic name that starts with @samp{PF_}.
98A corresponding symbolic name starting with @samp{AF_} designates the
99address format for that namespace.
100
101@cindex network protocol
102@cindex protocol (of socket)
103@cindex socket protocol
104@cindex protocol family
105Finally you must choose the @dfn{protocol} to carry out the
106communication. The protocol determines what low-level mechanism is used
107to transmit and receive data. Each protocol is valid for a particular
108namespace and communication style; a namespace is sometimes called a
109@dfn{protocol family} because of this, which is why the namespace names
110start with @samp{PF_}.
111
112The rules of a protocol apply to the data passing between two programs,
113perhaps on different computers; most of these rules are handled by the
04b9968b 114operating system and you need not know about them. What you do need to
28f540f4
RM
115know about protocols is this:
116
117@itemize @bullet
118@item
119In order to have communication between two sockets, they must specify
120the @emph{same} protocol.
121
122@item
123Each protocol is meaningful with particular style/namespace
124combinations and cannot be used with inappropriate combinations. For
125example, the TCP protocol fits only the byte stream style of
126communication and the Internet namespace.
127
128@item
04b9968b
UD
129For each combination of style and namespace there is a @dfn{default
130protocol}, which you can request by specifying 0 as the protocol
28f540f4
RM
131number. And that's what you should normally do---use the default.
132@end itemize
133
55c14926
UD
134Throughout the following description at various places
135variables/parameters to denote sizes are required. And here the trouble
136starts. In the first implementations the type of these variables was
04b9968b
UD
137simply @code{int}. On most machines at that time an @code{int} was 32
138bits wide, which created a @emph{de facto} standard requiring 32-bit
139variables. This is important since references to variables of this type
140are passed to the kernel.
141
142Then the POSIX people came and unified the interface with the words "all
143size values are of type @code{size_t}". On 64-bit machines
144@code{size_t} is 64 bits wide, so pointers to variables were no longer
55c14926
UD
145possible.
146
8163845f
UD
147The Unix98 specification provides a solution by introducing a type
148@code{socklen_t}. This type is used in all of the cases that POSIX
149changed to use @code{size_t}. The only requirement of this type is that
150it be an unsigned type of at least 32 bits. Therefore, implementations
04b9968b
UD
151which require that references to 32-bit variables be passed can be as
152happy as implementations which use 64-bit values.
55c14926
UD
153
154
28f540f4
RM
155@node Communication Styles
156@section Communication Styles
157
1f77f049 158@Theglibc{} includes support for several different kinds of sockets,
28f540f4
RM
159each with different characteristics. This section describes the
160supported socket types. The symbolic constants listed here are
161defined in @file{sys/socket.h}.
162@pindex sys/socket.h
163
28f540f4 164@deftypevr Macro int SOCK_STREAM
d08a7e4c 165@standards{BSD, sys/socket.h}
04b9968b
UD
166The @code{SOCK_STREAM} style is like a pipe (@pxref{Pipes and FIFOs}).
167It operates over a connection with a particular remote socket and
28f540f4
RM
168transmits data reliably as a stream of bytes.
169
170Use of this style is covered in detail in @ref{Connections}.
171@end deftypevr
172
28f540f4 173@deftypevr Macro int SOCK_DGRAM
d08a7e4c 174@standards{BSD, sys/socket.h}
28f540f4 175The @code{SOCK_DGRAM} style is used for sending
04b9968b 176individually-addressed packets unreliably.
28f540f4
RM
177It is the diametrical opposite of @code{SOCK_STREAM}.
178
179Each time you write data to a socket of this kind, that data becomes
180one packet. Since @code{SOCK_DGRAM} sockets do not have connections,
181you must specify the recipient address with each packet.
182
183The only guarantee that the system makes about your requests to
184transmit data is that it will try its best to deliver each packet you
185send. It may succeed with the sixth packet after failing with the
186fourth and fifth packets; the seventh packet may arrive before the
187sixth, and may arrive a second time after the sixth.
188
189The typical use for @code{SOCK_DGRAM} is in situations where it is
04b9968b 190acceptable to simply re-send a packet if no response is seen in a
28f540f4
RM
191reasonable amount of time.
192
193@xref{Datagrams}, for detailed information about how to use datagram
194sockets.
195@end deftypevr
196
197@ignore
198@c This appears to be only for the NS domain, which we aren't
199@c discussing and probably won't support either.
28f540f4 200@deftypevr Macro int SOCK_SEQPACKET
d08a7e4c 201@standards{BSD, sys/socket.h}
04b9968b 202This style is like @code{SOCK_STREAM} except that the data are
28f540f4
RM
203structured into packets.
204
205A program that receives data over a @code{SOCK_SEQPACKET} socket
206should be prepared to read the entire message packet in a single call
207to @code{read}; if it only reads part of the message, the remainder of
208the message is simply discarded instead of being available for
209subsequent calls to @code{read}.
210
211Many protocols do not support this communication style.
212@end deftypevr
213@end ignore
214
215@ignore
28f540f4 216@deftypevr Macro int SOCK_RDM
d08a7e4c 217@standards{BSD, sys/socket.h}
28f540f4
RM
218This style is a reliable version of @code{SOCK_DGRAM}: it sends
219individually addressed packets, but guarantees that each packet sent
220arrives exactly once.
221
222@strong{Warning:} It is not clear this is actually supported
223by any operating system.
224@end deftypevr
225@end ignore
226
28f540f4 227@deftypevr Macro int SOCK_RAW
d08a7e4c 228@standards{BSD, sys/socket.h}
28f540f4
RM
229This style provides access to low-level network protocols and
230interfaces. Ordinary user programs usually have no need to use this
231style.
232@end deftypevr
233
234@node Socket Addresses
235@section Socket Addresses
236
237@cindex address of socket
238@cindex name of socket
239@cindex binding a socket address
240@cindex socket address (name) binding
241The name of a socket is normally called an @dfn{address}. The
242functions and symbols for dealing with socket addresses were named
243inconsistently, sometimes using the term ``name'' and sometimes using
244``address''. You can regard these terms as synonymous where sockets
245are concerned.
246
247A socket newly created with the @code{socket} function has no
248address. Other processes can find it for communication only if you
249give it an address. We call this @dfn{binding} the address to the
250socket, and the way to do it is with the @code{bind} function.
251
b57dd246 252You need only be concerned with the address of a socket if other processes
28f540f4
RM
253are to find it and start communicating with it. You can specify an
254address for other sockets, but this is usually pointless; the first time
255you send data from a socket, or use it to initiate a connection, the
256system assigns an address automatically if you have not specified one.
257
258Occasionally a client needs to specify an address because the server
04b9968b 259discriminates based on address; for example, the rsh and rlogin
841785ba 260protocols look at the client's socket address and only bypass passphrase
4bca4c17 261checking if it is less than @code{IPPORT_RESERVED} (@pxref{Ports}).
28f540f4
RM
262
263The details of socket addresses vary depending on what namespace you are
4bca4c17 264using. @xref{Local Namespace}, or @ref{Internet Namespace}, for specific
28f540f4
RM
265information.
266
267Regardless of the namespace, you use the same functions @code{bind} and
268@code{getsockname} to set and examine a socket's address. These
269functions use a phony data type, @code{struct sockaddr *}, to accept the
270address. In practice, the address lives in a structure of some other
271data type appropriate to the address format you are using, but you cast
272its address to @code{struct sockaddr *} when you pass it to
273@code{bind}.
274
275@menu
276* Address Formats:: About @code{struct sockaddr}.
277* Setting Address:: Binding an address to a socket.
278* Reading Address:: Reading the address of a socket.
279@end menu
280
281@node Address Formats
282@subsection Address Formats
283
284The functions @code{bind} and @code{getsockname} use the generic data
285type @code{struct sockaddr *} to represent a pointer to a socket
286address. You can't use this data type effectively to interpret an
287address or construct one; for that, you must use the proper data type
288for the socket's namespace.
289
04b9968b 290Thus, the usual practice is to construct an address of the proper
28f540f4
RM
291namespace-specific type, then cast a pointer to @code{struct sockaddr *}
292when you call @code{bind} or @code{getsockname}.
293
294The one piece of information that you can get from the @code{struct
04b9968b 295sockaddr} data type is the @dfn{address format designator}. This tells
28f540f4
RM
296you which data type to use to understand the address fully.
297
298@pindex sys/socket.h
299The symbols in this section are defined in the header file
300@file{sys/socket.h}.
301
4bca4c17 302@deftp {Data Type} {struct sockaddr}
d08a7e4c 303@standards{BSD, sys/socket.h}
28f540f4
RM
304The @code{struct sockaddr} type itself has the following members:
305
306@table @code
307@item short int sa_family
308This is the code for the address format of this address. It
309identifies the format of the data which follows.
310
311@item char sa_data[14]
312This is the actual socket address data, which is format-dependent. Its
313length also depends on the format, and may well be more than 14. The
314length 14 of @code{sa_data} is essentially arbitrary.
315@end table
316@end deftp
317
318Each address format has a symbolic name which starts with @samp{AF_}.
319Each of them corresponds to a @samp{PF_} symbol which designates the
320corresponding namespace. Here is a list of address format names:
321
2fe82ca6 322@vtable @code
356d7100 323@item AF_LOCAL
d08a7e4c 324@standards{POSIX, sys/socket.h}
356d7100
UD
325This designates the address format that goes with the local namespace.
326(@code{PF_LOCAL} is the name of that namespace.) @xref{Local Namespace
28f540f4
RM
327Details}, for information about this address format.
328
28f540f4 329@item AF_UNIX
d08a7e4c
RJ
330@standards{BSD, sys/socket.h}
331@standards{Unix98, sys/socket.h}
fc4de705
UD
332This is a synonym for @code{AF_LOCAL}. Although @code{AF_LOCAL} is
333mandated by POSIX.1g, @code{AF_UNIX} is portable to more systems.
334@code{AF_UNIX} was the traditional name stemming from BSD, so even most
335POSIX systems support it. It is also the name of choice in the Unix98
336specification. (The same is true for @code{PF_UNIX}
337vs. @code{PF_LOCAL}).
28f540f4 338
356d7100 339@item AF_FILE
d08a7e4c 340@standards{GNU, sys/socket.h}
356d7100
UD
341This is another synonym for @code{AF_LOCAL}, for compatibility.
342(@code{PF_FILE} is likewise a synonym for @code{PF_LOCAL}.)
4bca4c17 343
28f540f4 344@item AF_INET
d08a7e4c 345@standards{BSD, sys/socket.h}
28f540f4
RM
346This designates the address format that goes with the Internet
347namespace. (@code{PF_INET} is the name of that namespace.)
3996f34b
UD
348@xref{Internet Address Formats}.
349
3996f34b 350@item AF_INET6
d08a7e4c 351@standards{IPv6 Basic API, sys/socket.h}
3996f34b
UD
352This is similar to @code{AF_INET}, but refers to the IPv6 protocol.
353(@code{PF_INET6} is the name of the corresponding namespace.)
28f540f4 354
28f540f4 355@item AF_UNSPEC
d08a7e4c 356@standards{BSD, sys/socket.h}
28f540f4
RM
357This designates no particular address format. It is used only in rare
358cases, such as to clear out the default destination address of a
359``connected'' datagram socket. @xref{Sending Datagrams}.
360
361The corresponding namespace designator symbol @code{PF_UNSPEC} exists
362for completeness, but there is no reason to use it in a program.
2fe82ca6 363@end vtable
28f540f4
RM
364
365@file{sys/socket.h} defines symbols starting with @samp{AF_} for many
04b9968b
UD
366different kinds of networks, most or all of which are not actually
367implemented. We will document those that really work as we receive
28f540f4
RM
368information about how to use them.
369
370@node Setting Address
371@subsection Setting the Address of a Socket
372
373@pindex sys/socket.h
374Use the @code{bind} function to assign an address to a socket. The
375prototype for @code{bind} is in the header file @file{sys/socket.h}.
4bca4c17 376For examples of use, see @ref{Local Socket Example}, or see @ref{Inet Example}.
28f540f4 377
55c14926 378@deftypefun int bind (int @var{socket}, struct sockaddr *@var{addr}, socklen_t @var{length})
d08a7e4c 379@standards{BSD, sys/socket.h}
973f180b
AO
380@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
381@c Direct syscall, except on Hurd.
28f540f4
RM
382The @code{bind} function assigns an address to the socket
383@var{socket}. The @var{addr} and @var{length} arguments specify the
384address; the detailed format of the address depends on the namespace.
385The first part of the address is always the format designator, which
04b9968b 386specifies a namespace, and says that the address is in the format of
28f540f4
RM
387that namespace.
388
389The return value is @code{0} on success and @code{-1} on failure. The
390following @code{errno} error conditions are defined for this function:
391
392@table @code
393@item EBADF
394The @var{socket} argument is not a valid file descriptor.
395
396@item ENOTSOCK
397The descriptor @var{socket} is not a socket.
398
399@item EADDRNOTAVAIL
400The specified address is not available on this machine.
401
402@item EADDRINUSE
403Some other socket is already using the specified address.
404
405@item EINVAL
406The socket @var{socket} already has an address.
407
408@item EACCES
409You do not have permission to access the requested address. (In the
410Internet domain, only the super-user is allowed to specify a port number
411in the range 0 through @code{IPPORT_RESERVED} minus one; see
412@ref{Ports}.)
413@end table
414
415Additional conditions may be possible depending on the particular namespace
416of the socket.
417@end deftypefun
418
419@node Reading Address
420@subsection Reading the Address of a Socket
421
422@pindex sys/socket.h
423Use the function @code{getsockname} to examine the address of an
424Internet socket. The prototype for this function is in the header file
425@file{sys/socket.h}.
426
55c14926 427@deftypefun int getsockname (int @var{socket}, struct sockaddr *@var{addr}, socklen_t *@var{length-ptr})
d08a7e4c 428@standards{BSD, sys/socket.h}
973f180b
AO
429@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsmem{/hurd}}}
430@c Direct syscall, except on Hurd, where it seems like it might leak
431@c VM if cancelled.
28f540f4
RM
432The @code{getsockname} function returns information about the
433address 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
436allocation size of @var{addr}, and on return it contains the actual
437size of the address data.
438
439The format of the address data depends on the socket namespace. The
440length of the information is usually fixed for a given namespace, so
441normally you can know exactly how much space is needed and can provide
442that much. The usual practice is to allocate a place for the value
443using the proper data type for the socket's namespace, then cast its
444address to @code{struct sockaddr *} to pass it to @code{getsockname}.
445
446The return value is @code{0} on success and @code{-1} on error. The
447following @code{errno} error conditions are defined for this function:
448
449@table @code
450@item EBADF
451The @var{socket} argument is not a valid file descriptor.
452
453@item ENOTSOCK
454The descriptor @var{socket} is not a socket.
455
456@item ENOBUFS
457There are not enough internal buffers available for the operation.
458@end table
459@end deftypefun
460
461You can't read the address of a socket in the file namespace. This is
462consistent with the rest of the system; in general, there's no way to
463find a file's name from a descriptor for that file.
464
4bca4c17
UD
465@node Interface Naming
466@section Interface Naming
467
468Each network interface has a name. This usually consists of a few
469letters that relate to the type of interface, which may be followed by a
470number if there is more than one interface of that type. Examples
471might be @code{lo} (the loopback interface) and @code{eth0} (the first
472Ethernet interface).
473
474Although such names are convenient for humans, it would be clumsy to
356d7100 475have to use them whenever a program needs to refer to an interface. In
4bca4c17
UD
476such situations an interface is referred to by its @dfn{index}, which is
477an arbitrarily-assigned small positive integer.
478
479The following functions, constants and data types are declared in the
480header file @file{net/if.h}.
481
4bca4c17 482@deftypevr Constant size_t IFNAMSIZ
d08a7e4c 483@standards{???, net/if.h}
4bca4c17
UD
484This constant defines the maximum buffer size needed to hold an
485interface name, including its terminating zero byte.
486@end deftypevr
487
cc6e48bc 488@deftypefun {unsigned int} if_nametoindex (const char *@var{ifname})
d08a7e4c 489@standards{IPv6 basic API, net/if.h}
973f180b
AO
490@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{}}}
491@c It opens a socket to use ioctl on the fd to get the index.
492@c opensock may call socket and access multiple times until it finds a
493@c socket family that works. The Linux implementation has a potential
494@c concurrency issue WRT last_type and last_family not being updated
495@c atomically, but it is harmless; the generic implementation, OTOH,
496@c takes a lock, which makes all callers AS- and AC-Unsafe.
497@c opensock @asulock @aculock @acsfd
4bca4c17
UD
498This function yields the interface index corresponding to a particular
499name. If no interface exists with the name given, it returns 0.
500@end deftypefun
501
cc6e48bc 502@deftypefun {char *} if_indextoname (unsigned int @var{ifindex}, char *@var{ifname})
d08a7e4c 503@standards{IPv6 basic API, net/if.h}
973f180b
AO
504@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{}}}
505@c It opens a socket with opensock to use ioctl on the fd to get the
506@c name from the index.
4bca4c17
UD
507This function maps an interface index to its corresponding name. The
508returned name is placed in the buffer pointed to by @code{ifname}, which
acdacef9 509must be at least @code{IFNAMSIZ} bytes in length. If the index was
4bca4c17
UD
510invalid, the function's return value is a null pointer, otherwise it is
511@code{ifname}.
512@end deftypefun
513
4bca4c17 514@deftp {Data Type} {struct if_nameindex}
d08a7e4c 515@standards{IPv6 basic API, net/if.h}
4bca4c17
UD
516This data type is used to hold the information about a single
517interface. It has the following members:
518
519@table @code
520@item unsigned int if_index;
521This is the interface index.
522
523@item char *if_name
524This is the null-terminated index name.
525
526@end table
527@end deftp
528
310b3460 529@deftypefun {struct if_nameindex *} if_nameindex (void)
d08a7e4c 530@standards{IPv6 basic API, net/if.h}
973f180b
AO
531@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{/hurd}}@acunsafe{@aculock{/hurd} @acsfd{} @acsmem{}}}
532@c if_nameindex @ascuheap @asulock/hurd @aculock/hurd @acsfd @acsmem
533@c [linux]
534@c netlink_open @acsfd @acsmem/hurd
535@c socket dup @acsfd
536@c memset dup ok
537@c bind dup ok
538@c netlink_close dup @acsfd
539@c getsockname dup @acsmem/hurd
540@c netlink_request @ascuheap @acsmem
541@c getpagesize dup ok
542@c malloc dup @ascuheap @acsmem
543@c netlink_sendreq ok
544@c memset dup ok
545@c sendto dup ok
546@c recvmsg dup ok
547@c memcpy dup ok
548@c free dup @ascuheap @acsmem
549@c netlink_free_handle @ascuheap @acsmem
550@c free dup @ascuheap @acsmem
551@c netlink_close @acsfd
552@c close dup @acsfd
553@c malloc dup @asuheap @acsmem
554@c strndup @ascuheap @acsmem
555@c if_freenameindex @ascuheap @acsmem
556@c [hurd]
557@c opensock dup @asulock @aculock @acsfd
558@c hurd_socket_server ok
559@c pfinet_siocgifconf ok
560@c malloc @ascuheap @acsmem
561@c strdup @ascuheap @acsmem
562@c ioctl dup ok
563@c free @ascuheap @acsmem
4bca4c17
UD
564This function returns an array of @code{if_nameindex} structures, one
565for every interface that is present. The end of the list is indicated
566by a structure with an interface of 0 and a null name pointer. If an
567error occurs, this function returns a null pointer.
568
569The returned structure must be freed with @code{if_freenameindex} after
570use.
571@end deftypefun
572
cc6e48bc 573@deftypefun void if_freenameindex (struct if_nameindex *@var{ptr})
d08a7e4c 574@standards{IPv6 basic API, net/if.h}
973f180b
AO
575@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
576@c if_freenameindex @ascuheap @acsmem
577@c free dup @ascuheap @acsmem
4bca4c17
UD
578This function frees the structure returned by an earlier call to
579@code{if_nameindex}.
580@end deftypefun
581
582@node Local Namespace
583@section The Local Namespace
584@cindex local namespace, for sockets
28f540f4 585
4bca4c17
UD
586This section describes the details of the local namespace, whose
587symbolic name (required when you create a socket) is @code{PF_LOCAL}.
588The local namespace is also known as ``Unix domain sockets''. Another
589name is file namespace since socket addresses are normally implemented
590as file names.
28f540f4
RM
591
592@menu
4bca4c17
UD
593* Concepts: Local Namespace Concepts. What you need to understand.
594* Details: Local Namespace Details. Address format, symbolic names, etc.
595* Example: Local Socket Example. Example of creating a socket.
28f540f4
RM
596@end menu
597
4bca4c17
UD
598@node Local Namespace Concepts
599@subsection Local Namespace Concepts
28f540f4 600
04b9968b 601In the local namespace socket addresses are file names. You can specify
28f540f4 602any file name you want as the address of the socket, but you must have
59a8849d
UD
603write permission on the directory containing it.
604@c XXX The following was said to be wrong.
605@c In order to connect to a socket you must have read permission for it.
606It's common to put these files in the @file{/tmp} directory.
28f540f4 607
04b9968b
UD
608One peculiarity of the local namespace is that the name is only used
609when opening the connection; once open the address is not meaningful and
610may not exist.
28f540f4
RM
611
612Another peculiarity is that you cannot connect to such a socket from
613another machine--not even if the other machine shares the file system
614which contains the name of the socket. You can see the socket in a
615directory listing, but connecting to it never succeeds. Some programs
616take advantage of this, such as by asking the client to send its own
617process ID, and using the process IDs to distinguish between clients.
04b9968b 618However, we recommend you not use this method in protocols you design,
28f540f4
RM
619as we might someday permit connections from other machines that mount
620the same file systems. Instead, send each new client an identifying
621number if you want it to have one.
622
4bca4c17 623After you close a socket in the local namespace, you should delete the
28f540f4
RM
624file name from the file system. Use @code{unlink} or @code{remove} to
625do this; see @ref{Deleting Files}.
626
4bca4c17 627The local namespace supports just one protocol for any communication
28f540f4
RM
628style; it is protocol number @code{0}.
629
4bca4c17
UD
630@node Local Namespace Details
631@subsection Details of Local Namespace
28f540f4
RM
632
633@pindex sys/socket.h
4bca4c17
UD
634To create a socket in the local namespace, use the constant
635@code{PF_LOCAL} as the @var{namespace} argument to @code{socket} or
28f540f4
RM
636@code{socketpair}. This constant is defined in @file{sys/socket.h}.
637
4bca4c17 638@deftypevr Macro int PF_LOCAL
d08a7e4c 639@standards{POSIX, sys/socket.h}
4bca4c17 640This designates the local namespace, in which socket addresses are local
b57dd246
RJ
641names, and its associated family of protocols. @code{PF_LOCAL} is the
642macro used by POSIX.1g.
28f540f4
RM
643@end deftypevr
644
28f540f4 645@deftypevr Macro int PF_UNIX
d08a7e4c 646@standards{BSD, sys/socket.h}
4bca4c17 647This is a synonym for @code{PF_LOCAL}, for compatibility's sake.
28f540f4
RM
648@end deftypevr
649
4bca4c17 650@deftypevr Macro int PF_FILE
d08a7e4c 651@standards{GNU, sys/socket.h}
4bca4c17
UD
652This is a synonym for @code{PF_LOCAL}, for compatibility's sake.
653@end deftypevr
654
655The structure for specifying socket names in the local namespace is
28f540f4
RM
656defined in the header file @file{sys/un.h}:
657@pindex sys/un.h
658
28f540f4 659@deftp {Data Type} {struct sockaddr_un}
d08a7e4c 660@standards{BSD, sys/un.h}
4bca4c17 661This structure is used to specify local namespace socket addresses. It has
28f540f4
RM
662the following members:
663
664@table @code
665@item short int sun_family
666This identifies the address family or format of the socket address.
4bca4c17 667You should store the value @code{AF_LOCAL} to designate the local
28f540f4
RM
668namespace. @xref{Socket Addresses}.
669
670@item char sun_path[108]
671This is the file name to use.
672
673@strong{Incomplete:} Why is 108 a magic number? RMS suggests making
04b9968b 674this a zero-length array and tweaking the following example to use
28f540f4
RM
675@code{alloca} to allocate an appropriate amount of storage based on
676the length of the filename.
677@end table
678@end deftp
679
680You should compute the @var{length} parameter for a socket address in
4bca4c17 681the local namespace as the sum of the size of the @code{sun_family}
28f540f4 682component and the string length (@emph{not} the allocation size!) of
4bca4c17 683the file name string. This can be done using the macro @code{SUN_LEN}:
28f540f4 684
73237de3 685@deftypefn {Macro} int SUN_LEN (@emph{struct sockaddr_un *} @var{ptr})
d08a7e4c 686@standards{BSD, sys/un.h}
973f180b 687@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
b57dd246 688This macro computes the length of the socket address in the local namespace.
4bca4c17 689@end deftypefn
28f540f4 690
4bca4c17
UD
691@node Local Socket Example
692@subsection Example of Local-Namespace Sockets
693
694Here is an example showing how to create and name a socket in the local
28f540f4
RM
695namespace.
696
697@smallexample
698@include mkfsock.c.texi
699@end smallexample
700
701@node Internet Namespace
702@section The Internet Namespace
703@cindex Internet namespace, for sockets
704
4bca4c17 705This section describes the details of the protocols and socket naming
28f540f4
RM
706conventions used in the Internet namespace.
707
04b9968b 708Originally the Internet namespace used only IP version 4 (IPv4). With
4bca4c17 709the growing number of hosts on the Internet, a new protocol with a
04b9968b
UD
710larger address space was necessary: IP version 6 (IPv6). IPv6
711introduces 128-bit addresses (IPv4 has 32-bit addresses) and other
712features, and will eventually replace IPv4.
4bca4c17
UD
713
714To create a socket in the IPv4 Internet namespace, use the symbolic name
28f540f4 715@code{PF_INET} of this namespace as the @var{namespace} argument to
04b9968b 716@code{socket} or @code{socketpair}. For IPv6 addresses you need the
cf822e3c 717macro @code{PF_INET6}. These macros are defined in @file{sys/socket.h}.
28f540f4
RM
718@pindex sys/socket.h
719
28f540f4 720@deftypevr Macro int PF_INET
d08a7e4c 721@standards{BSD, sys/socket.h}
4bca4c17
UD
722This designates the IPv4 Internet namespace and associated family of
723protocols.
724@end deftypevr
725
04b9968b 726@deftypevr Macro int PF_INET6
d08a7e4c 727@standards{X/Open, sys/socket.h}
4bca4c17 728This designates the IPv6 Internet namespace and associated family of
28f540f4
RM
729protocols.
730@end deftypevr
731
732A socket address for the Internet namespace includes the following components:
733
734@itemize @bullet
735@item
736The address of the machine you want to connect to. Internet addresses
737can be specified in several ways; these are discussed in @ref{Internet
04b9968b 738Address Formats}, @ref{Host Addresses} and @ref{Host Names}.
28f540f4
RM
739
740@item
741A port number for that machine. @xref{Ports}.
742@end itemize
743
744You must ensure that the address and port number are represented in a
745canonical format called @dfn{network byte order}. @xref{Byte Order},
746for information about this.
747
748@menu
3996f34b 749* Internet Address Formats:: How socket addresses are specified in the
28f540f4 750 Internet namespace.
04b9968b 751* Host Addresses:: All about host addresses of Internet host.
28f540f4
RM
752* Ports:: Internet port numbers.
753* Services Database:: Ports may have symbolic names.
754* Byte Order:: Different hosts may use different byte
755 ordering conventions; you need to
6e953631 756 canonicalize host address and port number.
faf6f8bc 757* Protocols Database:: Referring to protocols by name.
28f540f4
RM
758* Inet Example:: Putting it all together.
759@end menu
760
3996f34b
UD
761@node Internet Address Formats
762@subsection Internet Socket Address Formats
28f540f4 763
3996f34b
UD
764In the Internet namespace, for both IPv4 (@code{AF_INET}) and IPv6
765(@code{AF_INET6}), a socket address consists of a host address
28f540f4
RM
766and a port on that host. In addition, the protocol you choose serves
767effectively as a part of the address because local port numbers are
768meaningful only within a particular protocol.
769
3996f34b
UD
770The data types for representing socket addresses in the Internet namespace
771are defined in the header file @file{netinet/in.h}.
28f540f4
RM
772@pindex netinet/in.h
773
28f540f4 774@deftp {Data Type} {struct sockaddr_in}
d08a7e4c 775@standards{BSD, netinet/in.h}
28f540f4
RM
776This is the data type used to represent socket addresses in the
777Internet namespace. It has the following members:
778
779@table @code
4bca4c17 780@item sa_family_t sin_family
28f540f4 781This identifies the address family or format of the socket address.
04b9968b 782You should store the value @code{AF_INET} in this member.
28f540f4
RM
783@xref{Socket Addresses}.
784
785@item struct in_addr sin_addr
786This is the Internet address of the host machine. @xref{Host
787Addresses}, and @ref{Host Names}, for how to get a value to store
788here.
789
790@item unsigned short int sin_port
791This is the port number. @xref{Ports}.
792@end table
793@end deftp
794
795When you call @code{bind} or @code{getsockname}, you should specify
796@code{sizeof (struct sockaddr_in)} as the @var{length} parameter if
4bca4c17 797you are using an IPv4 Internet namespace socket address.
28f540f4 798
3996f34b
UD
799@deftp {Data Type} {struct sockaddr_in6}
800This is the data type used to represent socket addresses in the IPv6
801namespace. It has the following members:
802
803@table @code
4bca4c17 804@item sa_family_t sin6_family
3996f34b
UD
805This identifies the address family or format of the socket address.
806You should store the value of @code{AF_INET6} in this member.
807@xref{Socket Addresses}.
808
809@item struct in6_addr sin6_addr
810This is the IPv6 address of the host machine. @xref{Host
811Addresses}, and @ref{Host Names}, for how to get a value to store
812here.
813
814@item uint32_t sin6_flowinfo
815This is a currently unimplemented field.
816
817@item uint16_t sin6_port
818This is the port number. @xref{Ports}.
819
820@end table
821@end deftp
822
28f540f4
RM
823@node Host Addresses
824@subsection Host Addresses
825
826Each computer on the Internet has one or more @dfn{Internet addresses},
827numbers which identify that computer among all those on the Internet.
3996f34b
UD
828Users typically write IPv4 numeric host addresses as sequences of four
829numbers, separated by periods, as in @samp{128.52.46.32}, and IPv6
f2ea0f5b 830numeric host addresses as sequences of up to eight numbers separated by
3996f34b 831colons, as in @samp{5f03:1200:836f:c100::1}.
28f540f4
RM
832
833Each computer also has one or more @dfn{host names}, which are strings
1d3c8572 834of words separated by periods, as in @samp{www.gnu.org}.
28f540f4
RM
835
836Programs that let the user specify a host typically accept both numeric
04b9968b
UD
837addresses and host names. To open a connection a program needs a
838numeric address, and so must convert a host name to the numeric address
839it stands for.
28f540f4
RM
840
841@menu
842* Abstract Host Addresses:: What a host number consists of.
843* Data type: Host Address Data Type. Data type for a host number.
844* Functions: Host Address Functions. Functions to operate on them.
845* Names: Host Names. Translating host names to host numbers.
846@end menu
847
6e953631 848@node Abstract Host Addresses
28f540f4
RM
849@subsubsection Internet Host Addresses
850@cindex host address, Internet
851@cindex Internet host address
852
853@ifinfo
854Each computer on the Internet has one or more Internet addresses,
855numbers which identify that computer among all those on the Internet.
856@end ifinfo
857
858@cindex network number
859@cindex local network address number
4bca4c17
UD
860An IPv4 Internet host address is a number containing four bytes of data.
861Historically these are divided into two parts, a @dfn{network number} and a
862@dfn{local network address number} within that network. In the
04b9968b 863mid-1990s classless addresses were introduced which changed this
0bc93a2f 864behavior. Since some functions implicitly expect the old definitions,
04b9968b
UD
865we first describe the class-based network and will then describe
866classless addresses. IPv6 uses only classless addresses and therefore
4bca4c17
UD
867the following paragraphs don't apply.
868
04b9968b 869The class-based IPv4 network number consists of the first one, two or
4bca4c17
UD
870three bytes; the rest of the bytes are the local address.
871
872IPv4 network numbers are registered with the Network Information Center
04b9968b 873(NIC), and are divided into three classes---A, B and C. The local
28f540f4
RM
874network address numbers of individual machines are registered with the
875administrator of the particular network.
876
877Class A networks have single-byte numbers in the range 0 to 127. There
878are only a small number of Class A networks, but they can each support a
879very large number of hosts. Medium-sized Class B networks have two-byte
880network numbers, with the first byte in the range 128 to 191. Class C
881networks are the smallest; they have three-byte network numbers, with
882the first byte in the range 192-255. Thus, the first 1, 2, or 3 bytes
04b9968b 883of an Internet address specify a network. The remaining bytes of the
28f540f4
RM
884Internet address specify the address within that network.
885
886The Class A network 0 is reserved for broadcast to all networks. In
6e953631 887addition, the host number 0 within each network is reserved for broadcast
04b9968b 888to all hosts in that network. These uses are obsolete now but for
4bca4c17 889compatibility reasons you shouldn't use network 0 and host number 0.
28f540f4
RM
890
891The Class A network 127 is reserved for loopback; you can always use
892the Internet address @samp{127.0.0.1} to refer to the host machine.
893
894Since a single machine can be a member of multiple networks, it can
895have multiple Internet host addresses. However, there is never
896supposed to be more than one machine with the same host address.
897
898@c !!! this section could document the IN_CLASS* macros in <netinet/in.h>.
4bca4c17 899@c No, it shouldn't since they're obsolete.
28f540f4
RM
900
901@cindex standard dot notation, for Internet addresses
902@cindex dot notation, for Internet addresses
903There are four forms of the @dfn{standard numbers-and-dots notation}
904for Internet addresses:
905
906@table @code
907@item @var{a}.@var{b}.@var{c}.@var{d}
4bca4c17
UD
908This specifies all four bytes of the address individually and is the
909commonly used representation.
28f540f4
RM
910
911@item @var{a}.@var{b}.@var{c}
912The last part of the address, @var{c}, is interpreted as a 2-byte quantity.
913This is useful for specifying host addresses in a Class B network with
914network address number @code{@var{a}.@var{b}}.
915
916@item @var{a}.@var{b}
4bca4c17 917The last part of the address, @var{b}, is interpreted as a 3-byte quantity.
28f540f4
RM
918This is useful for specifying host addresses in a Class A network with
919network address number @var{a}.
920
921@item @var{a}
922If only one part is given, this corresponds directly to the host address
923number.
924@end table
925
926Within each part of the address, the usual C conventions for specifying
927the radix apply. In other words, a leading @samp{0x} or @samp{0X} implies
928hexadecimal radix; a leading @samp{0} implies octal; and otherwise decimal
929radix is assumed.
930
4bca4c17
UD
931@subsubheading Classless Addresses
932
04b9968b
UD
933IPv4 addresses (and IPv6 addresses also) are now considered classless;
934the distinction between classes A, B and C can be ignored. Instead an
935IPv4 host address consists of a 32-bit address and a 32-bit mask. The
936mask contains set bits for the network part and cleared bits for the
937host part. The network part is contiguous from the left, with the
938remaining bits representing the host. As a consequence, the netmask can
939simply be specified as the number of set bits. Classes A, B and C are
940just special cases of this general rule. For example, class A addresses
941have a netmask of @samp{255.0.0.0} or a prefix length of 8.
4bca4c17
UD
942
943Classless IPv4 network addresses are written in numbers-and-dots
944notation with the prefix length appended and a slash as separator. For
945example the class A network 10 is written as @samp{10.0.0.0/8}.
946
947@subsubheading IPv6 Addresses
948
949IPv6 addresses contain 128 bits (IPv4 has 32 bits) of data. A host
950address is usually written as eight 16-bit hexadecimal numbers that are
73237de3 951separated by colons. Two colons are used to abbreviate strings of
04b9968b
UD
952consecutive zeros. For example, the IPv6 loopback address
953@samp{0:0:0:0:0:0:0:1} can just be written as @samp{::1}.
4bca4c17 954
28f540f4
RM
955@node Host Address Data Type
956@subsubsection Host Address Data Type
957
4bca4c17
UD
958IPv4 Internet host addresses are represented in some contexts as integers
959(type @code{uint32_t}). In other contexts, the integer is
28f540f4
RM
960packaged inside a structure of type @code{struct in_addr}. It would
961be better if the usage were made consistent, but it is not hard to extract
962the integer from the structure or put the integer into a structure.
963
4bca4c17
UD
964You will find older code that uses @code{unsigned long int} for
965IPv4 Internet host addresses instead of @code{uint32_t} or @code{struct
04b9968b
UD
966in_addr}. Historically @code{unsigned long int} was a 32-bit number but
967with 64-bit machines this has changed. Using @code{unsigned long int}
4bca4c17
UD
968might break the code if it is used on machines where this type doesn't
969have 32 bits. @code{uint32_t} is specified by Unix98 and guaranteed to have
97032 bits.
971
972IPv6 Internet host addresses have 128 bits and are packaged inside a
973structure of type @code{struct in6_addr}.
974
9afc8a59
UD
975The following basic definitions for Internet addresses are declared in
976the header file @file{netinet/in.h}:
28f540f4
RM
977@pindex netinet/in.h
978
28f540f4 979@deftp {Data Type} {struct in_addr}
d08a7e4c 980@standards{BSD, netinet/in.h}
4bca4c17
UD
981This data type is used in certain contexts to contain an IPv4 Internet
982host address. It has just one field, named @code{s_addr}, which records
983the host address number as an @code{uint32_t}.
28f540f4
RM
984@end deftp
985
4bca4c17 986@deftypevr Macro {uint32_t} INADDR_LOOPBACK
d08a7e4c 987@standards{BSD, netinet/in.h}
28f540f4 988You can use this constant to stand for ``the address of this machine,''
4bca4c17 989instead of finding its actual address. It is the IPv4 Internet address
28f540f4
RM
990@samp{127.0.0.1}, which is usually called @samp{localhost}. This
991special constant saves you the trouble of looking up the address of your
992own machine. Also, the system usually implements @code{INADDR_LOOPBACK}
993specially, avoiding any network traffic for the case of one machine
994talking to itself.
995@end deftypevr
996
4bca4c17 997@deftypevr Macro {uint32_t} INADDR_ANY
d08a7e4c 998@standards{BSD, netinet/in.h}
04b9968b 999You can use this constant to stand for ``any incoming address'' when
28f540f4
RM
1000binding to an address. @xref{Setting Address}. This is the usual
1001address to give in the @code{sin_addr} member of @w{@code{struct
1002sockaddr_in}} when you want to accept Internet connections.
1003@end deftypevr
1004
4bca4c17 1005@deftypevr Macro {uint32_t} INADDR_BROADCAST
d08a7e4c 1006@standards{BSD, netinet/in.h}
28f540f4
RM
1007This constant is the address you use to send a broadcast message.
1008@c !!! broadcast needs further documented
1009@end deftypevr
1010
4bca4c17 1011@deftypevr Macro {uint32_t} INADDR_NONE
d08a7e4c 1012@standards{BSD, netinet/in.h}
28f540f4
RM
1013This constant is returned by some functions to indicate an error.
1014@end deftypevr
1015
3996f34b 1016@deftp {Data Type} {struct in6_addr}
d08a7e4c 1017@standards{IPv6 basic API, netinet/in.h}
3996f34b
UD
1018This data type is used to store an IPv6 address. It stores 128 bits of
1019data, which can be accessed (via a union) in a variety of ways.
1020@end deftp
1021
310b3460 1022@deftypevr Constant {struct in6_addr} in6addr_loopback
d08a7e4c 1023@standards{IPv6 basic API, netinet/in.h}
3996f34b
UD
1024This constant is the IPv6 address @samp{::1}, the loopback address. See
1025above for a description of what this means. The macro
04b9968b 1026@code{IN6ADDR_LOOPBACK_INIT} is provided to allow you to initialize your
3996f34b
UD
1027own variables to this value.
1028@end deftypevr
1029
3996f34b 1030@deftypevr Constant {struct in6_addr} in6addr_any
d08a7e4c 1031@standards{IPv6 basic API, netinet/in.h}
3996f34b
UD
1032This constant is the IPv6 address @samp{::}, the unspecified address. See
1033above for a description of what this means. The macro
04b9968b 1034@code{IN6ADDR_ANY_INIT} is provided to allow you to initialize your
3996f34b
UD
1035own variables to this value.
1036@end deftypevr
1037
28f540f4
RM
1038@node Host Address Functions
1039@subsubsection Host Address Functions
1040
1041@pindex arpa/inet.h
838e5ffe 1042@noindent
28f540f4 1043These additional functions for manipulating Internet addresses are
9afc8a59 1044declared in the header file @file{arpa/inet.h}. They represent Internet
04b9968b 1045addresses in network byte order, and network numbers and
9afc8a59
UD
1046local-address-within-network numbers in host byte order. @xref{Byte
1047Order}, for an explanation of network and host byte order.
28f540f4 1048
8f2ece69 1049@deftypefun int inet_aton (const char *@var{name}, struct in_addr *@var{addr})
d08a7e4c 1050@standards{BSD, arpa/inet.h}
973f180b
AO
1051@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
1052@c inet_aton @mtslocale
1053@c isdigit dup @mtslocale
1054@c strtoul dup @mtslocale
1055@c isascii dup @mtslocale
1056@c isspace dup @mtslocale
1057@c htonl dup ok
4bca4c17 1058This function converts the IPv4 Internet host address @var{name}
28f540f4
RM
1059from the standard numbers-and-dots notation into binary data and stores
1060it in the @code{struct in_addr} that @var{addr} points to.
1061@code{inet_aton} returns nonzero if the address is valid, zero if not.
1062@end deftypefun
1063
4bca4c17 1064@deftypefun {uint32_t} inet_addr (const char *@var{name})
d08a7e4c 1065@standards{BSD, arpa/inet.h}
973f180b
AO
1066@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
1067@c inet_addr @mtslocale
1068@c inet_aton dup @mtslocale
4bca4c17 1069This function converts the IPv4 Internet host address @var{name} from the
28f540f4
RM
1070standard numbers-and-dots notation into binary data. If the input is
1071not valid, @code{inet_addr} returns @code{INADDR_NONE}. This is an
cf822e3c 1072obsolete interface to @code{inet_aton}, described immediately above. It
28f540f4
RM
1073is obsolete because @code{INADDR_NONE} is a valid address
1074(255.255.255.255), and @code{inet_aton} provides a cleaner way to
1075indicate error return.
1076@end deftypefun
1077
4bca4c17 1078@deftypefun {uint32_t} inet_network (const char *@var{name})
d08a7e4c 1079@standards{BSD, arpa/inet.h}
973f180b
AO
1080@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
1081@c inet_network @mtslocale
1082@c isdigit dup @mtslocale
1083@c isxdigit dup @mtslocale
1084@c tolower dup @mtslocale
1085@c isspace dup @mtslocale
28f540f4 1086This function extracts the network number from the address @var{name},
cf822e3c
OB
1087given in the standard numbers-and-dots notation. The returned address is
1088in host order. If the input is not valid, @code{inet_network} returns
da2d1bc5 1089@code{-1}.
4bca4c17 1090
73237de3 1091The function works only with traditional IPv4 class A, B and C network
4bca4c17
UD
1092types. It doesn't work with classless addresses and shouldn't be used
1093anymore.
28f540f4
RM
1094@end deftypefun
1095
28f540f4 1096@deftypefun {char *} inet_ntoa (struct in_addr @var{addr})
d08a7e4c 1097@standards{BSD, arpa/inet.h}
973f180b
AO
1098@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asurace{}}@acsafe{}}
1099@c inet_ntoa @mtslocale @asurace
1100@c writes to a thread-local static buffer
1101@c snprintf @mtslocale [no @ascuheap or @acsmem]
4bca4c17 1102This function converts the IPv4 Internet host address @var{addr} to a
28f540f4
RM
1103string in the standard numbers-and-dots notation. The return value is
1104a pointer into a statically-allocated buffer. Subsequent calls will
1105overwrite the same buffer, so you should copy the string if you need
1106to save it.
fe7bdd63 1107
b57dd246 1108In multi-threaded programs each thread has its own statically-allocated
fe7bdd63
UD
1109buffer. But still subsequent calls of @code{inet_ntoa} in the same
1110thread will overwrite the result of the last call.
4bca4c17
UD
1111
1112Instead of @code{inet_ntoa} the newer function @code{inet_ntop} which is
1113described below should be used since it handles both IPv4 and IPv6
1114addresses.
28f540f4
RM
1115@end deftypefun
1116
4bca4c17 1117@deftypefun {struct in_addr} inet_makeaddr (uint32_t @var{net}, uint32_t @var{local})
d08a7e4c 1118@standards{BSD, arpa/inet.h}
973f180b
AO
1119@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1120@c inet_makeaddr ok
1121@c htonl dup ok
4bca4c17 1122This function makes an IPv4 Internet host address by combining the network
28f540f4
RM
1123number @var{net} with the local-address-within-network number
1124@var{local}.
1125@end deftypefun
1126
4bca4c17 1127@deftypefun uint32_t inet_lnaof (struct in_addr @var{addr})
d08a7e4c 1128@standards{BSD, arpa/inet.h}
973f180b
AO
1129@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1130@c inet_lnaof ok
1131@c ntohl dup ok
1132@c IN_CLASSA ok
1133@c IN_CLASSB ok
28f540f4
RM
1134This function returns the local-address-within-network part of the
1135Internet host address @var{addr}.
4bca4c17 1136
73237de3 1137The function works only with traditional IPv4 class A, B and C network
4bca4c17
UD
1138types. It doesn't work with classless addresses and shouldn't be used
1139anymore.
28f540f4
RM
1140@end deftypefun
1141
4bca4c17 1142@deftypefun uint32_t inet_netof (struct in_addr @var{addr})
d08a7e4c 1143@standards{BSD, arpa/inet.h}
973f180b
AO
1144@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1145@c inet_netof ok
1146@c ntohl dup ok
1147@c IN_CLASSA ok
1148@c IN_CLASSB ok
28f540f4
RM
1149This function returns the network number part of the Internet host
1150address @var{addr}.
4bca4c17 1151
73237de3 1152The function works only with traditional IPv4 class A, B and C network
4bca4c17
UD
1153types. It doesn't work with classless addresses and shouldn't be used
1154anymore.
28f540f4
RM
1155@end deftypefun
1156
8f2ece69 1157@deftypefun int inet_pton (int @var{af}, const char *@var{cp}, void *@var{buf})
d08a7e4c 1158@standards{IPv6 basic API, arpa/inet.h}
973f180b
AO
1159@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
1160@c inet_pton @mtslocale
1161@c inet_pton4 ok
1162@c memcpy dup ok
1163@c inet_pton6 @mtslocale
1164@c memset dup ok
1165@c tolower dup @mtslocale
1166@c strchr dup ok
1167@c inet_pton4 dup ok
1168@c memcpy dup ok
3996f34b
UD
1169This function converts an Internet address (either IPv4 or IPv6) from
1170presentation (textual) to network (binary) format. @var{af} should be
1171either @code{AF_INET} or @code{AF_INET6}, as appropriate for the type of
1172address being converted. @var{cp} is a pointer to the input string, and
1173@var{buf} is a pointer to a buffer for the result. It is the caller's
1174responsibility to make sure the buffer is large enough.
1175@end deftypefun
1176
8ded91fb 1177@deftypefun {const char *} inet_ntop (int @var{af}, const void *@var{cp}, char *@var{buf}, socklen_t @var{len})
d08a7e4c 1178@standards{IPv6 basic API, arpa/inet.h}
973f180b
AO
1179@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
1180@c inet_ntop @mtslocale
1181@c inet_ntop4 @mtslocale
1182@c sprintf dup @mtslocale [no @ascuheap or @acsmem]
1183@c strcpy dup ok
1184@c inet_ntop6 @mtslocale
1185@c memset dup ok
1186@c inet_ntop4 dup @mtslocale
1187@c sprintf dup @mtslocale [no @ascuheap or @acsmem]
1188@c strcpy dup ok
3996f34b
UD
1189This function converts an Internet address (either IPv4 or IPv6) from
1190network (binary) to presentation (textual) form. @var{af} should be
1191either @code{AF_INET} or @code{AF_INET6}, as appropriate. @var{cp} is a
1192pointer to the address to be converted. @var{buf} should be a pointer
1193to a buffer to hold the result, and @var{len} is the length of this
1194buffer. The return value from the function will be this buffer address.
1195@end deftypefun
1196
28f540f4
RM
1197@node Host Names
1198@subsubsection Host Names
1199@cindex hosts database
1200@cindex converting host name to address
1201@cindex converting host address to name
1202
1203Besides the standard numbers-and-dots notation for Internet addresses,
1204you can also refer to a host by a symbolic name. The advantage of a
1205symbolic name is that it is usually easier to remember. For example,
4bca4c17
UD
1206the machine with Internet address @samp{158.121.106.19} is also known as
1207@samp{alpha.gnu.org}; and other machines in the @samp{gnu.org}
1208domain can refer to it simply as @samp{alpha}.
28f540f4
RM
1209
1210@pindex /etc/hosts
1211@pindex netdb.h
1212Internally, the system uses a database to keep track of the mapping
1213between host names and host numbers. This database is usually either
1214the file @file{/etc/hosts} or an equivalent provided by a name server.
1215The functions and other symbols for accessing this database are declared
1216in @file{netdb.h}. They are BSD features, defined unconditionally if
1217you include @file{netdb.h}.
1218
28f540f4 1219@deftp {Data Type} {struct hostent}
d08a7e4c 1220@standards{BSD, netdb.h}
28f540f4
RM
1221This data type is used to represent an entry in the hosts database. It
1222has the following members:
1223
1224@table @code
1225@item char *h_name
1226This is the ``official'' name of the host.
1227
1228@item char **h_aliases
1229These are alternative names for the host, represented as a null-terminated
1230vector of strings.
1231
1232@item int h_addrtype
3996f34b
UD
1233This is the host address type; in practice, its value is always either
1234@code{AF_INET} or @code{AF_INET6}, with the latter being used for IPv6
1235hosts. In principle other kinds of addresses could be represented in
04b9968b 1236the database as well as Internet addresses; if this were done, you
3996f34b
UD
1237might find a value in this field other than @code{AF_INET} or
1238@code{AF_INET6}. @xref{Socket Addresses}.
28f540f4
RM
1239
1240@item int h_length
1241This is the length, in bytes, of each address.
1242
1243@item char **h_addr_list
1244This is the vector of addresses for the host. (Recall that the host
1245might be connected to multiple networks and have different addresses on
1246each one.) The vector is terminated by a null pointer.
1247
1248@item char *h_addr
1249This is a synonym for @code{h_addr_list[0]}; in other words, it is the
1250first host address.
1251@end table
1252@end deftp
1253
1254As far as the host database is concerned, each address is just a block
1255of memory @code{h_length} bytes long. But in other contexts there is an
4bca4c17
UD
1256implicit assumption that you can convert IPv4 addresses to a
1257@code{struct in_addr} or an @code{uint32_t}. Host addresses in
1258a @code{struct hostent} structure are always given in network byte
1259order; see @ref{Byte Order}.
28f540f4 1260
3996f34b
UD
1261You can use @code{gethostbyname}, @code{gethostbyname2} or
1262@code{gethostbyaddr} to search the hosts database for information about
1263a particular host. The information is returned in a
1264statically-allocated structure; you must copy the information if you
1265need to save it across calls. You can also use @code{getaddrinfo} and
1266@code{getnameinfo} to obtain this information.
28f540f4 1267
28f540f4 1268@deftypefun {struct hostent *} gethostbyname (const char *@var{name})
d08a7e4c 1269@standards{BSD, netdb.h}
973f180b
AO
1270@safety{@prelim{}@mtunsafe{@mtasurace{:hostbyname} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{} @acsfd{}}}
1271@c gethostbyname @mtasurace:hostbyname @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
1272@c libc_lock_lock dup @asulock @aculock
1273@c malloc dup @ascuheap @acsmem
1274@c nss_hostname_digits_dots @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
1275@c res_maybe_init(!preinit) @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
1276@c res_iclose @acsuheap @acsmem @acsfd
1277@c close_not_cancel_no_status dup @acsfd
1278@c free dup @acsuheap @acsmem
1279@c res_vinit @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
1280@c res_randomid ok
1281@c getpid dup ok
1282@c getenv dup @mtsenv
1283@c strncpy dup ok
1284@c fopen dup @ascuheap @asulock @acsmem @acsfd @aculock
1285@c fsetlocking dup ok [no concurrent uses]
1286@c fgets_unlocked dup ok [no concurrent uses]
1287@c MATCH ok
1288@c strncmp dup ok
1289@c strpbrk dup ok
1290@c strchr dup ok
1291@c inet_aton dup @mtslocale
1292@c htons dup
1293@c inet_pton dup @mtslocale
1294@c malloc dup @ascuheap @acsmem
1295@c IN6_IS_ADDR_LINKLOCAL ok
1296@c htonl dup ok
1297@c IN6_IS_ADDR_MC_LINKLOCAL ok
1298@c if_nametoindex dup @asulock @aculock @acsfd
1299@c strtoul dup @mtslocale
1300@c ISSORTMASK ok
1301@c strchr dup ok
1302@c isascii dup @mtslocale
1303@c isspace dup @mtslocale
1304@c net_mask ok
1305@c ntohl dup ok
1306@c IN_CLASSA dup ok
1307@c htonl dup ok
1308@c IN_CLASSB dup ok
1309@c res_setoptions @mtslocale
1310@c strncmp dup ok
1311@c atoi dup @mtslocale
1312@c fclose dup @ascuheap @asulock @aculock @acsmem @acsfd
1313@c inet_makeaddr dup ok
1314@c gethostname dup ok
1315@c strcpy dup ok
1316@c rawmemchr dup ok
1317@c res_ninit @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
1318@c res_vinit dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
1319@c isdigit dup @mtslocale
1320@c isxdigit dup @mtslocale
1321@c strlen dup ok
1322@c realloc dup @ascuheap @acsmem
1323@c free dup @ascuheap @acsmem
1324@c memset dup ok
1325@c inet_aton dup @mtslocale
1326@c inet_pton dup @mtslocale
1327@c strcpy dup ok
1328@c memcpy dup ok
1329@c strchr dup ok
1330@c gethostbyname_r dup @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
1331@c realloc dup @ascuheap @acsmem
1332@c free dup @ascuheap @acsmem
1333@c libc_lock_unlock dup @aculock
1334@c set_h_errno ok
28f540f4
RM
1335The @code{gethostbyname} function returns information about the host
1336named @var{name}. If the lookup fails, it returns a null pointer.
1337@end deftypefun
1338
3996f34b 1339@deftypefun {struct hostent *} gethostbyname2 (const char *@var{name}, int @var{af})
d08a7e4c 1340@standards{IPv6 Basic API, netdb.h}
973f180b
AO
1341@safety{@prelim{}@mtunsafe{@mtasurace{:hostbyname2} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{} @acsfd{}}}
1342@c gethostbyname2 @mtasurace:hostbyname2 @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
1343@c libc_lock_lock dup @asulock @aculock
1344@c malloc dup @ascuheap @acsmem
1345@c nss_hostname_digits_dots dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
1346@c gethostbyname2_r dup @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
1347@c realloc dup @ascuheap @acsmem
1348@c free dup @ascuheap @acsmem
1349@c libc_lock_unlock dup @aculock
1350@c set_h_errno dup ok
3996f34b
UD
1351The @code{gethostbyname2} function is like @code{gethostbyname}, but
1352allows the caller to specify the desired address family (e.g.@:
04b9968b 1353@code{AF_INET} or @code{AF_INET6}) of the result.
3996f34b
UD
1354@end deftypefun
1355
8ded91fb 1356@deftypefun {struct hostent *} gethostbyaddr (const void *@var{addr}, socklen_t @var{length}, int @var{format})
d08a7e4c 1357@standards{BSD, netdb.h}
973f180b
AO
1358@safety{@prelim{}@mtunsafe{@mtasurace{:hostbyaddr} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{} @acsfd{}}}
1359@c gethostbyaddr @mtasurace:hostbyaddr @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
1360@c libc_lock_lock dup @asulock @aculock
1361@c malloc dup @ascuheap @acsmem
1362@c gethostbyaddr_r dup @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
1363@c realloc dup @ascuheap @acsmem
1364@c free dup @ascuheap @acsmem
1365@c libc_lock_unlock dup @aculock
1366@c set_h_errno dup ok
28f540f4 1367The @code{gethostbyaddr} function returns information about the host
4bca4c17
UD
1368with Internet address @var{addr}. The parameter @var{addr} is not
1369really a pointer to char - it can be a pointer to an IPv4 or an IPv6
cf822e3c 1370address. The @var{length} argument is the size (in bytes) of the address
4bca4c17
UD
1371at @var{addr}. @var{format} specifies the address format; for an IPv4
1372Internet address, specify a value of @code{AF_INET}; for an IPv6
1373Internet address, use @code{AF_INET6}.
28f540f4
RM
1374
1375If the lookup fails, @code{gethostbyaddr} returns a null pointer.
1376@end deftypefun
1377
1378@vindex h_errno
1379If the name lookup by @code{gethostbyname} or @code{gethostbyaddr}
1380fails, you can find out the reason by looking at the value of the
1381variable @code{h_errno}. (It would be cleaner design for these
1382functions to set @code{errno}, but use of @code{h_errno} is compatible
b99a8a11 1383with other systems.)
28f540f4
RM
1384
1385Here are the error codes that you may find in @code{h_errno}:
1386
2fe82ca6 1387@vtable @code
28f540f4 1388@item HOST_NOT_FOUND
d08a7e4c 1389@standards{BSD, netdb.h}
04b9968b 1390No such host is known in the database.
28f540f4 1391
28f540f4 1392@item TRY_AGAIN
d08a7e4c 1393@standards{BSD, netdb.h}
28f540f4
RM
1394This condition happens when the name server could not be contacted. If
1395you try again later, you may succeed then.
1396
6e953631 1397@item NO_RECOVERY
d08a7e4c 1398@standards{BSD, netdb.h}
28f540f4
RM
1399A non-recoverable error occurred.
1400
28f540f4 1401@item NO_ADDRESS
d08a7e4c 1402@standards{BSD, netdb.h}
28f540f4
RM
1403The host database contains an entry for the name, but it doesn't have an
1404associated Internet address.
2fe82ca6 1405@end vtable
28f540f4 1406
b57dd246 1407The lookup functions above all have one thing in common: they are not
1670698f 1408reentrant and therefore unusable in multi-threaded applications.
1f77f049 1409Therefore provides @theglibc{} a new set of functions which can be
1670698f
UD
1410used in this context.
1411
1670698f 1412@deftypefun int gethostbyname_r (const char *restrict @var{name}, struct hostent *restrict @var{result_buf}, char *restrict @var{buf}, size_t @var{buflen}, struct hostent **restrict @var{result}, int *restrict @var{h_errnop})
d08a7e4c 1413@standards{GNU, netdb.h}
973f180b
AO
1414@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{} @acsfd{}}}
1415@c gethostbyname_r @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
1416@c nss_hostname_digits_dots dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
1417@c nscd_gethostbyname_r @mtsenv @ascuheap @acsfd @acsmem
1418@c nscd_gethst_r @mtsenv @ascuheap @acsfd @acsmem
1419@c getenv dup @mtsenv
1420@c nscd_get_map_ref dup @ascuheap @acsfd @acsmem
1421@c nscd_cache_search dup ok
1422@c memcpy dup ok
1423@c nscd_open_socket dup @acsfd
1424@c readvall dup ok
1425@c readall dup ok
1426@c close_not_cancel_no_status dup @acsfd
1427@c nscd_drop_map_ref dup @ascuheap @acsmem
1428@c nscd_unmap dup @ascuheap @acsmem
1429@c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
1430@c res_hconf_init @mtsenv @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem [no @asuinit:reshconf @acuinit:reshconf, conditionally called]
1431@c res_hconf.c:do_init @mtsenv @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem
1432@c memset dup ok
1433@c getenv dup @mtsenv
1434@c fopen dup @ascuheap @asulock @acsmem @acsfd @aculock
1435@c fsetlocking dup ok [no concurrent uses]
1436@c fgets_unlocked dup ok [no concurrent uses]
1437@c strchrnul dup ok
1438@c res_hconf.c:parse_line @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem
1439@c skip_ws dup @mtslocale
1440@c skip_string dup @mtslocale
1441@c strncasecmp dup @mtslocale
1442@c strlen dup ok
1443@c asprintf dup @mtslocale @ascuheap @acsmem
1444@c fxprintf dup @asucorrupt @aculock @acucorrupt
1445@c free dup @ascuheap @acsmem
1446@c arg_trimdomain_list dup @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem
1447@c arg_spoof dup @mtslocale
1448@c arg_bool dup @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem
1449@c isspace dup @mtslocale
1450@c fclose dup @ascuheap @asulock @acsmem @acsfd @aculock
1451@c arg_spoof @mtslocale
1452@c skip_string @mtslocale
1453@c isspace dup @mtslocale
1454@c strncasecmp dup @mtslocale
1455@c arg_bool @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem
1456@c strncasecmp dup @mtslocale
1457@c asprintf dup @mtslocale @ascuheap @acsmem
1458@c fxprintf dup @asucorrupt @aculock @acucorrupt
1459@c free dup @ascuheap @acsmem
1460@c arg_trimdomain_list @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem
1461@c skip_string dup @mtslocale
1462@c asprintf dup @mtslocale @ascuheap @acsmem
1463@c fxprintf dup @asucorrupt @aculock @acucorrupt
1464@c free dup @ascuheap @acsmem
1465@c strndup dup @ascuheap @acsmem
1466@c skip_ws @mtslocale
1467@c isspace dup @mtslocale
1468@c nss_hosts_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1469@c nss_database_lookup dup @mtslocale @ascuheap @asulock @acucorrupt @acsmem @acsfd @aculock
1470@c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1471@c *fct.l -> _nss_*_gethostbyname_r @ascuplugin
1472@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1473@c res_hconf_reorder_addrs @asulock @ascuheap @aculock @acsmem @acsfd
1474@c socket dup @acsfd
1475@c libc_lock_lock dup @asulock @aculock
1476@c ifreq @ascuheap @acsmem
1477@c malloc dup @ascuheap @acsmem
1478@c if_nextreq dup ok
1479@c ioctl dup ok
1480@c realloc dup @ascuheap @acsmem
1481@c if_freereq dup @acsmem
1482@c libc_lock_unlock dup @aculock
1483@c close dup @acsfd
1670698f
UD
1484The @code{gethostbyname_r} function returns information about the host
1485named @var{name}. The caller must pass a pointer to an object of type
1486@code{struct hostent} in the @var{result_buf} parameter. In addition
b57dd246 1487the function may need extra buffer space and the caller must pass a
1670698f
UD
1488pointer and the size of the buffer in the @var{buf} and @var{buflen}
1489parameters.
1490
1491A pointer to the buffer, in which the result is stored, is available in
cf822e3c 1492@code{*@var{result}} after the function call successfully returned. The
b86835ca
YC
1493buffer passed as the @var{buf} parameter can be freed only once the caller
1494has finished with the result hostent struct, or has copied it including all
cf822e3c
OB
1495the other memory that it points to. If an error occurs or if no entry is
1496found, the pointer @code{*@var{result}} is a null pointer. Success is
b86835ca
YC
1497signalled by a zero return value. If the function failed the return value
1498is an error number. In addition to the errors defined for
cf822e3c
OB
1499@code{gethostbyname} it can also be @code{ERANGE}. In this case the call
1500should be repeated with a larger buffer. Additional error information is
b86835ca
YC
1501not stored in the global variable @code{h_errno} but instead in the object
1502pointed to by @var{h_errnop}.
0ea5db4f
UD
1503
1504Here's a small example:
1505@smallexample
1506struct hostent *
1507gethostname (char *host)
1508@{
151659f6 1509 struct hostent *hostbuf, *hp;
0ea5db4f
UD
1510 size_t hstbuflen;
1511 char *tmphstbuf;
1512 int res;
1513 int herr;
1514
151659f6 1515 hostbuf = malloc (sizeof (struct hostent));
0ea5db4f
UD
1516 hstbuflen = 1024;
1517 tmphstbuf = malloc (hstbuflen);
1518
151659f6 1519 while ((res = gethostbyname_r (host, hostbuf, tmphstbuf, hstbuflen,
0ea5db4f
UD
1520 &hp, &herr)) == ERANGE)
1521 @{
1522 /* Enlarge the buffer. */
1523 hstbuflen *= 2;
1524 tmphstbuf = realloc (tmphstbuf, hstbuflen);
1525 @}
151659f6
OB
1526
1527 free (tmphstbuf);
0ea5db4f
UD
1528 /* Check for errors. */
1529 if (res || hp == NULL)
1530 return NULL;
9d8525f2 1531 return hp;
0ea5db4f
UD
1532@}
1533@end smallexample
1670698f
UD
1534@end deftypefun
1535
1670698f 1536@deftypefun int gethostbyname2_r (const char *@var{name}, int @var{af}, struct hostent *restrict @var{result_buf}, char *restrict @var{buf}, size_t @var{buflen}, struct hostent **restrict @var{result}, int *restrict @var{h_errnop})
d08a7e4c 1537@standards{GNU, netdb.h}
973f180b
AO
1538@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{} @acsfd{}}}
1539@c gethostbyname2_r @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
1540@c nss_hostname_digits_dots dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
1541@c nscd_gethostbyname2_r @mtsenv @ascuheap @asulock @aculock @acsfd @acsmem
1542@c nscd_gethst_r dup @mtsenv @ascuheap @asulock @aculock @acsfd @acsmem
1543@c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
1544@c res_hconf_init dup @mtsenv @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem [no @asuinit:reshconf @acuinit:reshconf, conditionally called]
1545@c nss_hosts_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1546@c *fct.l -> _nss_*_gethostbyname2_r @ascuplugin
1547@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1548@c res_hconf_reorder_addrs dup @asulock @ascuheap @aculock @acsmem @acsfd
1670698f
UD
1549The @code{gethostbyname2_r} function is like @code{gethostbyname_r}, but
1550allows the caller to specify the desired address family (e.g.@:
1551@code{AF_INET} or @code{AF_INET6}) for the result.
1552@end deftypefun
1553
8ded91fb 1554@deftypefun int gethostbyaddr_r (const void *@var{addr}, socklen_t @var{length}, int @var{format}, struct hostent *restrict @var{result_buf}, char *restrict @var{buf}, size_t @var{buflen}, struct hostent **restrict @var{result}, int *restrict @var{h_errnop})
d08a7e4c 1555@standards{GNU, netdb.h}
973f180b
AO
1556@safety{@prelim{}@mtsafe{@mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @asucorrupt{} @ascuheap{} @asulock{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{} @acsfd{}}}
1557@c gethostbyaddr_r @mtsenv @mtslocale @ascudlopen @ascuplugin @asucorrupt @ascuheap @asulock @aculock @acucorrupt @acsmem @acsfd
1558@c memcmp dup ok
1559@c nscd_gethostbyaddr_r @mtsenv @ascuheap @asulock @aculock @acsfd @acsmem
1560@c nscd_gethst_r dup @mtsenv @ascuheap @asulock @aculock @acsfd @acsmem
1561@c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
1562@c res_hconf_init dup @mtsenv @mtslocale @asucorrupt @ascuheap @aculock @acucorrupt @acsmem [no @asuinit:reshconf @acuinit:reshconf, conditionally called]
1563@c nss_hosts_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1564@c *fct.l -> _nss_*_gethostbyaddr_r @ascuplugin
1565@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1566@c res_hconf_reorder_addrs dup @asulock @ascuheap @aculock @acsmem @acsfd
1567@c res_hconf_trim_domains @mtslocale
1568@c res_hconf_trim_domain @mtslocale
1569@c strlen dup ok
1570@c strcasecmp dup @mtslocale
1670698f
UD
1571The @code{gethostbyaddr_r} function returns information about the host
1572with Internet address @var{addr}. The parameter @var{addr} is not
1573really a pointer to char - it can be a pointer to an IPv4 or an IPv6
cf822e3c 1574address. The @var{length} argument is the size (in bytes) of the address
1670698f
UD
1575at @var{addr}. @var{format} specifies the address format; for an IPv4
1576Internet address, specify a value of @code{AF_INET}; for an IPv6
1577Internet address, use @code{AF_INET6}.
1578
1579Similar to the @code{gethostbyname_r} function, the caller must provide
1580buffers for the result and memory used internally. In case of success
0ea5db4f 1581the function returns zero. Otherwise the value is an error number where
1670698f
UD
1582@code{ERANGE} has the special meaning that the caller-provided buffer is
1583too small.
1584@end deftypefun
1585
28f540f4 1586You can also scan the entire hosts database one entry at a time using
04b9968b
UD
1587@code{sethostent}, @code{gethostent} and @code{endhostent}. Be careful
1588when using these functions because they are not reentrant.
28f540f4 1589
28f540f4 1590@deftypefun void sethostent (int @var{stayopen})
d08a7e4c 1591@standards{BSD, netdb.h}
973f180b
AO
1592@safety{@prelim{}@mtunsafe{@mtasurace{:hostent} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
1593@c sethostent @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1594@c libc_lock_lock dup @asulock @aculock
1595@c nss_setent(nss_hosts_lookup2) @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1596@c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
1597@c set_h_errno dup ok
1598@c setup(nss_hosts_lookup2) @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1599@c *lookup_fct = nss_hosts_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1600@c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1601@c *fct.f @mtasurace:hostent @ascuplugin
1602@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1603@c libc_lock_unlock dup @aculock
28f540f4
RM
1604This function opens the hosts database to begin scanning it. You can
1605then call @code{gethostent} to read the entries.
1606
1607@c There was a rumor that this flag has different meaning if using the DNS,
1608@c but it appears this description is accurate in that case also.
1609If the @var{stayopen} argument is nonzero, this sets a flag so that
1610subsequent calls to @code{gethostbyname} or @code{gethostbyaddr} will
1611not close the database (as they usually would). This makes for more
1612efficiency if you call those functions several times, by avoiding
1613reopening the database for each call.
1614@end deftypefun
1615
4bca4c17 1616@deftypefun {struct hostent *} gethostent (void)
d08a7e4c 1617@standards{BSD, netdb.h}
973f180b
AO
1618@safety{@prelim{}@mtunsafe{@mtasurace{:hostent} @mtasurace{:hostentbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
1619@c gethostent @mtasurace:hostent @mtasurace:hostentbuf @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1620@c libc_lock_lock dup @asulock @aculock
1621@c nss_getent(gethostent_r) @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1622@c malloc dup @ascuheap @acsmem
1623@c *func = gethostent_r dup @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1624@c realloc dup @ascuheap @acsmem
1625@c free dup @ascuheap @acsmem
1626@c libc_lock_unlock dup @aculock
1627@c
1628@c gethostent_r @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1629@c libc_lock_lock dup @asulock @aculock
1630@c nss_getent_r(nss_hosts_lookup2) @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1631@c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
1632@c setup(nss_hosts_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1633@c *fct.f @mtasurace:hostent @ascuplugin
1634@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1635@c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1636@c *sfct.f @mtasurace:hostent @ascuplugin
1637@c libc_lock_unlock dup @aculock
1638
28f540f4
RM
1639This function returns the next entry in the hosts database. It
1640returns a null pointer if there are no more entries.
1641@end deftypefun
1642
4bca4c17 1643@deftypefun void endhostent (void)
d08a7e4c 1644@standards{BSD, netdb.h}
973f180b
AO
1645@safety{@prelim{}@mtunsafe{@mtasurace{:hostent} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
1646@c endhostent @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1647@c libc_lock_lock @asulock @aculock
1648@c nss_endent(nss_hosts_lookup2) @mtasurace:hostent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1649@c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
1650@c setup(nss_passwd_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1651@c *fct.f @mtasurace:hostent @ascuplugin
1652@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1653@c libc_lock_unlock @aculock
28f540f4
RM
1654This function closes the hosts database.
1655@end deftypefun
1656
1657@node Ports
1658@subsection Internet Ports
1659@cindex port number
1660
1661A socket address in the Internet namespace consists of a machine's
1662Internet address plus a @dfn{port number} which distinguishes the
1663sockets on a given machine (for a given protocol). Port numbers range
1664from 0 to 65,535.
1665
1666Port numbers less than @code{IPPORT_RESERVED} are reserved for standard
1667servers, such as @code{finger} and @code{telnet}. There is a database
1668that keeps track of these, and you can use the @code{getservbyname}
1669function to map a service name onto a port number; see @ref{Services
1670Database}.
1671
1672If you write a server that is not one of the standard ones defined in
1673the database, you must choose a port number for it. Use a number
1674greater than @code{IPPORT_USERRESERVED}; such numbers are reserved for
1675servers and won't ever be generated automatically by the system.
1676Avoiding conflicts with servers being run by other users is up to you.
1677
1678When you use a socket without specifying its address, the system
1679generates a port number for it. This number is between
1680@code{IPPORT_RESERVED} and @code{IPPORT_USERRESERVED}.
1681
1682On the Internet, it is actually legitimate to have two different
1683sockets with the same port number, as long as they never both try to
1684communicate with the same socket address (host address plus port
1685number). You shouldn't duplicate a port number except in special
1686circumstances where a higher-level protocol requires it. Normally,
1687the system won't let you do it; @code{bind} normally insists on
1688distinct port numbers. To reuse a port number, you must set the
1689socket option @code{SO_REUSEADDR}. @xref{Socket-Level Options}.
1690
1691@pindex netinet/in.h
1692These macros are defined in the header file @file{netinet/in.h}.
1693
28f540f4 1694@deftypevr Macro int IPPORT_RESERVED
d08a7e4c 1695@standards{BSD, netinet/in.h}
28f540f4
RM
1696Port numbers less than @code{IPPORT_RESERVED} are reserved for
1697superuser use.
1698@end deftypevr
1699
28f540f4 1700@deftypevr Macro int IPPORT_USERRESERVED
d08a7e4c 1701@standards{BSD, netinet/in.h}
28f540f4
RM
1702Port numbers greater than or equal to @code{IPPORT_USERRESERVED} are
1703reserved for explicit use; they will never be allocated automatically.
1704@end deftypevr
1705
1706@node Services Database
1707@subsection The Services Database
1708@cindex services database
1709@cindex converting service name to port number
1710@cindex converting port number to service name
1711
1712@pindex /etc/services
1713The database that keeps track of ``well-known'' services is usually
1714either the file @file{/etc/services} or an equivalent from a name server.
1715You can use these utilities, declared in @file{netdb.h}, to access
1716the services database.
1717@pindex netdb.h
1718
28f540f4 1719@deftp {Data Type} {struct servent}
d08a7e4c 1720@standards{BSD, netdb.h}
28f540f4
RM
1721This data type holds information about entries from the services database.
1722It has the following members:
1723
1724@table @code
1725@item char *s_name
1726This is the ``official'' name of the service.
1727
1728@item char **s_aliases
1729These are alternate names for the service, represented as an array of
1730strings. A null pointer terminates the array.
1731
1732@item int s_port
1733This is the port number for the service. Port numbers are given in
1734network byte order; see @ref{Byte Order}.
1735
1736@item char *s_proto
1737This is the name of the protocol to use with this service.
1738@xref{Protocols Database}.
1739@end table
1740@end deftp
1741
1742To get information about a particular service, use the
1743@code{getservbyname} or @code{getservbyport} functions. The information
1744is returned in a statically-allocated structure; you must copy the
1745information if you need to save it across calls.
1746
28f540f4 1747@deftypefun {struct servent *} getservbyname (const char *@var{name}, const char *@var{proto})
d08a7e4c 1748@standards{BSD, netdb.h}
973f180b
AO
1749@safety{@prelim{}@mtunsafe{@mtasurace{:servbyname} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
1750@c getservbyname =~ getpwuid @mtasurace:servbyname @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1751@c libc_lock_lock dup @asulock @aculock
1752@c malloc dup @ascuheap @acsmem
1753@c getservbyname_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1754@c realloc dup @ascuheap @acsmem
1755@c free dup @ascuheap @acsmem
1756@c libc_lock_unlock dup @aculock
1757@c
1758@c getservbyname_r =~ getpwuid_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1759@c nscd_getservbyname_r @ascuheap @acsfd @acsmem
1760@c nscd_getserv_r @ascuheap @acsfd @acsmem
1761@c nscd_get_map_ref dup @ascuheap @acsfd @acsmem
1762@c strlen dup ok
1763@c malloc dup @ascuheap @acsmem
1764@c mempcpy dup ok
1765@c memcpy dup ok
1766@c nscd_cache_search dup ok
1767@c nscd_open_socket dup @acsfd
1768@c readvall dup ok
1769@c readall dup ok
1770@c close_not_cancel_no_status dup @acsfd
1771@c nscd_drop_map_ref dup @ascuheap @acsmem
1772@c nscd_unmap dup @ascuheap @acsmem
1773@c free dup @ascuheap @acsmem
1774@c nss_services_lookup2 =~ nss_passwd_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1775@c *fct.l -> _nss_*_getservbyname_r @ascuplugin
1776@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
28f540f4
RM
1777The @code{getservbyname} function returns information about the
1778service named @var{name} using protocol @var{proto}. If it can't find
1779such a service, it returns a null pointer.
1780
1781This function is useful for servers as well as for clients; servers
1782use it to determine which port they should listen on (@pxref{Listening}).
1783@end deftypefun
1784
28f540f4 1785@deftypefun {struct servent *} getservbyport (int @var{port}, const char *@var{proto})
d08a7e4c 1786@standards{BSD, netdb.h}
973f180b
AO
1787@safety{@prelim{}@mtunsafe{@mtasurace{:servbyport} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
1788@c getservbyport =~ getservbyname @mtasurace:servbyport @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1789@c libc_lock_lock dup @asulock @aculock
1790@c malloc dup @ascuheap @acsmem
1791@c getservbyport_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1792@c realloc dup @ascuheap @acsmem
1793@c free dup @ascuheap @acsmem
1794@c libc_lock_unlock dup @aculock
1795@c
1796@c getservbyport_r =~ getservbyname_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1797@c nscd_getservbyport_r @ascuheap @acsfd @acsmem
1798@c nscd_getserv_r dup @ascuheap @acsfd @acsmem
1799@c nss_services_lookup2 =~ nss_passwd_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1800@c *fct.l -> _nss_*_getservbyport_r @ascuplugin
1801@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
28f540f4
RM
1802The @code{getservbyport} function returns information about the
1803service at port @var{port} using protocol @var{proto}. If it can't
1804find such a service, it returns a null pointer.
1805@end deftypefun
1806
838e5ffe 1807@noindent
28f540f4 1808You can also scan the services database using @code{setservent},
04b9968b
UD
1809@code{getservent} and @code{endservent}. Be careful when using these
1810functions because they are not reentrant.
28f540f4 1811
28f540f4 1812@deftypefun void setservent (int @var{stayopen})
d08a7e4c 1813@standards{BSD, netdb.h}
973f180b
AO
1814@safety{@prelim{}@mtunsafe{@mtasurace{:servent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
1815@c setservent @mtasurace:servent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1816@c libc_lock_lock dup @asulock @aculock
1817@c nss_setent(nss_services_lookup2) @mtasurace:servenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1818@c setup(nss_services_lookup2) @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1819@c *lookup_fct = nss_services_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1820@c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1821@c *fct.f @mtasurace:servent @ascuplugin
1822@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1823@c libc_lock_unlock dup @aculock
28f540f4
RM
1824This function opens the services database to begin scanning it.
1825
1826If the @var{stayopen} argument is nonzero, this sets a flag so that
1827subsequent calls to @code{getservbyname} or @code{getservbyport} will
1828not close the database (as they usually would). This makes for more
1829efficiency if you call those functions several times, by avoiding
1830reopening the database for each call.
1831@end deftypefun
1832
28f540f4 1833@deftypefun {struct servent *} getservent (void)
d08a7e4c 1834@standards{BSD, netdb.h}
973f180b
AO
1835@safety{@prelim{}@mtunsafe{@mtasurace{:servent} @mtasurace{:serventbuf} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
1836@c getservent @mtasurace:servent @mtasurace:serventbuf @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1837@c libc_lock_lock dup @asulock @aculock
1838@c nss_getent(getservent_r) @mtasurace:servent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1839@c malloc dup @ascuheap @acsmem
1840@c *func = getservent_r dup @mtasurace:servent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1841@c realloc dup @ascuheap @acsmem
1842@c free dup @ascuheap @acsmem
1843@c libc_lock_unlock dup @aculock
1844@c
1845@c getservent_r @mtasurace:servent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1846@c libc_lock_lock dup @asulock @aculock
1847@c nss_getent_r(nss_services_lookup2) @mtasurace:servent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1848@c setup(nss_services_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1849@c *fct.f @mtasurace:servent @ascuplugin
1850@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1851@c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1852@c *sfct.f @mtasurace:servent @ascuplugin
1853@c libc_lock_unlock dup @aculock
28f540f4
RM
1854This function returns the next entry in the services database. If
1855there are no more entries, it returns a null pointer.
1856@end deftypefun
1857
28f540f4 1858@deftypefun void endservent (void)
d08a7e4c 1859@standards{BSD, netdb.h}
973f180b
AO
1860@safety{@prelim{}@mtunsafe{@mtasurace{:servent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
1861@c endservent @mtasurace:servent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1862@c libc_lock_lock @asulock @aculock
1863@c nss_endent(nss_services_lookup2) @mtasurace:servent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1864@c setup(nss_services_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1865@c *fct.f @mtasurace:servent @ascuplugin
1866@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
1867@c libc_lock_unlock @aculock
28f540f4
RM
1868This function closes the services database.
1869@end deftypefun
1870
1871@node Byte Order
1872@subsection Byte Order Conversion
1873@cindex byte order conversion, for socket
1874@cindex converting byte order
1875
1876@cindex big-endian
1877@cindex little-endian
1878Different kinds of computers use different conventions for the
1879ordering of bytes within a word. Some computers put the most
1880significant byte within a word first (this is called ``big-endian''
1881order), and others put it last (``little-endian'' order).
1882
1883@cindex network byte order
1884So that machines with different byte order conventions can
1885communicate, the Internet protocols specify a canonical byte order
1886convention for data transmitted over the network. This is known
04b9968b 1887as @dfn{network byte order}.
28f540f4
RM
1888
1889When establishing an Internet socket connection, you must make sure that
1890the data in the @code{sin_port} and @code{sin_addr} members of the
04b9968b 1891@code{sockaddr_in} structure are represented in network byte order.
28f540f4
RM
1892If you are encoding integer data in the messages sent through the
1893socket, you should convert this to network byte order too. If you don't
1894do this, your program may fail when running on or talking to other kinds
1895of machines.
1896
1897If you use @code{getservbyname} and @code{gethostbyname} or
1898@code{inet_addr} to get the port number and host address, the values are
04b9968b 1899already in network byte order, and you can copy them directly into
28f540f4
RM
1900the @code{sockaddr_in} structure.
1901
4bca4c17
UD
1902Otherwise, you have to convert the values explicitly. Use @code{htons}
1903and @code{ntohs} to convert values for the @code{sin_port} member. Use
1904@code{htonl} and @code{ntohl} to convert IPv4 addresses for the
28f540f4 1905@code{sin_addr} member. (Remember, @code{struct in_addr} is equivalent
4bca4c17 1906to @code{uint32_t}.) These functions are declared in
28f540f4
RM
1907@file{netinet/in.h}.
1908@pindex netinet/in.h
1909
4bca4c17 1910@deftypefun {uint16_t} htons (uint16_t @var{hostshort})
d08a7e4c 1911@standards{BSD, netinet/in.h}
973f180b
AO
1912@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1913@c htons ok
1914@c bswap_16 ok
1915@c bswap_constant_16 ok
1916
4bca4c17 1917This function converts the @code{uint16_t} integer @var{hostshort} from
28f540f4
RM
1918host byte order to network byte order.
1919@end deftypefun
1920
4bca4c17 1921@deftypefun {uint16_t} ntohs (uint16_t @var{netshort})
d08a7e4c 1922@standards{BSD, netinet/in.h}
973f180b
AO
1923@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1924@c Alias to htons.
4bca4c17 1925This function converts the @code{uint16_t} integer @var{netshort} from
28f540f4
RM
1926network byte order to host byte order.
1927@end deftypefun
1928
4bca4c17 1929@deftypefun {uint32_t} htonl (uint32_t @var{hostlong})
d08a7e4c 1930@standards{BSD, netinet/in.h}
973f180b
AO
1931@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1932@c htonl ok
1933@c bswap_32 dup ok
4bca4c17 1934This function converts the @code{uint32_t} integer @var{hostlong} from
28f540f4 1935host byte order to network byte order.
4bca4c17 1936
04b9968b 1937This is used for IPv4 Internet addresses.
28f540f4
RM
1938@end deftypefun
1939
4bca4c17 1940@deftypefun {uint32_t} ntohl (uint32_t @var{netlong})
d08a7e4c 1941@standards{BSD, netinet/in.h}
973f180b
AO
1942@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1943@c Alias to htonl.
4bca4c17 1944This function converts the @code{uint32_t} integer @var{netlong} from
28f540f4 1945network byte order to host byte order.
4bca4c17 1946
04b9968b 1947This is used for IPv4 Internet addresses.
28f540f4
RM
1948@end deftypefun
1949
1950@node Protocols Database
1951@subsection Protocols Database
1952@cindex protocols database
1953
1954The communications protocol used with a socket controls low-level
04b9968b 1955details of how data are exchanged. For example, the protocol implements
28f540f4
RM
1956things like checksums to detect errors in transmissions, and routing
1957instructions for messages. Normal user programs have little reason to
1958mess with these details directly.
1959
1960@cindex TCP (Internet protocol)
1961The default communications protocol for the Internet namespace depends on
1962the communication style. For stream communication, the default is TCP
1963(``transmission control protocol''). For datagram communication, the
1964default is UDP (``user datagram protocol''). For reliable datagram
1965communication, the default is RDP (``reliable datagram protocol'').
1966You should nearly always use the default.
1967
1968@pindex /etc/protocols
1969Internet protocols are generally specified by a name instead of a
1970number. The network protocols that a host knows about are stored in a
1971database. This is usually either derived from the file
1972@file{/etc/protocols}, or it may be an equivalent provided by a name
1973server. You look up the protocol number associated with a named
1974protocol in the database using the @code{getprotobyname} function.
1975
1976Here are detailed descriptions of the utilities for accessing the
1977protocols database. These are declared in @file{netdb.h}.
1978@pindex netdb.h
1979
28f540f4 1980@deftp {Data Type} {struct protoent}
d08a7e4c 1981@standards{BSD, netdb.h}
28f540f4
RM
1982This data type is used to represent entries in the network protocols
1983database. It has the following members:
1984
1985@table @code
1986@item char *p_name
1987This is the official name of the protocol.
1988
1989@item char **p_aliases
1990These are alternate names for the protocol, specified as an array of
1991strings. The last element of the array is a null pointer.
1992
1993@item int p_proto
1994This is the protocol number (in host byte order); use this member as the
1995@var{protocol} argument to @code{socket}.
1996@end table
1997@end deftp
1998
1999You can use @code{getprotobyname} and @code{getprotobynumber} to search
2000the protocols database for a specific protocol. The information is
2001returned in a statically-allocated structure; you must copy the
2002information if you need to save it across calls.
2003
28f540f4 2004@deftypefun {struct protoent *} getprotobyname (const char *@var{name})
d08a7e4c 2005@standards{BSD, netdb.h}
973f180b
AO
2006@safety{@prelim{}@mtunsafe{@mtasurace{:protobyname} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
2007@c getprotobyname =~ getpwuid @mtasurace:protobyname @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2008@c libc_lock_lock dup @asulock @aculock
2009@c malloc dup @ascuheap @acsmem
2010@c getprotobyname_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2011@c realloc dup @ascuheap @acsmem
2012@c free dup @ascuheap @acsmem
2013@c libc_lock_unlock dup @aculock
2014@c
2015@c getprotobyname_r =~ getpwuid_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2016@c no nscd support
2017@c nss_protocols_lookup2 =~ nss_passwd_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2018@c *fct.l -> _nss_*_getprotobyname_r @ascuplugin
2019@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
28f540f4
RM
2020The @code{getprotobyname} function returns information about the
2021network protocol named @var{name}. If there is no such protocol, it
2022returns a null pointer.
2023@end deftypefun
2024
28f540f4 2025@deftypefun {struct protoent *} getprotobynumber (int @var{protocol})
d08a7e4c 2026@standards{BSD, netdb.h}
973f180b
AO
2027@safety{@prelim{}@mtunsafe{@mtasurace{:protobynumber} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
2028@c getprotobynumber =~ getpwuid @mtasurace:protobynumber @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2029@c libc_lock_lock dup @asulock @aculock
2030@c malloc dup @ascuheap @acsmem
2031@c getprotobynumber_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2032@c realloc dup @ascuheap @acsmem
2033@c free dup @ascuheap @acsmem
2034@c libc_lock_unlock dup @aculock
2035@c
2036@c getprotobynumber_r =~ getpwuid_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2037@c no nscd support
2038@c nss_protocols_lookup2 =~ nss_passwd_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2039@c *fct.l -> _nss_*_getprotobynumber_r @ascuplugin
2040@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
28f540f4
RM
2041The @code{getprotobynumber} function returns information about the
2042network protocol with number @var{protocol}. If there is no such
2043protocol, it returns a null pointer.
2044@end deftypefun
2045
2046You can also scan the whole protocols database one protocol at a time by
04b9968b
UD
2047using @code{setprotoent}, @code{getprotoent} and @code{endprotoent}.
2048Be careful when using these functions because they are not reentrant.
28f540f4 2049
28f540f4 2050@deftypefun void setprotoent (int @var{stayopen})
d08a7e4c 2051@standards{BSD, netdb.h}
973f180b
AO
2052@safety{@prelim{}@mtunsafe{@mtasurace{:protoent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
2053@c setprotoent @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2054@c libc_lock_lock dup @asulock @aculock
2055@c nss_setent(nss_protocols_lookup2) @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2056@c setup(nss_protocols_lookup2) @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2057@c *lookup_fct = nss_protocols_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2058@c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2059@c *fct.f @mtasurace:protoent @ascuplugin
2060@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2061@c libc_lock_unlock dup @aculock
28f540f4
RM
2062This function opens the protocols database to begin scanning it.
2063
2064If the @var{stayopen} argument is nonzero, this sets a flag so that
2065subsequent calls to @code{getprotobyname} or @code{getprotobynumber} will
2066not close the database (as they usually would). This makes for more
2067efficiency if you call those functions several times, by avoiding
2068reopening the database for each call.
2069@end deftypefun
2070
28f540f4 2071@deftypefun {struct protoent *} getprotoent (void)
d08a7e4c 2072@standards{BSD, netdb.h}
973f180b
AO
2073@safety{@prelim{}@mtunsafe{@mtasurace{:protoent} @mtasurace{:protoentbuf} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
2074@c getprotoent @mtasurace:protoent @mtasurace:protoentbuf @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2075@c libc_lock_lock dup @asulock @aculock
2076@c nss_getent(getprotoent_r) @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2077@c malloc dup @ascuheap @acsmem
2078@c *func = getprotoent_r dup @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2079@c realloc dup @ascuheap @acsmem
2080@c free dup @ascuheap @acsmem
2081@c libc_lock_unlock dup @aculock
2082@c
2083@c getprotoent_r @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2084@c libc_lock_lock dup @asulock @aculock
2085@c nss_getent_r(nss_protocols_lookup2) @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2086@c setup(nss_protocols_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2087@c *fct.f @mtasurace:servent @ascuplugin
2088@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2089@c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2090@c *sfct.f @mtasurace:protoent @ascuplugin
2091@c libc_lock_unlock dup @aculock
28f540f4
RM
2092This function returns the next entry in the protocols database. It
2093returns a null pointer if there are no more entries.
2094@end deftypefun
2095
28f540f4 2096@deftypefun void endprotoent (void)
d08a7e4c 2097@standards{BSD, netdb.h}
973f180b
AO
2098@safety{@prelim{}@mtunsafe{@mtasurace{:protoent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
2099@c endprotoent @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2100@c libc_lock_lock @asulock @aculock
2101@c nss_endent(nss_protocols_lookup2) @mtasurace:protoent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2102@c setup(nss_protocols_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2103@c *fct.f @mtasurace:protoent @ascuplugin
2104@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
2105@c libc_lock_unlock @aculock
28f540f4
RM
2106This function closes the protocols database.
2107@end deftypefun
2108
2109@node Inet Example
2110@subsection Internet Socket Example
2111
2112Here is an example showing how to create and name a socket in the
2113Internet namespace. The newly created socket exists on the machine that
2114the program is running on. Rather than finding and using the machine's
2115Internet address, this example specifies @code{INADDR_ANY} as the host
2116address; the system replaces that with the machine's actual address.
2117
2118@smallexample
2119@include mkisock.c.texi
2120@end smallexample
2121
2122Here is another example, showing how you can fill in a @code{sockaddr_in}
2123structure, given a host name string and a port number:
2124
2125@smallexample
2126@include isockad.c.texi
2127@end smallexample
2128
2129@node Misc Namespaces
2130@section Other Namespaces
2131
2132@vindex PF_NS
2133@vindex PF_ISO
2134@vindex PF_CCITT
2135@vindex PF_IMPLINK
2136@vindex PF_ROUTE
2137Certain other namespaces and associated protocol families are supported
2138but not documented yet because they are not often used. @code{PF_NS}
2139refers to the Xerox Network Software protocols. @code{PF_ISO} stands
2140for Open Systems Interconnect. @code{PF_CCITT} refers to protocols from
2141CCITT. @file{socket.h} defines these symbols and others naming protocols
2142not actually implemented.
2143
2144@code{PF_IMPLINK} is used for communicating between hosts and Internet
04b9968b 2145Message Processors. For information on this and @code{PF_ROUTE}, an
28f540f4
RM
2146occasionally-used local area routing protocol, see the GNU Hurd Manual
2147(to appear in the future).
2148
2149@node Open/Close Sockets
2150@section Opening and Closing Sockets
2151
2152This section describes the actual library functions for opening and
2153closing sockets. The same functions work for all namespaces and
2154connection styles.
2155
2156@menu
2157* Creating a Socket:: How to open a socket.
2158* Closing a Socket:: How to close a socket.
2159* Socket Pairs:: These are created like pipes.
2160@end menu
2161
2162@node Creating a Socket
2163@subsection Creating a Socket
2164@cindex creating a socket
2165@cindex socket, creating
2166@cindex opening a socket
2167
2168The primitive for creating a socket is the @code{socket} function,
2169declared in @file{sys/socket.h}.
2170@pindex sys/socket.h
2171
28f540f4 2172@deftypefun int socket (int @var{namespace}, int @var{style}, int @var{protocol})
d08a7e4c 2173@standards{BSD, sys/socket.h}
973f180b 2174@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
28f540f4
RM
2175This function creates a socket and specifies communication style
2176@var{style}, which should be one of the socket styles listed in
2177@ref{Communication Styles}. The @var{namespace} argument specifies
4bca4c17 2178the namespace; it must be @code{PF_LOCAL} (@pxref{Local Namespace}) or
28f540f4
RM
2179@code{PF_INET} (@pxref{Internet Namespace}). @var{protocol}
2180designates the specific protocol (@pxref{Socket Concepts}); zero is
2181usually right for @var{protocol}.
2182
2183The return value from @code{socket} is the file descriptor for the new
2184socket, or @code{-1} in case of error. The following @code{errno} error
2185conditions are defined for this function:
2186
2187@table @code
2188@item EPROTONOSUPPORT
2189The @var{protocol} or @var{style} is not supported by the
2190@var{namespace} specified.
2191
2192@item EMFILE
2193The process already has too many file descriptors open.
2194
2195@item ENFILE
2196The system already has too many file descriptors open.
2197
0bc93a2f 2198@item EACCES
04b9968b 2199The process does not have the privilege to create a socket of the specified
28f540f4
RM
2200@var{style} or @var{protocol}.
2201
2202@item ENOBUFS
2203The system ran out of internal buffer space.
2204@end table
2205
2206The file descriptor returned by the @code{socket} function supports both
04b9968b 2207read and write operations. However, like pipes, sockets do not support file
28f540f4
RM
2208positioning operations.
2209@end deftypefun
2210
6e953631 2211For examples of how to call the @code{socket} function,
4bca4c17 2212see @ref{Local Socket Example}, or @ref{Inet Example}.
28f540f4
RM
2213
2214
2215@node Closing a Socket
2216@subsection Closing a Socket
2217@cindex socket, closing
2218@cindex closing a socket
2219@cindex shutting down a socket
2220@cindex socket shutdown
2221
04b9968b 2222When you have finished using a socket, you can simply close its
28f540f4
RM
2223file descriptor with @code{close}; see @ref{Opening and Closing Files}.
2224If there is still data waiting to be transmitted over the connection,
2225normally @code{close} tries to complete this transmission. You
2226can control this behavior using the @code{SO_LINGER} socket option to
2227specify a timeout period; see @ref{Socket Options}.
2228
2229@pindex sys/socket.h
04b9968b 2230You can also shut down only reception or transmission on a
28f540f4
RM
2231connection by calling @code{shutdown}, which is declared in
2232@file{sys/socket.h}.
2233
28f540f4 2234@deftypefun int shutdown (int @var{socket}, int @var{how})
d08a7e4c 2235@standards{BSD, sys/socket.h}
973f180b 2236@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
28f540f4
RM
2237The @code{shutdown} function shuts down the connection of socket
2238@var{socket}. The argument @var{how} specifies what action to
2239perform:
2240
2241@table @code
2242@item 0
2243Stop receiving data for this socket. If further data arrives,
2244reject it.
2245
2246@item 1
2247Stop trying to transmit data from this socket. Discard any data
2248waiting to be sent. Stop looking for acknowledgement of data already
2249sent; don't retransmit it if it is lost.
2250
2251@item 2
2252Stop both reception and transmission.
2253@end table
2254
2255The return value is @code{0} on success and @code{-1} on failure. The
2256following @code{errno} error conditions are defined for this function:
2257
2258@table @code
2259@item EBADF
2260@var{socket} is not a valid file descriptor.
2261
2262@item ENOTSOCK
2263@var{socket} is not a socket.
2264
2265@item ENOTCONN
2266@var{socket} is not connected.
2267@end table
2268@end deftypefun
2269
2270@node Socket Pairs
2271@subsection Socket Pairs
2272@cindex creating a socket pair
2273@cindex socket pair
2274@cindex opening a socket pair
2275
2276@pindex sys/socket.h
2277A @dfn{socket pair} consists of a pair of connected (but unnamed)
2278sockets. It is very similar to a pipe and is used in much the same
2279way. Socket pairs are created with the @code{socketpair} function,
2280declared in @file{sys/socket.h}. A socket pair is much like a pipe; the
2281main difference is that the socket pair is bidirectional, whereas the
2282pipe has one input-only end and one output-only end (@pxref{Pipes and
2283FIFOs}).
2284
28f540f4 2285@deftypefun int socketpair (int @var{namespace}, int @var{style}, int @var{protocol}, int @var{filedes}@t{[2]})
d08a7e4c 2286@standards{BSD, sys/socket.h}
973f180b 2287@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
28f540f4
RM
2288This function creates a socket pair, returning the file descriptors in
2289@code{@var{filedes}[0]} and @code{@var{filedes}[1]}. The socket pair
2290is a full-duplex communications channel, so that both reading and writing
2291may be performed at either end.
2292
04b9968b 2293The @var{namespace}, @var{style} and @var{protocol} arguments are
28f540f4
RM
2294interpreted as for the @code{socket} function. @var{style} should be
2295one of the communication styles listed in @ref{Communication Styles}.
2296The @var{namespace} argument specifies the namespace, which must be
4bca4c17 2297@code{AF_LOCAL} (@pxref{Local Namespace}); @var{protocol} specifies the
28f540f4
RM
2298communications protocol, but zero is the only meaningful value.
2299
2300If @var{style} specifies a connectionless communication style, then
2301the two sockets you get are not @emph{connected}, strictly speaking,
2302but each of them knows the other as the default destination address,
2303so they can send packets to each other.
2304
2305The @code{socketpair} function returns @code{0} on success and @code{-1}
2306on failure. The following @code{errno} error conditions are defined
2307for this function:
2308
2309@table @code
2310@item EMFILE
2311The process has too many file descriptors open.
2312
2313@item EAFNOSUPPORT
2314The specified namespace is not supported.
2315
2316@item EPROTONOSUPPORT
2317The specified protocol is not supported.
2318
2319@item EOPNOTSUPP
2320The specified protocol does not support the creation of socket pairs.
2321@end table
2322@end deftypefun
2323
2324@node Connections
2325@section Using Sockets with Connections
2326
2327@cindex connection
2328@cindex client
2329@cindex server
2330The most common communication styles involve making a connection to a
2331particular other socket, and then exchanging data with that socket
2332over and over. Making a connection is asymmetric; one side (the
2333@dfn{client}) acts to request a connection, while the other side (the
2334@dfn{server}) makes a socket and waits for the connection request.
2335
2336@iftex
2337@itemize @bullet
2338@item
2339@ref{Connecting}, describes what the client program must do to
2340initiate a connection with a server.
2341
2342@item
04b9968b 2343@ref{Listening} and @ref{Accepting Connections} describe what the
28f540f4
RM
2344server program must do to wait for and act upon connection requests
2345from clients.
2346
2347@item
04b9968b 2348@ref{Transferring Data}, describes how data are transferred through the
28f540f4
RM
2349connected socket.
2350@end itemize
2351@end iftex
2352
2353@menu
2354* Connecting:: What the client program must do.
2355* Listening:: How a server program waits for requests.
2356* Accepting Connections:: What the server does when it gets a request.
2357* Who is Connected:: Getting the address of the
2358 other side of a connection.
2359* Transferring Data:: How to send and receive data.
2360* Byte Stream Example:: An example program: a client for communicating
2361 over a byte stream socket in the Internet namespace.
2362* Server Example:: A corresponding server program.
2363* Out-of-Band Data:: This is an advanced feature.
2364@end menu
2365
2366@node Connecting
2367@subsection Making a Connection
2368@cindex connecting a socket
2369@cindex socket, connecting
2370@cindex socket, initiating a connection
2371@cindex socket, client actions
2372
2373In making a connection, the client makes a connection while the server
2374waits for and accepts the connection. Here we discuss what the client
04b9968b 2375program must do with the @code{connect} function, which is declared in
28f540f4
RM
2376@file{sys/socket.h}.
2377
55c14926 2378@deftypefun int connect (int @var{socket}, struct sockaddr *@var{addr}, socklen_t @var{length})
d08a7e4c 2379@standards{BSD, sys/socket.h}
973f180b 2380@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
28f540f4
RM
2381The @code{connect} function initiates a connection from the socket
2382with file descriptor @var{socket} to the socket whose address is
2383specified by the @var{addr} and @var{length} arguments. (This socket
2384is typically on another machine, and it must be already set up as a
2385server.) @xref{Socket Addresses}, for information about how these
2386arguments are interpreted.
2387
2388Normally, @code{connect} waits until the server responds to the request
2389before it returns. You can set nonblocking mode on the socket
2390@var{socket} to make @code{connect} return immediately without waiting
2391for the response. @xref{File Status Flags}, for information about
2392nonblocking mode.
2393@c !!! how do you tell when it has finished connecting? I suspect the
2394@c way you do it is select for writing.
2395
2396The normal return value from @code{connect} is @code{0}. If an error
2397occurs, @code{connect} returns @code{-1}. The following @code{errno}
2398error conditions are defined for this function:
2399
2400@table @code
2401@item EBADF
2402The socket @var{socket} is not a valid file descriptor.
2403
2404@item ENOTSOCK
6e953631 2405File descriptor @var{socket} is not a socket.
28f540f4
RM
2406
2407@item EADDRNOTAVAIL
2408The specified address is not available on the remote machine.
2409
2410@item EAFNOSUPPORT
2411The namespace of the @var{addr} is not supported by this socket.
2412
2413@item EISCONN
2414The socket @var{socket} is already connected.
2415
2416@item ETIMEDOUT
2417The attempt to establish the connection timed out.
2418
2419@item ECONNREFUSED
2420The server has actively refused to establish the connection.
2421
2422@item ENETUNREACH
2423The network of the given @var{addr} isn't reachable from this host.
2424
2425@item EADDRINUSE
2426The socket address of the given @var{addr} is already in use.
2427
2428@item EINPROGRESS
2429The socket @var{socket} is non-blocking and the connection could not be
2430established immediately. You can determine when the connection is
2431completely established with @code{select}; @pxref{Waiting for I/O}.
2432Another @code{connect} call on the same socket, before the connection is
2433completely established, will fail with @code{EALREADY}.
2434
2435@item EALREADY
2436The socket @var{socket} is non-blocking and already has a pending
2437connection in progress (see @code{EINPROGRESS} above).
2438@end table
dd7d45e8 2439
04b9968b
UD
2440This function is defined as a cancellation point in multi-threaded
2441programs, so one has to be prepared for this and make sure that
b57dd246 2442allocated resources (like memory, file descriptors, semaphores or
04b9968b 2443whatever) are freed even if the thread is canceled.
dd7d45e8 2444@c @xref{pthread_cleanup_push}, for a method how to do this.
28f540f4
RM
2445@end deftypefun
2446
2447@node Listening
2448@subsection Listening for Connections
2449@cindex listening (sockets)
2450@cindex sockets, server actions
2451@cindex sockets, listening
2452
2453Now let us consider what the server process must do to accept
2454connections on a socket. First it must use the @code{listen} function
2455to enable connection requests on the socket, and then accept each
2456incoming connection with a call to @code{accept} (@pxref{Accepting
2457Connections}). Once connection requests are enabled on a server socket,
2458the @code{select} function reports when the socket has a connection
2459ready to be accepted (@pxref{Waiting for I/O}).
2460
2461The @code{listen} function is not allowed for sockets using
2462connectionless communication styles.
2463
2464You can write a network server that does not even start running until a
2465connection to it is requested. @xref{Inetd Servers}.
2466
2467In the Internet namespace, there are no special protection mechanisms
04b9968b 2468for controlling access to a port; any process on any machine
28f540f4
RM
2469can make a connection to your server. If you want to restrict access to
2470your server, make it examine the addresses associated with connection
2471requests or implement some other handshaking or identification
2472protocol.
2473
4bca4c17 2474In the local namespace, the ordinary file protection bits control who has
28f540f4
RM
2475access to connect to the socket.
2476
88197030 2477@deftypefun int listen (int @var{socket}, int @var{n})
d08a7e4c 2478@standards{BSD, sys/socket.h}
973f180b 2479@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
28f540f4
RM
2480The @code{listen} function enables the socket @var{socket} to accept
2481connections, thus making it a server socket.
2482
2483The argument @var{n} specifies the length of the queue for pending
2484connections. When the queue fills, new clients attempting to connect
2485fail with @code{ECONNREFUSED} until the server calls @code{accept} to
2486accept a connection from the queue.
2487
2488The @code{listen} function returns @code{0} on success and @code{-1}
2489on failure. The following @code{errno} error conditions are defined
2490for this function:
2491
2492@table @code
2493@item EBADF
2494The argument @var{socket} is not a valid file descriptor.
2495
2496@item ENOTSOCK
2497The argument @var{socket} is not a socket.
2498
2499@item EOPNOTSUPP
2500The socket @var{socket} does not support this operation.
2501@end table
2502@end deftypefun
2503
2504@node Accepting Connections
2505@subsection Accepting Connections
2506@cindex sockets, accepting connections
2507@cindex accepting connections
2508
2509When a server receives a connection request, it can complete the
2510connection by accepting the request. Use the function @code{accept}
2511to do this.
2512
2513A socket that has been established as a server can accept connection
2514requests from multiple clients. The server's original socket
04b9968b 2515@emph{does not become part of the connection}; instead, @code{accept}
28f540f4
RM
2516makes a new socket which participates in the connection.
2517@code{accept} returns the descriptor for this socket. The server's
2518original socket remains available for listening for further connection
2519requests.
2520
2521The number of pending connection requests on a server socket is finite.
2522If connection requests arrive from clients faster than the server can
2523act upon them, the queue can fill up and additional requests are refused
04b9968b 2524with an @code{ECONNREFUSED} error. You can specify the maximum length of
28f540f4
RM
2525this queue as an argument to the @code{listen} function, although the
2526system may also impose its own internal limit on the length of this
2527queue.
2528
29fe4d0d 2529@deftypefun int accept (int @var{socket}, struct sockaddr *@var{addr}, socklen_t *@var{length_ptr})
d08a7e4c 2530@standards{BSD, sys/socket.h}
973f180b 2531@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
28f540f4
RM
2532This function is used to accept a connection request on the server
2533socket @var{socket}.
2534
2535The @code{accept} function waits if there are no connections pending,
2536unless the socket @var{socket} has nonblocking mode set. (You can use
2537@code{select} to wait for a pending connection, with a nonblocking
2538socket.) @xref{File Status Flags}, for information about nonblocking
2539mode.
2540
2541The @var{addr} and @var{length-ptr} arguments are used to return
2542information about the name of the client socket that initiated the
2543connection. @xref{Socket Addresses}, for information about the format
2544of the information.
2545
2546Accepting a connection does not make @var{socket} part of the
2547connection. Instead, it creates a new socket which becomes
2548connected. The normal return value of @code{accept} is the file
2549descriptor for the new socket.
2550
2551After @code{accept}, the original socket @var{socket} remains open and
2552unconnected, and continues listening until you close it. You can
2553accept further connections with @var{socket} by calling @code{accept}
2554again.
2555
2556If an error occurs, @code{accept} returns @code{-1}. The following
2557@code{errno} error conditions are defined for this function:
2558
2559@table @code
2560@item EBADF
2561The @var{socket} argument is not a valid file descriptor.
2562
2563@item ENOTSOCK
2564The descriptor @var{socket} argument is not a socket.
2565
2566@item EOPNOTSUPP
2567The descriptor @var{socket} does not support this operation.
2568
2569@item EWOULDBLOCK
2570@var{socket} has nonblocking mode set, and there are no pending
2571connections immediately available.
2572@end table
dd7d45e8 2573
04b9968b
UD
2574This function is defined as a cancellation point in multi-threaded
2575programs, so one has to be prepared for this and make sure that
b57dd246 2576allocated resources (like memory, file descriptors, semaphores or
04b9968b 2577whatever) are freed even if the thread is canceled.
dd7d45e8 2578@c @xref{pthread_cleanup_push}, for a method how to do this.
28f540f4
RM
2579@end deftypefun
2580
2581The @code{accept} function is not allowed for sockets using
2582connectionless communication styles.
2583
2584@node Who is Connected
2585@subsection Who is Connected to Me?
2586
4bca4c17 2587@deftypefun int getpeername (int @var{socket}, struct sockaddr *@var{addr}, socklen_t *@var{length-ptr})
d08a7e4c 2588@standards{BSD, sys/socket.h}
973f180b 2589@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
28f540f4
RM
2590The @code{getpeername} function returns the address of the socket that
2591@var{socket} is connected to; it stores the address in the memory space
2592specified by @var{addr} and @var{length-ptr}. It stores the length of
2593the address in @code{*@var{length-ptr}}.
2594
2595@xref{Socket Addresses}, for information about the format of the
2596address. In some operating systems, @code{getpeername} works only for
2597sockets in the Internet domain.
2598
2599The return value is @code{0} on success and @code{-1} on error. The
2600following @code{errno} error conditions are defined for this function:
2601
2602@table @code
2603@item EBADF
2604The argument @var{socket} is not a valid file descriptor.
2605
2606@item ENOTSOCK
2607The descriptor @var{socket} is not a socket.
2608
2609@item ENOTCONN
2610The socket @var{socket} is not connected.
2611
2612@item ENOBUFS
2613There are not enough internal buffers available.
2614@end table
2615@end deftypefun
2616
2617
2618@node Transferring Data
2619@subsection Transferring Data
2620@cindex reading from a socket
2621@cindex writing to a socket
2622
2623Once a socket has been connected to a peer, you can use the ordinary
2624@code{read} and @code{write} operations (@pxref{I/O Primitives}) to
2625transfer data. A socket is a two-way communications channel, so read
2626and write operations can be performed at either end.
2627
2628There are also some I/O modes that are specific to socket operations.
2629In order to specify these modes, you must use the @code{recv} and
2630@code{send} functions instead of the more generic @code{read} and
2631@code{write} functions. The @code{recv} and @code{send} functions take
2632an additional argument which you can use to specify various flags to
04b9968b 2633control special I/O modes. For example, you can specify the
28f540f4
RM
2634@code{MSG_OOB} flag to read or write out-of-band data, the
2635@code{MSG_PEEK} flag to peek at input, or the @code{MSG_DONTROUTE} flag
2636to control inclusion of routing information on output.
2637
2638@menu
2639* Sending Data:: Sending data with @code{send}.
2640* Receiving Data:: Reading data with @code{recv}.
2641* Socket Data Options:: Using @code{send} and @code{recv}.
2642@end menu
2643
2644@node Sending Data
2645@subsubsection Sending Data
2646
2647@pindex sys/socket.h
2648The @code{send} function is declared in the header file
2649@file{sys/socket.h}. If your @var{flags} argument is zero, you can just
2650as well use @code{write} instead of @code{send}; see @ref{I/O
2651Primitives}. If the socket was connected but the connection has broken,
2652you get a @code{SIGPIPE} signal for any use of @code{send} or
2653@code{write} (@pxref{Miscellaneous Signals}).
2654
8ded91fb 2655@deftypefun ssize_t send (int @var{socket}, const void *@var{buffer}, size_t @var{size}, int @var{flags})
d08a7e4c 2656@standards{BSD, sys/socket.h}
973f180b 2657@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
28f540f4
RM
2658The @code{send} function is like @code{write}, but with the additional
2659flags @var{flags}. The possible values of @var{flags} are described
2660in @ref{Socket Data Options}.
2661
2662This function returns the number of bytes transmitted, or @code{-1} on
2663failure. If the socket is nonblocking, then @code{send} (like
2664@code{write}) can return after sending just part of the data.
2665@xref{File Status Flags}, for information about nonblocking mode.
2666
2667Note, however, that a successful return value merely indicates that
2668the message has been sent without error, not necessarily that it has
2669been received without error.
2670
2671The following @code{errno} error conditions are defined for this function:
2672
2673@table @code
2674@item EBADF
2675The @var{socket} argument is not a valid file descriptor.
2676
2677@item EINTR
2678The operation was interrupted by a signal before any data was sent.
2679@xref{Interrupted Primitives}.
2680
2681@item ENOTSOCK
2682The descriptor @var{socket} is not a socket.
2683
2684@item EMSGSIZE
2685The socket type requires that the message be sent atomically, but the
2686message is too large for this to be possible.
2687
2688@item EWOULDBLOCK
2689Nonblocking mode has been set on the socket, and the write operation
2690would block. (Normally @code{send} blocks until the operation can be
2691completed.)
2692
2693@item ENOBUFS
2694There is not enough internal buffer space available.
2695
2696@item ENOTCONN
2697You never connected this socket.
2698
2699@item EPIPE
2700This socket was connected but the connection is now broken. In this
2701case, @code{send} generates a @code{SIGPIPE} signal first; if that
2702signal is ignored or blocked, or if its handler returns, then
2703@code{send} fails with @code{EPIPE}.
2704@end table
dd7d45e8 2705
04b9968b
UD
2706This function is defined as a cancellation point in multi-threaded
2707programs, so one has to be prepared for this and make sure that
b57dd246 2708allocated resources (like memory, file descriptors, semaphores or
04b9968b 2709whatever) are freed even if the thread is canceled.
dd7d45e8 2710@c @xref{pthread_cleanup_push}, for a method how to do this.
28f540f4
RM
2711@end deftypefun
2712
2713@node Receiving Data
2714@subsubsection Receiving Data
2715
2716@pindex sys/socket.h
2717The @code{recv} function is declared in the header file
2718@file{sys/socket.h}. If your @var{flags} argument is zero, you can
2719just as well use @code{read} instead of @code{recv}; see @ref{I/O
2720Primitives}.
2721
8ded91fb 2722@deftypefun ssize_t recv (int @var{socket}, void *@var{buffer}, size_t @var{size}, int @var{flags})
d08a7e4c 2723@standards{BSD, sys/socket.h}
973f180b 2724@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
28f540f4
RM
2725The @code{recv} function is like @code{read}, but with the additional
2726flags @var{flags}. The possible values of @var{flags} are described
4bca4c17 2727in @ref{Socket Data Options}.
28f540f4 2728
04b9968b 2729If nonblocking mode is set for @var{socket}, and no data are available to
28f540f4
RM
2730be read, @code{recv} fails immediately rather than waiting. @xref{File
2731Status Flags}, for information about nonblocking mode.
2732
2733This function returns the number of bytes received, or @code{-1} on failure.
2734The following @code{errno} error conditions are defined for this function:
2735
2736@table @code
2737@item EBADF
2738The @var{socket} argument is not a valid file descriptor.
2739
2740@item ENOTSOCK
2741The descriptor @var{socket} is not a socket.
2742
2743@item EWOULDBLOCK
2744Nonblocking mode has been set on the socket, and the read operation
2745would block. (Normally, @code{recv} blocks until there is input
2746available to be read.)
2747
2748@item EINTR
2749The operation was interrupted by a signal before any data was read.
2750@xref{Interrupted Primitives}.
2751
2752@item ENOTCONN
2753You never connected this socket.
2754@end table
dd7d45e8 2755
04b9968b
UD
2756This function is defined as a cancellation point in multi-threaded
2757programs, so one has to be prepared for this and make sure that
b57dd246 2758allocated resources (like memory, file descriptors, semaphores or
04b9968b 2759whatever) are freed even if the thread is canceled.
dd7d45e8 2760@c @xref{pthread_cleanup_push}, for a method how to do this.
28f540f4
RM
2761@end deftypefun
2762
2763@node Socket Data Options
2764@subsubsection Socket Data Options
2765
2766@pindex sys/socket.h
2767The @var{flags} argument to @code{send} and @code{recv} is a bit
2768mask. You can bitwise-OR the values of the following macros together
2769to obtain a value for this argument. All are defined in the header
2770file @file{sys/socket.h}.
2771
28f540f4 2772@deftypevr Macro int MSG_OOB
d08a7e4c 2773@standards{BSD, sys/socket.h}
28f540f4
RM
2774Send or receive out-of-band data. @xref{Out-of-Band Data}.
2775@end deftypevr
2776
28f540f4 2777@deftypevr Macro int MSG_PEEK
d08a7e4c 2778@standards{BSD, sys/socket.h}
28f540f4
RM
2779Look at the data but don't remove it from the input queue. This is
2780only meaningful with input functions such as @code{recv}, not with
2781@code{send}.
2782@end deftypevr
2783
28f540f4 2784@deftypevr Macro int MSG_DONTROUTE
d08a7e4c 2785@standards{BSD, sys/socket.h}
28f540f4
RM
2786Don't include routing information in the message. This is only
2787meaningful with output operations, and is usually only of interest for
2788diagnostic or routing programs. We don't try to explain it here.
2789@end deftypevr
2790
2791@node Byte Stream Example
2792@subsection Byte Stream Socket Example
2793
2794Here is an example client program that makes a connection for a byte
2795stream socket in the Internet namespace. It doesn't do anything
2796particularly interesting once it has connected to the server; it just
2797sends a text string to the server and exits.
2798
4bca4c17
UD
2799This program uses @code{init_sockaddr} to set up the socket address; see
2800@ref{Inet Example}.
2801
28f540f4
RM
2802@smallexample
2803@include inetcli.c.texi
2804@end smallexample
2805
2806@node Server Example
2807@subsection Byte Stream Connection Server Example
2808
2809The server end is much more complicated. Since we want to allow
2810multiple clients to be connected to the server at the same time, it
2811would be incorrect to wait for input from a single client by simply
2812calling @code{read} or @code{recv}. Instead, the right thing to do is
2813to use @code{select} (@pxref{Waiting for I/O}) to wait for input on
2814all of the open sockets. This also allows the server to deal with
2815additional connection requests.
2816
2817This particular server doesn't do anything interesting once it has
2818gotten a message from a client. It does close the socket for that
2819client when it detects an end-of-file condition (resulting from the
2820client shutting down its end of the connection).
2821
4bca4c17
UD
2822This program uses @code{make_socket} to set up the socket address; see
2823@ref{Inet Example}.
28f540f4
RM
2824
2825@smallexample
2826@include inetsrv.c.texi
2827@end smallexample
2828
2829@node Out-of-Band Data
2830@subsection Out-of-Band Data
2831
2832@cindex out-of-band data
2833@cindex high-priority data
2834Streams with connections permit @dfn{out-of-band} data that is
2835delivered with higher priority than ordinary data. Typically the
2836reason for sending out-of-band data is to send notice of an
04b9968b 2837exceptional condition. To send out-of-band data use
28f540f4
RM
2838@code{send}, specifying the flag @code{MSG_OOB} (@pxref{Sending
2839Data}).
2840
04b9968b 2841Out-of-band data are received with higher priority because the
28f540f4
RM
2842receiving process need not read it in sequence; to read the next
2843available out-of-band data, use @code{recv} with the @code{MSG_OOB}
2844flag (@pxref{Receiving Data}). Ordinary read operations do not read
04b9968b 2845out-of-band data; they read only ordinary data.
28f540f4
RM
2846
2847@cindex urgent socket condition
04b9968b 2848When a socket finds that out-of-band data are on their way, it sends a
28f540f4
RM
2849@code{SIGURG} signal to the owner process or process group of the
2850socket. You can specify the owner using the @code{F_SETOWN} command
2851to the @code{fcntl} function; see @ref{Interrupt Input}. You must
2852also establish a handler for this signal, as described in @ref{Signal
2853Handling}, in order to take appropriate action such as reading the
2854out-of-band data.
2855
2856Alternatively, you can test for pending out-of-band data, or wait
2857until there is out-of-band data, using the @code{select} function; it
2858can wait for an exceptional condition on the socket. @xref{Waiting
2859for I/O}, for more information about @code{select}.
2860
2861Notification of out-of-band data (whether with @code{SIGURG} or with
04b9968b 2862@code{select}) indicates that out-of-band data are on the way; the data
28f540f4
RM
2863may not actually arrive until later. If you try to read the
2864out-of-band data before it arrives, @code{recv} fails with an
2865@code{EWOULDBLOCK} error.
2866
2867Sending out-of-band data automatically places a ``mark'' in the stream
2868of ordinary data, showing where in the sequence the out-of-band data
2869``would have been''. This is useful when the meaning of out-of-band
2870data is ``cancel everything sent so far''. Here is how you can test,
2871in the receiving process, whether any ordinary data was sent before
2872the mark:
2873
2874@smallexample
4bca4c17 2875success = ioctl (socket, SIOCATMARK, &atmark);
28f540f4
RM
2876@end smallexample
2877
4bca4c17
UD
2878The @code{integer} variable @var{atmark} is set to a nonzero value if
2879the socket's read pointer has reached the ``mark''.
2880
2881@c Posix 1.g specifies sockatmark for this ioctl. sockatmark is not
2882@c implemented yet.
2883
28f540f4
RM
2884Here's a function to discard any ordinary data preceding the
2885out-of-band mark:
2886
2887@smallexample
2888int
2889discard_until_mark (int socket)
2890@{
2891 while (1)
2892 @{
2893 /* @r{This is not an arbitrary limit; any size will do.} */
2894 char buffer[1024];
4bca4c17 2895 int atmark, success;
28f540f4
RM
2896
2897 /* @r{If we have reached the mark, return.} */
4bca4c17 2898 success = ioctl (socket, SIOCATMARK, &atmark);
28f540f4
RM
2899 if (success < 0)
2900 perror ("ioctl");
2901 if (result)
2902 return;
2903
2904 /* @r{Otherwise, read a bunch of ordinary data and discard it.}
2905 @r{This is guaranteed not to read past the mark}
2906 @r{if it starts before the mark.} */
2907 success = read (socket, buffer, sizeof buffer);
2908 if (success < 0)
2909 perror ("read");
2910 @}
2911@}
2912@end smallexample
2913
2914If you don't want to discard the ordinary data preceding the mark, you
2915may need to read some of it anyway, to make room in internal system
2916buffers for the out-of-band data. If you try to read out-of-band data
2917and get an @code{EWOULDBLOCK} error, try reading some ordinary data
2918(saving it so that you can use it when you want it) and see if that
2919makes room. Here is an example:
2920
2921@smallexample
2922struct buffer
2923@{
9133b79b 2924 char *buf;
28f540f4
RM
2925 int size;
2926 struct buffer *next;
2927@};
2928
2929/* @r{Read the out-of-band data from SOCKET and return it}
2930 @r{as a `struct buffer', which records the address of the data}
2931 @r{and its size.}
2932
2933 @r{It may be necessary to read some ordinary data}
2934 @r{in order to make room for the out-of-band data.}
04b9968b 2935 @r{If so, the ordinary data are saved as a chain of buffers}
28f540f4
RM
2936 @r{found in the `next' field of the value.} */
2937
2938struct buffer *
2939read_oob (int socket)
2940@{
2941 struct buffer *tail = 0;
2942 struct buffer *list = 0;
2943
2944 while (1)
2945 @{
2946 /* @r{This is an arbitrary limit.}
2947 @r{Does anyone know how to do this without a limit?} */
9133b79b
UD
2948#define BUF_SZ 1024
2949 char *buf = (char *) xmalloc (BUF_SZ);
28f540f4 2950 int success;
4bca4c17 2951 int atmark;
28f540f4
RM
2952
2953 /* @r{Try again to read the out-of-band data.} */
9133b79b 2954 success = recv (socket, buf, BUF_SZ, MSG_OOB);
28f540f4
RM
2955 if (success >= 0)
2956 @{
2957 /* @r{We got it, so return it.} */
2958 struct buffer *link
2959 = (struct buffer *) xmalloc (sizeof (struct buffer));
9133b79b 2960 link->buf = buf;
28f540f4
RM
2961 link->size = success;
2962 link->next = list;
2963 return link;
2964 @}
2965
2966 /* @r{If we fail, see if we are at the mark.} */
4bca4c17 2967 success = ioctl (socket, SIOCATMARK, &atmark);
28f540f4
RM
2968 if (success < 0)
2969 perror ("ioctl");
4bca4c17 2970 if (atmark)
28f540f4
RM
2971 @{
2972 /* @r{At the mark; skipping past more ordinary data cannot help.}
2973 @r{So just wait a while.} */
2974 sleep (1);
2975 continue;
2976 @}
2977
2978 /* @r{Otherwise, read a bunch of ordinary data and save it.}
2979 @r{This is guaranteed not to read past the mark}
2980 @r{if it starts before the mark.} */
9133b79b 2981 success = read (socket, buf, BUF_SZ);
28f540f4
RM
2982 if (success < 0)
2983 perror ("read");
2984
2985 /* @r{Save this data in the buffer list.} */
2986 @{
2987 struct buffer *link
2988 = (struct buffer *) xmalloc (sizeof (struct buffer));
9133b79b 2989 link->buf = buf;
28f540f4
RM
2990 link->size = success;
2991
2992 /* @r{Add the new link to the end of the list.} */
2993 if (tail)
2994 tail->next = link;
2995 else
2996 list = link;
2997 tail = link;
2998 @}
2999 @}
3000@}
3001@end smallexample
3002
3003@node Datagrams
3004@section Datagram Socket Operations
3005
3006@cindex datagram socket
3007This section describes how to use communication styles that don't use
3008connections (styles @code{SOCK_DGRAM} and @code{SOCK_RDM}). Using
3009these styles, you group data into packets and each packet is an
3010independent communication. You specify the destination for each
3011packet individually.
3012
04b9968b 3013Datagram packets are like letters: you send each one independently
28f540f4
RM
3014with its own destination address, and they may arrive in the wrong
3015order or not at all.
3016
3017The @code{listen} and @code{accept} functions are not allowed for
3018sockets using connectionless communication styles.
3019
3020@menu
3021* Sending Datagrams:: Sending packets on a datagram socket.
3022* Receiving Datagrams:: Receiving packets on a datagram socket.
3023* Datagram Example:: An example program: packets sent over a
4bca4c17 3024 datagram socket in the local namespace.
28f540f4
RM
3025* Example Receiver:: Another program, that receives those packets.
3026@end menu
3027
3028@node Sending Datagrams
3029@subsection Sending Datagrams
3030@cindex sending a datagram
3031@cindex transmitting datagrams
3032@cindex datagrams, transmitting
3033
3034@pindex sys/socket.h
3035The normal way of sending data on a datagram socket is by using the
3036@code{sendto} function, declared in @file{sys/socket.h}.
3037
3038You can call @code{connect} on a datagram socket, but this only
3039specifies a default destination for further data transmission on the
04b9968b 3040socket. When a socket has a default destination you can use
28f540f4
RM
3041@code{send} (@pxref{Sending Data}) or even @code{write} (@pxref{I/O
3042Primitives}) to send a packet there. You can cancel the default
3043destination by calling @code{connect} using an address format of
3044@code{AF_UNSPEC} in the @var{addr} argument. @xref{Connecting}, for
3045more information about the @code{connect} function.
3046
8ded91fb 3047@deftypefun ssize_t sendto (int @var{socket}, const void *@var{buffer}, size_t @var{size}, int @var{flags}, struct sockaddr *@var{addr}, socklen_t @var{length})
d08a7e4c 3048@standards{BSD, sys/socket.h}
973f180b 3049@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
28f540f4
RM
3050The @code{sendto} function transmits the data in the @var{buffer}
3051through the socket @var{socket} to the destination address specified
3052by the @var{addr} and @var{length} arguments. The @var{size} argument
3053specifies the number of bytes to be transmitted.
3054
3055The @var{flags} are interpreted the same way as for @code{send}; see
3056@ref{Socket Data Options}.
3057
3058The return value and error conditions are also the same as for
3059@code{send}, but you cannot rely on the system to detect errors and
3060report them; the most common error is that the packet is lost or there
04b9968b 3061is no-one at the specified address to receive it, and the operating
28f540f4
RM
3062system on your machine usually does not know this.
3063
3064It is also possible for one call to @code{sendto} to report an error
04b9968b 3065owing to a problem related to a previous call.
dd7d45e8 3066
04b9968b
UD
3067This function is defined as a cancellation point in multi-threaded
3068programs, so one has to be prepared for this and make sure that
b57dd246 3069allocated resources (like memory, file descriptors, semaphores or
04b9968b 3070whatever) are freed even if the thread is canceled.
dd7d45e8 3071@c @xref{pthread_cleanup_push}, for a method how to do this.
28f540f4
RM
3072@end deftypefun
3073
3074@node Receiving Datagrams
3075@subsection Receiving Datagrams
3076@cindex receiving datagrams
3077
3078The @code{recvfrom} function reads a packet from a datagram socket and
3079also tells you where it was sent from. This function is declared in
3080@file{sys/socket.h}.
3081
8ded91fb 3082@deftypefun ssize_t recvfrom (int @var{socket}, void *@var{buffer}, size_t @var{size}, int @var{flags}, struct sockaddr *@var{addr}, socklen_t *@var{length-ptr})
d08a7e4c 3083@standards{BSD, sys/socket.h}
973f180b 3084@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
28f540f4
RM
3085The @code{recvfrom} function reads one packet from the socket
3086@var{socket} into the buffer @var{buffer}. The @var{size} argument
3087specifies the maximum number of bytes to be read.
3088
3089If the packet is longer than @var{size} bytes, then you get the first
04b9968b 3090@var{size} bytes of the packet and the rest of the packet is lost.
28f540f4
RM
3091There's no way to read the rest of the packet. Thus, when you use a
3092packet protocol, you must always know how long a packet to expect.
3093
3094The @var{addr} and @var{length-ptr} arguments are used to return the
3095address where the packet came from. @xref{Socket Addresses}. For a
04b9968b 3096socket in the local domain the address information won't be meaningful,
4bca4c17 3097since you can't read the address of such a socket (@pxref{Local
28f540f4
RM
3098Namespace}). You can specify a null pointer as the @var{addr} argument
3099if you are not interested in this information.
3100
3101The @var{flags} are interpreted the same way as for @code{recv}
3102(@pxref{Socket Data Options}). The return value and error conditions
3103are also the same as for @code{recv}.
dd7d45e8 3104
04b9968b
UD
3105This function is defined as a cancellation point in multi-threaded
3106programs, so one has to be prepared for this and make sure that
b57dd246 3107allocated resources (like memory, file descriptors, semaphores or
04b9968b 3108whatever) are freed even if the thread is canceled.
dd7d45e8 3109@c @xref{pthread_cleanup_push}, for a method how to do this.
28f540f4
RM
3110@end deftypefun
3111
3112You can use plain @code{recv} (@pxref{Receiving Data}) instead of
04b9968b 3113@code{recvfrom} if you don't need to find out who sent the packet
28f540f4
RM
3114(either because you know where it should come from or because you
3115treat all possible senders alike). Even @code{read} can be used if
3116you don't want to specify @var{flags} (@pxref{I/O Primitives}).
3117
3118@ignore
3119@c sendmsg and recvmsg are like readv and writev in that they
3120@c use a series of buffers. It's not clear this is worth
3121@c supporting or that we support them.
3122@c !!! they can do more; it is hairy
3123
28f540f4 3124@deftp {Data Type} {struct msghdr}
d08a7e4c 3125@standards{BSD, sys/socket.h}
28f540f4
RM
3126@end deftp
3127
8ded91fb 3128@deftypefun ssize_t sendmsg (int @var{socket}, const struct msghdr *@var{message}, int @var{flags})
d08a7e4c 3129@standards{BSD, sys/socket.h}
973f180b 3130@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
dd7d45e8 3131
04b9968b
UD
3132This function is defined as a cancellation point in multi-threaded
3133programs, so one has to be prepared for this and make sure that
3134allocated resources (like memory, files descriptors, semaphores or
3135whatever) are freed even if the thread is cancel.
dd7d45e8 3136@c @xref{pthread_cleanup_push}, for a method how to do this.
28f540f4
RM
3137@end deftypefun
3138
8ded91fb 3139@deftypefun ssize_t recvmsg (int @var{socket}, struct msghdr *@var{message}, int @var{flags})
d08a7e4c 3140@standards{BSD, sys/socket.h}
973f180b 3141@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
dd7d45e8 3142
04b9968b
UD
3143This function is defined as a cancellation point in multi-threaded
3144programs, so one has to be prepared for this and make sure that
3145allocated resources (like memory, files descriptors, semaphores or
3146whatever) are freed even if the thread is canceled.
dd7d45e8 3147@c @xref{pthread_cleanup_push}, for a method how to do this.
28f540f4
RM
3148@end deftypefun
3149@end ignore
3150
3151@node Datagram Example
3152@subsection Datagram Socket Example
3153
3154Here is a set of example programs that send messages over a datagram
4bca4c17
UD
3155stream in the local namespace. Both the client and server programs use
3156the @code{make_named_socket} function that was presented in @ref{Local
3157Socket Example}, to create and name their sockets.
28f540f4
RM
3158
3159First, here is the server program. It sits in a loop waiting for
3160messages to arrive, bouncing each message back to the sender.
04b9968b 3161Obviously this isn't a particularly useful program, but it does show
28f540f4
RM
3162the general ideas involved.
3163
3164@smallexample
3165@include filesrv.c.texi
3166@end smallexample
3167
3168@node Example Receiver
3169@subsection Example of Reading Datagrams
3170
3171Here is the client program corresponding to the server above.
3172
3173It sends a datagram to the server and then waits for a reply. Notice
3174that the socket for the client (as well as for the server) in this
3175example has to be given a name. This is so that the server can direct
3176a message back to the client. Since the socket has no associated
3177connection state, the only way the server can do this is by
3178referencing the name of the client.
3179
3180@smallexample
3181@include filecli.c.texi
3182@end smallexample
3183
3184Keep in mind that datagram socket communications are unreliable. In
3185this example, the client program waits indefinitely if the message
3186never reaches the server or if the server's response never comes
04b9968b
UD
3187back. It's up to the user running the program to kill and restart
3188it if desired. A more automatic solution could be to use
28f540f4 3189@code{select} (@pxref{Waiting for I/O}) to establish a timeout period
04b9968b 3190for the reply, and in case of timeout either re-send the message or
28f540f4
RM
3191shut down the socket and exit.
3192
3193@node Inetd
3194@section The @code{inetd} Daemon
3195
3196We've explained above how to write a server program that does its own
3197listening. Such a server must already be running in order for anyone
3198to connect to it.
3199
04b9968b 3200Another way to provide a service on an Internet port is to let the daemon
28f540f4
RM
3201program @code{inetd} do the listening. @code{inetd} is a program that
3202runs all the time and waits (using @code{select}) for messages on a
3203specified set of ports. When it receives a message, it accepts the
3204connection (if the socket style calls for connections) and then forks a
3205child process to run the corresponding server program. You specify the
3206ports and their programs in the file @file{/etc/inetd.conf}.
3207
3208@menu
3209* Inetd Servers::
3210* Configuring Inetd::
3211@end menu
3212
3213@node Inetd Servers
3214@subsection @code{inetd} Servers
3215
3216Writing a server program to be run by @code{inetd} is very simple. Each time
3217someone requests a connection to the appropriate port, a new server
3218process starts. The connection already exists at this time; the
3219socket is available as the standard input descriptor and as the
3220standard output descriptor (descriptors 0 and 1) in the server
04b9968b 3221process. Thus the server program can begin reading and writing data
28f540f4
RM
3222right away. Often the program needs only the ordinary I/O facilities;
3223in fact, a general-purpose filter program that knows nothing about
3224sockets can work as a byte stream server run by @code{inetd}.
3225
3226You can also use @code{inetd} for servers that use connectionless
3227communication styles. For these servers, @code{inetd} does not try to accept
04b9968b 3228a connection since no connection is possible. It just starts the
28f540f4
RM
3229server program, which can read the incoming datagram packet from
3230descriptor 0. The server program can handle one request and then
3231exit, or you can choose to write it to keep reading more requests
3232until no more arrive, and then exit. You must specify which of these
04b9968b 3233two techniques the server uses when you configure @code{inetd}.
28f540f4
RM
3234
3235@node Configuring Inetd
3236@subsection Configuring @code{inetd}
3237
3238The file @file{/etc/inetd.conf} tells @code{inetd} which ports to listen to
3239and what server programs to run for them. Normally each entry in the
3240file is one line, but you can split it onto multiple lines provided
3241all but the first line of the entry start with whitespace. Lines that
3242start with @samp{#} are comments.
3243
3244Here are two standard entries in @file{/etc/inetd.conf}:
3245
3246@smallexample
3247ftp stream tcp nowait root /libexec/ftpd ftpd
3248talk dgram udp wait root /libexec/talkd talkd
3249@end smallexample
3250
3251An entry has this format:
3252
3253@smallexample
3254@var{service} @var{style} @var{protocol} @var{wait} @var{username} @var{program} @var{arguments}
3255@end smallexample
3256
3257The @var{service} field says which service this program provides. It
3258should be the name of a service defined in @file{/etc/services}.
3259@code{inetd} uses @var{service} to decide which port to listen on for
3260this entry.
3261
3262The fields @var{style} and @var{protocol} specify the communication
3263style and the protocol to use for the listening socket. The style
3264should be the name of a communication style, converted to lower case
3265and with @samp{SOCK_} deleted---for example, @samp{stream} or
3266@samp{dgram}. @var{protocol} should be one of the protocols listed in
3267@file{/etc/protocols}. The typical protocol names are @samp{tcp} for
3268byte stream connections and @samp{udp} for unreliable datagrams.
3269
3270The @var{wait} field should be either @samp{wait} or @samp{nowait}.
3271Use @samp{wait} if @var{style} is a connectionless style and the
04b9968b 3272server, once started, handles multiple requests as they come in.
28f540f4
RM
3273Use @samp{nowait} if @code{inetd} should start a new process for each message
3274or request that comes in. If @var{style} uses connections, then
3275@var{wait} @strong{must} be @samp{nowait}.
3276
3277@var{user} is the user name that the server should run as. @code{inetd} runs
3278as root, so it can set the user ID of its children arbitrarily. It's
3279best to avoid using @samp{root} for @var{user} if you can; but some
841785ba 3280servers, such as Telnet and FTP, read a username and passphrase
28f540f4
RM
3281themselves. These servers need to be root initially so they can log
3282in as commanded by the data coming over the network.
3283
3284@var{program} together with @var{arguments} specifies the command to
3285run to start the server. @var{program} should be an absolute file
3286name specifying the executable file to run. @var{arguments} consists
3287of any number of whitespace-separated words, which become the
3288command-line arguments of @var{program}. The first word in
3289@var{arguments} is argument zero, which should by convention be the
3290program name itself (sans directories).
3291
3292If you edit @file{/etc/inetd.conf}, you can tell @code{inetd} to reread the
3293file and obey its new contents by sending the @code{inetd} process the
3294@code{SIGHUP} signal. You'll have to use @code{ps} to determine the
04b9968b 3295process ID of the @code{inetd} process as it is not fixed.
28f540f4
RM
3296
3297@c !!! could document /etc/inetd.sec
3298
3299@node Socket Options
3300@section Socket Options
3301@cindex socket options
3302
3303This section describes how to read or set various options that modify
3304the behavior of sockets and their underlying communications protocols.
3305
3306@cindex level, for socket options
3307@cindex socket option level
3308When you are manipulating a socket option, you must specify which
3309@dfn{level} the option pertains to. This describes whether the option
3310applies to the socket interface, or to a lower-level communications
3311protocol interface.
3312
3313@menu
3314* Socket Option Functions:: The basic functions for setting and getting
3315 socket options.
3316* Socket-Level Options:: Details of the options at the socket level.
3317@end menu
3318
3319@node Socket Option Functions
3320@subsection Socket Option Functions
3321
3322@pindex sys/socket.h
3323Here are the functions for examining and modifying socket options.
3324They are declared in @file{sys/socket.h}.
3325
55c14926 3326@deftypefun int getsockopt (int @var{socket}, int @var{level}, int @var{optname}, void *@var{optval}, socklen_t *@var{optlen-ptr})
d08a7e4c 3327@standards{BSD, sys/socket.h}
973f180b 3328@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
28f540f4
RM
3329The @code{getsockopt} function gets information about the value of
3330option @var{optname} at level @var{level} for socket @var{socket}.
3331
b57dd246 3332The option value is stored in the buffer that @var{optval} points to.
28f540f4
RM
3333Before the call, you should supply in @code{*@var{optlen-ptr}} the
3334size of this buffer; on return, it contains the number of bytes of
3335information actually stored in the buffer.
3336
3337Most options interpret the @var{optval} buffer as a single @code{int}
3338value.
3339
3340The actual return value of @code{getsockopt} is @code{0} on success
3341and @code{-1} on failure. The following @code{errno} error conditions
3342are defined:
3343
3344@table @code
3345@item EBADF
3346The @var{socket} argument is not a valid file descriptor.
3347
3348@item ENOTSOCK
3349The descriptor @var{socket} is not a socket.
3350
3351@item ENOPROTOOPT
3352The @var{optname} doesn't make sense for the given @var{level}.
3353@end table
3354@end deftypefun
3355
8ded91fb 3356@deftypefun int setsockopt (int @var{socket}, int @var{level}, int @var{optname}, const void *@var{optval}, socklen_t @var{optlen})
d08a7e4c 3357@standards{BSD, sys/socket.h}
973f180b 3358@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
28f540f4
RM
3359This function is used to set the socket option @var{optname} at level
3360@var{level} for socket @var{socket}. The value of the option is passed
04b9968b 3361in the buffer @var{optval} of size @var{optlen}.
28f540f4 3362
838e5ffe
UD
3363@c Argh. -zw
3364@iftex
3365@hfuzz 6pt
28f540f4
RM
3366The return value and error codes for @code{setsockopt} are the same as
3367for @code{getsockopt}.
838e5ffe
UD
3368@end iftex
3369@ifinfo
3370The return value and error codes for @code{setsockopt} are the same as
3371for @code{getsockopt}.
3372@end ifinfo
3373
28f540f4
RM
3374@end deftypefun
3375
3376@node Socket-Level Options
3377@subsection Socket-Level Options
3378
28f540f4 3379@deftypevr Constant int SOL_SOCKET
d08a7e4c 3380@standards{BSD, sys/socket.h}
28f540f4
RM
3381Use this constant as the @var{level} argument to @code{getsockopt} or
3382@code{setsockopt} to manipulate the socket-level options described in
3383this section.
3384@end deftypevr
3385
3386@pindex sys/socket.h
838e5ffe 3387@noindent
28f540f4 3388Here is a table of socket-level option names; all are defined in the
9afc8a59 3389header file @file{sys/socket.h}.
28f540f4 3390
a449fc68 3391@vtable @code
28f540f4 3392@item SO_DEBUG
d08a7e4c 3393@standards{BSD, sys/socket.h}
28f540f4
RM
3394@c Extra blank line here makes the table look better.
3395
3396This option toggles recording of debugging information in the underlying
3397protocol modules. The value has type @code{int}; a nonzero value means
3398``yes''.
3399@c !!! should say how this is used
04b9968b 3400@c OK, anyone who knows, please explain.
28f540f4 3401
28f540f4 3402@item SO_REUSEADDR
d08a7e4c 3403@standards{BSD, sys/socket.h}
28f540f4
RM
3404This option controls whether @code{bind} (@pxref{Setting Address})
3405should permit reuse of local addresses for this socket. If you enable
3406this option, you can actually have two sockets with the same Internet
3407port number; but the system won't allow you to use the two
3408identically-named sockets in a way that would confuse the Internet. The
3409reason for this option is that some higher-level Internet protocols,
4bca4c17 3410including FTP, require you to keep reusing the same port number.
28f540f4
RM
3411
3412The value has type @code{int}; a nonzero value means ``yes''.
3413
28f540f4 3414@item SO_KEEPALIVE
d08a7e4c 3415@standards{BSD, sys/socket.h}
28f540f4
RM
3416This option controls whether the underlying protocol should
3417periodically transmit messages on a connected socket. If the peer
3418fails to respond to these messages, the connection is considered
3419broken. The value has type @code{int}; a nonzero value means
3420``yes''.
3421
28f540f4 3422@item SO_DONTROUTE
d08a7e4c 3423@standards{BSD, sys/socket.h}
28f540f4
RM
3424This option controls whether outgoing messages bypass the normal
3425message routing facilities. If set, messages are sent directly to the
3426network interface instead. The value has type @code{int}; a nonzero
3427value means ``yes''.
3428
28f540f4 3429@item SO_LINGER
d08a7e4c 3430@standards{BSD, sys/socket.h}
28f540f4
RM
3431This option specifies what should happen when the socket of a type
3432that promises reliable delivery still has untransmitted messages when
3433it is closed; see @ref{Closing a Socket}. The value has type
3434@code{struct linger}.
3435
28f540f4 3436@deftp {Data Type} {struct linger}
d08a7e4c 3437@standards{BSD, sys/socket.h}
28f540f4
RM
3438This structure type has the following members:
3439
3440@table @code
3441@item int l_onoff
3442This field is interpreted as a boolean. If nonzero, @code{close}
04b9968b 3443blocks until the data are transmitted or the timeout period has expired.
28f540f4
RM
3444
3445@item int l_linger
3446This specifies the timeout period, in seconds.
3447@end table
3448@end deftp
3449
28f540f4 3450@item SO_BROADCAST
d08a7e4c 3451@standards{BSD, sys/socket.h}
28f540f4
RM
3452This option controls whether datagrams may be broadcast from the socket.
3453The value has type @code{int}; a nonzero value means ``yes''.
3454
28f540f4 3455@item SO_OOBINLINE
d08a7e4c 3456@standards{BSD, sys/socket.h}
28f540f4
RM
3457If this option is set, out-of-band data received on the socket is
3458placed in the normal input queue. This permits it to be read using
3459@code{read} or @code{recv} without specifying the @code{MSG_OOB}
3460flag. @xref{Out-of-Band Data}. The value has type @code{int}; a
3461nonzero value means ``yes''.
3462
28f540f4 3463@item SO_SNDBUF
d08a7e4c 3464@standards{BSD, sys/socket.h}
28f540f4
RM
3465This option gets or sets the size of the output buffer. The value is a
3466@code{size_t}, which is the size in bytes.
3467
28f540f4 3468@item SO_RCVBUF
d08a7e4c 3469@standards{BSD, sys/socket.h}
28f540f4
RM
3470This option gets or sets the size of the input buffer. The value is a
3471@code{size_t}, which is the size in bytes.
3472
28f540f4 3473@item SO_STYLE
28f540f4 3474@itemx SO_TYPE
d08a7e4c
RJ
3475@standards{GNU, sys/socket.h}
3476@standardsx{SO_TYPE, BSD, sys/socket.h}
28f540f4
RM
3477This option can be used with @code{getsockopt} only. It is used to
3478get the socket's communication style. @code{SO_TYPE} is the
3479historical name, and @code{SO_STYLE} is the preferred name in GNU.
3480The value has type @code{int} and its value designates a communication
3481style; see @ref{Communication Styles}.
3482
28f540f4 3483@item SO_ERROR
d08a7e4c 3484@standards{BSD, sys/socket.h}
28f540f4
RM
3485@c Extra blank line here makes the table look better.
3486
3487This option can be used with @code{getsockopt} only. It is used to reset
3488the error status of the socket. The value is an @code{int}, which represents
3489the previous error status.
3490@c !!! what is "socket error status"? this is never defined.
a449fc68 3491@end vtable
28f540f4
RM
3492
3493@node Networks Database
3494@section Networks Database
3495@cindex networks database
3496@cindex converting network number to network name
3497@cindex converting network name to network number
3498
3499@pindex /etc/networks
3500@pindex netdb.h
3501Many systems come with a database that records a list of networks known
3502to the system developer. This is usually kept either in the file
3503@file{/etc/networks} or in an equivalent from a name server. This data
3504base is useful for routing programs such as @code{route}, but it is not
3505useful for programs that simply communicate over the network. We
04b9968b 3506provide functions to access this database, which are declared in
28f540f4
RM
3507@file{netdb.h}.
3508
28f540f4 3509@deftp {Data Type} {struct netent}
d08a7e4c 3510@standards{BSD, netdb.h}
28f540f4
RM
3511This data type is used to represent information about entries in the
3512networks database. It has the following members:
3513
3514@table @code
3515@item char *n_name
3516This is the ``official'' name of the network.
3517
3518@item char **n_aliases
3519These are alternative names for the network, represented as a vector
3520of strings. A null pointer terminates the array.
3521
3522@item int n_addrtype
3523This is the type of the network number; this is always equal to
3524@code{AF_INET} for Internet networks.
3525
3526@item unsigned long int n_net
3527This is the network number. Network numbers are returned in host
3528byte order; see @ref{Byte Order}.
3529@end table
3530@end deftp
3531
3532Use the @code{getnetbyname} or @code{getnetbyaddr} functions to search
3533the networks database for information about a specific network. The
3534information is returned in a statically-allocated structure; you must
3535copy the information if you need to save it.
3536
28f540f4 3537@deftypefun {struct netent *} getnetbyname (const char *@var{name})
d08a7e4c 3538@standards{BSD, netdb.h}
973f180b
AO
3539@safety{@prelim{}@mtunsafe{@mtasurace{:netbyname} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
3540@c getnetbyname =~ getpwuid @mtasurace:netbyname @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3541@c libc_lock_lock dup @asulock @aculock
3542@c malloc dup @ascuheap @acsmem
3543@c getnetbyname_r dup @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3544@c realloc dup @ascuheap @acsmem
3545@c free dup @ascuheap @acsmem
3546@c libc_lock_unlock dup @aculock
3547@c
3548@c getnetbyname_r =~ getpwuid_r @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3549@c no nscd support
3550@c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
3551@c nss_networks_lookup2 =~ nss_passwd_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3552@c *fct.l -> _nss_*_getnetbyname_r @ascuplugin
3553@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
28f540f4
RM
3554The @code{getnetbyname} function returns information about the network
3555named @var{name}. It returns a null pointer if there is no such
3556network.
3557@end deftypefun
3558
8ded91fb 3559@deftypefun {struct netent *} getnetbyaddr (uint32_t @var{net}, int @var{type})
d08a7e4c 3560@standards{BSD, netdb.h}
973f180b
AO
3561@safety{@prelim{}@mtunsafe{@mtasurace{:netbyaddr} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
3562@c getnetbyaddr =~ getpwuid @mtasurace:netbyaddr @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3563@c libc_lock_lock dup @asulock @aculock
3564@c malloc dup @ascuheap @acsmem
3565@c getnetbyaddr_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3566@c realloc dup @ascuheap @acsmem
3567@c free dup @ascuheap @acsmem
3568@c libc_lock_unlock dup @aculock
3569@c
3570@c getnetbyaddr_r =~ getpwuid_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3571@c no nscd support
3572@c nss_networks_lookup2 =~ nss_passwd_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3573@c *fct.l -> _nss_*_getnetbyaddr_r @ascuplugin
3574@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
28f540f4
RM
3575The @code{getnetbyaddr} function returns information about the network
3576of type @var{type} with number @var{net}. You should specify a value of
6e953631 3577@code{AF_INET} for the @var{type} argument for Internet networks.
28f540f4
RM
3578
3579@code{getnetbyaddr} returns a null pointer if there is no such
3580network.
3581@end deftypefun
3582
3583You can also scan the networks database using @code{setnetent},
04b9968b
UD
3584@code{getnetent} and @code{endnetent}. Be careful when using these
3585functions because they are not reentrant.
28f540f4 3586
28f540f4 3587@deftypefun void setnetent (int @var{stayopen})
d08a7e4c 3588@standards{BSD, netdb.h}
973f180b
AO
3589@safety{@prelim{}@mtunsafe{@mtasurace{:netent} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
3590@c setnetent @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3591@c libc_lock_lock dup @asulock @aculock
3592@c nss_setent(nss_networks_lookup2) @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3593@c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
3594@c setup(nss_networks_lookup2) @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3595@c *lookup_fct = nss_networks_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3596@c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3597@c *fct.f @mtasurace:netent @ascuplugin
3598@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3599@c libc_lock_unlock dup @aculock
28f540f4
RM
3600This function opens and rewinds the networks database.
3601
3602If the @var{stayopen} argument is nonzero, this sets a flag so that
3603subsequent calls to @code{getnetbyname} or @code{getnetbyaddr} will
3604not close the database (as they usually would). This makes for more
3605efficiency if you call those functions several times, by avoiding
3606reopening the database for each call.
3607@end deftypefun
3608
28f540f4 3609@deftypefun {struct netent *} getnetent (void)
d08a7e4c 3610@standards{BSD, netdb.h}
973f180b
AO
3611@safety{@prelim{}@mtunsafe{@mtasurace{:netent} @mtasurace{:netentbuf} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
3612@c getnetent @mtasurace:netent @mtasurace:netentbuf @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3613@c libc_lock_lock dup @asulock @aculock
3614@c nss_getent(getnetent_r) @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3615@c malloc dup @ascuheap @acsmem
3616@c *func = getnetent_r dup @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3617@c realloc dup @ascuheap @acsmem
3618@c free dup @ascuheap @acsmem
3619@c libc_lock_unlock dup @aculock
3620@c
3621@c getnetent_r @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3622@c libc_lock_lock dup @asulock @aculock
3623@c nss_getent_r(nss_networks_lookup2) @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3624@c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
3625@c setup(nss_networks_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3626@c *fct.f @mtasurace:servent @ascuplugin
3627@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3628@c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3629@c *sfct.f @mtasurace:netent @ascuplugin
3630@c libc_lock_unlock dup @aculock
28f540f4
RM
3631This function returns the next entry in the networks database. It
3632returns a null pointer if there are no more entries.
3633@end deftypefun
3634
28f540f4 3635@deftypefun void endnetent (void)
d08a7e4c 3636@standards{BSD, netdb.h}
973f180b
AO
3637@safety{@prelim{}@mtunsafe{@mtasurace{:netent} @mtsenv{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
3638@c endnetent @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3639@c libc_lock_lock @asulock @aculock
3640@c nss_endent(nss_networks_lookup2) @mtasurace:netent @mtsenv @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3641@c res_maybe_init(!preinit) dup @mtsenv @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
3642@c setup(nss_networks_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3643@c *fct.f @mtasurace:netent @ascuplugin
3644@c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
3645@c libc_lock_unlock @aculock
28f540f4
RM
3646This function closes the networks database.
3647@end deftypefun