Added close() method to stream classes.
[sockstream.git] / doc / design.xml
1 <?xml version="1.0"?>
2 <!DOCTYPE doc SYSTEM "/usr/local/xslt/doc.dtd">
3 <doc style="main.css"
4      xmlns:xi="http://www.w3.org/2001/XInclude">
5
6 <!--
7       Network Communication Class Library
8       Original author :  Arjen Baart - arjen@andromeda.nl
9       Version         : 0.1
10
11       This document is prepared for XMLDoc. Transform to HTML,
12       LaTeX, Postscript or plain text with XMLDoc utilities and
13       XSLT sheets from http://www.andromeda.nl/projects/xmldoc/
14 -->
15
16 <report>
17 <titlepage>
18    <title>Network Communication Class Library</title>
19    <subtitle>Iostreams and sockets in C++</subtitle>
20
21    <author>Arjen Baart <code>&lt;arjen@andromeda.nl&gt;</code></author>
22    <date>Mar 09, 2012</date>
23    <docinfo>
24       <infoitem label="Version">0.2</infoitem>
25       <infoitem label="Organization">Andromeda Technology &amp; Automation</infoitem>
26    </docinfo>
27    <abstract>
28    The network communication class library encapsulates the UNIX socket layer
29    for network communication.
30    </abstract>
31 </titlepage>
32
33 <toc/>
34
35
36 <chapter>
37 <heading>Problem Statement</heading>
38 <para>
39 The way processes communicate on the internet is through the use of -sockets-.
40 The socket layer provides the interface between user processes and the network
41 protocol stacks in the kernel of an operating system. A process can use a large
42 variation of sockets, depending on the type of communication such as streams or
43 datagrams or depending on the address familiy that is used like IPv6 or UNIX.
44 </para>
45 <para>
46 When two processes communicate through sockets, there is a socket on either side
47 of the communication link. In other words, each process has its own socket while
48 the underlying network stacks transfer the data from one socket to the other
49 and vice versa. Which sockets take part in the communication is determined by
50 the -host- and the -service- on which the socket resides. In many cases, the
51 -service- also dictates the way the information is exchanged between the processes
52 that hold the sockets.
53 </para>
54 <para>
55 In C++ standard iostream classes and objects exist to perform input and output
56 operations on the controlling terminal of a process ('cin' and 'cout'), on files
57 and device streams and on blocks of memory holding character strings.
58 The socket stream classes provide this functionality for sockets in a way that
59 is intended to be easy to use for the application programmer.
60 Classes similar to the fstream and stringstream classes implement reading and writing
61 data to and from sockets.
62 At the highest level, an application is able to read data directly by specifying
63 a URL, for example:
64 <verbatim>
65    string page;
66    link = isockstream("http://www.andromeda.nl/index.html");
67    link >> page;
68 </verbatim>
69 At a slightly lower level, an application program would open a stream which
70 connects to a host or address and a specific service.
71 On the other hand, a server application would create a listening socket on
72 a socket address and a service.
73 Such a socket waits for incoming connection requests and after accepting a connection,
74 a new socket is created through which the actual communication takes place.
75 The new socket can then be used in creating an iostream object.
76 Note that there can never be data transfer through a listening socket.
77
78 </para>
79 <para>
80 A socket needs an address to listen on or to connect to. Depending on the address
81 family used, a socket address has a different set of attributes, such as a pathname
82 for UNIX local addresses or an IP address and port number for internet addresses.
83 Also, diffrent kinds of addresses have different sets of operations defined on them.
84
85 </para>
86 <para>
87 Internally, sockets use numerical addresses and port numbers, whereas human users
88 and programmers may prefer names for these entities. The conversion of such names
89 into numbers and vice versa involves resolving through lookup systems such as DNS.
90 A host is usually identifeid by its name and has one or more addresses, IPv4, IPv6
91 or other possible addresses. Although a host can have multiple addresses, a socket
92 can connect to only one address. A service can have a similar problem. One service,
93 as identified by name, may be available on multiple ports. It is in general up to an
94 application to make appropriate selections in a multitude of addresses, although
95 sensible defaults may be provided by address objects.
96
97 </para>
98
99 <section>
100 <heading>TODO:</heading>
101
102 <itemize>
103 <item>higher-level protocols such as http</item>
104 <item>SSL/TLS support</item>
105 </itemize>
106
107 </section>
108
109 </chapter>
110
111
112 <chapter>
113 <heading>CLASSES</heading>
114
115 <para>
116 The following class diagram shows the classes and their relationships:
117 </para>
118
119  <picture src='classes.png' eps='classes' scale='0.7'/>
120
121 <para>
122 The top-level classes are very similar to the standard iostream classes.
123 The classes sockstream, isockstream, osockstream and sockbuf are almost the
124 same as their fstream, ifstream, ofstream and filebuf counterparts.
125 The key class in the hierarchy is the sockbuf class that holds a close relation
126 to the Socket class and performs the actual I/O operations.
127 Depending on the type of communication, two types of Socket objects are possible:
128 Stream and Datagram.
129 </para>
130 <para>
131 The SocketAddress determines what a Socket connects to or listens to.
132 For most Sockets, a SocketAddress basicly consists of a port and an IP address
133 which are determined from the Service and Host for which the socket is
134 intended. Only UNIX domain sockets are defined by a single pathname of the
135 socket device.
136 Depending on the address family that is used for the Socket Address, one of the
137 three kinds of Socket Address is used to connect a socket: UNIX, IPv4 or IPv6.
138 It is up to an application programmer to figure out where a Sockets connects to
139 and create an appropriate type of SocketAddress to which a Socket can connect
140 or listen.
141 </para>
142 <para>
143 Classes to help an application setup a Socket Address are Host and Service, which
144 perform lookup functions as implemented in getaddrinfo(3).
145 </para>
146
147 </chapter>
148
149 <chapter>
150 <heading>SCENARIOS</heading>
151
152 <section>
153 <heading>Make a stream socket connected to a specific host and service</heading>
154
155 <enumerate>
156 <item>Application creates a Host object with the desired hostname.</item>
157
158 <item>Host object finds IP addresses and returns the list.</item>
159
160 <item>Application selects one IP address from the list.</item>
161
162 <item>Application creates a Service object with the desired name or port number.</item>
163
164 <item>Service object determines a list of available port addresses.</item>
165
166 <item>Application selects one service port from the list.</item>
167
168 <item>Application creates an object derived from SocketAddres, using the selected Internet Address and Service.</item>
169
170 <item>Application creates a Stream Socket object and tells it to connect to the Socket Address.</item>
171
172 </enumerate>
173
174  <picture src='scenarios-1.png' eps='scenarios-1' scale='0.7'/>
175
176 </section>
177 <section>
178 <heading>Setup a listening stream socket</heading>
179
180 <enumerate>
181 <item>Application creates a Service object with the desired name or port number.</item>
182
183 <item>Service object determines a list of available port addresses.</item>
184
185 <item>Application selects one service port from the list.</item>
186
187 <item>Application creates an object derived from SocketAddres, using the ANY Internet Address and Service.</item>
188
189 <item>Application creates a Stream Socket object and tells it to listen on the Socket Address.</item>
190
191 </enumerate>
192
193  <picture src='scenarios-2.png' eps='scenarios-2' scale='0.7'/>
194
195 </section>
196 </chapter>
197
198 <chapter>
199 <heading>MEMBERS and METHODS</heading>
200
201
202 <section>
203 <heading>Socket</heading>
204
205 <para>
206 The Socket represents the device through which the actual communication takes place.
207 This class encapsulates the socket interface in the UNIX or Linux kernel.
208 A Socket is itself an abstract base class from which two types of Socket are derived:
209 A StreamSocket and a DatagramSocket. Each of these classes have special properties
210 and only the common parts are defined in the Socket base class.
211 </para>
212
213 <subsection>
214 <heading>Members</heading>
215
216 <description>
217
218 <item tag='fd'>
219 The UNIX file descriptor of the socket in the kernel.
220 </item>
221
222 </description>
223
224 </subsection>
225 <subsection>
226 <heading>Methods</heading>
227
228 <description>
229 <item tag='Socket()'>
230 Default contructor. Initializes members.
231 </item>
232
233 <item tag='~Socket()'>
234 Destructor. Closes the UNIX socket.
235 </item>
236
237 <item tag='Listen(SocketAddress)'>
238 Creates a server socket. Implmented in derived classes.
239 </item>
240
241 <item tag='Close()'>
242 Close the UNIX socket without destroying the object itself.
243 </item>
244 </description>
245 </subsection>
246 <subsection>
247 <heading>Exceptions</heading>
248 </subsection>
249
250 </section>
251
252 <section>
253 <heading>StreamSocket</heading>
254
255 <para>
256 Implements a socket of the SOCK_STREAM type.
257 Note that a StreamSocket object is either listening for incoming connection requests
258 or is connected to a peer socket.
259 </para>
260
261 <subsection>
262 <heading>Methods</heading>
263
264 <description>
265 <item tag='Listen(SocketAddress)'>
266 Bind the socket to the address and put the socket in the listening state.
267 </item>
268
269 <item tag='Connect(SocketAddress)'>
270 Make a connection to a server socket.
271 </item>
272
273 <item tag='Socket Accept()'>
274 Accept a client connection and return the new connected socket.
275 </item>
276
277 <item tag='Read()'>
278 Input data from the other side.
279 </item>
280
281 <item tag='Write()'>
282 Output data to the other side.
283 </item>
284
285 </description>
286 </subsection>
287 </section>
288
289
290 <section>
291 <heading>DatagramSocket</heading>
292
293 <para>
294 Implements a socket of the SOCK_DGRAM type.
295 </para>
296
297 <subsection>
298 <heading>Methods</heading>
299
300 <description>
301 <item tag='Listen(SocketAddress)'>
302 Bind the socket to the address and wait for incoming messages.
303 </item>
304
305 <item tag='SendTo()'>
306 Send data to another socket.
307 </item>
308
309 <item tag='ReceiveFrom()'>
310 Receive data from another socket.
311 </item>
312
313 </description>
314 </subsection>
315 </section>
316
317 <section>
318 <heading>SocketAddress</heading>
319
320 <para>
321 The abstract base class for all kinds of socket addresses.
322 The SocketAddress class provides an interface for binding and connecting
323 sockets.
324 </para>
325 <subsection>
326 <heading>Methods</heading>
327 <description>
328 <item tag='address_family()'>
329 Return the address family of the socket address.
330 Since this function is pure virtual, it must be overridden in a derived class.
331 The dervied classes described in following sections will return AF_UNIX, AF_INET
332 or AF_INET6.
333 </item>
334 <item tag='get_sockaddr()'>
335 Return the <code>sockaddr</code> pointer that can be used by the <strong>bind(2)</strong>
336 or <strong>connect(2)</strong> system calls.
337 </item>
338 <item tag='get_socklen()'>
339 Return the length of the <code>sockaddr</code> structure that can be used by the <strong>bind(2)</strong>
340 or <strong>connect(2)</strong> system calls.
341 </item>
342 </description>
343 </subsection>
344 <subsection>
345 <heading>Exceptions</heading>
346 </subsection>
347
348 </section>
349
350 <section>
351 <heading>UNIXSocketAddress</heading>
352
353 <para>
354 Implementation of SocketAddress for UNIX domain sockets.
355 </para>
356 <subsection>
357 <heading>Methods</heading>
358 <description>
359 <item tag='UNIXSocketAddress()'>
360 The constructor.
361 </item>
362 </description>
363 </subsection>
364 </section>
365
366 <section>
367 <heading>IPv4SocketAddress</heading>
368
369 <para>
370 Implementation of SocketAddress for IPv4 domain sockets.
371 </para>
372 <subsection>
373 <heading>Methods</heading>
374 <description>
375 <item tag='IPSocketAddress()'>
376 The constructor.
377 </item>
378 </description>
379 </subsection>
380 </section>
381
382 <section>
383 <heading>IPv6SocketAddress</heading>
384
385 <para>
386 Implementation of SocketAddress for IPv6 domain sockets.
387 </para>
388 </section>
389
390 <section>
391 <heading>Service</heading>
392 <para>
393 A Service is determined by its name, port number and service type (stream or datagram).
394 </para>
395 <subsection>
396 <heading>Methods</heading>
397 <description>
398 <item tag='Service()'>
399 The overloaded constructor can accept a string with a service name or a port number.
400 </item>
401 <item tag='FindAddress()'>
402 Find all port numbers of the service, given its name.
403 Since a single service can be available on more than one port, this creates a list of
404 port numbers.
405 </item>
406 <item tag='FindName()'>
407 Find the name of the service that belongs to the first port number in the list
408 of port numbers.
409 </item>
410 </description>
411 </subsection>
412 <subsection>
413 <heading>Exceptions</heading>
414 </subsection>
415
416 </section>
417
418 <section>
419 <heading>Port</heading>
420 <para>
421 A Port is determined by its port number and socket type (stream, datagram, etcetera).
422 </para>
423 <subsection>
424 <heading>Members</heading>
425 <description>
426 <item tag='port'>
427 The port number in network byte order.
428 </item>
429 <item tag='sockettype'>
430 The type of socket on which the service is available.
431 Can be stream (SOCK_STREAM), datagram (SOCK_DGRAM) or any other type as described
432 in <strong>socket(2)</strong>.
433 </item>
434 </description>
435 </subsection>
436
437 <subsection>
438 <heading>Methods</heading>
439 <description>
440 <item tag='get_port()'>
441 The port number in network byte order.
442 </item>
443 <item tag='get_sockettype()'>
444 The type of socket on which the service is available.
445 </item>
446 <item tag='operator unsigned short()'>
447 The port number in host byte order.
448 </item>
449 <item tag='operator ==()'>
450 </item>
451 <item tag='operator !=()'>
452 </item>
453 </description>
454 </subsection>
455 <subsection>
456 <heading>Exceptions</heading>
457 </subsection>
458
459 </section>
460
461 <section>
462 <heading>Host</heading>
463
464 <subsection>
465 <heading>Methods</heading>
466 <description>
467 <item tag='Host()'>
468 The overloaded constructor can accept a string with a hostname or an InternetAddress
469 object.
470 </item>
471 <item tag='FindAddress()'>
472 Find all network addresses of the host, given its name.
473 Since a single host can hold multiple IP addresses, this creates a list of
474 InternetAddress objects.
475 </item>
476 <item tag='FindName()'>
477 Find the name of the host that belongs to the first IP address in the list
478 of InternetAddress objects.
479 </item>
480 </description>
481
482 </subsection>
483 <subsection>
484 <heading>Exceptions</heading>
485 </subsection>
486
487 </section>
488
489 <section>
490 <heading>InternetAddress</heading>
491 </section>
492
493 <para>
494 An InternetAddress object holds an IP address which can be either an IPv4 address or
495 an IPv6 address.
496 Its main purpose is to perform the conversion between addresses in network byte order
497 end textual representations of an IP address.
498 </para>
499
500 <subsection>
501 <heading>Members</heading>
502 <description>
503 <item tag='af'>
504 The address family, AF_UNSPEC, AF_INET of AF_INET6
505 </item>
506 <item tag='inet_addr'>
507 The address itself, in network byte order. Large enough to hold a IPv6 address (16 bytes).
508 </item>
509 </description>
510 </subsection>
511
512 <subsection>
513 <heading>Methods</heading>
514 <description>
515 <item tag='InternetAddress()'>
516 Constructor creates the IP address from a variaty of forms, including conversion
517 from a String.
518 The address family (AF_INET or AF_INET6) is determined from the argument passed to
519 the constructor.
520 Possible argument types are <code>struct addrinfo</code> as used by <strong>getaddrinfo(3)</strong>,
521 <code>struct in_addr</code>, <code>struct in6_addr</code> and <code>String</code>
522 With the IP address in character string form, the address family is determined by the presence
523 of colon (:) characters in the IP address, as these can only occur in IPv6 addresses.
524 See also <strong>inet_pton(3)</strong>
525 </item>
526 <item tag='address_family()'>
527 Returns the address family to which the IP address belongs. Either AF_INET of AF_INET6.
528 </item>
529 <item tag='String()'>
530 Converts the IP address to textual form in a manner like <strong>inet_ntop(3)</strong>.
531 </item>
532 <item tag='get_in_addr()'>
533 Returns an IPv4 address in <code>struct in_addr</code> format.
534 </item>
535 <item tag='get_in6_addr()'>
536 Returns an IPv6 address in <code>struct in6_addr</code> format.
537 </item>
538 <item tag='operator ==()'>
539 Two internet addresses are equal if both the address family and the address
540 itself of the internet addresses are equal.
541 </item>
542 <item tag='operator !=()'>
543 The opposite of the == operator.
544 </item>
545 </description>
546 </subsection>
547
548 <subsection>
549 <heading>Exceptions</heading>
550 </subsection>
551
552 </chapter>
553 </report>
554 </doc>