1 /* net6 - Library providing IPv4/IPv6 network access
2  * Copyright (C) 2005 Armin Burgmeier / 0x539 dev group
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #ifndef _NET6_HOST_HPP_
20 #define _NET6_HOST_HPP_
21 
22 #include "local.hpp"
23 #include "server.hpp"
24 
25 namespace net6
26 {
27 
28 /** High-level TCP host object that is used as a server with a local user.
29  */
30 
31 template<typename selector_type = net6::selector>
32 class basic_host
33  : virtual public basic_local<selector_type>,
34    virtual public basic_server<selector_type>
35 {
36 public:
37 	/** Creates a new basic_host object.
38 	 * @param username user name to use for the local user.
39 	 * @param ipv6 Whether to use IPv6 when no parameter is given to
40 	 * reopen.
41 	 */
42 	basic_host(const std::string& username, bool ipv6 = true);
43 
44 	/** Creates a new basic_host object which will accept incoming
45 	 * connections on port <em>port</em> and use the user name
46 	 * <em>username</em> for the local user.
47 	 */
48 	basic_host(unsigned int port, const std::string& username,
49 	           bool ipv6 = true);
50 
51 	/** Sends a packet to all users.
52 	 */
53 	virtual void send(const packet& pack);
54 
55 	/** Sends a packet to a single user. The request is ignored if
56 	 * <em>to</em> is the local user.
57 	 */
58 	virtual void send(const packet& pack, const user& to);
59 
60 	/** @brief Requests encryption to the given user.
61 	 *
62 	 * Throws an error if <em>to</em> is the local user.
63 	 */
64 	virtual void request_encryption(const user& to);
65 
66 	/** Returns the local user.
67 	 */
68 	virtual user& get_self();
69 
70 	/** Returns the local user.
71 	 */
72 	virtual const user& get_self() const;
73 
74 protected:
75 	user* self;
76 };
77 
78 typedef basic_host<selector> host;
79 
80 template<typename selector_type>
basic_host(const std::string & username,bool ipv6)81 basic_host<selector_type>::basic_host(const std::string& username, bool ipv6)
82  : basic_object<selector_type>(),
83    basic_local<selector_type>(),
84    basic_server<selector_type>(ipv6),
85    self(new user(1, NULL) )
86 {
87 	self->login(username);
88 	basic_object<selector_type>::user_add(self);
89 }
90 
91 template<typename selector_type>
92 basic_host<selector_type>::
basic_host(unsigned int port,const std::string & username,bool ipv6)93 	basic_host(unsigned int port, const std::string& username, bool ipv6)
94  : basic_object<selector_type>(),
95    basic_local<selector_type>(),
96    basic_server<selector_type>(port, ipv6),
97    self(new user(1, NULL) )
98 {
99 	self->login(username);
100 	basic_object<selector_type>::user_add(self);
101 }
102 
103 template<typename selector_type>
send(const packet & pack)104 void basic_host<selector_type>::send(const packet& pack)
105 {
106 	basic_server<selector_type>::send(pack);
107 }
108 
109 template<typename selector_type>
send(const packet & pack,const user & to)110 void basic_host<selector_type>::send(const packet& pack, const user& to)
111 {
112 	if(&to != self) basic_server<selector_type>::send(pack, to);
113 }
114 
115 template<typename selector_type>
request_encryption(const user & to)116 void basic_host<selector_type>::request_encryption(const user& to)
117 {
118 	if(&to == self)
119 	{
120 		throw std::logic_error(
121 			"net6::basic_host::request_encryption:\n"
122 			"Cannot request encryption to local client"
123 		);
124 	}
125 
126 	basic_server<selector_type>::request_encryption(to);
127 }
128 
129 template<typename selector_type>
get_self()130 user& basic_host<selector_type>::get_self()
131 {
132 	return *self;
133 }
134 
135 template<typename selector_type>
get_self() const136 const user& basic_host<selector_type>::get_self() const
137 {
138 	return *self;
139 }
140 
141 } // namespace net6
142 
143 #endif // _NET6_HOST_HPP_
144 
145