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