]> git.ipfire.org Git - thirdparty/glibc.git/blob - manual/socket.texi
Update.
[thirdparty/glibc.git] / manual / socket.texi
1 @node Sockets, Low-Level Terminal Interface, Pipes and FIFOs, Top
2 @chapter Sockets
3
4 This chapter describes the GNU facilities for interprocess
5 communication using sockets.
6
7 @cindex socket
8 @cindex interprocess communication, with sockets
9 A @dfn{socket} is a generalized interprocess communication channel.
10 Like a pipe, a socket is represented as a file descriptor. But,
11 unlike pipes, sockets support communication between unrelated
12 processes, and even between processes running on different machines
13 that communicate over a network. Sockets are the primary means of
14 communicating with other machines; @code{telnet}, @code{rlogin},
15 @code{ftp}, @code{talk}, and the other familiar network programs use
16 sockets.
17
18 Not all operating systems support sockets. In the GNU library, the
19 header file @file{sys/socket.h} exists regardless of the operating
20 system, and the socket functions always exist, but if the system does
21 not really support sockets, these functions always fail.
22
23 @strong{Incomplete:} We do not currently document the facilities for
24 broadcast messages or for configuring Internet interfaces.
25
26 @menu
27 * Socket Concepts:: Basic concepts you need to know about.
28 * Communication Styles::Stream communication, datagrams, and other styles.
29 * Socket Addresses:: How socket names (``addresses'') work.
30 * File Namespace:: Details about the file namespace.
31 * Internet Namespace:: Details about the Internet namespace.
32 * Misc Namespaces:: Other namespaces not documented fully here.
33 * Open/Close Sockets:: Creating sockets and destroying them.
34 * Connections:: Operations on sockets with connection state.
35 * Datagrams:: Operations on datagram sockets.
36 * Inetd:: Inetd is a daemon that starts servers on request.
37 The most convenient way to write a server
38 is to make it work with Inetd.
39 * Socket Options:: Miscellaneous low-level socket options.
40 * Networks Database:: Accessing the database of network names.
41 @end menu
42
43 @node Socket Concepts
44 @section Socket Concepts
45
46 @cindex communication style (of a socket)
47 @cindex style of communication (of a socket)
48 When you create a socket, you must specify the style of communication
49 you want to use and the type of protocol that should implement it.
50 The @dfn{communication style} of a socket defines the user-level
51 semantics of sending and receiving data on the socket. Choosing a
52 communication style specifies the answers to questions such as these:
53
54 @itemize @bullet
55 @item
56 @cindex packet
57 @cindex byte stream
58 @cindex stream (sockets)
59 @strong{What are the units of data transmission?} Some communication
60 styles regard the data as a sequence of bytes, with no larger
61 structure; others group the bytes into records (which are known in
62 this context as @dfn{packets}).
63
64 @item
65 @cindex loss of data on sockets
66 @cindex data loss on sockets
67 @strong{Can data be lost during normal operation?} Some communication
68 styles guarantee that all the data sent arrives in the order it was
69 sent (barring system or network crashes); other styles occasionally
70 lose data as a normal part of operation, and may sometimes deliver
71 packets more than once or in the wrong order.
72
73 Designing a program to use unreliable communication styles usually
74 involves taking precautions to detect lost or misordered packets and
75 to retransmit data as needed.
76
77 @item
78 @strong{Is communication entirely with one partner?} Some
79 communication styles are like a telephone call---you make a
80 @dfn{connection} with one remote socket, and then exchange data
81 freely. Other styles are like mailing letters---you specify a
82 destination address for each message you send.
83 @end itemize
84
85 @cindex namespace (of socket)
86 @cindex domain (of socket)
87 @cindex socket namespace
88 @cindex socket domain
89 You must also choose a @dfn{namespace} for naming the socket. A socket
90 name (``address'') is meaningful only in the context of a particular
91 namespace. In fact, even the data type to use for a socket name may
92 depend on the namespace. Namespaces are also called ``domains'', but we
93 avoid that word as it can be confused with other usage of the same
94 term. Each namespace has a symbolic name that starts with @samp{PF_}.
95 A corresponding symbolic name starting with @samp{AF_} designates the
96 address format for that namespace.
97
98 @cindex network protocol
99 @cindex protocol (of socket)
100 @cindex socket protocol
101 @cindex protocol family
102 Finally you must choose the @dfn{protocol} to carry out the
103 communication. The protocol determines what low-level mechanism is used
104 to transmit and receive data. Each protocol is valid for a particular
105 namespace and communication style; a namespace is sometimes called a
106 @dfn{protocol family} because of this, which is why the namespace names
107 start with @samp{PF_}.
108
109 The rules of a protocol apply to the data passing between two programs,
110 perhaps on different computers; most of these rules are handled by the
111 operating system, and you need not know about them. What you do need to
112 know about protocols is this:
113
114 @itemize @bullet
115 @item
116 In order to have communication between two sockets, they must specify
117 the @emph{same} protocol.
118
119 @item
120 Each protocol is meaningful with particular style/namespace
121 combinations and cannot be used with inappropriate combinations. For
122 example, the TCP protocol fits only the byte stream style of
123 communication and the Internet namespace.
124
125 @item
126 For each combination of style and namespace, there is a @dfn{default
127 protocol} which you can request by specifying 0 as the protocol
128 number. And that's what you should normally do---use the default.
129 @end itemize
130
131 Throughout the following description at various places
132 variables/parameters to denote sizes are required. And here the trouble
133 starts. In the first implementations the type of these variables was
134 simply @code{int}. This type was on almost all machines of this time 32
135 bits wide and so a de-factor standard required 32 bit variables. This
136 is important since references to variables of this type are passed to
137 the kernel.
138
139 But now the POSIX people came and unified the interface with their words
140 "all size values are of type @code{size_t}". But on 64 bit machines
141 @code{size_t} is 64 bits wide and so variable references are not anymore
142 possible.
143
144 A solution provides the Unix98 specification which finally introduces a
145 type @code{socklen_t}. This type is used in all of the cases that were
146 previously changed to use @code{size_t}. The only requirement of this
147 type is that it is an unsigned type of at least 32 bits. Therefore,
148 implementations which require references to 32 bit variables be passed
149 can be as happy as implementations which use right from the start 64 bit
150 values.
151
152
153 @node Communication Styles
154 @section Communication Styles
155
156 The GNU library includes support for several different kinds of sockets,
157 each with different characteristics. This section describes the
158 supported socket types. The symbolic constants listed here are
159 defined in @file{sys/socket.h}.
160 @pindex sys/socket.h
161
162 @comment sys/socket.h
163 @comment BSD
164 @deftypevr Macro int SOCK_STREAM
165 The @code{SOCK_STREAM} style is like a pipe (@pxref{Pipes and FIFOs});
166 it operates over a connection with a particular remote socket, and
167 transmits data reliably as a stream of bytes.
168
169 Use of this style is covered in detail in @ref{Connections}.
170 @end deftypevr
171
172 @comment sys/socket.h
173 @comment BSD
174 @deftypevr Macro int SOCK_DGRAM
175 The @code{SOCK_DGRAM} style is used for sending
176 individually-addressed packets, unreliably.
177 It is the diametrical opposite of @code{SOCK_STREAM}.
178
179 Each time you write data to a socket of this kind, that data becomes
180 one packet. Since @code{SOCK_DGRAM} sockets do not have connections,
181 you must specify the recipient address with each packet.
182
183 The only guarantee that the system makes about your requests to
184 transmit data is that it will try its best to deliver each packet you
185 send. It may succeed with the sixth packet after failing with the
186 fourth and fifth packets; the seventh packet may arrive before the
187 sixth, and may arrive a second time after the sixth.
188
189 The typical use for @code{SOCK_DGRAM} is in situations where it is
190 acceptable to simply resend a packet if no response is seen in a
191 reasonable amount of time.
192
193 @xref{Datagrams}, for detailed information about how to use datagram
194 sockets.
195 @end deftypevr
196
197 @ignore
198 @c This appears to be only for the NS domain, which we aren't
199 @c discussing and probably won't support either.
200 @comment sys/socket.h
201 @comment BSD
202 @deftypevr Macro int SOCK_SEQPACKET
203 This style is like @code{SOCK_STREAM} except that the data is
204 structured into packets.
205
206 A program that receives data over a @code{SOCK_SEQPACKET} socket
207 should be prepared to read the entire message packet in a single call
208 to @code{read}; if it only reads part of the message, the remainder of
209 the message is simply discarded instead of being available for
210 subsequent calls to @code{read}.
211
212 Many protocols do not support this communication style.
213 @end deftypevr
214 @end ignore
215
216 @ignore
217 @comment sys/socket.h
218 @comment BSD
219 @deftypevr Macro int SOCK_RDM
220 This style is a reliable version of @code{SOCK_DGRAM}: it sends
221 individually addressed packets, but guarantees that each packet sent
222 arrives exactly once.
223
224 @strong{Warning:} It is not clear this is actually supported
225 by any operating system.
226 @end deftypevr
227 @end ignore
228
229 @comment sys/socket.h
230 @comment BSD
231 @deftypevr Macro int SOCK_RAW
232 This style provides access to low-level network protocols and
233 interfaces. Ordinary user programs usually have no need to use this
234 style.
235 @end deftypevr
236
237 @node Socket Addresses
238 @section Socket Addresses
239
240 @cindex address of socket
241 @cindex name of socket
242 @cindex binding a socket address
243 @cindex socket address (name) binding
244 The name of a socket is normally called an @dfn{address}. The
245 functions and symbols for dealing with socket addresses were named
246 inconsistently, sometimes using the term ``name'' and sometimes using
247 ``address''. You can regard these terms as synonymous where sockets
248 are concerned.
249
250 A socket newly created with the @code{socket} function has no
251 address. Other processes can find it for communication only if you
252 give it an address. We call this @dfn{binding} the address to the
253 socket, and the way to do it is with the @code{bind} function.
254
255 You need be concerned with the address of a socket if other processes
256 are to find it and start communicating with it. You can specify an
257 address for other sockets, but this is usually pointless; the first time
258 you send data from a socket, or use it to initiate a connection, the
259 system assigns an address automatically if you have not specified one.
260
261 Occasionally a client needs to specify an address because the server
262 discriminates based on addresses; for example, the rsh and rlogin
263 protocols look at the client's socket address and don't bypass password
264 checking unless it is less than @code{IPPORT_RESERVED} (@pxref{Ports}).
265
266 The details of socket addresses vary depending on what namespace you are
267 using. @xref{File Namespace}, or @ref{Internet Namespace}, for specific
268 information.
269
270 Regardless of the namespace, you use the same functions @code{bind} and
271 @code{getsockname} to set and examine a socket's address. These
272 functions use a phony data type, @code{struct sockaddr *}, to accept the
273 address. In practice, the address lives in a structure of some other
274 data type appropriate to the address format you are using, but you cast
275 its address to @code{struct sockaddr *} when you pass it to
276 @code{bind}.
277
278 @menu
279 * Address Formats:: About @code{struct sockaddr}.
280 * Setting Address:: Binding an address to a socket.
281 * Reading Address:: Reading the address of a socket.
282 @end menu
283
284 @node Address Formats
285 @subsection Address Formats
286
287 The functions @code{bind} and @code{getsockname} use the generic data
288 type @code{struct sockaddr *} to represent a pointer to a socket
289 address. You can't use this data type effectively to interpret an
290 address or construct one; for that, you must use the proper data type
291 for the socket's namespace.
292
293 Thus, the usual practice is to construct an address in the proper
294 namespace-specific type, then cast a pointer to @code{struct sockaddr *}
295 when you call @code{bind} or @code{getsockname}.
296
297 The one piece of information that you can get from the @code{struct
298 sockaddr} data type is the @dfn{address format} designator which tells
299 you which data type to use to understand the address fully.
300
301 @pindex sys/socket.h
302 The symbols in this section are defined in the header file
303 @file{sys/socket.h}.
304
305 @comment sys/socket.h
306 @comment BSD
307 @deftp {Date Type} {struct sockaddr}
308 The @code{struct sockaddr} type itself has the following members:
309
310 @table @code
311 @item short int sa_family
312 This is the code for the address format of this address. It
313 identifies the format of the data which follows.
314
315 @item char sa_data[14]
316 This is the actual socket address data, which is format-dependent. Its
317 length also depends on the format, and may well be more than 14. The
318 length 14 of @code{sa_data} is essentially arbitrary.
319 @end table
320 @end deftp
321
322 Each address format has a symbolic name which starts with @samp{AF_}.
323 Each of them corresponds to a @samp{PF_} symbol which designates the
324 corresponding namespace. Here is a list of address format names:
325
326 @table @code
327 @comment sys/socket.h
328 @comment GNU
329 @item AF_FILE
330 @vindex AF_FILE
331 This designates the address format that goes with the file namespace.
332 (@code{PF_FILE} is the name of that namespace.) @xref{File Namespace
333 Details}, for information about this address format.
334
335 @comment sys/socket.h
336 @comment BSD
337 @item AF_UNIX
338 @vindex AF_UNIX
339 This is a synonym for @code{AF_FILE}, for compatibility.
340 (@code{PF_UNIX} is likewise a synonym for @code{PF_FILE}.)
341
342 @comment sys/socket.h
343 @comment BSD
344 @item AF_INET
345 @vindex AF_INET
346 This designates the address format that goes with the Internet
347 namespace. (@code{PF_INET} is the name of that namespace.)
348 @xref{Internet Address Formats}.
349
350 @comment sys/socket.h
351 @comment IPv6 Basic API
352 @item AF_INET6
353 This is similar to @code{AF_INET}, but refers to the IPv6 protocol.
354 (@code{PF_INET6} is the name of the corresponding namespace.)
355
356 @comment sys/socket.h
357 @comment BSD
358 @item AF_UNSPEC
359 @vindex AF_UNSPEC
360 This designates no particular address format. It is used only in rare
361 cases, such as to clear out the default destination address of a
362 ``connected'' datagram socket. @xref{Sending Datagrams}.
363
364 The corresponding namespace designator symbol @code{PF_UNSPEC} exists
365 for completeness, but there is no reason to use it in a program.
366 @end table
367
368 @file{sys/socket.h} defines symbols starting with @samp{AF_} for many
369 different kinds of networks, all or most of which are not actually
370 implemented. We will document those that really work, as we receive
371 information about how to use them.
372
373 @node Setting Address
374 @subsection Setting the Address of a Socket
375
376 @pindex sys/socket.h
377 Use the @code{bind} function to assign an address to a socket. The
378 prototype for @code{bind} is in the header file @file{sys/socket.h}.
379 For examples of use, see @ref{File Namespace}, or see @ref{Inet Example}.
380
381 @comment sys/socket.h
382 @comment BSD
383 @deftypefun int bind (int @var{socket}, struct sockaddr *@var{addr}, socklen_t @var{length})
384 The @code{bind} function assigns an address to the socket
385 @var{socket}. The @var{addr} and @var{length} arguments specify the
386 address; the detailed format of the address depends on the namespace.
387 The first part of the address is always the format designator, which
388 specifies a namespace, and says that the address is in the format for
389 that namespace.
390
391 The return value is @code{0} on success and @code{-1} on failure. The
392 following @code{errno} error conditions are defined for this function:
393
394 @table @code
395 @item EBADF
396 The @var{socket} argument is not a valid file descriptor.
397
398 @item ENOTSOCK
399 The descriptor @var{socket} is not a socket.
400
401 @item EADDRNOTAVAIL
402 The specified address is not available on this machine.
403
404 @item EADDRINUSE
405 Some other socket is already using the specified address.
406
407 @item EINVAL
408 The socket @var{socket} already has an address.
409
410 @item EACCES
411 You do not have permission to access the requested address. (In the
412 Internet domain, only the super-user is allowed to specify a port number
413 in the range 0 through @code{IPPORT_RESERVED} minus one; see
414 @ref{Ports}.)
415 @end table
416
417 Additional conditions may be possible depending on the particular namespace
418 of the socket.
419 @end deftypefun
420
421 @node Reading Address
422 @subsection Reading the Address of a Socket
423
424 @pindex sys/socket.h
425 Use the function @code{getsockname} to examine the address of an
426 Internet socket. The prototype for this function is in the header file
427 @file{sys/socket.h}.
428
429 @comment sys/socket.h
430 @comment BSD
431 @deftypefun int getsockname (int @var{socket}, struct sockaddr *@var{addr}, socklen_t *@var{length-ptr})
432 The @code{getsockname} function returns information about the
433 address of the socket @var{socket} in the locations specified by the
434 @var{addr} and @var{length-ptr} arguments. Note that the
435 @var{length-ptr} is a pointer; you should initialize it to be the
436 allocation size of @var{addr}, and on return it contains the actual
437 size of the address data.
438
439 The format of the address data depends on the socket namespace. The
440 length of the information is usually fixed for a given namespace, so
441 normally you can know exactly how much space is needed and can provide
442 that much. The usual practice is to allocate a place for the value
443 using the proper data type for the socket's namespace, then cast its
444 address to @code{struct sockaddr *} to pass it to @code{getsockname}.
445
446 The return value is @code{0} on success and @code{-1} on error. The
447 following @code{errno} error conditions are defined for this function:
448
449 @table @code
450 @item EBADF
451 The @var{socket} argument is not a valid file descriptor.
452
453 @item ENOTSOCK
454 The descriptor @var{socket} is not a socket.
455
456 @item ENOBUFS
457 There are not enough internal buffers available for the operation.
458 @end table
459 @end deftypefun
460
461 You can't read the address of a socket in the file namespace. This is
462 consistent with the rest of the system; in general, there's no way to
463 find a file's name from a descriptor for that file.
464
465 @node File Namespace
466 @section The File Namespace
467 @cindex file namespace, for sockets
468
469 This section describes the details of the file namespace, whose
470 symbolic name (required when you create a socket) is @code{PF_FILE}.
471
472 @menu
473 * Concepts: File Namespace Concepts. What you need to understand.
474 * Details: File Namespace Details. Address format, symbolic names, etc.
475 * Example: File Socket Example. Example of creating a socket.
476 @end menu
477
478 @node File Namespace Concepts
479 @subsection File Namespace Concepts
480
481 In the file namespace, socket addresses are file names. You can specify
482 any file name you want as the address of the socket, but you must have
483 write permission on the directory containing it. In order to connect to
484 a socket, you must have read permission for it. It's common to put
485 these files in the @file{/tmp} directory.
486
487 One peculiarity of the file namespace is that the name is only used when
488 opening the connection; once that is over with, the address is not
489 meaningful and may not exist.
490
491 Another peculiarity is that you cannot connect to such a socket from
492 another machine--not even if the other machine shares the file system
493 which contains the name of the socket. You can see the socket in a
494 directory listing, but connecting to it never succeeds. Some programs
495 take advantage of this, such as by asking the client to send its own
496 process ID, and using the process IDs to distinguish between clients.
497 However, we recommend you not use this method in protocols you design,
498 as we might someday permit connections from other machines that mount
499 the same file systems. Instead, send each new client an identifying
500 number if you want it to have one.
501
502 After you close a socket in the file namespace, you should delete the
503 file name from the file system. Use @code{unlink} or @code{remove} to
504 do this; see @ref{Deleting Files}.
505
506 The file namespace supports just one protocol for any communication
507 style; it is protocol number @code{0}.
508
509 @node File Namespace Details
510 @subsection Details of File Namespace
511
512 @pindex sys/socket.h
513 To create a socket in the file namespace, use the constant
514 @code{PF_FILE} as the @var{namespace} argument to @code{socket} or
515 @code{socketpair}. This constant is defined in @file{sys/socket.h}.
516
517 @comment sys/socket.h
518 @comment GNU
519 @deftypevr Macro int PF_FILE
520 This designates the file namespace, in which socket addresses are file
521 names, and its associated family of protocols.
522 @end deftypevr
523
524 @comment sys/socket.h
525 @comment BSD
526 @deftypevr Macro int PF_UNIX
527 This is a synonym for @code{PF_FILE}, for compatibility's sake.
528 @end deftypevr
529
530 The structure for specifying socket names in the file namespace is
531 defined in the header file @file{sys/un.h}:
532 @pindex sys/un.h
533
534 @comment sys/un.h
535 @comment BSD
536 @deftp {Data Type} {struct sockaddr_un}
537 This structure is used to specify file namespace socket addresses. It has
538 the following members:
539
540 @table @code
541 @item short int sun_family
542 This identifies the address family or format of the socket address.
543 You should store the value @code{AF_FILE} to designate the file
544 namespace. @xref{Socket Addresses}.
545
546 @item char sun_path[108]
547 This is the file name to use.
548
549 @strong{Incomplete:} Why is 108 a magic number? RMS suggests making
550 this a zero-length array and tweaking the example following to use
551 @code{alloca} to allocate an appropriate amount of storage based on
552 the length of the filename.
553 @end table
554 @end deftp
555
556 You should compute the @var{length} parameter for a socket address in
557 the file namespace as the sum of the size of the @code{sun_family}
558 component and the string length (@emph{not} the allocation size!) of
559 the file name string.
560
561 @node File Socket Example
562 @subsection Example of File-Namespace Sockets
563
564 Here is an example showing how to create and name a socket in the file
565 namespace.
566
567 @smallexample
568 @include mkfsock.c.texi
569 @end smallexample
570
571 @node Internet Namespace
572 @section The Internet Namespace
573 @cindex Internet namespace, for sockets
574
575 This section describes the details the protocols and socket naming
576 conventions used in the Internet namespace.
577
578 To create a socket in the Internet namespace, use the symbolic name
579 @code{PF_INET} of this namespace as the @var{namespace} argument to
580 @code{socket} or @code{socketpair}. This macro is defined in
581 @file{sys/socket.h}.
582 @pindex sys/socket.h
583
584 @comment sys/socket.h
585 @comment BSD
586 @deftypevr Macro int PF_INET
587 This designates the Internet namespace and associated family of
588 protocols.
589 @end deftypevr
590
591 A socket address for the Internet namespace includes the following components:
592
593 @itemize @bullet
594 @item
595 The address of the machine you want to connect to. Internet addresses
596 can be specified in several ways; these are discussed in @ref{Internet
597 Address Formats}, @ref{Host Addresses}, and @ref{Host Names}.
598
599 @item
600 A port number for that machine. @xref{Ports}.
601 @end itemize
602
603 You must ensure that the address and port number are represented in a
604 canonical format called @dfn{network byte order}. @xref{Byte Order},
605 for information about this.
606
607 @menu
608 * Internet Address Formats:: How socket addresses are specified in the
609 Internet namespace.
610 * Host Addresses:: All about host addresses of internet host.
611 * Protocols Database:: Referring to protocols by name.
612 * Ports:: Internet port numbers.
613 * Services Database:: Ports may have symbolic names.
614 * Byte Order:: Different hosts may use different byte
615 ordering conventions; you need to
616 canonicalize host address and port number.
617 * Inet Example:: Putting it all together.
618 @end menu
619
620 @node Internet Address Formats
621 @subsection Internet Socket Address Formats
622
623 In the Internet namespace, for both IPv4 (@code{AF_INET}) and IPv6
624 (@code{AF_INET6}), a socket address consists of a host address
625 and a port on that host. In addition, the protocol you choose serves
626 effectively as a part of the address because local port numbers are
627 meaningful only within a particular protocol.
628
629 The data types for representing socket addresses in the Internet namespace
630 are defined in the header file @file{netinet/in.h}.
631 @pindex netinet/in.h
632
633 @comment netinet/in.h
634 @comment BSD
635 @deftp {Data Type} {struct sockaddr_in}
636 This is the data type used to represent socket addresses in the
637 Internet namespace. It has the following members:
638
639 @table @code
640 @item short int sin_family
641 This identifies the address family or format of the socket address.
642 You should store the value of @code{AF_INET} in this member.
643 @xref{Socket Addresses}.
644
645 @item struct in_addr sin_addr
646 This is the Internet address of the host machine. @xref{Host
647 Addresses}, and @ref{Host Names}, for how to get a value to store
648 here.
649
650 @item unsigned short int sin_port
651 This is the port number. @xref{Ports}.
652 @end table
653 @end deftp
654
655 When you call @code{bind} or @code{getsockname}, you should specify
656 @code{sizeof (struct sockaddr_in)} as the @var{length} parameter if
657 you are using an Internet namespace socket address.
658
659 @deftp {Data Type} {struct sockaddr_in6}
660 This is the data type used to represent socket addresses in the IPv6
661 namespace. It has the following members:
662
663 @table @code
664 @item short int sin6_family
665 This identifies the address family or format of the socket address.
666 You should store the value of @code{AF_INET6} in this member.
667 @xref{Socket Addresses}.
668
669 @item struct in6_addr sin6_addr
670 This is the IPv6 address of the host machine. @xref{Host
671 Addresses}, and @ref{Host Names}, for how to get a value to store
672 here.
673
674 @item uint32_t sin6_flowinfo
675 This is a currently unimplemented field.
676
677 @item uint16_t sin6_port
678 This is the port number. @xref{Ports}.
679
680 @end table
681 @end deftp
682
683 @node Host Addresses
684 @subsection Host Addresses
685
686 Each computer on the Internet has one or more @dfn{Internet addresses},
687 numbers which identify that computer among all those on the Internet.
688 Users typically write IPv4 numeric host addresses as sequences of four
689 numbers, separated by periods, as in @samp{128.52.46.32}, and IPv6
690 numeric host addresses as sequences of up to eight numbers separated by
691 colons, as in @samp{5f03:1200:836f:c100::1}.
692
693 Each computer also has one or more @dfn{host names}, which are strings
694 of words separated by periods, as in @samp{churchy.gnu.ai.mit.edu}.
695
696 Programs that let the user specify a host typically accept both numeric
697 addresses and host names. But the program needs a numeric address to
698 open a connection; to use a host name, you must convert it to the
699 numeric address it stands for.
700
701 @menu
702 * Abstract Host Addresses:: What a host number consists of.
703 * Data type: Host Address Data Type. Data type for a host number.
704 * Functions: Host Address Functions. Functions to operate on them.
705 * Names: Host Names. Translating host names to host numbers.
706 @end menu
707
708 @node Abstract Host Addresses
709 @subsubsection Internet Host Addresses
710 @cindex host address, Internet
711 @cindex Internet host address
712
713 @ifinfo
714 Each computer on the Internet has one or more Internet addresses,
715 numbers which identify that computer among all those on the Internet.
716 @end ifinfo
717
718 @c I think this whole section could possibly be removed. It is slightly
719 @c misleading these days.
720
721 @cindex network number
722 @cindex local network address number
723 An Internet host address is a number containing four bytes of data.
724 These are divided into two parts, a @dfn{network number} and a
725 @dfn{local network address number} within that network. The network
726 number consists of the first one, two or three bytes; the rest of the
727 bytes are the local address.
728
729 Network numbers are registered with the Network Information Center
730 (NIC), and are divided into three classes---A, B, and C. The local
731 network address numbers of individual machines are registered with the
732 administrator of the particular network.
733
734 Class A networks have single-byte numbers in the range 0 to 127. There
735 are only a small number of Class A networks, but they can each support a
736 very large number of hosts. Medium-sized Class B networks have two-byte
737 network numbers, with the first byte in the range 128 to 191. Class C
738 networks are the smallest; they have three-byte network numbers, with
739 the first byte in the range 192-255. Thus, the first 1, 2, or 3 bytes
740 of an Internet address specifies a network. The remaining bytes of the
741 Internet address specify the address within that network.
742
743 The Class A network 0 is reserved for broadcast to all networks. In
744 addition, the host number 0 within each network is reserved for broadcast
745 to all hosts in that network.
746
747 The Class A network 127 is reserved for loopback; you can always use
748 the Internet address @samp{127.0.0.1} to refer to the host machine.
749
750 Since a single machine can be a member of multiple networks, it can
751 have multiple Internet host addresses. However, there is never
752 supposed to be more than one machine with the same host address.
753
754 @c !!! this section could document the IN_CLASS* macros in <netinet/in.h>.
755
756 @cindex standard dot notation, for Internet addresses
757 @cindex dot notation, for Internet addresses
758 There are four forms of the @dfn{standard numbers-and-dots notation}
759 for Internet addresses:
760
761 @table @code
762 @item @var{a}.@var{b}.@var{c}.@var{d}
763 This specifies all four bytes of the address individually.
764
765 @item @var{a}.@var{b}.@var{c}
766 The last part of the address, @var{c}, is interpreted as a 2-byte quantity.
767 This is useful for specifying host addresses in a Class B network with
768 network address number @code{@var{a}.@var{b}}.
769
770 @item @var{a}.@var{b}
771 The last part of the address, @var{c}, is interpreted as a 3-byte quantity.
772 This is useful for specifying host addresses in a Class A network with
773 network address number @var{a}.
774
775 @item @var{a}
776 If only one part is given, this corresponds directly to the host address
777 number.
778 @end table
779
780 Within each part of the address, the usual C conventions for specifying
781 the radix apply. In other words, a leading @samp{0x} or @samp{0X} implies
782 hexadecimal radix; a leading @samp{0} implies octal; and otherwise decimal
783 radix is assumed.
784
785 @node Host Address Data Type
786 @subsubsection Host Address Data Type
787
788 Internet host addresses are represented in some contexts as integers
789 (type @code{unsigned long int}). In other contexts, the integer is
790 packaged inside a structure of type @code{struct in_addr}. It would
791 be better if the usage were made consistent, but it is not hard to extract
792 the integer from the structure or put the integer into a structure.
793
794 The following basic definitions for Internet addresses appear in the
795 header file @file{netinet/in.h}:
796 @pindex netinet/in.h
797
798 @comment netinet/in.h
799 @comment BSD
800 @deftp {Data Type} {struct in_addr}
801 This data type is used in certain contexts to contain an Internet host
802 address. It has just one field, named @code{s_addr}, which records the
803 host address number as an @code{unsigned long int}.
804 @end deftp
805
806 @comment netinet/in.h
807 @comment BSD
808 @deftypevr Macro {unsigned int} INADDR_LOOPBACK
809 You can use this constant to stand for ``the address of this machine,''
810 instead of finding its actual address. It is the Internet address
811 @samp{127.0.0.1}, which is usually called @samp{localhost}. This
812 special constant saves you the trouble of looking up the address of your
813 own machine. Also, the system usually implements @code{INADDR_LOOPBACK}
814 specially, avoiding any network traffic for the case of one machine
815 talking to itself.
816 @end deftypevr
817
818 @comment netinet/in.h
819 @comment BSD
820 @deftypevr Macro {unsigned int} INADDR_ANY
821 You can use this constant to stand for ``any incoming address,'' when
822 binding to an address. @xref{Setting Address}. This is the usual
823 address to give in the @code{sin_addr} member of @w{@code{struct
824 sockaddr_in}} when you want to accept Internet connections.
825 @end deftypevr
826
827 @comment netinet/in.h
828 @comment BSD
829 @deftypevr Macro {unsigned int} INADDR_BROADCAST
830 This constant is the address you use to send a broadcast message.
831 @c !!! broadcast needs further documented
832 @end deftypevr
833
834 @comment netinet/in.h
835 @comment BSD
836 @deftypevr Macro {unsigned int} INADDR_NONE
837 This constant is returned by some functions to indicate an error.
838 @end deftypevr
839
840 @comment netinet/in.h
841 @comment IPv6 basic API
842 @deftp {Data Type} {struct in6_addr}
843 This data type is used to store an IPv6 address. It stores 128 bits of
844 data, which can be accessed (via a union) in a variety of ways.
845 @end deftp
846
847 @comment netinet/in.h
848 @comment IPv6 basic API
849 @deftypevr Constant {struct in6_addr} in6addr_loopback.
850 This constant is the IPv6 address @samp{::1}, the loopback address. See
851 above for a description of what this means. The macro
852 @code{IN6ADDR_LOOPBACK_INIT} is provided to allow you to initialise your
853 own variables to this value.
854 @end deftypevr
855
856 @comment netinet/in.h
857 @comment IPv6 basic API
858 @deftypevr Constant {struct in6_addr} in6addr_any
859 This constant is the IPv6 address @samp{::}, the unspecified address. See
860 above for a description of what this means. The macro
861 @code{IN6ADDR_ANY_INIT} is provided to allow you to initialise your
862 own variables to this value.
863 @end deftypevr
864
865 @node Host Address Functions
866 @subsubsection Host Address Functions
867
868 @pindex arpa/inet.h
869 These additional functions for manipulating Internet addresses are
870 declared in @file{arpa/inet.h}. They represent Internet addresses in
871 network byte order; they represent network numbers and
872 local-address-within-network numbers in host byte order.
873 @xref{Byte Order}, for an explanation of network and host byte order.
874
875 @comment arpa/inet.h
876 @comment BSD
877 @deftypefun int inet_aton (const char *@var{name}, struct in_addr *@var{addr})
878 This function converts the Internet host address @var{name}
879 from the standard numbers-and-dots notation into binary data and stores
880 it in the @code{struct in_addr} that @var{addr} points to.
881 @code{inet_aton} returns nonzero if the address is valid, zero if not.
882 @end deftypefun
883
884 @comment arpa/inet.h
885 @comment BSD
886 @deftypefun {unsigned long int} inet_addr (const char *@var{name})
887 This function converts the Internet host address @var{name} from the
888 standard numbers-and-dots notation into binary data. If the input is
889 not valid, @code{inet_addr} returns @code{INADDR_NONE}. This is an
890 obsolete interface to @code{inet_aton}, described immediately above; it
891 is obsolete because @code{INADDR_NONE} is a valid address
892 (255.255.255.255), and @code{inet_aton} provides a cleaner way to
893 indicate error return.
894 @end deftypefun
895
896 @comment arpa/inet.h
897 @comment BSD
898 @deftypefun {unsigned long int} inet_network (const char *@var{name})
899 This function extracts the network number from the address @var{name},
900 given in the standard numbers-and-dots notation. The returned address is
901 in host order. If the input is not valid, @code{inet_network} returns
902 @code{-1}.
903 @end deftypefun
904
905 @comment arpa/inet.h
906 @comment BSD
907 @deftypefun {char *} inet_ntoa (struct in_addr @var{addr})
908 This function converts the Internet host address @var{addr} to a
909 string in the standard numbers-and-dots notation. The return value is
910 a pointer into a statically-allocated buffer. Subsequent calls will
911 overwrite the same buffer, so you should copy the string if you need
912 to save it.
913
914 In multi-threaded programs each thread has an own statically-allocated
915 buffer. But still subsequent calls of @code{inet_ntoa} in the same
916 thread will overwrite the result of the last call.
917 @end deftypefun
918
919 @comment arpa/inet.h
920 @comment BSD
921 @deftypefun {struct in_addr} inet_makeaddr (int @var{net}, int @var{local})
922 This function makes an Internet host address by combining the network
923 number @var{net} with the local-address-within-network number
924 @var{local}.
925 @end deftypefun
926
927 @comment arpa/inet.h
928 @comment BSD
929 @deftypefun int inet_lnaof (struct in_addr @var{addr})
930 This function returns the local-address-within-network part of the
931 Internet host address @var{addr}.
932 @end deftypefun
933
934 @comment arpa/inet.h
935 @comment BSD
936 @deftypefun int inet_netof (struct in_addr @var{addr})
937 This function returns the network number part of the Internet host
938 address @var{addr}.
939 @end deftypefun
940
941 @comment arpa/inet.h
942 @comment IPv6 basic API
943 @deftypefun int inet_pton (int @var{af}, const char *@var{cp}, void *@var{buf})
944 This function converts an Internet address (either IPv4 or IPv6) from
945 presentation (textual) to network (binary) format. @var{af} should be
946 either @code{AF_INET} or @code{AF_INET6}, as appropriate for the type of
947 address being converted. @var{cp} is a pointer to the input string, and
948 @var{buf} is a pointer to a buffer for the result. It is the caller's
949 responsibility to make sure the buffer is large enough.
950 @end deftypefun
951
952 @comment arpa/inet.h
953 @comment IPv6 basic API
954 @deftypefun {char *} inet_ntop (int @var{af}, const void *@var{cp}, char *@var{buf}, size_t @var{len})
955 This function converts an Internet address (either IPv4 or IPv6) from
956 network (binary) to presentation (textual) form. @var{af} should be
957 either @code{AF_INET} or @code{AF_INET6}, as appropriate. @var{cp} is a
958 pointer to the address to be converted. @var{buf} should be a pointer
959 to a buffer to hold the result, and @var{len} is the length of this
960 buffer. The return value from the function will be this buffer address.
961 @end deftypefun
962
963 @node Host Names
964 @subsubsection Host Names
965 @cindex hosts database
966 @cindex converting host name to address
967 @cindex converting host address to name
968
969 Besides the standard numbers-and-dots notation for Internet addresses,
970 you can also refer to a host by a symbolic name. The advantage of a
971 symbolic name is that it is usually easier to remember. For example,
972 the machine with Internet address @samp{128.52.46.32} is also known as
973 @samp{churchy.gnu.ai.mit.edu}; and other machines in the @samp{gnu.ai.mit.edu}
974 domain can refer to it simply as @samp{churchy}.
975
976 @pindex /etc/hosts
977 @pindex netdb.h
978 Internally, the system uses a database to keep track of the mapping
979 between host names and host numbers. This database is usually either
980 the file @file{/etc/hosts} or an equivalent provided by a name server.
981 The functions and other symbols for accessing this database are declared
982 in @file{netdb.h}. They are BSD features, defined unconditionally if
983 you include @file{netdb.h}.
984
985 @comment netdb.h
986 @comment BSD
987 @deftp {Data Type} {struct hostent}
988 This data type is used to represent an entry in the hosts database. It
989 has the following members:
990
991 @table @code
992 @item char *h_name
993 This is the ``official'' name of the host.
994
995 @item char **h_aliases
996 These are alternative names for the host, represented as a null-terminated
997 vector of strings.
998
999 @item int h_addrtype
1000 This is the host address type; in practice, its value is always either
1001 @code{AF_INET} or @code{AF_INET6}, with the latter being used for IPv6
1002 hosts. In principle other kinds of addresses could be represented in
1003 the data base as well as Internet addresses; if this were done, you
1004 might find a value in this field other than @code{AF_INET} or
1005 @code{AF_INET6}. @xref{Socket Addresses}.
1006
1007 @item int h_length
1008 This is the length, in bytes, of each address.
1009
1010 @item char **h_addr_list
1011 This is the vector of addresses for the host. (Recall that the host
1012 might be connected to multiple networks and have different addresses on
1013 each one.) The vector is terminated by a null pointer.
1014
1015 @item char *h_addr
1016 This is a synonym for @code{h_addr_list[0]}; in other words, it is the
1017 first host address.
1018 @end table
1019 @end deftp
1020
1021 As far as the host database is concerned, each address is just a block
1022 of memory @code{h_length} bytes long. But in other contexts there is an
1023 implicit assumption that you can convert this to a @code{struct in_addr} or
1024 an @code{unsigned long int}. Host addresses in a @code{struct hostent}
1025 structure are always given in network byte order; see @ref{Byte Order}.
1026
1027 You can use @code{gethostbyname}, @code{gethostbyname2} or
1028 @code{gethostbyaddr} to search the hosts database for information about
1029 a particular host. The information is returned in a
1030 statically-allocated structure; you must copy the information if you
1031 need to save it across calls. You can also use @code{getaddrinfo} and
1032 @code{getnameinfo} to obtain this information.
1033
1034 @comment netdb.h
1035 @comment BSD
1036 @deftypefun {struct hostent *} gethostbyname (const char *@var{name})
1037 The @code{gethostbyname} function returns information about the host
1038 named @var{name}. If the lookup fails, it returns a null pointer.
1039 @end deftypefun
1040
1041 @comment netdb.h
1042 @comment IPv6 Basic API
1043 @deftypefun {struct hostent *} gethostbyname2 (const char *@var{name}, int @var{af})
1044 The @code{gethostbyname2} function is like @code{gethostbyname}, but
1045 allows the caller to specify the desired address family (e.g.@:
1046 @code{AF_INET} or @code{AF_INET6}) for the result.
1047 @end deftypefun
1048
1049 @comment netdb.h
1050 @comment BSD
1051 @deftypefun {struct hostent *} gethostbyaddr (const char *@var{addr}, int @var{length}, int @var{format})
1052 The @code{gethostbyaddr} function returns information about the host
1053 with Internet address @var{addr}. The @var{length} argument is the
1054 size (in bytes) of the address at @var{addr}. @var{format} specifies
1055 the address format; for an Internet address, specify a value of
1056 @code{AF_INET}.
1057
1058 If the lookup fails, @code{gethostbyaddr} returns a null pointer.
1059 @end deftypefun
1060
1061 @vindex h_errno
1062 If the name lookup by @code{gethostbyname} or @code{gethostbyaddr}
1063 fails, you can find out the reason by looking at the value of the
1064 variable @code{h_errno}. (It would be cleaner design for these
1065 functions to set @code{errno}, but use of @code{h_errno} is compatible
1066 with other systems.) Before using @code{h_errno}, you must declare it
1067 like this:
1068
1069 @smallexample
1070 extern int h_errno;
1071 @end smallexample
1072
1073 Here are the error codes that you may find in @code{h_errno}:
1074
1075 @table @code
1076 @comment netdb.h
1077 @comment BSD
1078 @item HOST_NOT_FOUND
1079 @vindex HOST_NOT_FOUND
1080 No such host is known in the data base.
1081
1082 @comment netdb.h
1083 @comment BSD
1084 @item TRY_AGAIN
1085 @vindex TRY_AGAIN
1086 This condition happens when the name server could not be contacted. If
1087 you try again later, you may succeed then.
1088
1089 @comment netdb.h
1090 @comment BSD
1091 @item NO_RECOVERY
1092 @vindex NO_RECOVERY
1093 A non-recoverable error occurred.
1094
1095 @comment netdb.h
1096 @comment BSD
1097 @item NO_ADDRESS
1098 @vindex NO_ADDRESS
1099 The host database contains an entry for the name, but it doesn't have an
1100 associated Internet address.
1101 @end table
1102
1103 You can also scan the entire hosts database one entry at a time using
1104 @code{sethostent}, @code{gethostent}, and @code{endhostent}. Be careful
1105 in using these functions, because they are not reentrant.
1106
1107 @comment netdb.h
1108 @comment BSD
1109 @deftypefun void sethostent (int @var{stayopen})
1110 This function opens the hosts database to begin scanning it. You can
1111 then call @code{gethostent} to read the entries.
1112
1113 @c There was a rumor that this flag has different meaning if using the DNS,
1114 @c but it appears this description is accurate in that case also.
1115 If the @var{stayopen} argument is nonzero, this sets a flag so that
1116 subsequent calls to @code{gethostbyname} or @code{gethostbyaddr} will
1117 not close the database (as they usually would). This makes for more
1118 efficiency if you call those functions several times, by avoiding
1119 reopening the database for each call.
1120 @end deftypefun
1121
1122 @comment netdb.h
1123 @comment BSD
1124 @deftypefun {struct hostent *} gethostent ()
1125 This function returns the next entry in the hosts database. It
1126 returns a null pointer if there are no more entries.
1127 @end deftypefun
1128
1129 @comment netdb.h
1130 @comment BSD
1131 @deftypefun void endhostent ()
1132 This function closes the hosts database.
1133 @end deftypefun
1134
1135 @node Ports
1136 @subsection Internet Ports
1137 @cindex port number
1138
1139 A socket address in the Internet namespace consists of a machine's
1140 Internet address plus a @dfn{port number} which distinguishes the
1141 sockets on a given machine (for a given protocol). Port numbers range
1142 from 0 to 65,535.
1143
1144 Port numbers less than @code{IPPORT_RESERVED} are reserved for standard
1145 servers, such as @code{finger} and @code{telnet}. There is a database
1146 that keeps track of these, and you can use the @code{getservbyname}
1147 function to map a service name onto a port number; see @ref{Services
1148 Database}.
1149
1150 If you write a server that is not one of the standard ones defined in
1151 the database, you must choose a port number for it. Use a number
1152 greater than @code{IPPORT_USERRESERVED}; such numbers are reserved for
1153 servers and won't ever be generated automatically by the system.
1154 Avoiding conflicts with servers being run by other users is up to you.
1155
1156 When you use a socket without specifying its address, the system
1157 generates a port number for it. This number is between
1158 @code{IPPORT_RESERVED} and @code{IPPORT_USERRESERVED}.
1159
1160 On the Internet, it is actually legitimate to have two different
1161 sockets with the same port number, as long as they never both try to
1162 communicate with the same socket address (host address plus port
1163 number). You shouldn't duplicate a port number except in special
1164 circumstances where a higher-level protocol requires it. Normally,
1165 the system won't let you do it; @code{bind} normally insists on
1166 distinct port numbers. To reuse a port number, you must set the
1167 socket option @code{SO_REUSEADDR}. @xref{Socket-Level Options}.
1168
1169 @pindex netinet/in.h
1170 These macros are defined in the header file @file{netinet/in.h}.
1171
1172 @comment netinet/in.h
1173 @comment BSD
1174 @deftypevr Macro int IPPORT_RESERVED
1175 Port numbers less than @code{IPPORT_RESERVED} are reserved for
1176 superuser use.
1177 @end deftypevr
1178
1179 @comment netinet/in.h
1180 @comment BSD
1181 @deftypevr Macro int IPPORT_USERRESERVED
1182 Port numbers greater than or equal to @code{IPPORT_USERRESERVED} are
1183 reserved for explicit use; they will never be allocated automatically.
1184 @end deftypevr
1185
1186 @node Services Database
1187 @subsection The Services Database
1188 @cindex services database
1189 @cindex converting service name to port number
1190 @cindex converting port number to service name
1191
1192 @pindex /etc/services
1193 The database that keeps track of ``well-known'' services is usually
1194 either the file @file{/etc/services} or an equivalent from a name server.
1195 You can use these utilities, declared in @file{netdb.h}, to access
1196 the services database.
1197 @pindex netdb.h
1198
1199 @comment netdb.h
1200 @comment BSD
1201 @deftp {Data Type} {struct servent}
1202 This data type holds information about entries from the services database.
1203 It has the following members:
1204
1205 @table @code
1206 @item char *s_name
1207 This is the ``official'' name of the service.
1208
1209 @item char **s_aliases
1210 These are alternate names for the service, represented as an array of
1211 strings. A null pointer terminates the array.
1212
1213 @item int s_port
1214 This is the port number for the service. Port numbers are given in
1215 network byte order; see @ref{Byte Order}.
1216
1217 @item char *s_proto
1218 This is the name of the protocol to use with this service.
1219 @xref{Protocols Database}.
1220 @end table
1221 @end deftp
1222
1223 To get information about a particular service, use the
1224 @code{getservbyname} or @code{getservbyport} functions. The information
1225 is returned in a statically-allocated structure; you must copy the
1226 information if you need to save it across calls.
1227
1228 @comment netdb.h
1229 @comment BSD
1230 @deftypefun {struct servent *} getservbyname (const char *@var{name}, const char *@var{proto})
1231 The @code{getservbyname} function returns information about the
1232 service named @var{name} using protocol @var{proto}. If it can't find
1233 such a service, it returns a null pointer.
1234
1235 This function is useful for servers as well as for clients; servers
1236 use it to determine which port they should listen on (@pxref{Listening}).
1237 @end deftypefun
1238
1239 @comment netdb.h
1240 @comment BSD
1241 @deftypefun {struct servent *} getservbyport (int @var{port}, const char *@var{proto})
1242 The @code{getservbyport} function returns information about the
1243 service at port @var{port} using protocol @var{proto}. If it can't
1244 find such a service, it returns a null pointer.
1245 @end deftypefun
1246
1247 You can also scan the services database using @code{setservent},
1248 @code{getservent}, and @code{endservent}. Be careful in using these
1249 functions, because they are not reentrant.
1250
1251 @comment netdb.h
1252 @comment BSD
1253 @deftypefun void setservent (int @var{stayopen})
1254 This function opens the services database to begin scanning it.
1255
1256 If the @var{stayopen} argument is nonzero, this sets a flag so that
1257 subsequent calls to @code{getservbyname} or @code{getservbyport} will
1258 not close the database (as they usually would). This makes for more
1259 efficiency if you call those functions several times, by avoiding
1260 reopening the database for each call.
1261 @end deftypefun
1262
1263 @comment netdb.h
1264 @comment BSD
1265 @deftypefun {struct servent *} getservent (void)
1266 This function returns the next entry in the services database. If
1267 there are no more entries, it returns a null pointer.
1268 @end deftypefun
1269
1270 @comment netdb.h
1271 @comment BSD
1272 @deftypefun void endservent (void)
1273 This function closes the services database.
1274 @end deftypefun
1275
1276 @node Byte Order
1277 @subsection Byte Order Conversion
1278 @cindex byte order conversion, for socket
1279 @cindex converting byte order
1280
1281 @cindex big-endian
1282 @cindex little-endian
1283 Different kinds of computers use different conventions for the
1284 ordering of bytes within a word. Some computers put the most
1285 significant byte within a word first (this is called ``big-endian''
1286 order), and others put it last (``little-endian'' order).
1287
1288 @cindex network byte order
1289 So that machines with different byte order conventions can
1290 communicate, the Internet protocols specify a canonical byte order
1291 convention for data transmitted over the network. This is known
1292 as the @dfn{network byte order}.
1293
1294 When establishing an Internet socket connection, you must make sure that
1295 the data in the @code{sin_port} and @code{sin_addr} members of the
1296 @code{sockaddr_in} structure are represented in the network byte order.
1297 If you are encoding integer data in the messages sent through the
1298 socket, you should convert this to network byte order too. If you don't
1299 do this, your program may fail when running on or talking to other kinds
1300 of machines.
1301
1302 If you use @code{getservbyname} and @code{gethostbyname} or
1303 @code{inet_addr} to get the port number and host address, the values are
1304 already in the network byte order, and you can copy them directly into
1305 the @code{sockaddr_in} structure.
1306
1307 Otherwise, you have to convert the values explicitly. Use
1308 @code{htons} and @code{ntohs} to convert values for the @code{sin_port}
1309 member. Use @code{htonl} and @code{ntohl} to convert values for the
1310 @code{sin_addr} member. (Remember, @code{struct in_addr} is equivalent
1311 to @code{unsigned long int}.) These functions are declared in
1312 @file{netinet/in.h}.
1313 @pindex netinet/in.h
1314
1315 @comment netinet/in.h
1316 @comment BSD
1317 @deftypefun {unsigned short int} htons (unsigned short int @var{hostshort})
1318 This function converts the @code{short} integer @var{hostshort} from
1319 host byte order to network byte order.
1320 @end deftypefun
1321
1322 @comment netinet/in.h
1323 @comment BSD
1324 @deftypefun {unsigned short int} ntohs (unsigned short int @var{netshort})
1325 This function converts the @code{short} integer @var{netshort} from
1326 network byte order to host byte order.
1327 @end deftypefun
1328
1329 @comment netinet/in.h
1330 @comment BSD
1331 @deftypefun {unsigned long int} htonl (unsigned long int @var{hostlong})
1332 This function converts the @code{long} integer @var{hostlong} from
1333 host byte order to network byte order.
1334 @end deftypefun
1335
1336 @comment netinet/in.h
1337 @comment BSD
1338 @deftypefun {unsigned long int} ntohl (unsigned long int @var{netlong})
1339 This function converts the @code{long} integer @var{netlong} from
1340 network byte order to host byte order.
1341 @end deftypefun
1342
1343 @node Protocols Database
1344 @subsection Protocols Database
1345 @cindex protocols database
1346
1347 The communications protocol used with a socket controls low-level
1348 details of how data is exchanged. For example, the protocol implements
1349 things like checksums to detect errors in transmissions, and routing
1350 instructions for messages. Normal user programs have little reason to
1351 mess with these details directly.
1352
1353 @cindex TCP (Internet protocol)
1354 The default communications protocol for the Internet namespace depends on
1355 the communication style. For stream communication, the default is TCP
1356 (``transmission control protocol''). For datagram communication, the
1357 default is UDP (``user datagram protocol''). For reliable datagram
1358 communication, the default is RDP (``reliable datagram protocol'').
1359 You should nearly always use the default.
1360
1361 @pindex /etc/protocols
1362 Internet protocols are generally specified by a name instead of a
1363 number. The network protocols that a host knows about are stored in a
1364 database. This is usually either derived from the file
1365 @file{/etc/protocols}, or it may be an equivalent provided by a name
1366 server. You look up the protocol number associated with a named
1367 protocol in the database using the @code{getprotobyname} function.
1368
1369 Here are detailed descriptions of the utilities for accessing the
1370 protocols database. These are declared in @file{netdb.h}.
1371 @pindex netdb.h
1372
1373 @comment netdb.h
1374 @comment BSD
1375 @deftp {Data Type} {struct protoent}
1376 This data type is used to represent entries in the network protocols
1377 database. It has the following members:
1378
1379 @table @code
1380 @item char *p_name
1381 This is the official name of the protocol.
1382
1383 @item char **p_aliases
1384 These are alternate names for the protocol, specified as an array of
1385 strings. The last element of the array is a null pointer.
1386
1387 @item int p_proto
1388 This is the protocol number (in host byte order); use this member as the
1389 @var{protocol} argument to @code{socket}.
1390 @end table
1391 @end deftp
1392
1393 You can use @code{getprotobyname} and @code{getprotobynumber} to search
1394 the protocols database for a specific protocol. The information is
1395 returned in a statically-allocated structure; you must copy the
1396 information if you need to save it across calls.
1397
1398 @comment netdb.h
1399 @comment BSD
1400 @deftypefun {struct protoent *} getprotobyname (const char *@var{name})
1401 The @code{getprotobyname} function returns information about the
1402 network protocol named @var{name}. If there is no such protocol, it
1403 returns a null pointer.
1404 @end deftypefun
1405
1406 @comment netdb.h
1407 @comment BSD
1408 @deftypefun {struct protoent *} getprotobynumber (int @var{protocol})
1409 The @code{getprotobynumber} function returns information about the
1410 network protocol with number @var{protocol}. If there is no such
1411 protocol, it returns a null pointer.
1412 @end deftypefun
1413
1414 You can also scan the whole protocols database one protocol at a time by
1415 using @code{setprotoent}, @code{getprotoent}, and @code{endprotoent}.
1416 Be careful in using these functions, because they are not reentrant.
1417
1418 @comment netdb.h
1419 @comment BSD
1420 @deftypefun void setprotoent (int @var{stayopen})
1421 This function opens the protocols database to begin scanning it.
1422
1423 If the @var{stayopen} argument is nonzero, this sets a flag so that
1424 subsequent calls to @code{getprotobyname} or @code{getprotobynumber} will
1425 not close the database (as they usually would). This makes for more
1426 efficiency if you call those functions several times, by avoiding
1427 reopening the database for each call.
1428 @end deftypefun
1429
1430 @comment netdb.h
1431 @comment BSD
1432 @deftypefun {struct protoent *} getprotoent (void)
1433 This function returns the next entry in the protocols database. It
1434 returns a null pointer if there are no more entries.
1435 @end deftypefun
1436
1437 @comment netdb.h
1438 @comment BSD
1439 @deftypefun void endprotoent (void)
1440 This function closes the protocols database.
1441 @end deftypefun
1442
1443 @node Inet Example
1444 @subsection Internet Socket Example
1445
1446 Here is an example showing how to create and name a socket in the
1447 Internet namespace. The newly created socket exists on the machine that
1448 the program is running on. Rather than finding and using the machine's
1449 Internet address, this example specifies @code{INADDR_ANY} as the host
1450 address; the system replaces that with the machine's actual address.
1451
1452 @smallexample
1453 @include mkisock.c.texi
1454 @end smallexample
1455
1456 Here is another example, showing how you can fill in a @code{sockaddr_in}
1457 structure, given a host name string and a port number:
1458
1459 @smallexample
1460 @include isockad.c.texi
1461 @end smallexample
1462
1463 @node Misc Namespaces
1464 @section Other Namespaces
1465
1466 @vindex PF_NS
1467 @vindex PF_ISO
1468 @vindex PF_CCITT
1469 @vindex PF_IMPLINK
1470 @vindex PF_ROUTE
1471 Certain other namespaces and associated protocol families are supported
1472 but not documented yet because they are not often used. @code{PF_NS}
1473 refers to the Xerox Network Software protocols. @code{PF_ISO} stands
1474 for Open Systems Interconnect. @code{PF_CCITT} refers to protocols from
1475 CCITT. @file{socket.h} defines these symbols and others naming protocols
1476 not actually implemented.
1477
1478 @code{PF_IMPLINK} is used for communicating between hosts and Internet
1479 Message Processors. For information on this, and on @code{PF_ROUTE}, an
1480 occasionally-used local area routing protocol, see the GNU Hurd Manual
1481 (to appear in the future).
1482
1483 @node Open/Close Sockets
1484 @section Opening and Closing Sockets
1485
1486 This section describes the actual library functions for opening and
1487 closing sockets. The same functions work for all namespaces and
1488 connection styles.
1489
1490 @menu
1491 * Creating a Socket:: How to open a socket.
1492 * Closing a Socket:: How to close a socket.
1493 * Socket Pairs:: These are created like pipes.
1494 @end menu
1495
1496 @node Creating a Socket
1497 @subsection Creating a Socket
1498 @cindex creating a socket
1499 @cindex socket, creating
1500 @cindex opening a socket
1501
1502 The primitive for creating a socket is the @code{socket} function,
1503 declared in @file{sys/socket.h}.
1504 @pindex sys/socket.h
1505
1506 @comment sys/socket.h
1507 @comment BSD
1508 @deftypefun int socket (int @var{namespace}, int @var{style}, int @var{protocol})
1509 This function creates a socket and specifies communication style
1510 @var{style}, which should be one of the socket styles listed in
1511 @ref{Communication Styles}. The @var{namespace} argument specifies
1512 the namespace; it must be @code{PF_FILE} (@pxref{File Namespace}) or
1513 @code{PF_INET} (@pxref{Internet Namespace}). @var{protocol}
1514 designates the specific protocol (@pxref{Socket Concepts}); zero is
1515 usually right for @var{protocol}.
1516
1517 The return value from @code{socket} is the file descriptor for the new
1518 socket, or @code{-1} in case of error. The following @code{errno} error
1519 conditions are defined for this function:
1520
1521 @table @code
1522 @item EPROTONOSUPPORT
1523 The @var{protocol} or @var{style} is not supported by the
1524 @var{namespace} specified.
1525
1526 @item EMFILE
1527 The process already has too many file descriptors open.
1528
1529 @item ENFILE
1530 The system already has too many file descriptors open.
1531
1532 @item EACCESS
1533 The process does not have privilege to create a socket of the specified
1534 @var{style} or @var{protocol}.
1535
1536 @item ENOBUFS
1537 The system ran out of internal buffer space.
1538 @end table
1539
1540 The file descriptor returned by the @code{socket} function supports both
1541 read and write operations. But, like pipes, sockets do not support file
1542 positioning operations.
1543 @end deftypefun
1544
1545 For examples of how to call the @code{socket} function,
1546 see @ref{File Namespace}, or @ref{Inet Example}.
1547
1548
1549 @node Closing a Socket
1550 @subsection Closing a Socket
1551 @cindex socket, closing
1552 @cindex closing a socket
1553 @cindex shutting down a socket
1554 @cindex socket shutdown
1555
1556 When you are finished using a socket, you can simply close its
1557 file descriptor with @code{close}; see @ref{Opening and Closing Files}.
1558 If there is still data waiting to be transmitted over the connection,
1559 normally @code{close} tries to complete this transmission. You
1560 can control this behavior using the @code{SO_LINGER} socket option to
1561 specify a timeout period; see @ref{Socket Options}.
1562
1563 @pindex sys/socket.h
1564 You can also shut down only reception or only transmission on a
1565 connection by calling @code{shutdown}, which is declared in
1566 @file{sys/socket.h}.
1567
1568 @comment sys/socket.h
1569 @comment BSD
1570 @deftypefun int shutdown (int @var{socket}, int @var{how})
1571 The @code{shutdown} function shuts down the connection of socket
1572 @var{socket}. The argument @var{how} specifies what action to
1573 perform:
1574
1575 @table @code
1576 @item 0
1577 Stop receiving data for this socket. If further data arrives,
1578 reject it.
1579
1580 @item 1
1581 Stop trying to transmit data from this socket. Discard any data
1582 waiting to be sent. Stop looking for acknowledgement of data already
1583 sent; don't retransmit it if it is lost.
1584
1585 @item 2
1586 Stop both reception and transmission.
1587 @end table
1588
1589 The return value is @code{0} on success and @code{-1} on failure. The
1590 following @code{errno} error conditions are defined for this function:
1591
1592 @table @code
1593 @item EBADF
1594 @var{socket} is not a valid file descriptor.
1595
1596 @item ENOTSOCK
1597 @var{socket} is not a socket.
1598
1599 @item ENOTCONN
1600 @var{socket} is not connected.
1601 @end table
1602 @end deftypefun
1603
1604 @node Socket Pairs
1605 @subsection Socket Pairs
1606 @cindex creating a socket pair
1607 @cindex socket pair
1608 @cindex opening a socket pair
1609
1610 @pindex sys/socket.h
1611 A @dfn{socket pair} consists of a pair of connected (but unnamed)
1612 sockets. It is very similar to a pipe and is used in much the same
1613 way. Socket pairs are created with the @code{socketpair} function,
1614 declared in @file{sys/socket.h}. A socket pair is much like a pipe; the
1615 main difference is that the socket pair is bidirectional, whereas the
1616 pipe has one input-only end and one output-only end (@pxref{Pipes and
1617 FIFOs}).
1618
1619 @comment sys/socket.h
1620 @comment BSD
1621 @deftypefun int socketpair (int @var{namespace}, int @var{style}, int @var{protocol}, int @var{filedes}@t{[2]})
1622 This function creates a socket pair, returning the file descriptors in
1623 @code{@var{filedes}[0]} and @code{@var{filedes}[1]}. The socket pair
1624 is a full-duplex communications channel, so that both reading and writing
1625 may be performed at either end.
1626
1627 The @var{namespace}, @var{style}, and @var{protocol} arguments are
1628 interpreted as for the @code{socket} function. @var{style} should be
1629 one of the communication styles listed in @ref{Communication Styles}.
1630 The @var{namespace} argument specifies the namespace, which must be
1631 @code{AF_FILE} (@pxref{File Namespace}); @var{protocol} specifies the
1632 communications protocol, but zero is the only meaningful value.
1633
1634 If @var{style} specifies a connectionless communication style, then
1635 the two sockets you get are not @emph{connected}, strictly speaking,
1636 but each of them knows the other as the default destination address,
1637 so they can send packets to each other.
1638
1639 The @code{socketpair} function returns @code{0} on success and @code{-1}
1640 on failure. The following @code{errno} error conditions are defined
1641 for this function:
1642
1643 @table @code
1644 @item EMFILE
1645 The process has too many file descriptors open.
1646
1647 @item EAFNOSUPPORT
1648 The specified namespace is not supported.
1649
1650 @item EPROTONOSUPPORT
1651 The specified protocol is not supported.
1652
1653 @item EOPNOTSUPP
1654 The specified protocol does not support the creation of socket pairs.
1655 @end table
1656 @end deftypefun
1657
1658 @node Connections
1659 @section Using Sockets with Connections
1660
1661 @cindex connection
1662 @cindex client
1663 @cindex server
1664 The most common communication styles involve making a connection to a
1665 particular other socket, and then exchanging data with that socket
1666 over and over. Making a connection is asymmetric; one side (the
1667 @dfn{client}) acts to request a connection, while the other side (the
1668 @dfn{server}) makes a socket and waits for the connection request.
1669
1670 @iftex
1671 @itemize @bullet
1672 @item
1673 @ref{Connecting}, describes what the client program must do to
1674 initiate a connection with a server.
1675
1676 @item
1677 @ref{Listening}, and @ref{Accepting Connections}, describe what the
1678 server program must do to wait for and act upon connection requests
1679 from clients.
1680
1681 @item
1682 @ref{Transferring Data}, describes how data is transferred through the
1683 connected socket.
1684 @end itemize
1685 @end iftex
1686
1687 @menu
1688 * Connecting:: What the client program must do.
1689 * Listening:: How a server program waits for requests.
1690 * Accepting Connections:: What the server does when it gets a request.
1691 * Who is Connected:: Getting the address of the
1692 other side of a connection.
1693 * Transferring Data:: How to send and receive data.
1694 * Byte Stream Example:: An example program: a client for communicating
1695 over a byte stream socket in the Internet namespace.
1696 * Server Example:: A corresponding server program.
1697 * Out-of-Band Data:: This is an advanced feature.
1698 @end menu
1699
1700 @node Connecting
1701 @subsection Making a Connection
1702 @cindex connecting a socket
1703 @cindex socket, connecting
1704 @cindex socket, initiating a connection
1705 @cindex socket, client actions
1706
1707 In making a connection, the client makes a connection while the server
1708 waits for and accepts the connection. Here we discuss what the client
1709 program must do, using the @code{connect} function, which is declared in
1710 @file{sys/socket.h}.
1711
1712 @comment sys/socket.h
1713 @comment BSD
1714 @deftypefun int connect (int @var{socket}, struct sockaddr *@var{addr}, socklen_t @var{length})
1715 The @code{connect} function initiates a connection from the socket
1716 with file descriptor @var{socket} to the socket whose address is
1717 specified by the @var{addr} and @var{length} arguments. (This socket
1718 is typically on another machine, and it must be already set up as a
1719 server.) @xref{Socket Addresses}, for information about how these
1720 arguments are interpreted.
1721
1722 Normally, @code{connect} waits until the server responds to the request
1723 before it returns. You can set nonblocking mode on the socket
1724 @var{socket} to make @code{connect} return immediately without waiting
1725 for the response. @xref{File Status Flags}, for information about
1726 nonblocking mode.
1727 @c !!! how do you tell when it has finished connecting? I suspect the
1728 @c way you do it is select for writing.
1729
1730 The normal return value from @code{connect} is @code{0}. If an error
1731 occurs, @code{connect} returns @code{-1}. The following @code{errno}
1732 error conditions are defined for this function:
1733
1734 @table @code
1735 @item EBADF
1736 The socket @var{socket} is not a valid file descriptor.
1737
1738 @item ENOTSOCK
1739 File descriptor @var{socket} is not a socket.
1740
1741 @item EADDRNOTAVAIL
1742 The specified address is not available on the remote machine.
1743
1744 @item EAFNOSUPPORT
1745 The namespace of the @var{addr} is not supported by this socket.
1746
1747 @item EISCONN
1748 The socket @var{socket} is already connected.
1749
1750 @item ETIMEDOUT
1751 The attempt to establish the connection timed out.
1752
1753 @item ECONNREFUSED
1754 The server has actively refused to establish the connection.
1755
1756 @item ENETUNREACH
1757 The network of the given @var{addr} isn't reachable from this host.
1758
1759 @item EADDRINUSE
1760 The socket address of the given @var{addr} is already in use.
1761
1762 @item EINPROGRESS
1763 The socket @var{socket} is non-blocking and the connection could not be
1764 established immediately. You can determine when the connection is
1765 completely established with @code{select}; @pxref{Waiting for I/O}.
1766 Another @code{connect} call on the same socket, before the connection is
1767 completely established, will fail with @code{EALREADY}.
1768
1769 @item EALREADY
1770 The socket @var{socket} is non-blocking and already has a pending
1771 connection in progress (see @code{EINPROGRESS} above).
1772 @end table
1773
1774 This function is defined as a cancelation point in multi-threaded
1775 programs. So one has to be prepared for this and make sure that
1776 possibly allocated resources (like memory, files descriptors,
1777 semaphores or whatever) are freed even if the thread is cancel.
1778 @c @xref{pthread_cleanup_push}, for a method how to do this.
1779 @end deftypefun
1780
1781 @node Listening
1782 @subsection Listening for Connections
1783 @cindex listening (sockets)
1784 @cindex sockets, server actions
1785 @cindex sockets, listening
1786
1787 Now let us consider what the server process must do to accept
1788 connections on a socket. First it must use the @code{listen} function
1789 to enable connection requests on the socket, and then accept each
1790 incoming connection with a call to @code{accept} (@pxref{Accepting
1791 Connections}). Once connection requests are enabled on a server socket,
1792 the @code{select} function reports when the socket has a connection
1793 ready to be accepted (@pxref{Waiting for I/O}).
1794
1795 The @code{listen} function is not allowed for sockets using
1796 connectionless communication styles.
1797
1798 You can write a network server that does not even start running until a
1799 connection to it is requested. @xref{Inetd Servers}.
1800
1801 In the Internet namespace, there are no special protection mechanisms
1802 for controlling access to connect to a port; any process on any machine
1803 can make a connection to your server. If you want to restrict access to
1804 your server, make it examine the addresses associated with connection
1805 requests or implement some other handshaking or identification
1806 protocol.
1807
1808 In the File namespace, the ordinary file protection bits control who has
1809 access to connect to the socket.
1810
1811 @comment sys/socket.h
1812 @comment BSD
1813 @deftypefun int listen (int @var{socket}, unsigned int @var{n})
1814 The @code{listen} function enables the socket @var{socket} to accept
1815 connections, thus making it a server socket.
1816
1817 The argument @var{n} specifies the length of the queue for pending
1818 connections. When the queue fills, new clients attempting to connect
1819 fail with @code{ECONNREFUSED} until the server calls @code{accept} to
1820 accept a connection from the queue.
1821
1822 The @code{listen} function returns @code{0} on success and @code{-1}
1823 on failure. The following @code{errno} error conditions are defined
1824 for this function:
1825
1826 @table @code
1827 @item EBADF
1828 The argument @var{socket} is not a valid file descriptor.
1829
1830 @item ENOTSOCK
1831 The argument @var{socket} is not a socket.
1832
1833 @item EOPNOTSUPP
1834 The socket @var{socket} does not support this operation.
1835 @end table
1836 @end deftypefun
1837
1838 @node Accepting Connections
1839 @subsection Accepting Connections
1840 @cindex sockets, accepting connections
1841 @cindex accepting connections
1842
1843 When a server receives a connection request, it can complete the
1844 connection by accepting the request. Use the function @code{accept}
1845 to do this.
1846
1847 A socket that has been established as a server can accept connection
1848 requests from multiple clients. The server's original socket
1849 @emph{does not become part} of the connection; instead, @code{accept}
1850 makes a new socket which participates in the connection.
1851 @code{accept} returns the descriptor for this socket. The server's
1852 original socket remains available for listening for further connection
1853 requests.
1854
1855 The number of pending connection requests on a server socket is finite.
1856 If connection requests arrive from clients faster than the server can
1857 act upon them, the queue can fill up and additional requests are refused
1858 with a @code{ECONNREFUSED} error. You can specify the maximum length of
1859 this queue as an argument to the @code{listen} function, although the
1860 system may also impose its own internal limit on the length of this
1861 queue.
1862
1863 @comment sys/socket.h
1864 @comment BSD
1865 @deftypefun int accept (int @var{socket}, struct sockaddr *@var{addr}, socklen_t *@var{length-ptr})
1866 This function is used to accept a connection request on the server
1867 socket @var{socket}.
1868
1869 The @code{accept} function waits if there are no connections pending,
1870 unless the socket @var{socket} has nonblocking mode set. (You can use
1871 @code{select} to wait for a pending connection, with a nonblocking
1872 socket.) @xref{File Status Flags}, for information about nonblocking
1873 mode.
1874
1875 The @var{addr} and @var{length-ptr} arguments are used to return
1876 information about the name of the client socket that initiated the
1877 connection. @xref{Socket Addresses}, for information about the format
1878 of the information.
1879
1880 Accepting a connection does not make @var{socket} part of the
1881 connection. Instead, it creates a new socket which becomes
1882 connected. The normal return value of @code{accept} is the file
1883 descriptor for the new socket.
1884
1885 After @code{accept}, the original socket @var{socket} remains open and
1886 unconnected, and continues listening until you close it. You can
1887 accept further connections with @var{socket} by calling @code{accept}
1888 again.
1889
1890 If an error occurs, @code{accept} returns @code{-1}. The following
1891 @code{errno} error conditions are defined for this function:
1892
1893 @table @code
1894 @item EBADF
1895 The @var{socket} argument is not a valid file descriptor.
1896
1897 @item ENOTSOCK
1898 The descriptor @var{socket} argument is not a socket.
1899
1900 @item EOPNOTSUPP
1901 The descriptor @var{socket} does not support this operation.
1902
1903 @item EWOULDBLOCK
1904 @var{socket} has nonblocking mode set, and there are no pending
1905 connections immediately available.
1906 @end table
1907
1908 This function is defined as a cancelation point in multi-threaded
1909 programs. So one has to be prepared for this and make sure that
1910 possibly allocated resources (like memory, files descriptors,
1911 semaphores or whatever) are freed even if the thread is cancel.
1912 @c @xref{pthread_cleanup_push}, for a method how to do this.
1913 @end deftypefun
1914
1915 The @code{accept} function is not allowed for sockets using
1916 connectionless communication styles.
1917
1918 @node Who is Connected
1919 @subsection Who is Connected to Me?
1920
1921 @comment sys/socket.h
1922 @comment BSD
1923 @deftypefun int getpeername (int @var{socket}, struct sockaddr *@var{addr}, size_t *@var{length-ptr})
1924 The @code{getpeername} function returns the address of the socket that
1925 @var{socket} is connected to; it stores the address in the memory space
1926 specified by @var{addr} and @var{length-ptr}. It stores the length of
1927 the address in @code{*@var{length-ptr}}.
1928
1929 @xref{Socket Addresses}, for information about the format of the
1930 address. In some operating systems, @code{getpeername} works only for
1931 sockets in the Internet domain.
1932
1933 The return value is @code{0} on success and @code{-1} on error. The
1934 following @code{errno} error conditions are defined for this function:
1935
1936 @table @code
1937 @item EBADF
1938 The argument @var{socket} is not a valid file descriptor.
1939
1940 @item ENOTSOCK
1941 The descriptor @var{socket} is not a socket.
1942
1943 @item ENOTCONN
1944 The socket @var{socket} is not connected.
1945
1946 @item ENOBUFS
1947 There are not enough internal buffers available.
1948 @end table
1949 @end deftypefun
1950
1951
1952 @node Transferring Data
1953 @subsection Transferring Data
1954 @cindex reading from a socket
1955 @cindex writing to a socket
1956
1957 Once a socket has been connected to a peer, you can use the ordinary
1958 @code{read} and @code{write} operations (@pxref{I/O Primitives}) to
1959 transfer data. A socket is a two-way communications channel, so read
1960 and write operations can be performed at either end.
1961
1962 There are also some I/O modes that are specific to socket operations.
1963 In order to specify these modes, you must use the @code{recv} and
1964 @code{send} functions instead of the more generic @code{read} and
1965 @code{write} functions. The @code{recv} and @code{send} functions take
1966 an additional argument which you can use to specify various flags to
1967 control the special I/O modes. For example, you can specify the
1968 @code{MSG_OOB} flag to read or write out-of-band data, the
1969 @code{MSG_PEEK} flag to peek at input, or the @code{MSG_DONTROUTE} flag
1970 to control inclusion of routing information on output.
1971
1972 @menu
1973 * Sending Data:: Sending data with @code{send}.
1974 * Receiving Data:: Reading data with @code{recv}.
1975 * Socket Data Options:: Using @code{send} and @code{recv}.
1976 @end menu
1977
1978 @node Sending Data
1979 @subsubsection Sending Data
1980
1981 @pindex sys/socket.h
1982 The @code{send} function is declared in the header file
1983 @file{sys/socket.h}. If your @var{flags} argument is zero, you can just
1984 as well use @code{write} instead of @code{send}; see @ref{I/O
1985 Primitives}. If the socket was connected but the connection has broken,
1986 you get a @code{SIGPIPE} signal for any use of @code{send} or
1987 @code{write} (@pxref{Miscellaneous Signals}).
1988
1989 @comment sys/socket.h
1990 @comment BSD
1991 @deftypefun int send (int @var{socket}, void *@var{buffer}, size_t @var{size}, int @var{flags})
1992 The @code{send} function is like @code{write}, but with the additional
1993 flags @var{flags}. The possible values of @var{flags} are described
1994 in @ref{Socket Data Options}.
1995
1996 This function returns the number of bytes transmitted, or @code{-1} on
1997 failure. If the socket is nonblocking, then @code{send} (like
1998 @code{write}) can return after sending just part of the data.
1999 @xref{File Status Flags}, for information about nonblocking mode.
2000
2001 Note, however, that a successful return value merely indicates that
2002 the message has been sent without error, not necessarily that it has
2003 been received without error.
2004
2005 The following @code{errno} error conditions are defined for this function:
2006
2007 @table @code
2008 @item EBADF
2009 The @var{socket} argument is not a valid file descriptor.
2010
2011 @item EINTR
2012 The operation was interrupted by a signal before any data was sent.
2013 @xref{Interrupted Primitives}.
2014
2015 @item ENOTSOCK
2016 The descriptor @var{socket} is not a socket.
2017
2018 @item EMSGSIZE
2019 The socket type requires that the message be sent atomically, but the
2020 message is too large for this to be possible.
2021
2022 @item EWOULDBLOCK
2023 Nonblocking mode has been set on the socket, and the write operation
2024 would block. (Normally @code{send} blocks until the operation can be
2025 completed.)
2026
2027 @item ENOBUFS
2028 There is not enough internal buffer space available.
2029
2030 @item ENOTCONN
2031 You never connected this socket.
2032
2033 @item EPIPE
2034 This socket was connected but the connection is now broken. In this
2035 case, @code{send} generates a @code{SIGPIPE} signal first; if that
2036 signal is ignored or blocked, or if its handler returns, then
2037 @code{send} fails with @code{EPIPE}.
2038 @end table
2039
2040 This function is defined as a cancelation point in multi-threaded
2041 programs. So one has to be prepared for this and make sure that
2042 possibly allocated resources (like memory, files descriptors,
2043 semaphores or whatever) are freed even if the thread is cancel.
2044 @c @xref{pthread_cleanup_push}, for a method how to do this.
2045 @end deftypefun
2046
2047 @node Receiving Data
2048 @subsubsection Receiving Data
2049
2050 @pindex sys/socket.h
2051 The @code{recv} function is declared in the header file
2052 @file{sys/socket.h}. If your @var{flags} argument is zero, you can
2053 just as well use @code{read} instead of @code{recv}; see @ref{I/O
2054 Primitives}.
2055
2056 @comment sys/socket.h
2057 @comment BSD
2058 @deftypefun int recv (int @var{socket}, void *@var{buffer}, size_t @var{size}, int @var{flags})
2059 The @code{recv} function is like @code{read}, but with the additional
2060 flags @var{flags}. The possible values of @var{flags} are described
2061 In @ref{Socket Data Options}.
2062
2063 If nonblocking mode is set for @var{socket}, and no data is available to
2064 be read, @code{recv} fails immediately rather than waiting. @xref{File
2065 Status Flags}, for information about nonblocking mode.
2066
2067 This function returns the number of bytes received, or @code{-1} on failure.
2068 The following @code{errno} error conditions are defined for this function:
2069
2070 @table @code
2071 @item EBADF
2072 The @var{socket} argument is not a valid file descriptor.
2073
2074 @item ENOTSOCK
2075 The descriptor @var{socket} is not a socket.
2076
2077 @item EWOULDBLOCK
2078 Nonblocking mode has been set on the socket, and the read operation
2079 would block. (Normally, @code{recv} blocks until there is input
2080 available to be read.)
2081
2082 @item EINTR
2083 The operation was interrupted by a signal before any data was read.
2084 @xref{Interrupted Primitives}.
2085
2086 @item ENOTCONN
2087 You never connected this socket.
2088 @end table
2089
2090 This function is defined as a cancelation point in multi-threaded
2091 programs. So one has to be prepared for this and make sure that
2092 possibly allocated resources (like memory, files descriptors,
2093 semaphores or whatever) are freed even if the thread is cancel.
2094 @c @xref{pthread_cleanup_push}, for a method how to do this.
2095 @end deftypefun
2096
2097 @node Socket Data Options
2098 @subsubsection Socket Data Options
2099
2100 @pindex sys/socket.h
2101 The @var{flags} argument to @code{send} and @code{recv} is a bit
2102 mask. You can bitwise-OR the values of the following macros together
2103 to obtain a value for this argument. All are defined in the header
2104 file @file{sys/socket.h}.
2105
2106 @comment sys/socket.h
2107 @comment BSD
2108 @deftypevr Macro int MSG_OOB
2109 Send or receive out-of-band data. @xref{Out-of-Band Data}.
2110 @end deftypevr
2111
2112 @comment sys/socket.h
2113 @comment BSD
2114 @deftypevr Macro int MSG_PEEK
2115 Look at the data but don't remove it from the input queue. This is
2116 only meaningful with input functions such as @code{recv}, not with
2117 @code{send}.
2118 @end deftypevr
2119
2120 @comment sys/socket.h
2121 @comment BSD
2122 @deftypevr Macro int MSG_DONTROUTE
2123 Don't include routing information in the message. This is only
2124 meaningful with output operations, and is usually only of interest for
2125 diagnostic or routing programs. We don't try to explain it here.
2126 @end deftypevr
2127
2128 @node Byte Stream Example
2129 @subsection Byte Stream Socket Example
2130
2131 Here is an example client program that makes a connection for a byte
2132 stream socket in the Internet namespace. It doesn't do anything
2133 particularly interesting once it has connected to the server; it just
2134 sends a text string to the server and exits.
2135
2136 @smallexample
2137 @include inetcli.c.texi
2138 @end smallexample
2139
2140 @node Server Example
2141 @subsection Byte Stream Connection Server Example
2142
2143 The server end is much more complicated. Since we want to allow
2144 multiple clients to be connected to the server at the same time, it
2145 would be incorrect to wait for input from a single client by simply
2146 calling @code{read} or @code{recv}. Instead, the right thing to do is
2147 to use @code{select} (@pxref{Waiting for I/O}) to wait for input on
2148 all of the open sockets. This also allows the server to deal with
2149 additional connection requests.
2150
2151 This particular server doesn't do anything interesting once it has
2152 gotten a message from a client. It does close the socket for that
2153 client when it detects an end-of-file condition (resulting from the
2154 client shutting down its end of the connection).
2155
2156 This program uses @code{make_socket} and @code{init_sockaddr} to set
2157 up the socket address; see @ref{Inet Example}.
2158
2159 @smallexample
2160 @include inetsrv.c.texi
2161 @end smallexample
2162
2163 @node Out-of-Band Data
2164 @subsection Out-of-Band Data
2165
2166 @cindex out-of-band data
2167 @cindex high-priority data
2168 Streams with connections permit @dfn{out-of-band} data that is
2169 delivered with higher priority than ordinary data. Typically the
2170 reason for sending out-of-band data is to send notice of an
2171 exceptional condition. The way to send out-of-band data is using
2172 @code{send}, specifying the flag @code{MSG_OOB} (@pxref{Sending
2173 Data}).
2174
2175 Out-of-band data is received with higher priority because the
2176 receiving process need not read it in sequence; to read the next
2177 available out-of-band data, use @code{recv} with the @code{MSG_OOB}
2178 flag (@pxref{Receiving Data}). Ordinary read operations do not read
2179 out-of-band data; they read only the ordinary data.
2180
2181 @cindex urgent socket condition
2182 When a socket finds that out-of-band data is on its way, it sends a
2183 @code{SIGURG} signal to the owner process or process group of the
2184 socket. You can specify the owner using the @code{F_SETOWN} command
2185 to the @code{fcntl} function; see @ref{Interrupt Input}. You must
2186 also establish a handler for this signal, as described in @ref{Signal
2187 Handling}, in order to take appropriate action such as reading the
2188 out-of-band data.
2189
2190 Alternatively, you can test for pending out-of-band data, or wait
2191 until there is out-of-band data, using the @code{select} function; it
2192 can wait for an exceptional condition on the socket. @xref{Waiting
2193 for I/O}, for more information about @code{select}.
2194
2195 Notification of out-of-band data (whether with @code{SIGURG} or with
2196 @code{select}) indicates that out-of-band data is on the way; the data
2197 may not actually arrive until later. If you try to read the
2198 out-of-band data before it arrives, @code{recv} fails with an
2199 @code{EWOULDBLOCK} error.
2200
2201 Sending out-of-band data automatically places a ``mark'' in the stream
2202 of ordinary data, showing where in the sequence the out-of-band data
2203 ``would have been''. This is useful when the meaning of out-of-band
2204 data is ``cancel everything sent so far''. Here is how you can test,
2205 in the receiving process, whether any ordinary data was sent before
2206 the mark:
2207
2208 @smallexample
2209 success = ioctl (socket, SIOCATMARK, &result);
2210 @end smallexample
2211
2212 Here's a function to discard any ordinary data preceding the
2213 out-of-band mark:
2214
2215 @smallexample
2216 int
2217 discard_until_mark (int socket)
2218 @{
2219 while (1)
2220 @{
2221 /* @r{This is not an arbitrary limit; any size will do.} */
2222 char buffer[1024];
2223 int result, success;
2224
2225 /* @r{If we have reached the mark, return.} */
2226 success = ioctl (socket, SIOCATMARK, &result);
2227 if (success < 0)
2228 perror ("ioctl");
2229 if (result)
2230 return;
2231
2232 /* @r{Otherwise, read a bunch of ordinary data and discard it.}
2233 @r{This is guaranteed not to read past the mark}
2234 @r{if it starts before the mark.} */
2235 success = read (socket, buffer, sizeof buffer);
2236 if (success < 0)
2237 perror ("read");
2238 @}
2239 @}
2240 @end smallexample
2241
2242 If you don't want to discard the ordinary data preceding the mark, you
2243 may need to read some of it anyway, to make room in internal system
2244 buffers for the out-of-band data. If you try to read out-of-band data
2245 and get an @code{EWOULDBLOCK} error, try reading some ordinary data
2246 (saving it so that you can use it when you want it) and see if that
2247 makes room. Here is an example:
2248
2249 @smallexample
2250 struct buffer
2251 @{
2252 char *buffer;
2253 int size;
2254 struct buffer *next;
2255 @};
2256
2257 /* @r{Read the out-of-band data from SOCKET and return it}
2258 @r{as a `struct buffer', which records the address of the data}
2259 @r{and its size.}
2260
2261 @r{It may be necessary to read some ordinary data}
2262 @r{in order to make room for the out-of-band data.}
2263 @r{If so, the ordinary data is saved as a chain of buffers}
2264 @r{found in the `next' field of the value.} */
2265
2266 struct buffer *
2267 read_oob (int socket)
2268 @{
2269 struct buffer *tail = 0;
2270 struct buffer *list = 0;
2271
2272 while (1)
2273 @{
2274 /* @r{This is an arbitrary limit.}
2275 @r{Does anyone know how to do this without a limit?} */
2276 char *buffer = (char *) xmalloc (1024);
2277 struct buffer *link;
2278 int success;
2279 int result;
2280
2281 /* @r{Try again to read the out-of-band data.} */
2282 success = recv (socket, buffer, sizeof buffer, MSG_OOB);
2283 if (success >= 0)
2284 @{
2285 /* @r{We got it, so return it.} */
2286 struct buffer *link
2287 = (struct buffer *) xmalloc (sizeof (struct buffer));
2288 link->buffer = buffer;
2289 link->size = success;
2290 link->next = list;
2291 return link;
2292 @}
2293
2294 /* @r{If we fail, see if we are at the mark.} */
2295 success = ioctl (socket, SIOCATMARK, &result);
2296 if (success < 0)
2297 perror ("ioctl");
2298 if (result)
2299 @{
2300 /* @r{At the mark; skipping past more ordinary data cannot help.}
2301 @r{So just wait a while.} */
2302 sleep (1);
2303 continue;
2304 @}
2305
2306 /* @r{Otherwise, read a bunch of ordinary data and save it.}
2307 @r{This is guaranteed not to read past the mark}
2308 @r{if it starts before the mark.} */
2309 success = read (socket, buffer, sizeof buffer);
2310 if (success < 0)
2311 perror ("read");
2312
2313 /* @r{Save this data in the buffer list.} */
2314 @{
2315 struct buffer *link
2316 = (struct buffer *) xmalloc (sizeof (struct buffer));
2317 link->buffer = buffer;
2318 link->size = success;
2319
2320 /* @r{Add the new link to the end of the list.} */
2321 if (tail)
2322 tail->next = link;
2323 else
2324 list = link;
2325 tail = link;
2326 @}
2327 @}
2328 @}
2329 @end smallexample
2330
2331 @node Datagrams
2332 @section Datagram Socket Operations
2333
2334 @cindex datagram socket
2335 This section describes how to use communication styles that don't use
2336 connections (styles @code{SOCK_DGRAM} and @code{SOCK_RDM}). Using
2337 these styles, you group data into packets and each packet is an
2338 independent communication. You specify the destination for each
2339 packet individually.
2340
2341 Datagram packets are like letters: you send each one independently,
2342 with its own destination address, and they may arrive in the wrong
2343 order or not at all.
2344
2345 The @code{listen} and @code{accept} functions are not allowed for
2346 sockets using connectionless communication styles.
2347
2348 @menu
2349 * Sending Datagrams:: Sending packets on a datagram socket.
2350 * Receiving Datagrams:: Receiving packets on a datagram socket.
2351 * Datagram Example:: An example program: packets sent over a
2352 datagram socket in the file namespace.
2353 * Example Receiver:: Another program, that receives those packets.
2354 @end menu
2355
2356 @node Sending Datagrams
2357 @subsection Sending Datagrams
2358 @cindex sending a datagram
2359 @cindex transmitting datagrams
2360 @cindex datagrams, transmitting
2361
2362 @pindex sys/socket.h
2363 The normal way of sending data on a datagram socket is by using the
2364 @code{sendto} function, declared in @file{sys/socket.h}.
2365
2366 You can call @code{connect} on a datagram socket, but this only
2367 specifies a default destination for further data transmission on the
2368 socket. When a socket has a default destination, then you can use
2369 @code{send} (@pxref{Sending Data}) or even @code{write} (@pxref{I/O
2370 Primitives}) to send a packet there. You can cancel the default
2371 destination by calling @code{connect} using an address format of
2372 @code{AF_UNSPEC} in the @var{addr} argument. @xref{Connecting}, for
2373 more information about the @code{connect} function.
2374
2375 @comment sys/socket.h
2376 @comment BSD
2377 @deftypefun int sendto (int @var{socket}, void *@var{buffer}. size_t @var{size}, int @var{flags}, struct sockaddr *@var{addr}, socklen_t @var{length})
2378 The @code{sendto} function transmits the data in the @var{buffer}
2379 through the socket @var{socket} to the destination address specified
2380 by the @var{addr} and @var{length} arguments. The @var{size} argument
2381 specifies the number of bytes to be transmitted.
2382
2383 The @var{flags} are interpreted the same way as for @code{send}; see
2384 @ref{Socket Data Options}.
2385
2386 The return value and error conditions are also the same as for
2387 @code{send}, but you cannot rely on the system to detect errors and
2388 report them; the most common error is that the packet is lost or there
2389 is no one at the specified address to receive it, and the operating
2390 system on your machine usually does not know this.
2391
2392 It is also possible for one call to @code{sendto} to report an error
2393 due to a problem related to a previous call.
2394
2395 This function is defined as a cancelation point in multi-threaded
2396 programs. So one has to be prepared for this and make sure that
2397 possibly allocated resources (like memory, files descriptors,
2398 semaphores or whatever) are freed even if the thread is cancel.
2399 @c @xref{pthread_cleanup_push}, for a method how to do this.
2400 @end deftypefun
2401
2402 @node Receiving Datagrams
2403 @subsection Receiving Datagrams
2404 @cindex receiving datagrams
2405
2406 The @code{recvfrom} function reads a packet from a datagram socket and
2407 also tells you where it was sent from. This function is declared in
2408 @file{sys/socket.h}.
2409
2410 @comment sys/socket.h
2411 @comment BSD
2412 @deftypefun int recvfrom (int @var{socket}, void *@var{buffer}, size_t @var{size}, int @var{flags}, struct sockaddr *@var{addr}, socklen_t *@var{length-ptr})
2413 The @code{recvfrom} function reads one packet from the socket
2414 @var{socket} into the buffer @var{buffer}. The @var{size} argument
2415 specifies the maximum number of bytes to be read.
2416
2417 If the packet is longer than @var{size} bytes, then you get the first
2418 @var{size} bytes of the packet, and the rest of the packet is lost.
2419 There's no way to read the rest of the packet. Thus, when you use a
2420 packet protocol, you must always know how long a packet to expect.
2421
2422 The @var{addr} and @var{length-ptr} arguments are used to return the
2423 address where the packet came from. @xref{Socket Addresses}. For a
2424 socket in the file domain, the address information won't be meaningful,
2425 since you can't read the address of such a socket (@pxref{File
2426 Namespace}). You can specify a null pointer as the @var{addr} argument
2427 if you are not interested in this information.
2428
2429 The @var{flags} are interpreted the same way as for @code{recv}
2430 (@pxref{Socket Data Options}). The return value and error conditions
2431 are also the same as for @code{recv}.
2432
2433 This function is defined as a cancelation point in multi-threaded
2434 programs. So one has to be prepared for this and make sure that
2435 possibly allocated resources (like memory, files descriptors,
2436 semaphores or whatever) are freed even if the thread is cancel.
2437 @c @xref{pthread_cleanup_push}, for a method how to do this.
2438 @end deftypefun
2439
2440 You can use plain @code{recv} (@pxref{Receiving Data}) instead of
2441 @code{recvfrom} if you know don't need to find out who sent the packet
2442 (either because you know where it should come from or because you
2443 treat all possible senders alike). Even @code{read} can be used if
2444 you don't want to specify @var{flags} (@pxref{I/O Primitives}).
2445
2446 @ignore
2447 @c sendmsg and recvmsg are like readv and writev in that they
2448 @c use a series of buffers. It's not clear this is worth
2449 @c supporting or that we support them.
2450 @c !!! they can do more; it is hairy
2451
2452 @comment sys/socket.h
2453 @comment BSD
2454 @deftp {Data Type} {struct msghdr}
2455 @end deftp
2456
2457 @comment sys/socket.h
2458 @comment BSD
2459 @deftypefun int sendmsg (int @var{socket}, const struct msghdr *@var{message}, int @var{flags})
2460
2461 This function is defined as a cancelation point in multi-threaded
2462 programs. So one has to be prepared for this and make sure that
2463 possibly allocated resources (like memory, files descriptors,
2464 semaphores or whatever) are freed even if the thread is cancel.
2465 @c @xref{pthread_cleanup_push}, for a method how to do this.
2466 @end deftypefun
2467
2468 @comment sys/socket.h
2469 @comment BSD
2470 @deftypefun int recvmsg (int @var{socket}, struct msghdr *@var{message}, int @var{flags})
2471
2472 This function is defined as a cancelation point in multi-threaded
2473 programs. So one has to be prepared for this and make sure that
2474 possibly allocated resources (like memory, files descriptors,
2475 semaphores or whatever) are freed even if the thread is cancel.
2476 @c @xref{pthread_cleanup_push}, for a method how to do this.
2477 @end deftypefun
2478 @end ignore
2479
2480 @node Datagram Example
2481 @subsection Datagram Socket Example
2482
2483 Here is a set of example programs that send messages over a datagram
2484 stream in the file namespace. Both the client and server programs use the
2485 @code{make_named_socket} function that was presented in @ref{File
2486 Namespace}, to create and name their sockets.
2487
2488 First, here is the server program. It sits in a loop waiting for
2489 messages to arrive, bouncing each message back to the sender.
2490 Obviously, this isn't a particularly useful program, but it does show
2491 the general ideas involved.
2492
2493 @smallexample
2494 @include filesrv.c.texi
2495 @end smallexample
2496
2497 @node Example Receiver
2498 @subsection Example of Reading Datagrams
2499
2500 Here is the client program corresponding to the server above.
2501
2502 It sends a datagram to the server and then waits for a reply. Notice
2503 that the socket for the client (as well as for the server) in this
2504 example has to be given a name. This is so that the server can direct
2505 a message back to the client. Since the socket has no associated
2506 connection state, the only way the server can do this is by
2507 referencing the name of the client.
2508
2509 @smallexample
2510 @include filecli.c.texi
2511 @end smallexample
2512
2513 Keep in mind that datagram socket communications are unreliable. In
2514 this example, the client program waits indefinitely if the message
2515 never reaches the server or if the server's response never comes
2516 back. It's up to the user running the program to kill it and restart
2517 it, if desired. A more automatic solution could be to use
2518 @code{select} (@pxref{Waiting for I/O}) to establish a timeout period
2519 for the reply, and in case of timeout either resend the message or
2520 shut down the socket and exit.
2521
2522 @node Inetd
2523 @section The @code{inetd} Daemon
2524
2525 We've explained above how to write a server program that does its own
2526 listening. Such a server must already be running in order for anyone
2527 to connect to it.
2528
2529 Another way to provide service for an Internet port is to let the daemon
2530 program @code{inetd} do the listening. @code{inetd} is a program that
2531 runs all the time and waits (using @code{select}) for messages on a
2532 specified set of ports. When it receives a message, it accepts the
2533 connection (if the socket style calls for connections) and then forks a
2534 child process to run the corresponding server program. You specify the
2535 ports and their programs in the file @file{/etc/inetd.conf}.
2536
2537 @menu
2538 * Inetd Servers::
2539 * Configuring Inetd::
2540 @end menu
2541
2542 @node Inetd Servers
2543 @subsection @code{inetd} Servers
2544
2545 Writing a server program to be run by @code{inetd} is very simple. Each time
2546 someone requests a connection to the appropriate port, a new server
2547 process starts. The connection already exists at this time; the
2548 socket is available as the standard input descriptor and as the
2549 standard output descriptor (descriptors 0 and 1) in the server
2550 process. So the server program can begin reading and writing data
2551 right away. Often the program needs only the ordinary I/O facilities;
2552 in fact, a general-purpose filter program that knows nothing about
2553 sockets can work as a byte stream server run by @code{inetd}.
2554
2555 You can also use @code{inetd} for servers that use connectionless
2556 communication styles. For these servers, @code{inetd} does not try to accept
2557 a connection, since no connection is possible. It just starts the
2558 server program, which can read the incoming datagram packet from
2559 descriptor 0. The server program can handle one request and then
2560 exit, or you can choose to write it to keep reading more requests
2561 until no more arrive, and then exit. You must specify which of these
2562 two techniques the server uses, when you configure @code{inetd}.
2563
2564 @node Configuring Inetd
2565 @subsection Configuring @code{inetd}
2566
2567 The file @file{/etc/inetd.conf} tells @code{inetd} which ports to listen to
2568 and what server programs to run for them. Normally each entry in the
2569 file is one line, but you can split it onto multiple lines provided
2570 all but the first line of the entry start with whitespace. Lines that
2571 start with @samp{#} are comments.
2572
2573 Here are two standard entries in @file{/etc/inetd.conf}:
2574
2575 @smallexample
2576 ftp stream tcp nowait root /libexec/ftpd ftpd
2577 talk dgram udp wait root /libexec/talkd talkd
2578 @end smallexample
2579
2580 An entry has this format:
2581
2582 @smallexample
2583 @var{service} @var{style} @var{protocol} @var{wait} @var{username} @var{program} @var{arguments}
2584 @end smallexample
2585
2586 The @var{service} field says which service this program provides. It
2587 should be the name of a service defined in @file{/etc/services}.
2588 @code{inetd} uses @var{service} to decide which port to listen on for
2589 this entry.
2590
2591 The fields @var{style} and @var{protocol} specify the communication
2592 style and the protocol to use for the listening socket. The style
2593 should be the name of a communication style, converted to lower case
2594 and with @samp{SOCK_} deleted---for example, @samp{stream} or
2595 @samp{dgram}. @var{protocol} should be one of the protocols listed in
2596 @file{/etc/protocols}. The typical protocol names are @samp{tcp} for
2597 byte stream connections and @samp{udp} for unreliable datagrams.
2598
2599 The @var{wait} field should be either @samp{wait} or @samp{nowait}.
2600 Use @samp{wait} if @var{style} is a connectionless style and the
2601 server, once started, handles multiple requests, as many as come in.
2602 Use @samp{nowait} if @code{inetd} should start a new process for each message
2603 or request that comes in. If @var{style} uses connections, then
2604 @var{wait} @strong{must} be @samp{nowait}.
2605
2606 @var{user} is the user name that the server should run as. @code{inetd} runs
2607 as root, so it can set the user ID of its children arbitrarily. It's
2608 best to avoid using @samp{root} for @var{user} if you can; but some
2609 servers, such as Telnet and FTP, read a username and password
2610 themselves. These servers need to be root initially so they can log
2611 in as commanded by the data coming over the network.
2612
2613 @var{program} together with @var{arguments} specifies the command to
2614 run to start the server. @var{program} should be an absolute file
2615 name specifying the executable file to run. @var{arguments} consists
2616 of any number of whitespace-separated words, which become the
2617 command-line arguments of @var{program}. The first word in
2618 @var{arguments} is argument zero, which should by convention be the
2619 program name itself (sans directories).
2620
2621 If you edit @file{/etc/inetd.conf}, you can tell @code{inetd} to reread the
2622 file and obey its new contents by sending the @code{inetd} process the
2623 @code{SIGHUP} signal. You'll have to use @code{ps} to determine the
2624 process ID of the @code{inetd} process, as it is not fixed.
2625
2626 @c !!! could document /etc/inetd.sec
2627
2628 @node Socket Options
2629 @section Socket Options
2630 @cindex socket options
2631
2632 This section describes how to read or set various options that modify
2633 the behavior of sockets and their underlying communications protocols.
2634
2635 @cindex level, for socket options
2636 @cindex socket option level
2637 When you are manipulating a socket option, you must specify which
2638 @dfn{level} the option pertains to. This describes whether the option
2639 applies to the socket interface, or to a lower-level communications
2640 protocol interface.
2641
2642 @menu
2643 * Socket Option Functions:: The basic functions for setting and getting
2644 socket options.
2645 * Socket-Level Options:: Details of the options at the socket level.
2646 @end menu
2647
2648 @node Socket Option Functions
2649 @subsection Socket Option Functions
2650
2651 @pindex sys/socket.h
2652 Here are the functions for examining and modifying socket options.
2653 They are declared in @file{sys/socket.h}.
2654
2655 @comment sys/socket.h
2656 @comment BSD
2657 @deftypefun int getsockopt (int @var{socket}, int @var{level}, int @var{optname}, void *@var{optval}, socklen_t *@var{optlen-ptr})
2658 The @code{getsockopt} function gets information about the value of
2659 option @var{optname} at level @var{level} for socket @var{socket}.
2660
2661 The option value is stored in a buffer that @var{optval} points to.
2662 Before the call, you should supply in @code{*@var{optlen-ptr}} the
2663 size of this buffer; on return, it contains the number of bytes of
2664 information actually stored in the buffer.
2665
2666 Most options interpret the @var{optval} buffer as a single @code{int}
2667 value.
2668
2669 The actual return value of @code{getsockopt} is @code{0} on success
2670 and @code{-1} on failure. The following @code{errno} error conditions
2671 are defined:
2672
2673 @table @code
2674 @item EBADF
2675 The @var{socket} argument is not a valid file descriptor.
2676
2677 @item ENOTSOCK
2678 The descriptor @var{socket} is not a socket.
2679
2680 @item ENOPROTOOPT
2681 The @var{optname} doesn't make sense for the given @var{level}.
2682 @end table
2683 @end deftypefun
2684
2685 @comment sys/socket.h
2686 @comment BSD
2687 @deftypefun int setsockopt (int @var{socket}, int @var{level}, int @var{optname}, void *@var{optval}, socklen_t @var{optlen})
2688 This function is used to set the socket option @var{optname} at level
2689 @var{level} for socket @var{socket}. The value of the option is passed
2690 in the buffer @var{optval}, which has size @var{optlen}.
2691
2692 The return value and error codes for @code{setsockopt} are the same as
2693 for @code{getsockopt}.
2694 @end deftypefun
2695
2696 @node Socket-Level Options
2697 @subsection Socket-Level Options
2698
2699 @comment sys/socket.h
2700 @comment BSD
2701 @deftypevr Constant int SOL_SOCKET
2702 Use this constant as the @var{level} argument to @code{getsockopt} or
2703 @code{setsockopt} to manipulate the socket-level options described in
2704 this section.
2705 @end deftypevr
2706
2707 @pindex sys/socket.h
2708 Here is a table of socket-level option names; all are defined in the
2709 header file @file{sys/socket.h}.
2710
2711 @table @code
2712 @comment sys/socket.h
2713 @comment BSD
2714 @item SO_DEBUG
2715 @c Extra blank line here makes the table look better.
2716
2717 This option toggles recording of debugging information in the underlying
2718 protocol modules. The value has type @code{int}; a nonzero value means
2719 ``yes''.
2720 @c !!! should say how this is used
2721 @c Ok, anyone who knows, please explain.
2722
2723 @comment sys/socket.h
2724 @comment BSD
2725 @item SO_REUSEADDR
2726 This option controls whether @code{bind} (@pxref{Setting Address})
2727 should permit reuse of local addresses for this socket. If you enable
2728 this option, you can actually have two sockets with the same Internet
2729 port number; but the system won't allow you to use the two
2730 identically-named sockets in a way that would confuse the Internet. The
2731 reason for this option is that some higher-level Internet protocols,
2732 including FTP, require you to keep reusing the same socket number.
2733
2734 The value has type @code{int}; a nonzero value means ``yes''.
2735
2736 @comment sys/socket.h
2737 @comment BSD
2738 @item SO_KEEPALIVE
2739 This option controls whether the underlying protocol should
2740 periodically transmit messages on a connected socket. If the peer
2741 fails to respond to these messages, the connection is considered
2742 broken. The value has type @code{int}; a nonzero value means
2743 ``yes''.
2744
2745 @comment sys/socket.h
2746 @comment BSD
2747 @item SO_DONTROUTE
2748 This option controls whether outgoing messages bypass the normal
2749 message routing facilities. If set, messages are sent directly to the
2750 network interface instead. The value has type @code{int}; a nonzero
2751 value means ``yes''.
2752
2753 @comment sys/socket.h
2754 @comment BSD
2755 @item SO_LINGER
2756 This option specifies what should happen when the socket of a type
2757 that promises reliable delivery still has untransmitted messages when
2758 it is closed; see @ref{Closing a Socket}. The value has type
2759 @code{struct linger}.
2760
2761 @comment sys/socket.h
2762 @comment BSD
2763 @deftp {Data Type} {struct linger}
2764 This structure type has the following members:
2765
2766 @table @code
2767 @item int l_onoff
2768 This field is interpreted as a boolean. If nonzero, @code{close}
2769 blocks until the data is transmitted or the timeout period has expired.
2770
2771 @item int l_linger
2772 This specifies the timeout period, in seconds.
2773 @end table
2774 @end deftp
2775
2776 @comment sys/socket.h
2777 @comment BSD
2778 @item SO_BROADCAST
2779 This option controls whether datagrams may be broadcast from the socket.
2780 The value has type @code{int}; a nonzero value means ``yes''.
2781
2782 @comment sys/socket.h
2783 @comment BSD
2784 @item SO_OOBINLINE
2785 If this option is set, out-of-band data received on the socket is
2786 placed in the normal input queue. This permits it to be read using
2787 @code{read} or @code{recv} without specifying the @code{MSG_OOB}
2788 flag. @xref{Out-of-Band Data}. The value has type @code{int}; a
2789 nonzero value means ``yes''.
2790
2791 @comment sys/socket.h
2792 @comment BSD
2793 @item SO_SNDBUF
2794 This option gets or sets the size of the output buffer. The value is a
2795 @code{size_t}, which is the size in bytes.
2796
2797 @comment sys/socket.h
2798 @comment BSD
2799 @item SO_RCVBUF
2800 This option gets or sets the size of the input buffer. The value is a
2801 @code{size_t}, which is the size in bytes.
2802
2803 @comment sys/socket.h
2804 @comment GNU
2805 @item SO_STYLE
2806 @comment sys/socket.h
2807 @comment BSD
2808 @itemx SO_TYPE
2809 This option can be used with @code{getsockopt} only. It is used to
2810 get the socket's communication style. @code{SO_TYPE} is the
2811 historical name, and @code{SO_STYLE} is the preferred name in GNU.
2812 The value has type @code{int} and its value designates a communication
2813 style; see @ref{Communication Styles}.
2814
2815 @comment sys/socket.h
2816 @comment BSD
2817 @item SO_ERROR
2818 @c Extra blank line here makes the table look better.
2819
2820 This option can be used with @code{getsockopt} only. It is used to reset
2821 the error status of the socket. The value is an @code{int}, which represents
2822 the previous error status.
2823 @c !!! what is "socket error status"? this is never defined.
2824 @end table
2825
2826 @node Networks Database
2827 @section Networks Database
2828 @cindex networks database
2829 @cindex converting network number to network name
2830 @cindex converting network name to network number
2831
2832 @pindex /etc/networks
2833 @pindex netdb.h
2834 Many systems come with a database that records a list of networks known
2835 to the system developer. This is usually kept either in the file
2836 @file{/etc/networks} or in an equivalent from a name server. This data
2837 base is useful for routing programs such as @code{route}, but it is not
2838 useful for programs that simply communicate over the network. We
2839 provide functions to access this data base, which are declared in
2840 @file{netdb.h}.
2841
2842 @comment netdb.h
2843 @comment BSD
2844 @deftp {Data Type} {struct netent}
2845 This data type is used to represent information about entries in the
2846 networks database. It has the following members:
2847
2848 @table @code
2849 @item char *n_name
2850 This is the ``official'' name of the network.
2851
2852 @item char **n_aliases
2853 These are alternative names for the network, represented as a vector
2854 of strings. A null pointer terminates the array.
2855
2856 @item int n_addrtype
2857 This is the type of the network number; this is always equal to
2858 @code{AF_INET} for Internet networks.
2859
2860 @item unsigned long int n_net
2861 This is the network number. Network numbers are returned in host
2862 byte order; see @ref{Byte Order}.
2863 @end table
2864 @end deftp
2865
2866 Use the @code{getnetbyname} or @code{getnetbyaddr} functions to search
2867 the networks database for information about a specific network. The
2868 information is returned in a statically-allocated structure; you must
2869 copy the information if you need to save it.
2870
2871 @comment netdb.h
2872 @comment BSD
2873 @deftypefun {struct netent *} getnetbyname (const char *@var{name})
2874 The @code{getnetbyname} function returns information about the network
2875 named @var{name}. It returns a null pointer if there is no such
2876 network.
2877 @end deftypefun
2878
2879 @comment netdb.h
2880 @comment BSD
2881 @deftypefun {struct netent *} getnetbyaddr (long @var{net}, int @var{type})
2882 The @code{getnetbyaddr} function returns information about the network
2883 of type @var{type} with number @var{net}. You should specify a value of
2884 @code{AF_INET} for the @var{type} argument for Internet networks.
2885
2886 @code{getnetbyaddr} returns a null pointer if there is no such
2887 network.
2888 @end deftypefun
2889
2890 You can also scan the networks database using @code{setnetent},
2891 @code{getnetent}, and @code{endnetent}. Be careful in using these
2892 functions, because they are not reentrant.
2893
2894 @comment netdb.h
2895 @comment BSD
2896 @deftypefun void setnetent (int @var{stayopen})
2897 This function opens and rewinds the networks database.
2898
2899 If the @var{stayopen} argument is nonzero, this sets a flag so that
2900 subsequent calls to @code{getnetbyname} or @code{getnetbyaddr} will
2901 not close the database (as they usually would). This makes for more
2902 efficiency if you call those functions several times, by avoiding
2903 reopening the database for each call.
2904 @end deftypefun
2905
2906 @comment netdb.h
2907 @comment BSD
2908 @deftypefun {struct netent *} getnetent (void)
2909 This function returns the next entry in the networks database. It
2910 returns a null pointer if there are no more entries.
2911 @end deftypefun
2912
2913 @comment netdb.h
2914 @comment BSD
2915 @deftypefun void endnetent (void)
2916 This function closes the networks database.
2917 @end deftypefun