1 /*
2  * Copyright (C) 2001-2004 Peter J Jones (pjones@pmade.org)
3  * All Rights Reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  * 3. Neither the name of the Author nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
23  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /** @file
34  * This file contains the definition of the Netxx::Address class.
35 **/
36 
37 #ifndef _netxx_address_h_
38 #define _netxx_address_h_
39 
40 // Netxx includes
41 #include "types.h"
42 #include "peer.h"
43 
44 // standard includes
45 #include <string>
46 #include <vector>
47 
48 namespace Netxx {
49 
50 /**
51  * The Netxx::Address class is used by Netxx as a container for network
52  * addresses. An address can be used by a server to specify which local
53  * address and port to bind to. An address can also be used by a client to
54  * know which remote host to connect to and on which port.
55  *
56  * In Netxx, the address also specifies other connection related
57  * information, such as the connection protocol.
58 **/
59 class Address {
60 public:
61     /// address container type
62     typedef std::vector<Peer> container_type;
63 
64     /// address const_iterator type
65     typedef container_type::const_iterator const_iterator;
66 
67     //####################################################################
68     /**
69      * Netxx::Address class constructor. This constructor is designed to be
70      * used by clients wanting the address information about a server. The
71      * url parameter can be a host name with or without a protocol, port
72      * number or path. Here are some examples: pmade.org pmade.org:80
73      * http://pmade.org http://pmade.org:80/index.html.
74      *
75      * Here is the order in which the port number is determined. (1) from
76      * the port part of the hostname as in pmade.org:80, (2) from the given
77      * protocol part such as http:// and (3) from the given default port.
78      *
79      * @param uri A string as described above
80      * @param default_port The default port number for this address
81      * @param use_ipv6 Allow the use of IPv6 if compiled in.
82      * @author Peter Jones
83     **/
84     //####################################################################
85     explicit Address (const char *uri, port_type default_port=0, bool use_ipv6=false);
86 
87     //####################################################################
88     /**
89      * Netxx::Address class constructor. This constructor is designed to be
90      * used by servers when they need to specify the local addresses and
91      * ports to which it will bind.
92      *
93      * After the constructor is called, you will want to add addresses to
94      * the address list using one of the appropriate member functions such
95      * as add_address.
96      *
97      * @param use_ipv6 Allow the use of IPv6 if compiled in.
98      * @author Peter Jones
99     **/
100     //####################################################################
101     explicit Address (bool use_ipv6=false);
102 
103     //####################################################################
104     /**
105      * Netxx::Address class destructor.
106      *
107      * @author Peter Jones
108     **/
109     //####################################################################
110     ~Address (void);
111 
112     //####################################################################
113     /**
114      * If the URI that was given in the constructor had a protocol specifier
115      * such as http:// this member function will return that string.
116      * Otherwise this function will return 0.
117      *
118      * @return The protcol string like "http" or 0.
119      * @author Peter Jones
120     **/
121     //####################################################################
122     const char* get_protocol (void) const;
123 
124     //####################################################################
125     /**
126      * Get the hostname that was given in the constructor. This may be a raw
127      * address if that was given to the constructor.
128      *
129      * @return The hostname.
130      * @author Peter Jones
131     **/
132     //####################################################################
133     const char* get_name (void) const;
134 
135     //####################################################################
136     /**
137      * Get the URI path information of that was present in the constructor.
138      * For example, if the given URI was pmade.org/index.html, this member
139      * function will return "/index.html". If no path information was given
140      * in the URI, this function will return 0.
141      *
142      * @return The path string or 0.
143      * @author Peter Jones
144     **/
145     //####################################################################
146     const char* get_path (void) const;
147 
148     //####################################################################
149     /**
150      * This function will return the actual port number that was chosen by
151      * the constructor.
152      *
153      * @return The final port number for this address.
154      * @author Peter Jones
155     **/
156     //####################################################################
157     port_type get_port (void) const;
158 
159     //####################################################################
160     /**
161      * Get an iterator to the first address pair.
162      *
163      * @return The address begin iterator.
164      * @author Peter Jones
165     **/
166     //####################################################################
167     const_iterator begin (void) const;
168 
169     //####################################################################
170     /**
171      * Get an iterator that points one past the last address pair.
172      *
173      * @return The address end iterator.
174      * @author Peter Jones
175     **/
176     //####################################################################
177     const_iterator end (void) const;
178 
179     //####################################################################
180     /**
181      * Find out how many addresses are stored in this address class.
182      *
183      * @return The number of addresses that this class is holding.
184      * @author Peter Jones
185     **/
186     //####################################################################
187     size_type size (void) const;
188 
189     //####################################################################
190     /**
191      * Add another address to the list. The URI parameter follows that of
192      * the first constructor.
193      *
194      * @param uri The URI of the address.
195      * @param default_port The default port to use.
196      * @author Peter Jones
197     **/
198     //####################################################################
199     void add_address(const char *uri, port_type default_port=0);
200 
201     //####################################################################
202     /**
203      * Add all local addresses to the address list. This is meant to be used
204      * by servers who wish to bind to all local addresses.
205      *
206      * @param port The port to use for all local addresses.
207      * @author Peter Jones
208     **/
209     //####################################################################
210     void add_all_addresses (port_type port);
211 
212 private:
213     std::string		protocol_;
214     std::string		name_;
215     std::string		path_;
216     port_type		port_;
217     container_type	addrs_;
218     bool		ipv6_;
219 
220 }; // end Netxx::Address class
221 } // end Netxx namespace
222 #endif
223