Remove powerpc, sparc fdim inlines (bug 22987).
[glibc.git] / manual / socket.texi
blob79eb4208be108ed40fa2f2ea2bf860e6810f6bc8
1 @node Sockets, Low-Level Terminal Interface, Pipes and FIFOs, Top
2 @c %MENU% A more complicated IPC mechanism, with networking support
3 @chapter Sockets
5 This chapter describes the GNU facilities for interprocess
6 communication using sockets.
8 @cindex socket
9 @cindex interprocess communication, with sockets
10 A @dfn{socket} is a generalized interprocess communication channel.
11 Like a pipe, a socket is represented as a file descriptor.  Unlike pipes
12 sockets support communication between unrelated processes, and even
13 between processes running on different machines that communicate over a
14 network.  Sockets are the primary means of communicating with other
15 machines; @code{telnet}, @code{rlogin}, @code{ftp}, @code{talk} and the
16 other familiar network programs use sockets.
18 Not all operating systems support sockets.  In @theglibc{}, the
19 header file @file{sys/socket.h} exists regardless of the operating
20 system, and the socket functions always exist, but if the system does
21 not really support sockets these functions always fail.
23 @strong{Incomplete:} We do not currently document the facilities for
24 broadcast messages or for configuring Internet interfaces.  The
25 reentrant functions and some newer functions that are related to IPv6
26 aren't documented either so far.
28 @menu
29 * Socket Concepts::     Basic concepts you need to know about.
30 * Communication Styles::Stream communication, datagrams and other styles.
31 * Socket Addresses::    How socket names (``addresses'') work.
32 * Interface Naming::    Identifying specific network interfaces.
33 * Local Namespace::     Details about the local namespace.
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
46 @node Socket Concepts
47 @section Socket Concepts
49 @cindex communication style (of a socket)
50 @cindex style of communication (of a socket)
51 When you create a socket, you must specify the style of communication
52 you want to use and the type of protocol that should implement it.
53 The @dfn{communication style} of a socket defines the user-level
54 semantics of sending and receiving data on the socket.  Choosing a
55 communication style specifies the answers to questions such as these:
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
63 styles regard the data as a sequence of bytes with no larger
64 structure; others group the bytes into records (which are known in
65 this context as @dfn{packets}).
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
71 styles guarantee that all the data sent arrives in the order it was
72 sent (barring system or network crashes); other styles occasionally
73 lose data as a normal part of operation, and may sometimes deliver
74 packets more than once or in the wrong order.
76 Designing a program to use unreliable communication styles usually
77 involves taking precautions to detect lost or misordered packets and
78 to retransmit data as needed.
80 @item
81 @strong{Is communication entirely with one partner?}  Some
82 communication styles are like a telephone call---you make a
83 @dfn{connection} with one remote socket and then exchange data
84 freely.  Other styles are like mailing letters---you specify a
85 destination address for each message you send.
86 @end itemize
88 @cindex namespace (of socket)
89 @cindex domain (of socket)
90 @cindex socket namespace
91 @cindex socket domain
92 You must also choose a @dfn{namespace} for naming the socket.  A socket
93 name (``address'') is meaningful only in the context of a particular
94 namespace.  In fact, even the data type to use for a socket name may
95 depend on the namespace.  Namespaces are also called ``domains'', but we
96 avoid that word as it can be confused with other usage of the same
97 term.  Each namespace has a symbolic name that starts with @samp{PF_}.
98 A corresponding symbolic name starting with @samp{AF_} designates the
99 address format for that namespace.
101 @cindex network protocol
102 @cindex protocol (of socket)
103 @cindex socket protocol
104 @cindex protocol family
105 Finally you must choose the @dfn{protocol} to carry out the
106 communication.  The protocol determines what low-level mechanism is used
107 to transmit and receive data.  Each protocol is valid for a particular
108 namespace and communication style; a namespace is sometimes called a
109 @dfn{protocol family} because of this, which is why the namespace names
110 start with @samp{PF_}.
112 The rules of a protocol apply to the data passing between two programs,
113 perhaps on different computers; most of these rules are handled by the
114 operating system and you need not know about them.  What you do need to
115 know about protocols is this:
117 @itemize @bullet
118 @item
119 In order to have communication between two sockets, they must specify
120 the @emph{same} protocol.
122 @item
123 Each protocol is meaningful with particular style/namespace
124 combinations and cannot be used with inappropriate combinations.  For
125 example, the TCP protocol fits only the byte stream style of
126 communication and the Internet namespace.
128 @item
129 For each combination of style and namespace there is a @dfn{default
130 protocol}, which you can request by specifying 0 as the protocol
131 number.  And that's what you should normally do---use the default.
132 @end itemize
134 Throughout the following description at various places
135 variables/parameters to denote sizes are required.  And here the trouble
136 starts.  In the first implementations the type of these variables was
137 simply @code{int}.  On most machines at that time an @code{int} was 32
138 bits wide, which created a @emph{de facto} standard requiring 32-bit
139 variables.  This is important since references to variables of this type
140 are passed to the kernel.
142 Then the POSIX people came and unified the interface with the words "all
143 size 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
145 possible.
147 The Unix98 specification provides a solution by introducing a type
148 @code{socklen_t}.  This type is used in all of the cases that POSIX
149 changed to use @code{size_t}.  The only requirement of this type is that
150 it be an unsigned type of at least 32 bits.  Therefore, implementations
151 which require that references to 32-bit variables be passed can be as
152 happy as implementations which use 64-bit values.
155 @node Communication Styles
156 @section Communication Styles
158 @Theglibc{} includes support for several different kinds of sockets,
159 each with different characteristics.  This section describes the
160 supported socket types.  The symbolic constants listed here are
161 defined in @file{sys/socket.h}.
162 @pindex sys/socket.h
164 @deftypevr Macro int SOCK_STREAM
165 @standards{BSD, sys/socket.h}
166 The @code{SOCK_STREAM} style is like a pipe (@pxref{Pipes and FIFOs}).
167 It operates over a connection with a particular remote socket and
168 transmits data reliably as a stream of bytes.
170 Use of this style is covered in detail in @ref{Connections}.
171 @end deftypevr
173 @deftypevr Macro int SOCK_DGRAM
174 @standards{BSD, sys/socket.h}
175 The @code{SOCK_DGRAM} style is used for sending
176 individually-addressed packets unreliably.
177 It is the diametrical opposite of @code{SOCK_STREAM}.
179 Each time you write data to a socket of this kind, that data becomes
180 one packet.  Since @code{SOCK_DGRAM} sockets do not have connections,
181 you must specify the recipient address with each packet.
183 The only guarantee that the system makes about your requests to
184 transmit data is that it will try its best to deliver each packet you
185 send.  It may succeed with the sixth packet after failing with the
186 fourth and fifth packets; the seventh packet may arrive before the
187 sixth, and may arrive a second time after the sixth.
189 The typical use for @code{SOCK_DGRAM} is in situations where it is
190 acceptable to simply re-send a packet if no response is seen in a
191 reasonable amount of time.
193 @xref{Datagrams}, for detailed information about how to use datagram
194 sockets.
195 @end deftypevr
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.
200 @deftypevr Macro int SOCK_SEQPACKET
201 @standards{BSD, sys/socket.h}
202 This style is like @code{SOCK_STREAM} except that the data are
203 structured into packets.
205 A program that receives data over a @code{SOCK_SEQPACKET} socket
206 should be prepared to read the entire message packet in a single call
207 to @code{read}; if it only reads part of the message, the remainder of
208 the message is simply discarded instead of being available for
209 subsequent calls to @code{read}.
211 Many protocols do not support this communication style.
212 @end deftypevr
213 @end ignore
215 @ignore
216 @deftypevr Macro int SOCK_RDM
217 @standards{BSD, sys/socket.h}
218 This style is a reliable version of @code{SOCK_DGRAM}: it sends
219 individually addressed packets, but guarantees that each packet sent
220 arrives exactly once.
222 @strong{Warning:} It is not clear this is actually supported
223 by any operating system.
224 @end deftypevr
225 @end ignore
227 @deftypevr Macro int SOCK_RAW
228 @standards{BSD, sys/socket.h}
229 This style provides access to low-level network protocols and
230 interfaces.  Ordinary user programs usually have no need to use this
231 style.
232 @end deftypevr
234 @node Socket Addresses
235 @section Socket Addresses
237 @cindex address of socket
238 @cindex name of socket
239 @cindex binding a socket address
240 @cindex socket address (name) binding
241 The name of a socket is normally called an @dfn{address}.  The
242 functions and symbols for dealing with socket addresses were named
243 inconsistently, sometimes using the term ``name'' and sometimes using
244 ``address''.  You can regard these terms as synonymous where sockets
245 are concerned.
247 A socket newly created with the @code{socket} function has no
248 address.  Other processes can find it for communication only if you
249 give it an address.  We call this @dfn{binding} the address to the
250 socket, and the way to do it is with the @code{bind} function.
252 You need only be concerned with the address of a socket if other processes
253 are to find it and start communicating with it.  You can specify an
254 address for other sockets, but this is usually pointless; the first time
255 you send data from a socket, or use it to initiate a connection, the
256 system assigns an address automatically if you have not specified one.
258 Occasionally a client needs to specify an address because the server
259 discriminates based on address; for example, the rsh and rlogin
260 protocols look at the client's socket address and only bypass password
261 checking if it is less than @code{IPPORT_RESERVED} (@pxref{Ports}).
263 The details of socket addresses vary depending on what namespace you are
264 using.  @xref{Local Namespace}, or @ref{Internet Namespace}, for specific
265 information.
267 Regardless of the namespace, you use the same functions @code{bind} and
268 @code{getsockname} to set and examine a socket's address.  These
269 functions use a phony data type, @code{struct sockaddr *}, to accept the
270 address.  In practice, the address lives in a structure of some other
271 data type appropriate to the address format you are using, but you cast
272 its address to @code{struct sockaddr *} when you pass it to
273 @code{bind}.
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
281 @node Address Formats
282 @subsection Address Formats
284 The functions @code{bind} and @code{getsockname} use the generic data
285 type @code{struct sockaddr *} to represent a pointer to a socket
286 address.  You can't use this data type effectively to interpret an
287 address or construct one; for that, you must use the proper data type
288 for the socket's namespace.
290 Thus, the usual practice is to construct an address of the proper
291 namespace-specific type, then cast a pointer to @code{struct sockaddr *}
292 when you call @code{bind} or @code{getsockname}.
294 The one piece of information that you can get from the @code{struct
295 sockaddr} data type is the @dfn{address format designator}.  This tells
296 you which data type to use to understand the address fully.
298 @pindex sys/socket.h
299 The symbols in this section are defined in the header file
300 @file{sys/socket.h}.
302 @deftp {Data Type} {struct sockaddr}
303 @standards{BSD, sys/socket.h}
304 The @code{struct sockaddr} type itself has the following members:
306 @table @code
307 @item short int sa_family
308 This is the code for the address format of this address.  It
309 identifies the format of the data which follows.
311 @item char sa_data[14]
312 This is the actual socket address data, which is format-dependent.  Its
313 length also depends on the format, and may well be more than 14.  The
314 length 14 of @code{sa_data} is essentially arbitrary.
315 @end table
316 @end deftp
318 Each address format has a symbolic name which starts with @samp{AF_}.
319 Each of them corresponds to a @samp{PF_} symbol which designates the
320 corresponding namespace.  Here is a list of address format names:
322 @vtable @code
323 @item AF_LOCAL
324 @standards{POSIX, sys/socket.h}
325 This designates the address format that goes with the local namespace.
326 (@code{PF_LOCAL} is the name of that namespace.)  @xref{Local Namespace
327 Details}, for information about this address format.
329 @item AF_UNIX
330 @standards{BSD, sys/socket.h}
331 @standards{Unix98, sys/socket.h}
332 This is a synonym for @code{AF_LOCAL}.  Although @code{AF_LOCAL} is
333 mandated 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
335 POSIX systems support it.  It is also the name of choice in the Unix98
336 specification. (The same is true for @code{PF_UNIX}
337 vs. @code{PF_LOCAL}).
339 @item AF_FILE
340 @standards{GNU, sys/socket.h}
341 This is another synonym for @code{AF_LOCAL}, for compatibility.
342 (@code{PF_FILE} is likewise a synonym for @code{PF_LOCAL}.)
344 @item AF_INET
345 @standards{BSD, sys/socket.h}
346 This designates the address format that goes with the Internet
347 namespace.  (@code{PF_INET} is the name of that namespace.)
348 @xref{Internet Address Formats}.
350 @item AF_INET6
351 @standards{IPv6 Basic API, sys/socket.h}
352 This is similar to @code{AF_INET}, but refers to the IPv6 protocol.
353 (@code{PF_INET6} is the name of the corresponding namespace.)
355 @item AF_UNSPEC
356 @standards{BSD, sys/socket.h}
357 This designates no particular address format.  It is used only in rare
358 cases, such as to clear out the default destination address of a
359 ``connected'' datagram socket.  @xref{Sending Datagrams}.
361 The corresponding namespace designator symbol @code{PF_UNSPEC} exists
362 for completeness, but there is no reason to use it in a program.
363 @end vtable
365 @file{sys/socket.h} defines symbols starting with @samp{AF_} for many
366 different kinds of networks, most or all of which are not actually
367 implemented.  We will document those that really work as we receive
368 information about how to use them.
370 @node Setting Address
371 @subsection Setting the Address of a Socket
373 @pindex sys/socket.h
374 Use the @code{bind} function to assign an address to a socket.  The
375 prototype for @code{bind} is in the header file @file{sys/socket.h}.
376 For examples of use, see @ref{Local Socket Example}, or see @ref{Inet Example}.
378 @deftypefun int bind (int @var{socket}, struct sockaddr *@var{addr}, socklen_t @var{length})
379 @standards{BSD, sys/socket.h}
380 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
381 @c Direct syscall, except on Hurd.
382 The @code{bind} function assigns an address to the socket
383 @var{socket}.  The @var{addr} and @var{length} arguments specify the
384 address; the detailed format of the address depends on the namespace.
385 The first part of the address is always the format designator, which
386 specifies a namespace, and says that the address is in the format of
387 that namespace.
389 The return value is @code{0} on success and @code{-1} on failure.  The
390 following @code{errno} error conditions are defined for this function:
392 @table @code
393 @item EBADF
394 The @var{socket} argument is not a valid file descriptor.
396 @item ENOTSOCK
397 The descriptor @var{socket} is not a socket.
399 @item EADDRNOTAVAIL
400 The specified address is not available on this machine.
402 @item EADDRINUSE
403 Some other socket is already using the specified address.
405 @item EINVAL
406 The socket @var{socket} already has an address.
408 @item EACCES
409 You do not have permission to access the requested address.  (In the
410 Internet domain, only the super-user is allowed to specify a port number
411 in the range 0 through @code{IPPORT_RESERVED} minus one; see
412 @ref{Ports}.)
413 @end table
415 Additional conditions may be possible depending on the particular namespace
416 of the socket.
417 @end deftypefun
419 @node Reading Address
420 @subsection Reading the Address of a Socket
422 @pindex sys/socket.h
423 Use the function @code{getsockname} to examine the address of an
424 Internet socket.  The prototype for this function is in the header file
425 @file{sys/socket.h}.
427 @deftypefun int getsockname (int @var{socket}, struct sockaddr *@var{addr}, socklen_t *@var{length-ptr})
428 @standards{BSD, sys/socket.h}
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.
432 The @code{getsockname} function returns information about the
433 address of the socket @var{socket} in the locations specified by the
434 @var{addr} and @var{length-ptr} arguments.  Note that the
435 @var{length-ptr} is a pointer; you should initialize it to be the
436 allocation size of @var{addr}, and on return it contains the actual
437 size of the address data.
439 The format of the address data depends on the socket namespace.  The
440 length of the information is usually fixed for a given namespace, so
441 normally you can know exactly how much space is needed and can provide
442 that much.  The usual practice is to allocate a place for the value
443 using the proper data type for the socket's namespace, then cast its
444 address to @code{struct sockaddr *} to pass it to @code{getsockname}.
446 The return value is @code{0} on success and @code{-1} on error.  The
447 following @code{errno} error conditions are defined for this function:
449 @table @code
450 @item EBADF
451 The @var{socket} argument is not a valid file descriptor.
453 @item ENOTSOCK
454 The descriptor @var{socket} is not a socket.
456 @item ENOBUFS
457 There are not enough internal buffers available for the operation.
458 @end table
459 @end deftypefun
461 You can't read the address of a socket in the file namespace.  This is
462 consistent with the rest of the system; in general, there's no way to
463 find a file's name from a descriptor for that file.
465 @node Interface Naming
466 @section Interface Naming
468 Each network interface has a name.  This usually consists of a few
469 letters that relate to the type of interface, which may be followed by a
470 number if there is more than one interface of that type.  Examples
471 might be @code{lo} (the loopback interface) and @code{eth0} (the first
472 Ethernet interface).
474 Although such names are convenient for humans, it would be clumsy to
475 have to use them whenever a program needs to refer to an interface.  In
476 such situations an interface is referred to by its @dfn{index}, which is
477 an arbitrarily-assigned small positive integer.
479 The following functions, constants and data types are declared in the
480 header file @file{net/if.h}.
482 @deftypevr Constant size_t IFNAMSIZ
483 @standards{???, net/if.h}
484 This constant defines the maximum buffer size needed to hold an
485 interface name, including its terminating zero byte.
486 @end deftypevr
488 @deftypefun {unsigned int} if_nametoindex (const char *@var{ifname})
489 @standards{IPv6 basic API, net/if.h}
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
498 This function yields the interface index corresponding to a particular
499 name.  If no interface exists with the name given, it returns 0.
500 @end deftypefun
502 @deftypefun {char *} if_indextoname (unsigned int @var{ifindex}, char *@var{ifname})
503 @standards{IPv6 basic API, net/if.h}
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.
507 This function maps an interface index to its corresponding name.  The
508 returned name is placed in the buffer pointed to by @code{ifname}, which
509 must be at least @code{IFNAMSIZ} bytes in length.  If the index was
510 invalid, the function's return value is a null pointer, otherwise it is
511 @code{ifname}.
512 @end deftypefun
514 @deftp {Data Type} {struct if_nameindex}
515 @standards{IPv6 basic API, net/if.h}
516 This data type is used to hold the information about a single
517 interface.  It has the following members:
519 @table @code
520 @item unsigned int if_index;
521 This is the interface index.
523 @item char *if_name
524 This is the null-terminated index name.
526 @end table
527 @end deftp
529 @deftypefun {struct if_nameindex *} if_nameindex (void)
530 @standards{IPv6 basic API, net/if.h}
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
564 This function returns an array of @code{if_nameindex} structures, one
565 for every interface that is present.  The end of the list is indicated
566 by a structure with an interface of 0 and a null name pointer.  If an
567 error occurs, this function returns a null pointer.
569 The returned structure must be freed with @code{if_freenameindex} after
570 use.
571 @end deftypefun
573 @deftypefun void if_freenameindex (struct if_nameindex *@var{ptr})
574 @standards{IPv6 basic API, net/if.h}
575 @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
576 @c if_freenameindex @ascuheap @acsmem
577 @c  free dup @ascuheap @acsmem
578 This function frees the structure returned by an earlier call to
579 @code{if_nameindex}.
580 @end deftypefun
582 @node Local Namespace
583 @section The Local Namespace
584 @cindex local namespace, for sockets
586 This section describes the details of the local namespace, whose
587 symbolic name (required when you create a socket) is @code{PF_LOCAL}.
588 The local namespace is also known as ``Unix domain sockets''.  Another
589 name is file namespace since socket addresses are normally implemented
590 as file names.
592 @menu
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.
596 @end menu
598 @node Local Namespace Concepts
599 @subsection Local Namespace Concepts
601 In the local namespace socket addresses are file names.  You can specify
602 any file name you want as the address of the socket, but you must have
603 write 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.
606 It's common to put these files in the @file{/tmp} directory.
608 One peculiarity of the local namespace is that the name is only used
609 when opening the connection; once open the address is not meaningful and
610 may not exist.
612 Another peculiarity is that you cannot connect to such a socket from
613 another machine--not even if the other machine shares the file system
614 which contains the name of the socket.  You can see the socket in a
615 directory listing, but connecting to it never succeeds.  Some programs
616 take advantage of this, such as by asking the client to send its own
617 process ID, and using the process IDs to distinguish between clients.
618 However, we recommend you not use this method in protocols you design,
619 as we might someday permit connections from other machines that mount
620 the same file systems.  Instead, send each new client an identifying
621 number if you want it to have one.
623 After you close a socket in the local namespace, you should delete the
624 file name from the file system.  Use @code{unlink} or @code{remove} to
625 do this; see @ref{Deleting Files}.
627 The local namespace supports just one protocol for any communication
628 style; it is protocol number @code{0}.
630 @node Local Namespace Details
631 @subsection Details of Local Namespace
633 @pindex sys/socket.h
634 To create a socket in the local namespace, use the constant
635 @code{PF_LOCAL} as the @var{namespace} argument to @code{socket} or
636 @code{socketpair}.  This constant is defined in @file{sys/socket.h}.
638 @deftypevr Macro int PF_LOCAL
639 @standards{POSIX, sys/socket.h}
640 This designates the local namespace, in which socket addresses are local
641 names, and its associated family of protocols.  @code{PF_LOCAL} is the
642 macro used by POSIX.1g.
643 @end deftypevr
645 @deftypevr Macro int PF_UNIX
646 @standards{BSD, sys/socket.h}
647 This is a synonym for @code{PF_LOCAL}, for compatibility's sake.
648 @end deftypevr
650 @deftypevr Macro int PF_FILE
651 @standards{GNU, sys/socket.h}
652 This is a synonym for @code{PF_LOCAL}, for compatibility's sake.
653 @end deftypevr
655 The structure for specifying socket names in the local namespace is
656 defined in the header file @file{sys/un.h}:
657 @pindex sys/un.h
659 @deftp {Data Type} {struct sockaddr_un}
660 @standards{BSD, sys/un.h}
661 This structure is used to specify local namespace socket addresses.  It has
662 the following members:
664 @table @code
665 @item short int sun_family
666 This identifies the address family or format of the socket address.
667 You should store the value @code{AF_LOCAL} to designate the local
668 namespace.  @xref{Socket Addresses}.
670 @item char sun_path[108]
671 This is the file name to use.
673 @strong{Incomplete:}  Why is 108 a magic number?  RMS suggests making
674 this a zero-length array and tweaking the following example to use
675 @code{alloca} to allocate an appropriate amount of storage based on
676 the length of the filename.
677 @end table
678 @end deftp
680 You should compute the @var{length} parameter for a socket address in
681 the local namespace as the sum of the size of the @code{sun_family}
682 component and the string length (@emph{not} the allocation size!) of
683 the file name string.  This can be done using the macro @code{SUN_LEN}:
685 @deftypefn {Macro} int SUN_LEN (@emph{struct sockaddr_un *} @var{ptr})
686 @standards{BSD, sys/un.h}
687 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
688 This macro computes the length of the socket address in the local namespace.
689 @end deftypefn
691 @node Local Socket Example
692 @subsection Example of Local-Namespace Sockets
694 Here is an example showing how to create and name a socket in the local
695 namespace.
697 @smallexample
698 @include mkfsock.c.texi
699 @end smallexample
701 @node Internet Namespace
702 @section The Internet Namespace
703 @cindex Internet namespace, for sockets
705 This section describes the details of the protocols and socket naming
706 conventions used in the Internet namespace.
708 Originally the Internet namespace used only IP version 4 (IPv4).  With
709 the growing number of hosts on the Internet, a new protocol with a
710 larger address space was necessary: IP version 6 (IPv6).  IPv6
711 introduces 128-bit addresses (IPv4 has 32-bit addresses) and other
712 features, and will eventually replace IPv4.
714 To create a socket in the IPv4 Internet namespace, use the symbolic name
715 @code{PF_INET} of this namespace as the @var{namespace} argument to
716 @code{socket} or @code{socketpair}.  For IPv6 addresses you need the
717 macro @code{PF_INET6}.  These macros are defined in @file{sys/socket.h}.
718 @pindex sys/socket.h
720 @deftypevr Macro int PF_INET
721 @standards{BSD, sys/socket.h}
722 This designates the IPv4 Internet namespace and associated family of
723 protocols.
724 @end deftypevr
726 @deftypevr Macro int PF_INET6
727 @standards{X/Open, sys/socket.h}
728 This designates the IPv6 Internet namespace and associated family of
729 protocols.
730 @end deftypevr
732 A socket address for the Internet namespace includes the following components:
734 @itemize @bullet
735 @item
736 The address of the machine you want to connect to.  Internet addresses
737 can be specified in several ways; these are discussed in @ref{Internet
738 Address Formats}, @ref{Host Addresses} and @ref{Host Names}.
740 @item
741 A port number for that machine.  @xref{Ports}.
742 @end itemize
744 You must ensure that the address and port number are represented in a
745 canonical format called @dfn{network byte order}.  @xref{Byte Order},
746 for information about this.
748 @menu
749 * Internet Address Formats::    How socket addresses are specified in the
750                                  Internet namespace.
751 * Host Addresses::              All about host addresses of Internet host.
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
756                                  canonicalize host address and port number.
757 * Protocols Database::          Referring to protocols by name.
758 * Inet Example::                Putting it all together.
759 @end menu
761 @node Internet Address Formats
762 @subsection Internet Socket Address Formats
764 In the Internet namespace, for both IPv4 (@code{AF_INET}) and IPv6
765 (@code{AF_INET6}), a socket address consists of a host address
766 and a port on that host.  In addition, the protocol you choose serves
767 effectively as a part of the address because local port numbers are
768 meaningful only within a particular protocol.
770 The data types for representing socket addresses in the Internet namespace
771 are defined in the header file @file{netinet/in.h}.
772 @pindex netinet/in.h
774 @deftp {Data Type} {struct sockaddr_in}
775 @standards{BSD, netinet/in.h}
776 This is the data type used to represent socket addresses in the
777 Internet namespace.  It has the following members:
779 @table @code
780 @item sa_family_t sin_family
781 This identifies the address family or format of the socket address.
782 You should store the value @code{AF_INET} in this member.
783 @xref{Socket Addresses}.
785 @item struct in_addr sin_addr
786 This is the Internet address of the host machine.  @xref{Host
787 Addresses}, and @ref{Host Names}, for how to get a value to store
788 here.
790 @item unsigned short int sin_port
791 This is the port number.  @xref{Ports}.
792 @end table
793 @end deftp
795 When you call @code{bind} or @code{getsockname}, you should specify
796 @code{sizeof (struct sockaddr_in)} as the @var{length} parameter if
797 you are using an IPv4 Internet namespace socket address.
799 @deftp {Data Type} {struct sockaddr_in6}
800 This is the data type used to represent socket addresses in the IPv6
801 namespace.  It has the following members:
803 @table @code
804 @item sa_family_t sin6_family
805 This identifies the address family or format of the socket address.
806 You should store the value of @code{AF_INET6} in this member.
807 @xref{Socket Addresses}.
809 @item struct in6_addr sin6_addr
810 This is the IPv6 address of the host machine.  @xref{Host
811 Addresses}, and @ref{Host Names}, for how to get a value to store
812 here.
814 @item uint32_t sin6_flowinfo
815 This is a currently unimplemented field.
817 @item uint16_t sin6_port
818 This is the port number.  @xref{Ports}.
820 @end table
821 @end deftp
823 @node Host Addresses
824 @subsection Host Addresses
826 Each computer on the Internet has one or more @dfn{Internet addresses},
827 numbers which identify that computer among all those on the Internet.
828 Users typically write IPv4 numeric host addresses as sequences of four
829 numbers, separated by periods, as in @samp{128.52.46.32}, and IPv6
830 numeric host addresses as sequences of up to eight numbers separated by
831 colons, as in @samp{5f03:1200:836f:c100::1}.
833 Each computer also has one or more @dfn{host names}, which are strings
834 of words separated by periods, as in @samp{www.gnu.org}.
836 Programs that let the user specify a host typically accept both numeric
837 addresses and host names.  To open a connection a program needs a
838 numeric address, and so must convert a host name to the numeric address
839 it stands for.
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
848 @node Abstract Host Addresses
849 @subsubsection Internet Host Addresses
850 @cindex host address, Internet
851 @cindex Internet host address
853 @ifinfo
854 Each computer on the Internet has one or more Internet addresses,
855 numbers which identify that computer among all those on the Internet.
856 @end ifinfo
858 @cindex network number
859 @cindex local network address number
860 An IPv4 Internet host address is a number containing four bytes of data.
861 Historically these are divided into two parts, a @dfn{network number} and a
862 @dfn{local network address number} within that network.  In the
863 mid-1990s classless addresses were introduced which changed this
864 behavior.  Since some functions implicitly expect the old definitions,
865 we first describe the class-based network and will then describe
866 classless addresses.  IPv6 uses only classless addresses and therefore
867 the following paragraphs don't apply.
869 The class-based IPv4 network number consists of the first one, two or
870 three bytes; the rest of the bytes are the local address.
872 IPv4 network numbers are registered with the Network Information Center
873 (NIC), and are divided into three classes---A, B and C.  The local
874 network address numbers of individual machines are registered with the
875 administrator of the particular network.
877 Class A networks have single-byte numbers in the range 0 to 127.  There
878 are only a small number of Class A networks, but they can each support a
879 very large number of hosts.  Medium-sized Class B networks have two-byte
880 network numbers, with the first byte in the range 128 to 191.  Class C
881 networks are the smallest; they have three-byte network numbers, with
882 the first byte in the range 192-255.  Thus, the first 1, 2, or 3 bytes
883 of an Internet address specify a network.  The remaining bytes of the
884 Internet address specify the address within that network.
886 The Class A network 0 is reserved for broadcast to all networks.  In
887 addition, the host number 0 within each network is reserved for broadcast
888 to all hosts in that network.  These uses are obsolete now but for
889 compatibility reasons you shouldn't use network 0 and host number 0.
891 The Class A network 127 is reserved for loopback; you can always use
892 the Internet address @samp{127.0.0.1} to refer to the host machine.
894 Since a single machine can be a member of multiple networks, it can
895 have multiple Internet host addresses.  However, there is never
896 supposed to be more than one machine with the same host address.
898 @c !!! this section could document the IN_CLASS* macros in <netinet/in.h>.
899 @c No, it shouldn't since they're obsolete.
901 @cindex standard dot notation, for Internet addresses
902 @cindex dot notation, for Internet addresses
903 There are four forms of the @dfn{standard numbers-and-dots notation}
904 for Internet addresses:
906 @table @code
907 @item @var{a}.@var{b}.@var{c}.@var{d}
908 This specifies all four bytes of the address individually and is the
909 commonly used representation.
911 @item @var{a}.@var{b}.@var{c}
912 The last part of the address, @var{c}, is interpreted as a 2-byte quantity.
913 This is useful for specifying host addresses in a Class B network with
914 network address number @code{@var{a}.@var{b}}.
916 @item @var{a}.@var{b}
917 The last part of the address, @var{b}, is interpreted as a 3-byte quantity.
918 This is useful for specifying host addresses in a Class A network with
919 network address number @var{a}.
921 @item @var{a}
922 If only one part is given, this corresponds directly to the host address
923 number.
924 @end table
926 Within each part of the address, the usual C conventions for specifying
927 the radix apply.  In other words, a leading @samp{0x} or @samp{0X} implies
928 hexadecimal radix; a leading @samp{0} implies octal; and otherwise decimal
929 radix is assumed.
931 @subsubheading Classless Addresses
933 IPv4 addresses (and IPv6 addresses also) are now considered classless;
934 the distinction between classes A, B and C can be ignored.  Instead an
935 IPv4 host address consists of a 32-bit address and a 32-bit mask.  The
936 mask contains set bits for the network part and cleared bits for the
937 host part.  The network part is contiguous from the left, with the
938 remaining bits representing the host.  As a consequence, the netmask can
939 simply be specified as the number of set bits.  Classes A, B and C are
940 just special cases of this general rule.  For example, class A addresses
941 have a netmask of @samp{255.0.0.0} or a prefix length of 8.
943 Classless IPv4 network addresses are written in numbers-and-dots
944 notation with the prefix length appended and a slash as separator.  For
945 example the class A network 10 is written as @samp{10.0.0.0/8}.
947 @subsubheading IPv6 Addresses
949 IPv6 addresses contain 128 bits (IPv4 has 32 bits) of data.  A host
950 address is usually written as eight 16-bit hexadecimal numbers that are
951 separated by colons.  Two colons are used to abbreviate strings of
952 consecutive zeros.  For example, the IPv6 loopback address
953 @samp{0:0:0:0:0:0:0:1} can just be written as @samp{::1}.
955 @node Host Address Data Type
956 @subsubsection Host Address Data Type
958 IPv4 Internet host addresses are represented in some contexts as integers
959 (type @code{uint32_t}).  In other contexts, the integer is
960 packaged inside a structure of type @code{struct in_addr}.  It would
961 be better if the usage were made consistent, but it is not hard to extract
962 the integer from the structure or put the integer into a structure.
964 You will find older code that uses @code{unsigned long int} for
965 IPv4 Internet host addresses instead of @code{uint32_t} or @code{struct
966 in_addr}.  Historically @code{unsigned long int} was a 32-bit number but
967 with 64-bit machines this has changed.  Using @code{unsigned long int}
968 might break the code if it is used on machines where this type doesn't
969 have 32 bits.  @code{uint32_t} is specified by Unix98 and guaranteed to have
970 32 bits.
972 IPv6 Internet host addresses have 128 bits and are packaged inside a
973 structure of type @code{struct in6_addr}.
975 The following basic definitions for Internet addresses are declared in
976 the header file @file{netinet/in.h}:
977 @pindex netinet/in.h
979 @deftp {Data Type} {struct in_addr}
980 @standards{BSD, netinet/in.h}
981 This data type is used in certain contexts to contain an IPv4 Internet
982 host address.  It has just one field, named @code{s_addr}, which records
983 the host address number as an @code{uint32_t}.
984 @end deftp
986 @deftypevr Macro {uint32_t} INADDR_LOOPBACK
987 @standards{BSD, netinet/in.h}
988 You can use this constant to stand for ``the address of this machine,''
989 instead of finding its actual address.  It is the IPv4 Internet address
990 @samp{127.0.0.1}, which is usually called @samp{localhost}.  This
991 special constant saves you the trouble of looking up the address of your
992 own machine.  Also, the system usually implements @code{INADDR_LOOPBACK}
993 specially, avoiding any network traffic for the case of one machine
994 talking to itself.
995 @end deftypevr
997 @deftypevr Macro {uint32_t} INADDR_ANY
998 @standards{BSD, netinet/in.h}
999 You can use this constant to stand for ``any incoming address'' when
1000 binding to an address.  @xref{Setting Address}.  This is the usual
1001 address to give in the @code{sin_addr} member of @w{@code{struct
1002 sockaddr_in}} when you want to accept Internet connections.
1003 @end deftypevr
1005 @deftypevr Macro {uint32_t} INADDR_BROADCAST
1006 @standards{BSD, netinet/in.h}
1007 This constant is the address you use to send a broadcast message.
1008 @c !!! broadcast needs further documented
1009 @end deftypevr
1011 @deftypevr Macro {uint32_t} INADDR_NONE
1012 @standards{BSD, netinet/in.h}
1013 This constant is returned by some functions to indicate an error.
1014 @end deftypevr
1016 @deftp {Data Type} {struct in6_addr}
1017 @standards{IPv6 basic API, netinet/in.h}
1018 This data type is used to store an IPv6 address.  It stores 128 bits of
1019 data, which can be accessed (via a union) in a variety of ways.
1020 @end deftp
1022 @deftypevr Constant {struct in6_addr} in6addr_loopback
1023 @standards{IPv6 basic API, netinet/in.h}
1024 This constant is the IPv6 address @samp{::1}, the loopback address.  See
1025 above for a description of what this means.  The macro
1026 @code{IN6ADDR_LOOPBACK_INIT} is provided to allow you to initialize your
1027 own variables to this value.
1028 @end deftypevr
1030 @deftypevr Constant {struct in6_addr} in6addr_any
1031 @standards{IPv6 basic API, netinet/in.h}
1032 This constant is the IPv6 address @samp{::}, the unspecified address.  See
1033 above for a description of what this means.  The macro
1034 @code{IN6ADDR_ANY_INIT} is provided to allow you to initialize your
1035 own variables to this value.
1036 @end deftypevr
1038 @node Host Address Functions
1039 @subsubsection Host Address Functions
1041 @pindex arpa/inet.h
1042 @noindent
1043 These additional functions for manipulating Internet addresses are
1044 declared in the header file @file{arpa/inet.h}.  They represent Internet
1045 addresses in network byte order, and network numbers and
1046 local-address-within-network numbers in host byte order.  @xref{Byte
1047 Order}, for an explanation of network and host byte order.
1049 @deftypefun int inet_aton (const char *@var{name}, struct in_addr *@var{addr})
1050 @standards{BSD, arpa/inet.h}
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
1058 This function converts the IPv4 Internet host address @var{name}
1059 from the standard numbers-and-dots notation into binary data and stores
1060 it 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
1064 @deftypefun {uint32_t} inet_addr (const char *@var{name})
1065 @standards{BSD, arpa/inet.h}
1066 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
1067 @c inet_addr @mtslocale
1068 @c  inet_aton dup @mtslocale
1069 This function converts the IPv4 Internet host address @var{name} from the
1070 standard numbers-and-dots notation into binary data.  If the input is
1071 not valid, @code{inet_addr} returns @code{INADDR_NONE}.  This is an
1072 obsolete interface to @code{inet_aton}, described immediately above.  It
1073 is obsolete because @code{INADDR_NONE} is a valid address
1074 (255.255.255.255), and @code{inet_aton} provides a cleaner way to
1075 indicate error return.
1076 @end deftypefun
1078 @deftypefun {uint32_t} inet_network (const char *@var{name})
1079 @standards{BSD, arpa/inet.h}
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
1086 This function extracts the network number from the address @var{name},
1087 given in the standard numbers-and-dots notation.  The returned address is
1088 in host order.  If the input is not valid, @code{inet_network} returns
1089 @code{-1}.
1091 The function works only with traditional IPv4 class A, B and C network
1092 types.  It doesn't work with classless addresses and shouldn't be used
1093 anymore.
1094 @end deftypefun
1096 @deftypefun {char *} inet_ntoa (struct in_addr @var{addr})
1097 @standards{BSD, arpa/inet.h}
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]
1102 This function converts the IPv4 Internet host address @var{addr} to a
1103 string in the standard numbers-and-dots notation.  The return value is
1104 a pointer into a statically-allocated buffer.  Subsequent calls will
1105 overwrite the same buffer, so you should copy the string if you need
1106 to save it.
1108 In multi-threaded programs each thread has its own statically-allocated
1109 buffer.  But still subsequent calls of @code{inet_ntoa} in the same
1110 thread will overwrite the result of the last call.
1112 Instead of @code{inet_ntoa} the newer function @code{inet_ntop} which is
1113 described below should be used since it handles both IPv4 and IPv6
1114 addresses.
1115 @end deftypefun
1117 @deftypefun {struct in_addr} inet_makeaddr (uint32_t @var{net}, uint32_t @var{local})
1118 @standards{BSD, arpa/inet.h}
1119 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1120 @c inet_makeaddr ok
1121 @c  htonl dup ok
1122 This function makes an IPv4 Internet host address by combining the network
1123 number @var{net} with the local-address-within-network number
1124 @var{local}.
1125 @end deftypefun
1127 @deftypefun uint32_t inet_lnaof (struct in_addr @var{addr})
1128 @standards{BSD, arpa/inet.h}
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
1134 This function returns the local-address-within-network part of the
1135 Internet host address @var{addr}.
1137 The function works only with traditional IPv4 class A, B and C network
1138 types.  It doesn't work with classless addresses and shouldn't be used
1139 anymore.
1140 @end deftypefun
1142 @deftypefun uint32_t inet_netof (struct in_addr @var{addr})
1143 @standards{BSD, arpa/inet.h}
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
1149 This function returns the network number part of the Internet host
1150 address @var{addr}.
1152 The function works only with traditional IPv4 class A, B and C network
1153 types.  It doesn't work with classless addresses and shouldn't be used
1154 anymore.
1155 @end deftypefun
1157 @deftypefun int inet_pton (int @var{af}, const char *@var{cp}, void *@var{buf})
1158 @standards{IPv6 basic API, arpa/inet.h}
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
1169 This function converts an Internet address (either IPv4 or IPv6) from
1170 presentation (textual) to network (binary) format.  @var{af} should be
1171 either @code{AF_INET} or @code{AF_INET6}, as appropriate for the type of
1172 address 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
1174 responsibility to make sure the buffer is large enough.
1175 @end deftypefun
1177 @deftypefun {const char *} inet_ntop (int @var{af}, const void *@var{cp}, char *@var{buf}, socklen_t @var{len})
1178 @standards{IPv6 basic API, arpa/inet.h}
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
1189 This function converts an Internet address (either IPv4 or IPv6) from
1190 network (binary) to presentation (textual) form.  @var{af} should be
1191 either @code{AF_INET} or @code{AF_INET6}, as appropriate.  @var{cp} is a
1192 pointer to the address to be converted.  @var{buf} should be a pointer
1193 to a buffer to hold the result, and @var{len} is the length of this
1194 buffer.  The return value from the function will be this buffer address.
1195 @end deftypefun
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
1203 Besides the standard numbers-and-dots notation for Internet addresses,
1204 you can also refer to a host by a symbolic name.  The advantage of a
1205 symbolic name is that it is usually easier to remember.  For example,
1206 the 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}
1208 domain can refer to it simply as @samp{alpha}.
1210 @pindex /etc/hosts
1211 @pindex netdb.h
1212 Internally, the system uses a database to keep track of the mapping
1213 between host names and host numbers.  This database is usually either
1214 the file @file{/etc/hosts} or an equivalent provided by a name server.
1215 The functions and other symbols for accessing this database are declared
1216 in @file{netdb.h}.  They are BSD features, defined unconditionally if
1217 you include @file{netdb.h}.
1219 @deftp {Data Type} {struct hostent}
1220 @standards{BSD, netdb.h}
1221 This data type is used to represent an entry in the hosts database.  It
1222 has the following members:
1224 @table @code
1225 @item char *h_name
1226 This is the ``official'' name of the host.
1228 @item char **h_aliases
1229 These are alternative names for the host, represented as a null-terminated
1230 vector of strings.
1232 @item int h_addrtype
1233 This 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
1235 hosts.  In principle other kinds of addresses could be represented in
1236 the database as well as Internet addresses; if this were done, you
1237 might find a value in this field other than @code{AF_INET} or
1238 @code{AF_INET6}.  @xref{Socket Addresses}.
1240 @item int h_length
1241 This is the length, in bytes, of each address.
1243 @item char **h_addr_list
1244 This is the vector of addresses for the host.  (Recall that the host
1245 might be connected to multiple networks and have different addresses on
1246 each one.)  The vector is terminated by a null pointer.
1248 @item char *h_addr
1249 This is a synonym for @code{h_addr_list[0]}; in other words, it is the
1250 first host address.
1251 @end table
1252 @end deftp
1254 As far as the host database is concerned, each address is just a block
1255 of memory @code{h_length} bytes long.  But in other contexts there is an
1256 implicit assumption that you can convert IPv4 addresses to a
1257 @code{struct in_addr} or an @code{uint32_t}.  Host addresses in
1258 a @code{struct hostent} structure are always given in network byte
1259 order; see @ref{Byte Order}.
1261 You can use @code{gethostbyname}, @code{gethostbyname2} or
1262 @code{gethostbyaddr} to search the hosts database for information about
1263 a particular host.  The information is returned in a
1264 statically-allocated structure; you must copy the information if you
1265 need to save it across calls.  You can also use @code{getaddrinfo} and
1266 @code{getnameinfo} to obtain this information.
1268 @deftypefun {struct hostent *} gethostbyname (const char *@var{name})
1269 @standards{BSD, netdb.h}
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
1335 The @code{gethostbyname} function returns information about the host
1336 named @var{name}.  If the lookup fails, it returns a null pointer.
1337 @end deftypefun
1339 @deftypefun {struct hostent *} gethostbyname2 (const char *@var{name}, int @var{af})
1340 @standards{IPv6 Basic API, netdb.h}
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
1351 The @code{gethostbyname2} function is like @code{gethostbyname}, but
1352 allows the caller to specify the desired address family (e.g.@:
1353 @code{AF_INET} or @code{AF_INET6}) of the result.
1354 @end deftypefun
1356 @deftypefun {struct hostent *} gethostbyaddr (const void *@var{addr}, socklen_t @var{length}, int @var{format})
1357 @standards{BSD, netdb.h}
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
1367 The @code{gethostbyaddr} function returns information about the host
1368 with Internet address @var{addr}.  The parameter @var{addr} is not
1369 really a pointer to char - it can be a pointer to an IPv4 or an IPv6
1370 address.  The @var{length} argument is the size (in bytes) of the address
1371 at @var{addr}.  @var{format} specifies the address format; for an IPv4
1372 Internet address, specify a value of @code{AF_INET}; for an IPv6
1373 Internet address, use @code{AF_INET6}.
1375 If the lookup fails, @code{gethostbyaddr} returns a null pointer.
1376 @end deftypefun
1378 @vindex h_errno
1379 If the name lookup by @code{gethostbyname} or @code{gethostbyaddr}
1380 fails, you can find out the reason by looking at the value of the
1381 variable @code{h_errno}.  (It would be cleaner design for these
1382 functions to set @code{errno}, but use of @code{h_errno} is compatible
1383 with other systems.)
1385 Here are the error codes that you may find in @code{h_errno}:
1387 @vtable @code
1388 @item HOST_NOT_FOUND
1389 @standards{BSD, netdb.h}
1390 No such host is known in the database.
1392 @item TRY_AGAIN
1393 @standards{BSD, netdb.h}
1394 This condition happens when the name server could not be contacted.  If
1395 you try again later, you may succeed then.
1397 @item NO_RECOVERY
1398 @standards{BSD, netdb.h}
1399 A non-recoverable error occurred.
1401 @item NO_ADDRESS
1402 @standards{BSD, netdb.h}
1403 The host database contains an entry for the name, but it doesn't have an
1404 associated Internet address.
1405 @end vtable
1407 The lookup functions above all have one thing in common: they are not
1408 reentrant and therefore unusable in multi-threaded applications.
1409 Therefore provides @theglibc{} a new set of functions which can be
1410 used in this context.
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})
1413 @standards{GNU, netdb.h}
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
1484 The @code{gethostbyname_r} function returns information about the host
1485 named @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
1487 the function may need extra buffer space and the caller must pass a
1488 pointer and the size of the buffer in the @var{buf} and @var{buflen}
1489 parameters.
1491 A pointer to the buffer, in which the result is stored, is available in
1492 @code{*@var{result}} after the function call successfully returned.  The
1493 buffer passed as the @var{buf} parameter can be freed only once the caller
1494 has finished with the result hostent struct, or has copied it including all
1495 the other memory that it points to.  If an error occurs or if no entry is
1496 found, the pointer @code{*@var{result}} is a null pointer.  Success is
1497 signalled by a zero return value.  If the function failed the return value
1498 is an error number.  In addition to the errors defined for
1499 @code{gethostbyname} it can also be @code{ERANGE}.  In this case the call
1500 should be repeated with a larger buffer.  Additional error information is
1501 not stored in the global variable @code{h_errno} but instead in the object
1502 pointed to by @var{h_errnop}.
1504 Here's a small example:
1505 @smallexample
1506 struct hostent *
1507 gethostname (char *host)
1509   struct hostent *hostbuf, *hp;
1510   size_t hstbuflen;
1511   char *tmphstbuf;
1512   int res;
1513   int herr;
1515   hostbuf = malloc (sizeof (struct hostent));
1516   hstbuflen = 1024;
1517   tmphstbuf = malloc (hstbuflen);
1519   while ((res = gethostbyname_r (host, hostbuf, tmphstbuf, hstbuflen,
1520                                  &hp, &herr)) == ERANGE)
1521     @{
1522       /* Enlarge the buffer.  */
1523       hstbuflen *= 2;
1524       tmphstbuf = realloc (tmphstbuf, hstbuflen);
1525     @}
1527   free (tmphstbuf);
1528   /*  Check for errors.  */
1529   if (res || hp == NULL)
1530     return NULL;
1531   return hp;
1533 @end smallexample
1534 @end deftypefun
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})
1537 @standards{GNU, netdb.h}
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
1549 The @code{gethostbyname2_r} function is like @code{gethostbyname_r}, but
1550 allows the caller to specify the desired address family (e.g.@:
1551 @code{AF_INET} or @code{AF_INET6}) for the result.
1552 @end deftypefun
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})
1555 @standards{GNU, netdb.h}
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
1571 The @code{gethostbyaddr_r} function returns information about the host
1572 with Internet address @var{addr}.  The parameter @var{addr} is not
1573 really a pointer to char - it can be a pointer to an IPv4 or an IPv6
1574 address.  The @var{length} argument is the size (in bytes) of the address
1575 at @var{addr}.  @var{format} specifies the address format; for an IPv4
1576 Internet address, specify a value of @code{AF_INET}; for an IPv6
1577 Internet address, use @code{AF_INET6}.
1579 Similar to the @code{gethostbyname_r} function, the caller must provide
1580 buffers for the result and memory used internally.  In case of success
1581 the function returns zero.  Otherwise the value is an error number where
1582 @code{ERANGE} has the special meaning that the caller-provided buffer is
1583 too small.
1584 @end deftypefun
1586 You can also scan the entire hosts database one entry at a time using
1587 @code{sethostent}, @code{gethostent} and @code{endhostent}.  Be careful
1588 when using these functions because they are not reentrant.
1590 @deftypefun void sethostent (int @var{stayopen})
1591 @standards{BSD, netdb.h}
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
1604 This function opens the hosts database to begin scanning it.  You can
1605 then call @code{gethostent} to read the entries.
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.
1609 If the @var{stayopen} argument is nonzero, this sets a flag so that
1610 subsequent calls to @code{gethostbyname} or @code{gethostbyaddr} will
1611 not close the database (as they usually would).  This makes for more
1612 efficiency if you call those functions several times, by avoiding
1613 reopening the database for each call.
1614 @end deftypefun
1616 @deftypefun {struct hostent *} gethostent (void)
1617 @standards{BSD, netdb.h}
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
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
1639 This function returns the next entry in the hosts database.  It
1640 returns a null pointer if there are no more entries.
1641 @end deftypefun
1643 @deftypefun void endhostent (void)
1644 @standards{BSD, netdb.h}
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
1654 This function closes the hosts database.
1655 @end deftypefun
1657 @node Ports
1658 @subsection Internet Ports
1659 @cindex port number
1661 A socket address in the Internet namespace consists of a machine's
1662 Internet address plus a @dfn{port number} which distinguishes the
1663 sockets on a given machine (for a given protocol).  Port numbers range
1664 from 0 to 65,535.
1666 Port numbers less than @code{IPPORT_RESERVED} are reserved for standard
1667 servers, such as @code{finger} and @code{telnet}.  There is a database
1668 that keeps track of these, and you can use the @code{getservbyname}
1669 function to map a service name onto a port number; see @ref{Services
1670 Database}.
1672 If you write a server that is not one of the standard ones defined in
1673 the database, you must choose a port number for it.  Use a number
1674 greater than @code{IPPORT_USERRESERVED}; such numbers are reserved for
1675 servers and won't ever be generated automatically by the system.
1676 Avoiding conflicts with servers being run by other users is up to you.
1678 When you use a socket without specifying its address, the system
1679 generates a port number for it.  This number is between
1680 @code{IPPORT_RESERVED} and @code{IPPORT_USERRESERVED}.
1682 On the Internet, it is actually legitimate to have two different
1683 sockets with the same port number, as long as they never both try to
1684 communicate with the same socket address (host address plus port
1685 number).  You shouldn't duplicate a port number except in special
1686 circumstances where a higher-level protocol requires it.  Normally,
1687 the system won't let you do it; @code{bind} normally insists on
1688 distinct port numbers.  To reuse a port number, you must set the
1689 socket option @code{SO_REUSEADDR}.  @xref{Socket-Level Options}.
1691 @pindex netinet/in.h
1692 These macros are defined in the header file @file{netinet/in.h}.
1694 @deftypevr Macro int IPPORT_RESERVED
1695 @standards{BSD, netinet/in.h}
1696 Port numbers less than @code{IPPORT_RESERVED} are reserved for
1697 superuser use.
1698 @end deftypevr
1700 @deftypevr Macro int IPPORT_USERRESERVED
1701 @standards{BSD, netinet/in.h}
1702 Port numbers greater than or equal to @code{IPPORT_USERRESERVED} are
1703 reserved for explicit use; they will never be allocated automatically.
1704 @end deftypevr
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
1712 @pindex /etc/services
1713 The database that keeps track of ``well-known'' services is usually
1714 either the file @file{/etc/services} or an equivalent from a name server.
1715 You can use these utilities, declared in @file{netdb.h}, to access
1716 the services database.
1717 @pindex netdb.h
1719 @deftp {Data Type} {struct servent}
1720 @standards{BSD, netdb.h}
1721 This data type holds information about entries from the services database.
1722 It has the following members:
1724 @table @code
1725 @item char *s_name
1726 This is the ``official'' name of the service.
1728 @item char **s_aliases
1729 These are alternate names for the service, represented as an array of
1730 strings.  A null pointer terminates the array.
1732 @item int s_port
1733 This is the port number for the service.  Port numbers are given in
1734 network byte order; see @ref{Byte Order}.
1736 @item char *s_proto
1737 This is the name of the protocol to use with this service.
1738 @xref{Protocols Database}.
1739 @end table
1740 @end deftp
1742 To get information about a particular service, use the
1743 @code{getservbyname} or @code{getservbyport} functions.  The information
1744 is returned in a statically-allocated structure; you must copy the
1745 information if you need to save it across calls.
1747 @deftypefun {struct servent *} getservbyname (const char *@var{name}, const char *@var{proto})
1748 @standards{BSD, netdb.h}
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
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
1777 The @code{getservbyname} function returns information about the
1778 service named @var{name} using protocol @var{proto}.  If it can't find
1779 such a service, it returns a null pointer.
1781 This function is useful for servers as well as for clients; servers
1782 use it to determine which port they should listen on (@pxref{Listening}).
1783 @end deftypefun
1785 @deftypefun {struct servent *} getservbyport (int @var{port}, const char *@var{proto})
1786 @standards{BSD, netdb.h}
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
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
1802 The @code{getservbyport} function returns information about the
1803 service at port @var{port} using protocol @var{proto}.  If it can't
1804 find such a service, it returns a null pointer.
1805 @end deftypefun
1807 @noindent
1808 You can also scan the services database using @code{setservent},
1809 @code{getservent} and @code{endservent}.  Be careful when using these
1810 functions because they are not reentrant.
1812 @deftypefun void setservent (int @var{stayopen})
1813 @standards{BSD, netdb.h}
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
1824 This function opens the services database to begin scanning it.
1826 If the @var{stayopen} argument is nonzero, this sets a flag so that
1827 subsequent calls to @code{getservbyname} or @code{getservbyport} will
1828 not close the database (as they usually would).  This makes for more
1829 efficiency if you call those functions several times, by avoiding
1830 reopening the database for each call.
1831 @end deftypefun
1833 @deftypefun {struct servent *} getservent (void)
1834 @standards{BSD, netdb.h}
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
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
1854 This function returns the next entry in the services database.  If
1855 there are no more entries, it returns a null pointer.
1856 @end deftypefun
1858 @deftypefun void endservent (void)
1859 @standards{BSD, netdb.h}
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
1868 This function closes the services database.
1869 @end deftypefun
1871 @node Byte Order
1872 @subsection Byte Order Conversion
1873 @cindex byte order conversion, for socket
1874 @cindex converting byte order
1876 @cindex big-endian
1877 @cindex little-endian
1878 Different kinds of computers use different conventions for the
1879 ordering of bytes within a word.  Some computers put the most
1880 significant byte within a word first (this is called ``big-endian''
1881 order), and others put it last (``little-endian'' order).
1883 @cindex network byte order
1884 So that machines with different byte order conventions can
1885 communicate, the Internet protocols specify a canonical byte order
1886 convention for data transmitted over the network.  This is known
1887 as @dfn{network byte order}.
1889 When establishing an Internet socket connection, you must make sure that
1890 the data in the @code{sin_port} and @code{sin_addr} members of the
1891 @code{sockaddr_in} structure are represented in network byte order.
1892 If you are encoding integer data in the messages sent through the
1893 socket, you should convert this to network byte order too.  If you don't
1894 do this, your program may fail when running on or talking to other kinds
1895 of machines.
1897 If you use @code{getservbyname} and @code{gethostbyname} or
1898 @code{inet_addr} to get the port number and host address, the values are
1899 already in network byte order, and you can copy them directly into
1900 the @code{sockaddr_in} structure.
1902 Otherwise, you have to convert the values explicitly.  Use @code{htons}
1903 and @code{ntohs} to convert values for the @code{sin_port} member.  Use
1904 @code{htonl} and @code{ntohl} to convert IPv4 addresses for the
1905 @code{sin_addr} member.  (Remember, @code{struct in_addr} is equivalent
1906 to @code{uint32_t}.)  These functions are declared in
1907 @file{netinet/in.h}.
1908 @pindex netinet/in.h
1910 @deftypefun {uint16_t} htons (uint16_t @var{hostshort})
1911 @standards{BSD, netinet/in.h}
1912 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1913 @c htons ok
1914 @c  bswap_16 ok
1915 @c   bswap_constant_16 ok
1917 This function converts the @code{uint16_t} integer @var{hostshort} from
1918 host byte order to network byte order.
1919 @end deftypefun
1921 @deftypefun {uint16_t} ntohs (uint16_t @var{netshort})
1922 @standards{BSD, netinet/in.h}
1923 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1924 @c Alias to htons.
1925 This function converts the @code{uint16_t} integer @var{netshort} from
1926 network byte order to host byte order.
1927 @end deftypefun
1929 @deftypefun {uint32_t} htonl (uint32_t @var{hostlong})
1930 @standards{BSD, netinet/in.h}
1931 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1932 @c htonl ok
1933 @c  bswap_32 dup ok
1934 This function converts the @code{uint32_t} integer @var{hostlong} from
1935 host byte order to network byte order.
1937 This is used for IPv4 Internet addresses.
1938 @end deftypefun
1940 @deftypefun {uint32_t} ntohl (uint32_t @var{netlong})
1941 @standards{BSD, netinet/in.h}
1942 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
1943 @c Alias to htonl.
1944 This function converts the @code{uint32_t} integer @var{netlong} from
1945 network byte order to host byte order.
1947 This is used for IPv4 Internet addresses.
1948 @end deftypefun
1950 @node Protocols Database
1951 @subsection Protocols Database
1952 @cindex protocols database
1954 The communications protocol used with a socket controls low-level
1955 details of how data are exchanged.  For example, the protocol implements
1956 things like checksums to detect errors in transmissions, and routing
1957 instructions for messages.  Normal user programs have little reason to
1958 mess with these details directly.
1960 @cindex TCP (Internet protocol)
1961 The default communications protocol for the Internet namespace depends on
1962 the communication style.  For stream communication, the default is TCP
1963 (``transmission control protocol'').  For datagram communication, the
1964 default is UDP (``user datagram protocol'').  For reliable datagram
1965 communication, the default is RDP (``reliable datagram protocol'').
1966 You should nearly always use the default.
1968 @pindex /etc/protocols
1969 Internet protocols are generally specified by a name instead of a
1970 number.  The network protocols that a host knows about are stored in a
1971 database.  This is usually either derived from the file
1972 @file{/etc/protocols}, or it may be an equivalent provided by a name
1973 server.  You look up the protocol number associated with a named
1974 protocol in the database using the @code{getprotobyname} function.
1976 Here are detailed descriptions of the utilities for accessing the
1977 protocols database.  These are declared in @file{netdb.h}.
1978 @pindex netdb.h
1980 @deftp {Data Type} {struct protoent}
1981 @standards{BSD, netdb.h}
1982 This data type is used to represent entries in the network protocols
1983 database.  It has the following members:
1985 @table @code
1986 @item char *p_name
1987 This is the official name of the protocol.
1989 @item char **p_aliases
1990 These are alternate names for the protocol, specified as an array of
1991 strings.  The last element of the array is a null pointer.
1993 @item int p_proto
1994 This 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
1999 You can use @code{getprotobyname} and @code{getprotobynumber} to search
2000 the protocols database for a specific protocol.  The information is
2001 returned in a statically-allocated structure; you must copy the
2002 information if you need to save it across calls.
2004 @deftypefun {struct protoent *} getprotobyname (const char *@var{name})
2005 @standards{BSD, netdb.h}
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
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
2020 The @code{getprotobyname} function returns information about the
2021 network protocol named @var{name}.  If there is no such protocol, it
2022 returns a null pointer.
2023 @end deftypefun
2025 @deftypefun {struct protoent *} getprotobynumber (int @var{protocol})
2026 @standards{BSD, netdb.h}
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
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
2041 The @code{getprotobynumber} function returns information about the
2042 network protocol with number @var{protocol}.  If there is no such
2043 protocol, it returns a null pointer.
2044 @end deftypefun
2046 You can also scan the whole protocols database one protocol at a time by
2047 using @code{setprotoent}, @code{getprotoent} and @code{endprotoent}.
2048 Be careful when using these functions because they are not reentrant.
2050 @deftypefun void setprotoent (int @var{stayopen})
2051 @standards{BSD, netdb.h}
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
2062 This function opens the protocols database to begin scanning it.
2064 If the @var{stayopen} argument is nonzero, this sets a flag so that
2065 subsequent calls to @code{getprotobyname} or @code{getprotobynumber} will
2066 not close the database (as they usually would).  This makes for more
2067 efficiency if you call those functions several times, by avoiding
2068 reopening the database for each call.
2069 @end deftypefun
2071 @deftypefun {struct protoent *} getprotoent (void)
2072 @standards{BSD, netdb.h}
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
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
2092 This function returns the next entry in the protocols database.  It
2093 returns a null pointer if there are no more entries.
2094 @end deftypefun
2096 @deftypefun void endprotoent (void)
2097 @standards{BSD, netdb.h}
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
2106 This function closes the protocols database.
2107 @end deftypefun
2109 @node Inet Example
2110 @subsection Internet Socket Example
2112 Here is an example showing how to create and name a socket in the
2113 Internet namespace.  The newly created socket exists on the machine that
2114 the program is running on.  Rather than finding and using the machine's
2115 Internet address, this example specifies @code{INADDR_ANY} as the host
2116 address; the system replaces that with the machine's actual address.
2118 @smallexample
2119 @include mkisock.c.texi
2120 @end smallexample
2122 Here is another example, showing how you can fill in a @code{sockaddr_in}
2123 structure, given a host name string and a port number:
2125 @smallexample
2126 @include isockad.c.texi
2127 @end smallexample
2129 @node Misc Namespaces
2130 @section Other Namespaces
2132 @vindex PF_NS
2133 @vindex PF_ISO
2134 @vindex PF_CCITT
2135 @vindex PF_IMPLINK
2136 @vindex PF_ROUTE
2137 Certain other namespaces and associated protocol families are supported
2138 but not documented yet because they are not often used.  @code{PF_NS}
2139 refers to the Xerox Network Software protocols.  @code{PF_ISO} stands
2140 for Open Systems Interconnect.  @code{PF_CCITT} refers to protocols from
2141 CCITT.  @file{socket.h} defines these symbols and others naming protocols
2142 not actually implemented.
2144 @code{PF_IMPLINK} is used for communicating between hosts and Internet
2145 Message Processors.  For information on this and @code{PF_ROUTE}, an
2146 occasionally-used local area routing protocol, see the GNU Hurd Manual
2147 (to appear in the future).
2149 @node Open/Close Sockets
2150 @section Opening and Closing Sockets
2152 This section describes the actual library functions for opening and
2153 closing sockets.  The same functions work for all namespaces and
2154 connection styles.
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
2162 @node Creating a Socket
2163 @subsection Creating a Socket
2164 @cindex creating a socket
2165 @cindex socket, creating
2166 @cindex opening a socket
2168 The primitive for creating a socket is the @code{socket} function,
2169 declared in @file{sys/socket.h}.
2170 @pindex sys/socket.h
2172 @deftypefun int socket (int @var{namespace}, int @var{style}, int @var{protocol})
2173 @standards{BSD, sys/socket.h}
2174 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
2175 This 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
2178 the namespace; it must be @code{PF_LOCAL} (@pxref{Local Namespace}) or
2179 @code{PF_INET} (@pxref{Internet Namespace}).  @var{protocol}
2180 designates the specific protocol (@pxref{Socket Concepts}); zero is
2181 usually right for @var{protocol}.
2183 The return value from @code{socket} is the file descriptor for the new
2184 socket, or @code{-1} in case of error.  The following @code{errno} error
2185 conditions are defined for this function:
2187 @table @code
2188 @item EPROTONOSUPPORT
2189 The @var{protocol} or @var{style} is not supported by the
2190 @var{namespace} specified.
2192 @item EMFILE
2193 The process already has too many file descriptors open.
2195 @item ENFILE
2196 The system already has too many file descriptors open.
2198 @item EACCES
2199 The process does not have the privilege to create a socket of the specified
2200 @var{style} or @var{protocol}.
2202 @item ENOBUFS
2203 The system ran out of internal buffer space.
2204 @end table
2206 The file descriptor returned by the @code{socket} function supports both
2207 read and write operations.  However, like pipes, sockets do not support file
2208 positioning operations.
2209 @end deftypefun
2211 For examples of how to call the @code{socket} function,
2212 see @ref{Local Socket Example}, or @ref{Inet Example}.
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
2222 When you have finished using a socket, you can simply close its
2223 file descriptor with @code{close}; see @ref{Opening and Closing Files}.
2224 If there is still data waiting to be transmitted over the connection,
2225 normally @code{close} tries to complete this transmission.  You
2226 can control this behavior using the @code{SO_LINGER} socket option to
2227 specify a timeout period; see @ref{Socket Options}.
2229 @pindex sys/socket.h
2230 You can also shut down only reception or transmission on a
2231 connection by calling @code{shutdown}, which is declared in
2232 @file{sys/socket.h}.
2234 @deftypefun int shutdown (int @var{socket}, int @var{how})
2235 @standards{BSD, sys/socket.h}
2236 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2237 The @code{shutdown} function shuts down the connection of socket
2238 @var{socket}.  The argument @var{how} specifies what action to
2239 perform:
2241 @table @code
2242 @item 0
2243 Stop receiving data for this socket.  If further data arrives,
2244 reject it.
2246 @item 1
2247 Stop trying to transmit data from this socket.  Discard any data
2248 waiting to be sent.  Stop looking for acknowledgement of data already
2249 sent; don't retransmit it if it is lost.
2251 @item 2
2252 Stop both reception and transmission.
2253 @end table
2255 The return value is @code{0} on success and @code{-1} on failure.  The
2256 following @code{errno} error conditions are defined for this function:
2258 @table @code
2259 @item EBADF
2260 @var{socket} is not a valid file descriptor.
2262 @item ENOTSOCK
2263 @var{socket} is not a socket.
2265 @item ENOTCONN
2266 @var{socket} is not connected.
2267 @end table
2268 @end deftypefun
2270 @node Socket Pairs
2271 @subsection Socket Pairs
2272 @cindex creating a socket pair
2273 @cindex socket pair
2274 @cindex opening a socket pair
2276 @pindex sys/socket.h
2277 A @dfn{socket pair} consists of a pair of connected (but unnamed)
2278 sockets.  It is very similar to a pipe and is used in much the same
2279 way.  Socket pairs are created with the @code{socketpair} function,
2280 declared in @file{sys/socket.h}.  A socket pair is much like a pipe; the
2281 main difference is that the socket pair is bidirectional, whereas the
2282 pipe has one input-only end and one output-only end (@pxref{Pipes and
2283 FIFOs}).
2285 @deftypefun int socketpair (int @var{namespace}, int @var{style}, int @var{protocol}, int @var{filedes}@t{[2]})
2286 @standards{BSD, sys/socket.h}
2287 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
2288 This function creates a socket pair, returning the file descriptors in
2289 @code{@var{filedes}[0]} and @code{@var{filedes}[1]}.  The socket pair
2290 is a full-duplex communications channel, so that both reading and writing
2291 may be performed at either end.
2293 The @var{namespace}, @var{style} and @var{protocol} arguments are
2294 interpreted as for the @code{socket} function.  @var{style} should be
2295 one of the communication styles listed in @ref{Communication Styles}.
2296 The @var{namespace} argument specifies the namespace, which must be
2297 @code{AF_LOCAL} (@pxref{Local Namespace}); @var{protocol} specifies the
2298 communications protocol, but zero is the only meaningful value.
2300 If @var{style} specifies a connectionless communication style, then
2301 the two sockets you get are not @emph{connected}, strictly speaking,
2302 but each of them knows the other as the default destination address,
2303 so they can send packets to each other.
2305 The @code{socketpair} function returns @code{0} on success and @code{-1}
2306 on failure.  The following @code{errno} error conditions are defined
2307 for this function:
2309 @table @code
2310 @item EMFILE
2311 The process has too many file descriptors open.
2313 @item EAFNOSUPPORT
2314 The specified namespace is not supported.
2316 @item EPROTONOSUPPORT
2317 The specified protocol is not supported.
2319 @item EOPNOTSUPP
2320 The specified protocol does not support the creation of socket pairs.
2321 @end table
2322 @end deftypefun
2324 @node Connections
2325 @section Using Sockets with Connections
2327 @cindex connection
2328 @cindex client
2329 @cindex server
2330 The most common communication styles involve making a connection to a
2331 particular other socket, and then exchanging data with that socket
2332 over 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.
2336 @iftex
2337 @itemize @bullet
2338 @item
2339 @ref{Connecting}, describes what the client program must do to
2340 initiate a connection with a server.
2342 @item
2343 @ref{Listening} and @ref{Accepting Connections} describe what the
2344 server program must do to wait for and act upon connection requests
2345 from clients.
2347 @item
2348 @ref{Transferring Data}, describes how data are transferred through the
2349 connected socket.
2350 @end itemize
2351 @end iftex
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
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
2373 In making a connection, the client makes a connection while the server
2374 waits for and accepts the connection.  Here we discuss what the client
2375 program must do with the @code{connect} function, which is declared in
2376 @file{sys/socket.h}.
2378 @deftypefun int connect (int @var{socket}, struct sockaddr *@var{addr}, socklen_t @var{length})
2379 @standards{BSD, sys/socket.h}
2380 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2381 The @code{connect} function initiates a connection from the socket
2382 with file descriptor @var{socket} to the socket whose address is
2383 specified by the @var{addr} and @var{length} arguments.  (This socket
2384 is typically on another machine, and it must be already set up as a
2385 server.)  @xref{Socket Addresses}, for information about how these
2386 arguments are interpreted.
2388 Normally, @code{connect} waits until the server responds to the request
2389 before it returns.  You can set nonblocking mode on the socket
2390 @var{socket} to make @code{connect} return immediately without waiting
2391 for the response.  @xref{File Status Flags}, for information about
2392 nonblocking 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.
2396 The normal return value from @code{connect} is @code{0}.  If an error
2397 occurs, @code{connect} returns @code{-1}.  The following @code{errno}
2398 error conditions are defined for this function:
2400 @table @code
2401 @item EBADF
2402 The socket @var{socket} is not a valid file descriptor.
2404 @item ENOTSOCK
2405 File descriptor @var{socket} is not a socket.
2407 @item EADDRNOTAVAIL
2408 The specified address is not available on the remote machine.
2410 @item EAFNOSUPPORT
2411 The namespace of the @var{addr} is not supported by this socket.
2413 @item EISCONN
2414 The socket @var{socket} is already connected.
2416 @item ETIMEDOUT
2417 The attempt to establish the connection timed out.
2419 @item ECONNREFUSED
2420 The server has actively refused to establish the connection.
2422 @item ENETUNREACH
2423 The network of the given @var{addr} isn't reachable from this host.
2425 @item EADDRINUSE
2426 The socket address of the given @var{addr} is already in use.
2428 @item EINPROGRESS
2429 The socket @var{socket} is non-blocking and the connection could not be
2430 established immediately.  You can determine when the connection is
2431 completely established with @code{select}; @pxref{Waiting for I/O}.
2432 Another @code{connect} call on the same socket, before the connection is
2433 completely established, will fail with @code{EALREADY}.
2435 @item EALREADY
2436 The socket @var{socket} is non-blocking and already has a pending
2437 connection in progress (see @code{EINPROGRESS} above).
2438 @end table
2440 This function is defined as a cancellation point in multi-threaded
2441 programs, so one has to be prepared for this and make sure that
2442 allocated resources (like memory, file descriptors, semaphores or
2443 whatever) are freed even if the thread is canceled.
2444 @c @xref{pthread_cleanup_push}, for a method how to do this.
2445 @end deftypefun
2447 @node Listening
2448 @subsection Listening for Connections
2449 @cindex listening (sockets)
2450 @cindex sockets, server actions
2451 @cindex sockets, listening
2453 Now let us consider what the server process must do to accept
2454 connections on a socket.  First it must use the @code{listen} function
2455 to enable connection requests on the socket, and then accept each
2456 incoming connection with a call to @code{accept} (@pxref{Accepting
2457 Connections}).  Once connection requests are enabled on a server socket,
2458 the @code{select} function reports when the socket has a connection
2459 ready to be accepted (@pxref{Waiting for I/O}).
2461 The @code{listen} function is not allowed for sockets using
2462 connectionless communication styles.
2464 You can write a network server that does not even start running until a
2465 connection to it is requested.  @xref{Inetd Servers}.
2467 In the Internet namespace, there are no special protection mechanisms
2468 for controlling access to a port; any process on any machine
2469 can make a connection to your server.  If you want to restrict access to
2470 your server, make it examine the addresses associated with connection
2471 requests or implement some other handshaking or identification
2472 protocol.
2474 In the local namespace, the ordinary file protection bits control who has
2475 access to connect to the socket.
2477 @deftypefun int listen (int @var{socket}, int @var{n})
2478 @standards{BSD, sys/socket.h}
2479 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
2480 The @code{listen} function enables the socket @var{socket} to accept
2481 connections, thus making it a server socket.
2483 The argument @var{n} specifies the length of the queue for pending
2484 connections.  When the queue fills, new clients attempting to connect
2485 fail with @code{ECONNREFUSED} until the server calls @code{accept} to
2486 accept a connection from the queue.
2488 The @code{listen} function returns @code{0} on success and @code{-1}
2489 on failure.  The following @code{errno} error conditions are defined
2490 for this function:
2492 @table @code
2493 @item EBADF
2494 The argument @var{socket} is not a valid file descriptor.
2496 @item ENOTSOCK
2497 The argument @var{socket} is not a socket.
2499 @item EOPNOTSUPP
2500 The socket @var{socket} does not support this operation.
2501 @end table
2502 @end deftypefun
2504 @node Accepting Connections
2505 @subsection Accepting Connections
2506 @cindex sockets, accepting connections
2507 @cindex accepting connections
2509 When a server receives a connection request, it can complete the
2510 connection by accepting the request.  Use the function @code{accept}
2511 to do this.
2513 A socket that has been established as a server can accept connection
2514 requests from multiple clients.  The server's original socket
2515 @emph{does not become part of the connection}; instead, @code{accept}
2516 makes a new socket which participates in the connection.
2517 @code{accept} returns the descriptor for this socket.  The server's
2518 original socket remains available for listening for further connection
2519 requests.
2521 The number of pending connection requests on a server socket is finite.
2522 If connection requests arrive from clients faster than the server can
2523 act upon them, the queue can fill up and additional requests are refused
2524 with an @code{ECONNREFUSED} error.  You can specify the maximum length of
2525 this queue as an argument to the @code{listen} function, although the
2526 system may also impose its own internal limit on the length of this
2527 queue.
2529 @deftypefun int accept (int @var{socket}, struct sockaddr *@var{addr}, socklen_t *@var{length_ptr})
2530 @standards{BSD, sys/socket.h}
2531 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
2532 This function is used to accept a connection request on the server
2533 socket @var{socket}.
2535 The @code{accept} function waits if there are no connections pending,
2536 unless the socket @var{socket} has nonblocking mode set.  (You can use
2537 @code{select} to wait for a pending connection, with a nonblocking
2538 socket.)  @xref{File Status Flags}, for information about nonblocking
2539 mode.
2541 The @var{addr} and @var{length-ptr} arguments are used to return
2542 information about the name of the client socket that initiated the
2543 connection.  @xref{Socket Addresses}, for information about the format
2544 of the information.
2546 Accepting a connection does not make @var{socket} part of the
2547 connection.  Instead, it creates a new socket which becomes
2548 connected.  The normal return value of @code{accept} is the file
2549 descriptor for the new socket.
2551 After @code{accept}, the original socket @var{socket} remains open and
2552 unconnected, and continues listening until you close it.  You can
2553 accept further connections with @var{socket} by calling @code{accept}
2554 again.
2556 If an error occurs, @code{accept} returns @code{-1}.  The following
2557 @code{errno} error conditions are defined for this function:
2559 @table @code
2560 @item EBADF
2561 The @var{socket} argument is not a valid file descriptor.
2563 @item ENOTSOCK
2564 The descriptor @var{socket} argument is not a socket.
2566 @item EOPNOTSUPP
2567 The descriptor @var{socket} does not support this operation.
2569 @item EWOULDBLOCK
2570 @var{socket} has nonblocking mode set, and there are no pending
2571 connections immediately available.
2572 @end table
2574 This function is defined as a cancellation point in multi-threaded
2575 programs, so one has to be prepared for this and make sure that
2576 allocated resources (like memory, file descriptors, semaphores or
2577 whatever) are freed even if the thread is canceled.
2578 @c @xref{pthread_cleanup_push}, for a method how to do this.
2579 @end deftypefun
2581 The @code{accept} function is not allowed for sockets using
2582 connectionless communication styles.
2584 @node Who is Connected
2585 @subsection Who is Connected to Me?
2587 @deftypefun int getpeername (int @var{socket}, struct sockaddr *@var{addr}, socklen_t *@var{length-ptr})
2588 @standards{BSD, sys/socket.h}
2589 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2590 The @code{getpeername} function returns the address of the socket that
2591 @var{socket} is connected to; it stores the address in the memory space
2592 specified by @var{addr} and @var{length-ptr}.  It stores the length of
2593 the address in @code{*@var{length-ptr}}.
2595 @xref{Socket Addresses}, for information about the format of the
2596 address.  In some operating systems, @code{getpeername} works only for
2597 sockets in the Internet domain.
2599 The return value is @code{0} on success and @code{-1} on error.  The
2600 following @code{errno} error conditions are defined for this function:
2602 @table @code
2603 @item EBADF
2604 The argument @var{socket} is not a valid file descriptor.
2606 @item ENOTSOCK
2607 The descriptor @var{socket} is not a socket.
2609 @item ENOTCONN
2610 The socket @var{socket} is not connected.
2612 @item ENOBUFS
2613 There are not enough internal buffers available.
2614 @end table
2615 @end deftypefun
2618 @node Transferring Data
2619 @subsection Transferring Data
2620 @cindex reading from a socket
2621 @cindex writing to a socket
2623 Once 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
2625 transfer data.  A socket is a two-way communications channel, so read
2626 and write operations can be performed at either end.
2628 There are also some I/O modes that are specific to socket operations.
2629 In 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
2632 an additional argument which you can use to specify various flags to
2633 control special I/O modes.  For example, you can specify the
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
2636 to control inclusion of routing information on output.
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
2644 @node Sending Data
2645 @subsubsection Sending Data
2647 @pindex sys/socket.h
2648 The @code{send} function is declared in the header file
2649 @file{sys/socket.h}.  If your @var{flags} argument is zero, you can just
2650 as well use @code{write} instead of @code{send}; see @ref{I/O
2651 Primitives}.  If the socket was connected but the connection has broken,
2652 you get a @code{SIGPIPE} signal for any use of @code{send} or
2653 @code{write} (@pxref{Miscellaneous Signals}).
2655 @deftypefun ssize_t send (int @var{socket}, const void *@var{buffer}, size_t @var{size}, int @var{flags})
2656 @standards{BSD, sys/socket.h}
2657 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2658 The @code{send} function is like @code{write}, but with the additional
2659 flags @var{flags}.  The possible values of @var{flags} are described
2660 in @ref{Socket Data Options}.
2662 This function returns the number of bytes transmitted, or @code{-1} on
2663 failure.  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.
2667 Note, however, that a successful return value merely indicates that
2668 the message has been sent without error, not necessarily that it has
2669 been received without error.
2671 The following @code{errno} error conditions are defined for this function:
2673 @table @code
2674 @item EBADF
2675 The @var{socket} argument is not a valid file descriptor.
2677 @item EINTR
2678 The operation was interrupted by a signal before any data was sent.
2679 @xref{Interrupted Primitives}.
2681 @item ENOTSOCK
2682 The descriptor @var{socket} is not a socket.
2684 @item EMSGSIZE
2685 The socket type requires that the message be sent atomically, but the
2686 message is too large for this to be possible.
2688 @item EWOULDBLOCK
2689 Nonblocking mode has been set on the socket, and the write operation
2690 would block.  (Normally @code{send} blocks until the operation can be
2691 completed.)
2693 @item ENOBUFS
2694 There is not enough internal buffer space available.
2696 @item ENOTCONN
2697 You never connected this socket.
2699 @item EPIPE
2700 This socket was connected but the connection is now broken.  In this
2701 case, @code{send} generates a @code{SIGPIPE} signal first; if that
2702 signal is ignored or blocked, or if its handler returns, then
2703 @code{send} fails with @code{EPIPE}.
2704 @end table
2706 This function is defined as a cancellation point in multi-threaded
2707 programs, so one has to be prepared for this and make sure that
2708 allocated resources (like memory, file descriptors, semaphores or
2709 whatever) are freed even if the thread is canceled.
2710 @c @xref{pthread_cleanup_push}, for a method how to do this.
2711 @end deftypefun
2713 @node Receiving Data
2714 @subsubsection Receiving Data
2716 @pindex sys/socket.h
2717 The @code{recv} function is declared in the header file
2718 @file{sys/socket.h}.  If your @var{flags} argument is zero, you can
2719 just as well use @code{read} instead of @code{recv}; see @ref{I/O
2720 Primitives}.
2722 @deftypefun ssize_t recv (int @var{socket}, void *@var{buffer}, size_t @var{size}, int @var{flags})
2723 @standards{BSD, sys/socket.h}
2724 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
2725 The @code{recv} function is like @code{read}, but with the additional
2726 flags @var{flags}.  The possible values of @var{flags} are described
2727 in @ref{Socket Data Options}.
2729 If nonblocking mode is set for @var{socket}, and no data are available to
2730 be read, @code{recv} fails immediately rather than waiting.  @xref{File
2731 Status Flags}, for information about nonblocking mode.
2733 This function returns the number of bytes received, or @code{-1} on failure.
2734 The following @code{errno} error conditions are defined for this function:
2736 @table @code
2737 @item EBADF
2738 The @var{socket} argument is not a valid file descriptor.
2740 @item ENOTSOCK
2741 The descriptor @var{socket} is not a socket.
2743 @item EWOULDBLOCK
2744 Nonblocking mode has been set on the socket, and the read operation
2745 would block.  (Normally, @code{recv} blocks until there is input
2746 available to be read.)
2748 @item EINTR
2749 The operation was interrupted by a signal before any data was read.
2750 @xref{Interrupted Primitives}.
2752 @item ENOTCONN
2753 You never connected this socket.
2754 @end table
2756 This function is defined as a cancellation point in multi-threaded
2757 programs, so one has to be prepared for this and make sure that
2758 allocated resources (like memory, file descriptors, semaphores or
2759 whatever) are freed even if the thread is canceled.
2760 @c @xref{pthread_cleanup_push}, for a method how to do this.
2761 @end deftypefun
2763 @node Socket Data Options
2764 @subsubsection Socket Data Options
2766 @pindex sys/socket.h
2767 The @var{flags} argument to @code{send} and @code{recv} is a bit
2768 mask.  You can bitwise-OR the values of the following macros together
2769 to obtain a value for this argument.  All are defined in the header
2770 file @file{sys/socket.h}.
2772 @deftypevr Macro int MSG_OOB
2773 @standards{BSD, sys/socket.h}
2774 Send or receive out-of-band data.  @xref{Out-of-Band Data}.
2775 @end deftypevr
2777 @deftypevr Macro int MSG_PEEK
2778 @standards{BSD, sys/socket.h}
2779 Look at the data but don't remove it from the input queue.  This is
2780 only meaningful with input functions such as @code{recv}, not with
2781 @code{send}.
2782 @end deftypevr
2784 @deftypevr Macro int MSG_DONTROUTE
2785 @standards{BSD, sys/socket.h}
2786 Don't include routing information in the message.  This is only
2787 meaningful with output operations, and is usually only of interest for
2788 diagnostic or routing programs.  We don't try to explain it here.
2789 @end deftypevr
2791 @node Byte Stream Example
2792 @subsection Byte Stream Socket Example
2794 Here is an example client program that makes a connection for a byte
2795 stream socket in the Internet namespace.  It doesn't do anything
2796 particularly interesting once it has connected to the server; it just
2797 sends a text string to the server and exits.
2799 This program uses @code{init_sockaddr} to set up the socket address; see
2800 @ref{Inet Example}.
2802 @smallexample
2803 @include inetcli.c.texi
2804 @end smallexample
2806 @node Server Example
2807 @subsection Byte Stream Connection Server Example
2809 The server end is much more complicated.  Since we want to allow
2810 multiple clients to be connected to the server at the same time, it
2811 would be incorrect to wait for input from a single client by simply
2812 calling @code{read} or @code{recv}.  Instead, the right thing to do is
2813 to use @code{select} (@pxref{Waiting for I/O}) to wait for input on
2814 all of the open sockets.  This also allows the server to deal with
2815 additional connection requests.
2817 This particular server doesn't do anything interesting once it has
2818 gotten a message from a client.  It does close the socket for that
2819 client when it detects an end-of-file condition (resulting from the
2820 client shutting down its end of the connection).
2822 This program uses @code{make_socket} to set up the socket address; see
2823 @ref{Inet Example}.
2825 @smallexample
2826 @include inetsrv.c.texi
2827 @end smallexample
2829 @node Out-of-Band Data
2830 @subsection Out-of-Band Data
2832 @cindex out-of-band data
2833 @cindex high-priority data
2834 Streams with connections permit @dfn{out-of-band} data that is
2835 delivered with higher priority than ordinary data.  Typically the
2836 reason for sending out-of-band data is to send notice of an
2837 exceptional condition.  To send out-of-band data use
2838 @code{send}, specifying the flag @code{MSG_OOB} (@pxref{Sending
2839 Data}).
2841 Out-of-band data are received with higher priority because the
2842 receiving process need not read it in sequence; to read the next
2843 available out-of-band data, use @code{recv} with the @code{MSG_OOB}
2844 flag (@pxref{Receiving Data}).  Ordinary read operations do not read
2845 out-of-band data; they read only ordinary data.
2847 @cindex urgent socket condition
2848 When a socket finds that out-of-band data are on their way, it sends a
2849 @code{SIGURG} signal to the owner process or process group of the
2850 socket.  You can specify the owner using the @code{F_SETOWN} command
2851 to the @code{fcntl} function; see @ref{Interrupt Input}.  You must
2852 also establish a handler for this signal, as described in @ref{Signal
2853 Handling}, in order to take appropriate action such as reading the
2854 out-of-band data.
2856 Alternatively, you can test for pending out-of-band data, or wait
2857 until there is out-of-band data, using the @code{select} function; it
2858 can wait for an exceptional condition on the socket.  @xref{Waiting
2859 for I/O}, for more information about @code{select}.
2861 Notification of out-of-band data (whether with @code{SIGURG} or with
2862 @code{select}) indicates that out-of-band data are on the way; the data
2863 may not actually arrive until later.  If you try to read the
2864 out-of-band data before it arrives, @code{recv} fails with an
2865 @code{EWOULDBLOCK} error.
2867 Sending out-of-band data automatically places a ``mark'' in the stream
2868 of 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
2870 data is ``cancel everything sent so far''.  Here is how you can test,
2871 in the receiving process, whether any ordinary data was sent before
2872 the mark:
2874 @smallexample
2875 success = ioctl (socket, SIOCATMARK, &atmark);
2876 @end smallexample
2878 The @code{integer} variable @var{atmark} is set to a nonzero value if
2879 the socket's read pointer has reached the ``mark''.
2881 @c Posix  1.g specifies sockatmark for this ioctl.  sockatmark is not
2882 @c implemented yet.
2884 Here's a function to discard any ordinary data preceding the
2885 out-of-band mark:
2887 @smallexample
2889 discard_until_mark (int socket)
2891   while (1)
2892     @{
2893       /* @r{This is not an arbitrary limit; any size will do.}  */
2894       char buffer[1024];
2895       int atmark, success;
2897       /* @r{If we have reached the mark, return.}  */
2898       success = ioctl (socket, SIOCATMARK, &atmark);
2899       if (success < 0)
2900         perror ("ioctl");
2901       if (result)
2902         return;
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     @}
2912 @end smallexample
2914 If you don't want to discard the ordinary data preceding the mark, you
2915 may need to read some of it anyway, to make room in internal system
2916 buffers for the out-of-band data.  If you try to read out-of-band data
2917 and 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
2919 makes room.  Here is an example:
2921 @smallexample
2922 struct buffer
2924   char *buf;
2925   int size;
2926   struct buffer *next;
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.}
2933    @r{It may be necessary to read some ordinary data}
2934    @r{in order to make room for the out-of-band data.}
2935    @r{If so, the ordinary data are saved as a chain of buffers}
2936    @r{found in the `next' field of the value.}  */
2938 struct buffer *
2939 read_oob (int socket)
2941   struct buffer *tail = 0;
2942   struct buffer *list = 0;
2944   while (1)
2945     @{
2946       /* @r{This is an arbitrary limit.}
2947          @r{Does anyone know how to do this without a limit?}  */
2948 #define BUF_SZ 1024
2949       char *buf = (char *) xmalloc (BUF_SZ);
2950       int success;
2951       int atmark;
2953       /* @r{Try again to read the out-of-band data.}  */
2954       success = recv (socket, buf, BUF_SZ, MSG_OOB);
2955       if (success >= 0)
2956         @{
2957           /* @r{We got it, so return it.}  */
2958           struct buffer *link
2959             = (struct buffer *) xmalloc (sizeof (struct buffer));
2960           link->buf = buf;
2961           link->size = success;
2962           link->next = list;
2963           return link;
2964         @}
2966       /* @r{If we fail, see if we are at the mark.}  */
2967       success = ioctl (socket, SIOCATMARK, &atmark);
2968       if (success < 0)
2969         perror ("ioctl");
2970       if (atmark)
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         @}
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.}  */
2981       success = read (socket, buf, BUF_SZ);
2982       if (success < 0)
2983         perror ("read");
2985       /* @r{Save this data in the buffer list.}  */
2986       @{
2987         struct buffer *link
2988           = (struct buffer *) xmalloc (sizeof (struct buffer));
2989         link->buf = buf;
2990         link->size = success;
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     @}
3001 @end smallexample
3003 @node Datagrams
3004 @section Datagram Socket Operations
3006 @cindex datagram socket
3007 This section describes how to use communication styles that don't use
3008 connections (styles @code{SOCK_DGRAM} and @code{SOCK_RDM}).  Using
3009 these styles, you group data into packets and each packet is an
3010 independent communication.  You specify the destination for each
3011 packet individually.
3013 Datagram packets are like letters: you send each one independently
3014 with its own destination address, and they may arrive in the wrong
3015 order or not at all.
3017 The @code{listen} and @code{accept} functions are not allowed for
3018 sockets using connectionless communication styles.
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
3024                            datagram socket in the local namespace.
3025 * Example Receiver::     Another program, that receives those packets.
3026 @end menu
3028 @node Sending Datagrams
3029 @subsection Sending Datagrams
3030 @cindex sending a datagram
3031 @cindex transmitting datagrams
3032 @cindex datagrams, transmitting
3034 @pindex sys/socket.h
3035 The normal way of sending data on a datagram socket is by using the
3036 @code{sendto} function, declared in @file{sys/socket.h}.
3038 You can call @code{connect} on a datagram socket, but this only
3039 specifies a default destination for further data transmission on the
3040 socket.  When a socket has a default destination you can use
3041 @code{send} (@pxref{Sending Data}) or even @code{write} (@pxref{I/O
3042 Primitives}) to send a packet there.  You can cancel the default
3043 destination by calling @code{connect} using an address format of
3044 @code{AF_UNSPEC} in the @var{addr} argument.  @xref{Connecting}, for
3045 more information about the @code{connect} function.
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})
3048 @standards{BSD, sys/socket.h}
3049 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3050 The @code{sendto} function transmits the data in the @var{buffer}
3051 through the socket @var{socket} to the destination address specified
3052 by the @var{addr} and @var{length} arguments.  The @var{size} argument
3053 specifies the number of bytes to be transmitted.
3055 The @var{flags} are interpreted the same way as for @code{send}; see
3056 @ref{Socket Data Options}.
3058 The 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
3060 report them; the most common error is that the packet is lost or there
3061 is no-one at the specified address to receive it, and the operating
3062 system on your machine usually does not know this.
3064 It is also possible for one call to @code{sendto} to report an error
3065 owing to a problem related to a previous call.
3067 This function is defined as a cancellation point in multi-threaded
3068 programs, so one has to be prepared for this and make sure that
3069 allocated resources (like memory, file descriptors, semaphores or
3070 whatever) are freed even if the thread is canceled.
3071 @c @xref{pthread_cleanup_push}, for a method how to do this.
3072 @end deftypefun
3074 @node Receiving Datagrams
3075 @subsection Receiving Datagrams
3076 @cindex receiving datagrams
3078 The @code{recvfrom} function reads a packet from a datagram socket and
3079 also tells you where it was sent from.  This function is declared in
3080 @file{sys/socket.h}.
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})
3083 @standards{BSD, sys/socket.h}
3084 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3085 The @code{recvfrom} function reads one packet from the socket
3086 @var{socket} into the buffer @var{buffer}.  The @var{size} argument
3087 specifies the maximum number of bytes to be read.
3089 If the packet is longer than @var{size} bytes, then you get the first
3090 @var{size} bytes of the packet and the rest of the packet is lost.
3091 There's no way to read the rest of the packet.  Thus, when you use a
3092 packet protocol, you must always know how long a packet to expect.
3094 The @var{addr} and @var{length-ptr} arguments are used to return the
3095 address where the packet came from.  @xref{Socket Addresses}.  For a
3096 socket in the local domain the address information won't be meaningful,
3097 since you can't read the address of such a socket (@pxref{Local
3098 Namespace}).  You can specify a null pointer as the @var{addr} argument
3099 if you are not interested in this information.
3101 The @var{flags} are interpreted the same way as for @code{recv}
3102 (@pxref{Socket Data Options}).  The return value and error conditions
3103 are also the same as for @code{recv}.
3105 This function is defined as a cancellation point in multi-threaded
3106 programs, so one has to be prepared for this and make sure that
3107 allocated resources (like memory, file descriptors, semaphores or
3108 whatever) are freed even if the thread is canceled.
3109 @c @xref{pthread_cleanup_push}, for a method how to do this.
3110 @end deftypefun
3112 You can use plain @code{recv} (@pxref{Receiving Data}) instead of
3113 @code{recvfrom} if you don't need to find out who sent the packet
3114 (either because you know where it should come from or because you
3115 treat all possible senders alike).  Even @code{read} can be used if
3116 you don't want to specify @var{flags} (@pxref{I/O Primitives}).
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
3124 @deftp {Data Type} {struct msghdr}
3125 @standards{BSD, sys/socket.h}
3126 @end deftp
3128 @deftypefun ssize_t sendmsg (int @var{socket}, const struct msghdr *@var{message}, int @var{flags})
3129 @standards{BSD, sys/socket.h}
3130 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3132 This function is defined as a cancellation point in multi-threaded
3133 programs, so one has to be prepared for this and make sure that
3134 allocated resources (like memory, files descriptors, semaphores or
3135 whatever) are freed even if the thread is cancel.
3136 @c @xref{pthread_cleanup_push}, for a method how to do this.
3137 @end deftypefun
3139 @deftypefun ssize_t recvmsg (int @var{socket}, struct msghdr *@var{message}, int @var{flags})
3140 @standards{BSD, sys/socket.h}
3141 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3143 This function is defined as a cancellation point in multi-threaded
3144 programs, so one has to be prepared for this and make sure that
3145 allocated resources (like memory, files descriptors, semaphores or
3146 whatever) are freed even if the thread is canceled.
3147 @c @xref{pthread_cleanup_push}, for a method how to do this.
3148 @end deftypefun
3149 @end ignore
3151 @node Datagram Example
3152 @subsection Datagram Socket Example
3154 Here is a set of example programs that send messages over a datagram
3155 stream in the local namespace.  Both the client and server programs use
3156 the @code{make_named_socket} function that was presented in @ref{Local
3157 Socket Example}, to create and name their sockets.
3159 First, here is the server program.  It sits in a loop waiting for
3160 messages to arrive, bouncing each message back to the sender.
3161 Obviously this isn't a particularly useful program, but it does show
3162 the general ideas involved.
3164 @smallexample
3165 @include filesrv.c.texi
3166 @end smallexample
3168 @node Example Receiver
3169 @subsection Example of Reading Datagrams
3171 Here is the client program corresponding to the server above.
3173 It sends a datagram to the server and then waits for a reply.  Notice
3174 that the socket for the client (as well as for the server) in this
3175 example has to be given a name.  This is so that the server can direct
3176 a message back to the client.  Since the socket has no associated
3177 connection state, the only way the server can do this is by
3178 referencing the name of the client.
3180 @smallexample
3181 @include filecli.c.texi
3182 @end smallexample
3184 Keep in mind that datagram socket communications are unreliable.  In
3185 this example, the client program waits indefinitely if the message
3186 never reaches the server or if the server's response never comes
3187 back.  It's up to the user running the program to kill and restart
3188 it if desired.  A more automatic solution could be to use
3189 @code{select} (@pxref{Waiting for I/O}) to establish a timeout period
3190 for the reply, and in case of timeout either re-send the message or
3191 shut down the socket and exit.
3193 @node Inetd
3194 @section The @code{inetd} Daemon
3196 We've explained above how to write a server program that does its own
3197 listening.  Such a server must already be running in order for anyone
3198 to connect to it.
3200 Another way to provide a service on an Internet port is to let the daemon
3201 program @code{inetd} do the listening.  @code{inetd} is a program that
3202 runs all the time and waits (using @code{select}) for messages on a
3203 specified set of ports.  When it receives a message, it accepts the
3204 connection (if the socket style calls for connections) and then forks a
3205 child process to run the corresponding server program.  You specify the
3206 ports and their programs in the file @file{/etc/inetd.conf}.
3208 @menu
3209 * Inetd Servers::
3210 * Configuring Inetd::
3211 @end menu
3213 @node Inetd Servers
3214 @subsection @code{inetd} Servers
3216 Writing a server program to be run by @code{inetd} is very simple.  Each time
3217 someone requests a connection to the appropriate port, a new server
3218 process starts.  The connection already exists at this time; the
3219 socket is available as the standard input descriptor and as the
3220 standard output descriptor (descriptors 0 and 1) in the server
3221 process.  Thus the server program can begin reading and writing data
3222 right away.  Often the program needs only the ordinary I/O facilities;
3223 in fact, a general-purpose filter program that knows nothing about
3224 sockets can work as a byte stream server run by @code{inetd}.
3226 You can also use @code{inetd} for servers that use connectionless
3227 communication styles.  For these servers, @code{inetd} does not try to accept
3228 a connection since no connection is possible.  It just starts the
3229 server program, which can read the incoming datagram packet from
3230 descriptor 0.  The server program can handle one request and then
3231 exit, or you can choose to write it to keep reading more requests
3232 until no more arrive, and then exit.  You must specify which of these
3233 two techniques the server uses when you configure @code{inetd}.
3235 @node Configuring Inetd
3236 @subsection Configuring @code{inetd}
3238 The file @file{/etc/inetd.conf} tells @code{inetd} which ports to listen to
3239 and what server programs to run for them.  Normally each entry in the
3240 file is one line, but you can split it onto multiple lines provided
3241 all but the first line of the entry start with whitespace.  Lines that
3242 start with @samp{#} are comments.
3244 Here are two standard entries in @file{/etc/inetd.conf}:
3246 @smallexample
3247 ftp     stream  tcp     nowait  root    /libexec/ftpd   ftpd
3248 talk    dgram   udp     wait    root    /libexec/talkd  talkd
3249 @end smallexample
3251 An entry has this format:
3253 @smallexample
3254 @var{service} @var{style} @var{protocol} @var{wait} @var{username} @var{program} @var{arguments}
3255 @end smallexample
3257 The @var{service} field says which service this program provides.  It
3258 should 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
3260 this entry.
3262 The fields @var{style} and @var{protocol} specify the communication
3263 style and the protocol to use for the listening socket.  The style
3264 should be the name of a communication style, converted to lower case
3265 and 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
3268 byte stream connections and @samp{udp} for unreliable datagrams.
3270 The @var{wait} field should be either @samp{wait} or @samp{nowait}.
3271 Use @samp{wait} if @var{style} is a connectionless style and the
3272 server, once started, handles multiple requests as they come in.
3273 Use @samp{nowait} if @code{inetd} should start a new process for each message
3274 or request that comes in.  If @var{style} uses connections, then
3275 @var{wait} @strong{must} be @samp{nowait}.
3277 @var{user} is the user name that the server should run as.  @code{inetd} runs
3278 as root, so it can set the user ID of its children arbitrarily.  It's
3279 best to avoid using @samp{root} for @var{user} if you can; but some
3280 servers, such as Telnet and FTP, read a username and password
3281 themselves.  These servers need to be root initially so they can log
3282 in as commanded by the data coming over the network.
3284 @var{program} together with @var{arguments} specifies the command to
3285 run to start the server.  @var{program} should be an absolute file
3286 name specifying the executable file to run.  @var{arguments} consists
3287 of any number of whitespace-separated words, which become the
3288 command-line arguments of @var{program}.  The first word in
3289 @var{arguments} is argument zero, which should by convention be the
3290 program name itself (sans directories).
3292 If you edit @file{/etc/inetd.conf}, you can tell @code{inetd} to reread the
3293 file 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
3295 process ID of the @code{inetd} process as it is not fixed.
3297 @c !!! could document /etc/inetd.sec
3299 @node Socket Options
3300 @section Socket Options
3301 @cindex socket options
3303 This section describes how to read or set various options that modify
3304 the behavior of sockets and their underlying communications protocols.
3306 @cindex level, for socket options
3307 @cindex socket option level
3308 When you are manipulating a socket option, you must specify which
3309 @dfn{level} the option pertains to.  This describes whether the option
3310 applies to the socket interface, or to a lower-level communications
3311 protocol interface.
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
3319 @node Socket Option Functions
3320 @subsection Socket Option Functions
3322 @pindex sys/socket.h
3323 Here are the functions for examining and modifying socket options.
3324 They are declared in @file{sys/socket.h}.
3326 @deftypefun int getsockopt (int @var{socket}, int @var{level}, int @var{optname}, void *@var{optval}, socklen_t *@var{optlen-ptr})
3327 @standards{BSD, sys/socket.h}
3328 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3329 The @code{getsockopt} function gets information about the value of
3330 option @var{optname} at level @var{level} for socket @var{socket}.
3332 The option value is stored in the buffer that @var{optval} points to.
3333 Before the call, you should supply in @code{*@var{optlen-ptr}} the
3334 size of this buffer; on return, it contains the number of bytes of
3335 information actually stored in the buffer.
3337 Most options interpret the @var{optval} buffer as a single @code{int}
3338 value.
3340 The actual return value of @code{getsockopt} is @code{0} on success
3341 and @code{-1} on failure.  The following @code{errno} error conditions
3342 are defined:
3344 @table @code
3345 @item EBADF
3346 The @var{socket} argument is not a valid file descriptor.
3348 @item ENOTSOCK
3349 The descriptor @var{socket} is not a socket.
3351 @item ENOPROTOOPT
3352 The @var{optname} doesn't make sense for the given @var{level}.
3353 @end table
3354 @end deftypefun
3356 @deftypefun int setsockopt (int @var{socket}, int @var{level}, int @var{optname}, const void *@var{optval}, socklen_t @var{optlen})
3357 @standards{BSD, sys/socket.h}
3358 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
3359 This 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
3361 in the buffer @var{optval} of size @var{optlen}.
3363 @c Argh. -zw
3364 @iftex
3365 @hfuzz 6pt
3366 The return value and error codes for @code{setsockopt} are the same as
3367 for @code{getsockopt}.
3368 @end iftex
3369 @ifinfo
3370 The return value and error codes for @code{setsockopt} are the same as
3371 for @code{getsockopt}.
3372 @end ifinfo
3374 @end deftypefun
3376 @node Socket-Level Options
3377 @subsection Socket-Level Options
3379 @deftypevr Constant int SOL_SOCKET
3380 @standards{BSD, sys/socket.h}
3381 Use this constant as the @var{level} argument to @code{getsockopt} or
3382 @code{setsockopt} to manipulate the socket-level options described in
3383 this section.
3384 @end deftypevr
3386 @pindex sys/socket.h
3387 @noindent
3388 Here is a table of socket-level option names; all are defined in the
3389 header file @file{sys/socket.h}.
3391 @vtable @code
3392 @item SO_DEBUG
3393 @standards{BSD, sys/socket.h}
3394 @c Extra blank line here makes the table look better.
3396 This option toggles recording of debugging information in the underlying
3397 protocol modules.  The value has type @code{int}; a nonzero value means
3398 ``yes''.
3399 @c !!! should say how this is used
3400 @c OK, anyone who knows, please explain.
3402 @item SO_REUSEADDR
3403 @standards{BSD, sys/socket.h}
3404 This option controls whether @code{bind} (@pxref{Setting Address})
3405 should permit reuse of local addresses for this socket.  If you enable
3406 this option, you can actually have two sockets with the same Internet
3407 port number; but the system won't allow you to use the two
3408 identically-named sockets in a way that would confuse the Internet.  The
3409 reason for this option is that some higher-level Internet protocols,
3410 including FTP, require you to keep reusing the same port number.
3412 The value has type @code{int}; a nonzero value means ``yes''.
3414 @item SO_KEEPALIVE
3415 @standards{BSD, sys/socket.h}
3416 This option controls whether the underlying protocol should
3417 periodically transmit messages on a connected socket.  If the peer
3418 fails to respond to these messages, the connection is considered
3419 broken.  The value has type @code{int}; a nonzero value means
3420 ``yes''.
3422 @item SO_DONTROUTE
3423 @standards{BSD, sys/socket.h}
3424 This option controls whether outgoing messages bypass the normal
3425 message routing facilities.  If set, messages are sent directly to the
3426 network interface instead.  The value has type @code{int}; a nonzero
3427 value means ``yes''.
3429 @item SO_LINGER
3430 @standards{BSD, sys/socket.h}
3431 This option specifies what should happen when the socket of a type
3432 that promises reliable delivery still has untransmitted messages when
3433 it is closed; see @ref{Closing a Socket}.  The value has type
3434 @code{struct linger}.
3436 @deftp {Data Type} {struct linger}
3437 @standards{BSD, sys/socket.h}
3438 This structure type has the following members:
3440 @table @code
3441 @item int l_onoff
3442 This field is interpreted as a boolean.  If nonzero, @code{close}
3443 blocks until the data are transmitted or the timeout period has expired.
3445 @item int l_linger
3446 This specifies the timeout period, in seconds.
3447 @end table
3448 @end deftp
3450 @item SO_BROADCAST
3451 @standards{BSD, sys/socket.h}
3452 This option controls whether datagrams may be broadcast from the socket.
3453 The value has type @code{int}; a nonzero value means ``yes''.
3455 @item SO_OOBINLINE
3456 @standards{BSD, sys/socket.h}
3457 If this option is set, out-of-band data received on the socket is
3458 placed in the normal input queue.  This permits it to be read using
3459 @code{read} or @code{recv} without specifying the @code{MSG_OOB}
3460 flag.  @xref{Out-of-Band Data}.  The value has type @code{int}; a
3461 nonzero value means ``yes''.
3463 @item SO_SNDBUF
3464 @standards{BSD, sys/socket.h}
3465 This option gets or sets the size of the output buffer.  The value is a
3466 @code{size_t}, which is the size in bytes.
3468 @item SO_RCVBUF
3469 @standards{BSD, sys/socket.h}
3470 This option gets or sets the size of the input buffer.  The value is a
3471 @code{size_t}, which is the size in bytes.
3473 @item SO_STYLE
3474 @itemx SO_TYPE
3475 @standards{GNU, sys/socket.h}
3476 @standardsx{SO_TYPE, BSD, sys/socket.h}
3477 This option can be used with @code{getsockopt} only.  It is used to
3478 get the socket's communication style.  @code{SO_TYPE} is the
3479 historical name, and @code{SO_STYLE} is the preferred name in GNU.
3480 The value has type @code{int} and its value designates a communication
3481 style; see @ref{Communication Styles}.
3483 @item SO_ERROR
3484 @standards{BSD, sys/socket.h}
3485 @c Extra blank line here makes the table look better.
3487 This option can be used with @code{getsockopt} only.  It is used to reset
3488 the error status of the socket.  The value is an @code{int}, which represents
3489 the previous error status.
3490 @c !!! what is "socket error status"?  this is never defined.
3491 @end vtable
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
3499 @pindex /etc/networks
3500 @pindex netdb.h
3501 Many systems come with a database that records a list of networks known
3502 to 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
3504 base is useful for routing programs such as @code{route}, but it is not
3505 useful for programs that simply communicate over the network.  We
3506 provide functions to access this database, which are declared in
3507 @file{netdb.h}.
3509 @deftp {Data Type} {struct netent}
3510 @standards{BSD, netdb.h}
3511 This data type is used to represent information about entries in the
3512 networks database.  It has the following members:
3514 @table @code
3515 @item char *n_name
3516 This is the ``official'' name of the network.
3518 @item char **n_aliases
3519 These are alternative names for the network, represented as a vector
3520 of strings.  A null pointer terminates the array.
3522 @item int n_addrtype
3523 This is the type of the network number; this is always equal to
3524 @code{AF_INET} for Internet networks.
3526 @item unsigned long int n_net
3527 This is the network number.  Network numbers are returned in host
3528 byte order; see @ref{Byte Order}.
3529 @end table
3530 @end deftp
3532 Use the @code{getnetbyname} or @code{getnetbyaddr} functions to search
3533 the networks database for information about a specific network.  The
3534 information is returned in a statically-allocated structure; you must
3535 copy the information if you need to save it.
3537 @deftypefun {struct netent *} getnetbyname (const char *@var{name})
3538 @standards{BSD, netdb.h}
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
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
3554 The @code{getnetbyname} function returns information about the network
3555 named @var{name}.  It returns a null pointer if there is no such
3556 network.
3557 @end deftypefun
3559 @deftypefun {struct netent *} getnetbyaddr (uint32_t @var{net}, int @var{type})
3560 @standards{BSD, netdb.h}
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
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
3575 The @code{getnetbyaddr} function returns information about the network
3576 of type @var{type} with number @var{net}.  You should specify a value of
3577 @code{AF_INET} for the @var{type} argument for Internet networks.
3579 @code{getnetbyaddr} returns a null pointer if there is no such
3580 network.
3581 @end deftypefun
3583 You can also scan the networks database using @code{setnetent},
3584 @code{getnetent} and @code{endnetent}.  Be careful when using these
3585 functions because they are not reentrant.
3587 @deftypefun void setnetent (int @var{stayopen})
3588 @standards{BSD, netdb.h}
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
3600 This function opens and rewinds the networks database.
3602 If the @var{stayopen} argument is nonzero, this sets a flag so that
3603 subsequent calls to @code{getnetbyname} or @code{getnetbyaddr} will
3604 not close the database (as they usually would).  This makes for more
3605 efficiency if you call those functions several times, by avoiding
3606 reopening the database for each call.
3607 @end deftypefun
3609 @deftypefun {struct netent *} getnetent (void)
3610 @standards{BSD, netdb.h}
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
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
3631 This function returns the next entry in the networks database.  It
3632 returns a null pointer if there are no more entries.
3633 @end deftypefun
3635 @deftypefun void endnetent (void)
3636 @standards{BSD, netdb.h}
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
3646 This function closes the networks database.
3647 @end deftypefun