Debian Codegraph @

doc/sockets.desc

raw
1 /*=* Global settings.
2 *
3 * package: Sockets
4 * intro:
5 * The @code{Sockets} package contains all the definitions and
6 * subprograms needed to build a simple unicast client or server.
7 =*/
8
9 /*=type Socket_FD
10 *
11 * what: Socket object
12 * def: tagged private
13 * doc:
14 * The @code{Socket_FD} tagged type is the root type of all
15 * sockets. It gets initialized by calling @ref{Socket (procedure)}.
16 * An uninitialized @code{Socket_FD} can be successfully compared to
17 * @code{Null_Socket_FD}.
18 =*/
19
20 /*=subprogram Socket
21 *
22 * what: Create a socket of the given mode
23 * kind: procedure
24 * arg: Socket, out, Socket_FD,, Socket object to initialize
25 * arg: Domain, in, Socket_Domain, PF_INET, Protocol family
26 * arg: Typ, in, Socket_Type, SOCK_STREAM, Kind of sockets
27 * doc:
28 * @ctindex PF_INET
29 * @ctindex AF_INET
30 * @ctindex SOCK_STREAM
31 * @ctindex SOCK_DGRAM
32 * This procedure initializes a new socket object by reserving a file
33 * descriptor to the operating system. For backward compatibility with
34 * older versions of this library, @code{AF_INET} is still accepted as
35 * a value but should be replaced as soon as possible with the proper
36 * @code{PF_INET}. Using @code{SOCK_STREAM} for the @var{Typ}
37 * argument will create a TCP socket while a @code{SOCK_DGRAM} will
38 * create a UDP one.
39 * concept: Creating a socket
40 * example:
41 * $ declare
42 * $ Sock : Socket_FD;
43 * $ begin
44 * $ -- Create a TCP socket
45 * $ Socket (Sock, PF_INET, SOCK_STREAM);
46 * $ -- Perform some operations on socket
47 * $ [...]
48 * $ -- Shutdown the socket in both directions
49 * $ Shutdown (Sock, Both);
50 * $ end;
51 * see: Shutdown (procedure)
52 =*/
53
54 /*=subprogram Connect
55 *
56 * what: Connect a socket on a given host/port
57 * kind: procedure
58 * arg: Socket, in, Socket_FD,, Initialized socket object
59 * arg: Host, in, String,, Host to connect to
60 * arg: Port, in, Positive,, Port to connect to
61 * exc: Connection_Refused, The connection has been refused by the server
62 * exc: Socket_Error, Another error occurred during the connection
63 * doc:
64 * This procedure connects an initialized socket to a given host on
65 * a given port. In the case of a TCP socket, a real connection is
66 * attempted. In the case of a UDP socket, no connection takes place
67 * but the endpoint coordinates are recorded.
68 * concept: Connecting a socket
69 * example:
70 * $ declare
71 * $ Sock : Socket_FD;
72 * $ begin
73 * $ -- Create a TCP socket
74 * $ Socket (Sock, PF_INET, SOCK_STREAM);
75 * $ -- Connect it to rfc1149.net's mail server
76 * $ Connect (Sock, "mail.rfc1149.net", 25);
77 * $ -- Do a mail transaction then close the socket
78 * $ [...]
79 * $ end;
80 * see: Socket (procedure)
81 =*/
82
83 /*=subprogram Bind
84 *
85 * what: Associate a local port to a socket
86 * kind: procedure
87 * arg: Socket, in, Socket_FD,, Initialized socket object
88 * arg: Port, in, Natural,, Local port to bind to
89 * arg: Host, in, String, @w{}"", Local interface to bind to
90 * exc: Socket_Error, Requested port or interface not available
91 * doc:
92 * This procedure requests a local port from the operating
93 * system. If 0 is given in @var{Port}, the system will assign a
94 * free port whose number can later be retrieved using
95 * @ref{Get_Sock_Port (function)}. Also, most operating systems require
96 * special privileges if you want to bind to ports below 1024.
97 *
98 * If @var{Host} is not the empty string, it must contain the
99 * IP address of a local interface to bind to, or a name which
100 * resolves into such an address. If an empty string is given (the
101 * default), the socket will be bound to all the available
102 * interfaces.
103 * concept: Binding a socket
104 * concept: Assigning a local port
105 * concept: Creating a server
106 * see: Listen (procedure)
107 * see: Socket (procedure)
108 =*/
109
110 /*=subprogram Listen
111 *
112 * what: Establish a listen queue
113 * kind: procedure
114 * arg: Socket, in, Socket_FD,, Initialized and bound socket object
115 * arg: Queue_Size, in, Positive, 5, Requested slots in the listen queue
116 * doc:
117 * This procedure establishes a listen queue after a TCP socket as been
118 * initialized and bound. Each slot in the queue can hold one
119 * incoming connection that has not been accepted yet. Note that
120 * most operating systems ignore queue sizes larger than five.
121 * concept: Establishing a listen queue
122 * concept: Listen queue
123 * see: Accept_Socket (procedure)
124 * see: Bind (procedure)
125 * see: Socket (procedure)
126 =*/
127
128 /*=subprogram Getsockopt
129 *
130 * what: Retrieve a socket option
131 * kind: procedure
132 * arg: Socket, in, Socket_FD,, Initialized and bound socket object
133 * arg: Level, in, Socket_Level, SOL_SOCKET, Protocol level
134 * arg: Optname, in, Socket_Option,, Option name
135 * arg: Optval, out, Integer,, Option value
136 * doc:
137 * @ctindex Sockets.SOL_SOCKET
138 * @ctindex Sockets.IPPROTO_IP
139 * @ctindex Sockets.SO_REUSEADDR
140 * @ctindex Sockets.SO_REUSEPORT
141 * @ctindex Sockets.IP_MULTICAST_TTL
142 * @ctindex Sockets.IP_ADD_MEMBERSHIP
143 * @ctindex Sockets.IP_DROP_MEMBERSHIP
144 * @ctindex Sockets.IP_MULTICAST_LOOP
145 * @ctindex Sockets.SO_SNDBUF
146 * @ctindex Sockets.SO_RCVBUF
147 * @ctindex SOL_SOCKET
148 * @ctindex IPPROTO_IP
149 * @ctindex SO_REUSEADDR
150 * @ctindex SO_REUSEPORT
151 * @ctindex IP_MULTICAST_TTL
152 * @ctindex IP_ADD_MEMBERSHIP
153 * @ctindex IP_DROP_MEMBERSHIP
154 * @ctindex IP_MULTICAST_LOOP
155 * @ctindex SO_SNDBUF
156 * @ctindex SO_RCVBUF
157 * This procedure retrieves options applicable to a socket. Please
158 * see your operating system manual for usable levels and options.
159 *
160 * Two levels are defined: @code{SOL_SOCKET} (the default) and
161 * @code{IPPROTO_IP}. The options are @code{SO_REUSEADDR},
162 * @code{SO_REUSEPORT}, @code{IP_MULTICAST_TTL},
163 * @code{IP_ADD_MEMBERSHIP}, @code{IP_DROP_MEMBERSHIP},
164 * @code{IP_MULTICAST_LOOP}, @code{SO_SNDBUF} and
165 * @code{IP_RCVBUF}.
166 *
167 * Note that unlike their C language counterpart, @code{Getsockopt}
168 * and @code{Setsockopt} do not require an extra parameter
169 * representing the length in bytes of the option value. AdaSockets
170 * knows the right size for every option.
171 * concept: Manipulating socket options
172 * concept: Retrieving socket options
173 * see: Setsockopt (procedure)
174 =*/
175
176 /*=subprogram Setsockopt
177 *
178 * what: Set a socket option
179 * kind: procedure
180 * arg: Socket, in, Socket_FD,, Initialized and bound socket object
181 * arg: Level, in, Socket_Level, SOL_SOCKET, Protocol level
182 * arg: Optname, in, Socket_Option,, Option name
183 * arg: Optval, in, Integer,, Option value
184 * doc:
185 * @ctindex Sockets.SOL_SOCKET
186 * @ctindex Sockets.IPPROTO_IP
187 * @ctindex Sockets.SO_REUSEADDR
188 * @ctindex Sockets.SO_REUSEPORT
189 * @ctindex Sockets.IP_MULTICAST_TTL
190 * @ctindex Sockets.IP_ADD_MEMBERSHIP
191 * @ctindex Sockets.IP_DROP_MEMBERSHIP
192 * @ctindex Sockets.IP_MULTICAST_LOOP
193 * @ctindex Sockets.SO_SNDBUF
194 * @ctindex Sockets.SO_RCVBUF
195 * @ctindex SOL_SOCKET
196 * @ctindex IPPROTO_IP
197 * @ctindex SO_REUSEADDR
198 * @ctindex SO_REUSEPORT
199 * @ctindex IP_MULTICAST_TTL
200 * @ctindex IP_ADD_MEMBERSHIP
201 * @ctindex IP_DROP_MEMBERSHIP
202 * @ctindex IP_MULTICAST_LOOP
203 * @ctindex SO_SNDBUF
204 * @ctindex SO_RCVBUF
205 * This procedure sets options applicable to a socket. Please
206 * see your operating system manual for usable levels and options.
207 *
208 * Two levels are defined: @code{SOL_SOCKET} (the default) and
209 * @code{IPPROTO_IP}. The options are @code{SO_REUSEADDR},
210 * @code{SO_REUSEPORT}, @code{IP_MULTICAST_TTL},
211 * @code{IP_ADD_MEMBERSHIP}, @code{IP_DROP_MEMBERSHIP},
212 * @code{IP_MULTICAST_LOOP}, @code{SO_SNDBUF} and
213 * @code{IP_RCVBUF}.
214 *
215 * Note that unlike their C language counterpart, @code{Getsockopt}
216 * and @code{Setsockopt} do not require an extra parameter
217 * representing the length in bytes of the option value. AdaSockets
218 * knows the right size for every option.
219 * concept: Manipulating socket options
220 * concept: Setting socket options
221 * see: Getsockopt (procedure)
222 =*/
223
224 /*=subprogram Accept_Socket
225 *
226 * what: Accept an incoming connection
227 * kind: procedure
228 * arg: Socket, in, Socket_FD,, Initialized, bound and listening socket object
229 * arg: New_Socket, out, Socket_FD,, Incoming socket object
230 * doc:
231 * This procedure creates a new socket corresponding to an incoming
232 * connection on TCP socket @var{Socket}. All the communications
233 * with the peer will take place on @var{New_Socket}, while the
234 * program can accept another connection on @var{Socket}.
235 *
236 * @var{New_Socket} must not be initialized before calling this
237 * procedure, or must have been cleaned up by calling
238 * @code{Shutdown}, in order to avoid a file descriptors leak.
239 *
240 * @code{Accept_Socket} will block until an incoming connection is
241 * ready to be accepted.
242 * concept: Accepting a new connection
243 * concept: Handling a new connection
244 * example:
245 * $ declare
246 * $ Sock : Socket_FD;
247 * $ Incoming : Socket_FD;
248 * $ begin
249 * $ -- Create a TCP socket listening on local port 4161
250 * $ Socket (Sock, PF_INET, SOCK_STREAM);
251 * $ Bind (Sock, 4161);
252 * $ Listen (Sock, 3);
253 * $ -- One-connection-at-a-time server (3 may be pending)
254 * $ loop
255 * $ -- Wait for a new connection and accept it
256 * $ Accept_Socket (Sock, Incoming);
257 * $ -- Do some dialog with the remote host
258 * $ Do_Some_Dialog (Incoming);
259 * $ -- Close incoming socket and wait for next connection
260 * $ Shutdown (Incoming);
261 * $ end loop;
262 * $ end;
263 * see: Bind (procedure)
264 * see: Listen (procedure)
265 * see: Shutdown (procedure)
266 * see: Socket (procedure)
267 =*/
268
269 /*=subprogram Send
270 *
271 * what: Send raw data over a socket
272 * kind: procedure
273 * arg: Socket, in, Socket_FD,, Initialized and connected socket object
274 * arg: Data, out, Ada.Streams.Stream_Element_Array,, Data to be sent
275 * doc:
276 * @tindex Ada.Streams.Stream_Element_Array
277 * @tindex Stream_Element_Array
278 * This procedure sends data over a connected outgoing socket or
279 * over an incoming socket.
280 * concept: Sending data
281 * exc: Connection_Closed, Peer has prematurely closed the connection
282 * see: Put (procedure)
283 * see: Put_Line (procedure)
284 =*/
285
286 /*=subprogram Receive
287 *
288 * what: Receive raw data over a socket
289 * kind: function
290 * arg:
291 * Socket, in, Socket_FD,, Initialized and bound or connected socket object
292 * arg: Max, in, Ada.Streams.Stream_Element_Count, 4096, Maximum data length
293 * ret: Ada.Streams.Stream_Element_Array, Received raw data
294 * doc:
295 * @tindex Ada.Streams.Stream_Element_Count
296 * @tindex Ada.Streams.Stream_Element_Array
297 * @tindex Stream_Element_Count
298 * @tindex Stream_Element_Array
299 * This procedure receives data from a bound UDP socket or a connected
300 * TCP socket. Only one system call will be performed; this
301 * function will return whatever data has arrived. Note that in
302 * GNAT the secondary stack may be used to store the data and may
303 * result in stack storage exhaustion.
304 * concept: Receiving data
305 * exc:
306 * Connection_Closed, Peer has closed the connection before sending any data
307 * see: Receive (procedure)
308 * see: Receive_Some (procedure)
309 * see: Get_Line (function)
310 * see: Get_Line (procedure)
311 =*/
312
313 /*=subprogram Receive
314 *
315 * what: Receive raw data over a socket
316 * kind: procedure
317 * arg:
318 * Socket, in, Socket_FD'Class,,
319 * Initialized and bound or connected socket object
320 * arg: Data, out, Ada.Streams.Stream_Element_Array,, Incoming data buffer
321 * doc:
322 * @tindex Ada.Streams.Stream_Element_Array
323 * @tindex Stream_Element_Array
324 * This procedure receives data from a bound UDP socket or a connected
325 * TCP socket. It will block until the @var{Data} reception buffer
326 * has been totally filled.
327 * concept: Receiving data
328 * exc:
329 * Connection_Closed,
330 * Peer has closed the connection before @code{Data'Length} bytes@*
331 * were received
332 * see: Get_Line (function)
333 * see: Get_Line (procedure)
334 * see: Receive (function)
335 * see: Receive_Some (procedure)
336 =*/
337
338 /*=subprogram Receive_Some
339 *
340 * what: Receive raw data over a socket
341 * kind: procedure
342 * arg:
343 * Socket, in, Socket_FD'Class,,
344 * Initialized and bound or connected socket object
345 * arg: Data, out, Ada.Streams.Stream_Element_Array,, Incoming data buffer
346 * arg:
347 * Last, out, Ada.Streams.Stream_Element_Offset,,
348 * Index of last element placed into @var{Data}
349 * doc:
350 * @tindex Ada.Streams.Stream_Element_Array
351 * @tindex Stream_Element_Array
352 * @tindex Ada.Streams.Stream_Element_Count
353 * @tindex Stream_Element_Count
354 * This procedure receives data from a bound UDP socket or a connected
355 * TCP socket. As soon as at least one byte has been read, it returns
356 * with @var{Last} set to the index of the latest written element
357 * of @var{Data}.
358 * concept: Receiving data
359 * exc:
360 * Connection_Closed,
361 * Peer has closed the connection before sending any data
362 * see: Get_Line (function)
363 * see: Get_Line (procedure)
364 * see: Receive (function)
365 * see: Receive (procedure)
366 =*/
367
368 /*=subprogram Shutdown
369 *
370 * what: Shutdown a socket
371 * kind: procedure
372 * arg: Socket, in out, Socket_FD,, Socket object to shutdown
373 * arg: How, in, Shutdown_Type, Both, Direction to shutdown
374 * doc:
375 * @ctindex Send
376 * @ctindex Receive
377 * @ctindex Both
378 * This procedure shutdowns either direction of the socket. @var{How}
379 * can take the value @samp{Send}, @samp{Receive} or @samp{Both}.
380 * concept: Closing a socket
381 * concept: Socket shutdown
382 * see: Socket (procedure)
383 =*/
384
385 /*=subprogram Put
386 *
387 * what: Send a string to a remote host
388 * kind: procedure
389 * arg: Socket, in, Socket_FD'Class,, Initialized and connected socket object
390 * arg: Str, in, String,, String to send
391 * doc:
392 * This procedure sends the content of @var{Str} over an outgoing
393 * or incoming socket.
394 * concept: Sending data
395 * exc: Connection_Closed, Peer has prematurely closed the connection
396 * see: New_Line (procedure)
397 * see: Put_Line (procedure)
398 * see: Send (procedure)
399 =*/
400
401 /*=subprogram New_Line
402 *
403 * what: Send a CR/LF to a remote host
404 * kind: procedure
405 * arg: Socket, in, Socket_FD'Class,, Initialized and connected socket object
406 * arg: Count, in, Natural, 1, Number of CR/LF sequences to send
407 * doc:
408 * @ctindex CR
409 * @ctindex LF
410 * This procedure sends one or more CR/LF combinations to the peer.
411 * concept: Sending data
412 * exc: Connection_Closed, Peer has prematurely closed the connection
413 * see: Put (procedure)
414 * see: Put_Line (procedure)
415 =*/
416
417 /*=subprogram Put_Line
418 *
419 * what: Send a CR/LF terminated string to a remote host
420 * kind: procedure
421 * arg: Socket, in, Socket_FD'Class,, Initialized and connected socket object
422 * arg: Str, in, String,, String to send
423 * doc:
424 * This procedure sends the content of @var{Str} plus a CR/LF combination
425 * over an outgoing or incoming socket.
426 * concept: Sending data
427 * exc: Connection_Closed, Peer has prematurely closed the connection
428 * see: New_Line (procedure)
429 * see: Put (procedure)
430 * see: Send (procedure)
431 =*/
432
433 /*=subprogram Get
434 *
435 * what: Get a string from a remote host
436 * kind: function
437 * arg: Socket, in, Socket_FD'Class,, Initialized and connected socket object
438 * ret: String, Some characters that have been received
439 * doc:
440 * This function receives some characters from a remote host.
441 * As soon that at least one character is available, the current
442 * reception buffer is returned.
443 *
444 * There is usually little gain in using this function whose behavior is
445 * comparable to the one of @ref{Receive (function)}. Other
446 * functions such as @ref{Get_Char (function)}, or
447 * @ref{Get_Line (function)}, allow more structured programming.
448 *
449 * However, this function may be used to avoid losing characters
450 * when calling @ref{Unset_Buffer (procedure)}, if, for some reason,
451 * the remote host may have sent some.
452 * concept: Receiving data
453 * exc:
454 * Connection_Closed,
455 * Peer has closed the connection before sending any data
456 =*/
457
458 /*=subprogram Get_Char
459 *
460 * what: Get a character from a remote host
461 * kind: function
462 * arg: Socket, in, Socket_FD'Class,, Initialized and connected socket object
463 * ret: Character, One character sent by the remote host
464 * doc:
465 * This function receives exactly one character from the remote host.
466 * concept: Receiving data
467 * exc:
468 * Connection_Closed,
469 * Peer has closed the connection before sending the character
470 * see: Get (function)
471 * see: Get_Line (function)
472 * see: Get_Line (procedure)
473 * see: Receive (procedure)
474 * see: Set_Buffer (procedure)
475 =*/
476
477 /*=subprogram Get_Line
478 *
479 * what: Get a whole line from a remote host
480 * kind: function
481 * arg: Socket, in, Socket_FD'Class,, Initialized and connected socket object
482 * arg: Max_Length, in, Positive, 2048, Maximum returned line length
483 * ret: String, A line without the CR and LF separators
484 * doc:
485 * @ctindex CR
486 * @ctindex LF
487 * This function receives one line from the remote host. A line
488 * consists into zero or more characters followed by an optional CR
489 * and by a LF. Those terminators are stripped before the line is
490 * returned.
491 *
492 * This function blocks until one full line has been received. The line
493 * length is limited with the value of the Max_Length argument, to avoid
494 * exhaustion of the secondary stack.
495 * concept: Receiving data
496 * exc:
497 * Connection_Closed,
498 * Peer has closed the connection before sending a whole line
499 * see: Get (function)
500 * see: Get_Char (function)
501 * see: Get_Line (procedure)
502 * see: Receive (procedure)
503 * see: Set_Buffer (procedure)
504 =*/
505
506 /*=subprogram Get_Line
507 *
508 * what: Get a whole line from a remote host
509 * kind: procedure
510 * arg: Socket, in, Socket_FD'Class,, Initialized and connected socket object
511 * arg: Str, in out, String,, String to fill
512 * arg: Last, out, Natural,, Last index used in the string
513 * doc:
514 * @ctindex CR
515 * @ctindex LF
516 * This procedure receives one line from the remote host. A line
517 * consists into zero or more characters followed by an optional CR
518 * and by a LF. Those terminators are stripped before the line is
519 * returned.
520 *
521 * This procedure blocks until one full line has been received.
522 * concept: Receiving data
523 * exc:
524 * Connection_Closed,
525 * Peer has closed the connection before sending a whole line
526 * see: Get (function)
527 * see: Get_Char (function)
528 * see: Get_Line (function)
529 * see: Receive (procedure)
530 * see: Set_Buffer (procedure)
531 =*/
532
533 /*=subprogram Set_Buffer
534 *
535 * what: Install a line-oriented buffer of the socket object
536 * kind: procedure
537 * arg: Socket, in, Socket_FD'Class,, Initialized and connected socket object
538 * arg: Length, in, Positive, 1500, Size in bytes of the newly installed buffer
539 * doc:
540 * This procedure puts the socket object in buffered mode. If the
541 * socket was already buffered, the content of the previous buffer
542 * will be lost. The buffered mode only affects character- and
543 * line-oriented read operation such as @ref{Get (function)},
544 * @ref{Get_Char (function)}, and @ref{Get_Line (function)}. Other
545 * reception subprograms will not function properly if buffered mode
546 * is used at the same time.
547 *
548 * The size of the buffer has to be greater than the biggest
549 * possible packet sent by the remote host, otherwise data loss may
550 * occur.
551 * concept: Receiving data
552 * see: Unset_Buffer (procedure)
553 =*/
554
555 /*=subprogram Unset_Buffer
556 *
557 * what: Deinstall the line-oriented buffer of the socket object
558 * kind: procedure
559 * arg: Socket, in, Socket_FD'Class,, Initialized and connected socket object
560 * doc:
561 * This procedure uninstalls the buffer previously installed by
562 * @ref{Set_Buffer (procedure)}. If any data is still present in
563 * the buffer, it will be lost. To avoid this situation, the buffer
564 * can be flushed by calling @ref{Get (function)}.
565 * concept: Receiving data
566 =*/
567
568 /*=subprogram Get_Send_Queue_Size
569 *
570 * what: Return the size of the unsent data in the output queue
571 * kind: function
572 * arg: Socket, in, Socket_FD'Class,, Initialized and connected socket object
573 * ret: Integer, The non-negative size in bytes of the output queue, or a negative value in case of error
574 * doc:
575 * This function is only available on Linux platforms. On other operating
576 * systems, it will return -2 to indicate that the necessary mechanisms to
577 * retrieve the information are not available.
578 * concept: Sending data
579 * see: Send (procedure)
580 =*/
581
582 /*=subprogram Get_Receive_Queue_Size
583 *
584 * what: Return the size of the unread data in the input queue
585 * kind: function
586 * arg: Socket, in, Socket_FD'Class,, Initialized and connected socket object
587 * ret: Integer, The non-negative size in bytes of the input queue, or a negative value in case of error
588 * doc:
589 * This function is only available on Linux platforms. On other operating
590 * systems, it will return -2 to indicate that the necessary mechanisms to
591 * retrieve the information are not available.
592 * concept: Receiving data
593 * see: Receive (procedure)
594 =*/