server.cpp
Go to the documentation of this file.
1 /*
2  *
3  * D-Bus++ - C++ bindings for D-Bus
4  *
5  * Copyright (C) 2005-2007 Paolo Durante <shackan@gmail.com>
6  *
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <dbus-c++/debug.h>
29 #include <dbus-c++/server.h>
30 
31 #include "internalerror.h"
32 #include "server_p.h"
33 #include "connection_p.h"
34 #include "dispatcher_p.h"
35 #include "error.h"
36 
37 using namespace DBus;
38 
39 Server::Private::Private(DBusServer *s)
40  : server(s), auth_enabled(false), anon_enabled(false)
41 {
42 }
43 
44 Server::Private::~Private()
45 {
46 }
47 
48 void Server::Private::on_new_conn_cb(DBusServer *server, DBusConnection *conn, void *data)
49 {
50  Server *s = static_cast<Server *>(data);
51 
52  Connection nc(new Connection::Private(conn, s->_pvt.get()));
53 
54  s->_pvt->connections.push_back(nc);
55 
56  s->on_new_connection(nc);
57 
58  if (s->_pvt->auth_enabled)
59  dbus_connection_set_unix_user_function (conn, Private::on_unix_auth_cb, s, NULL);
60 
61  if (s->_pvt->anon_enabled)
62  dbus_connection_set_allow_anonymous (conn, true);
63 
64  debug_log("incoming connection 0x%08x", conn);
65 }
66 
67 dbus_bool_t Server::Private::on_unix_auth_cb(DBusConnection *c, unsigned long uid, void *data)
68 {
69  Server *s = static_cast<Server *>(data);
70 
71  return s->on_user_auth(uid);
72 }
73 
74 Server::Server(const char *address)
75 {
76  InternalError e;
77  DBusServer *server = dbus_server_listen(address, e);
78 
79  if (e) throw Error(e);
80 
81  debug_log("server 0x%08x listening on %s", server, address);
82 
83  _pvt = new Private(server);
84 
85  dbus_server_set_new_connection_function(_pvt->server, Private::on_new_conn_cb, this, NULL);
86 
88 }
89 /*
90 Server::Server(const Server &s)
91 : _pvt(s._pvt)
92 {
93  dbus_server_ref(_pvt->server);
94 }
95 */
97 {
98  dbus_server_unref(_pvt->server);
99 }
100 
102 {
103  debug_log("registering stubs for server %p", _pvt->server);
104 
105  Dispatcher *prev = _pvt->dispatcher;
106 
107  dbus_server_set_watch_functions(
108  _pvt->server,
109  Dispatcher::Private::on_add_watch,
110  Dispatcher::Private::on_rem_watch,
111  Dispatcher::Private::on_toggle_watch,
112  dispatcher,
113  0
114  );
115 
116  dbus_server_set_timeout_functions(
117  _pvt->server,
118  Dispatcher::Private::on_add_timeout,
119  Dispatcher::Private::on_rem_timeout,
120  Dispatcher::Private::on_toggle_timeout,
121  dispatcher,
122  0
123  );
124 
125  _pvt->dispatcher = dispatcher;
126 
127  return prev;
128 }
129 
130 bool Server::operator == (const Server &s) const
131 {
132  return _pvt->server == s._pvt->server;
133 }
134 
135 bool Server::listening() const
136 {
137  return dbus_server_get_is_connected(_pvt->server);
138 }
140 {
141  dbus_server_disconnect(_pvt->server);
142 }
143 
144 bool Server::on_user_auth(unsigned long uid)
145 {
146  return true;
147 }
148 
149 void Server::enable_auth(bool enable)
150 {
151  _pvt->auth_enabled = enable;
152 }
153 
154 void Server::enable_anon(bool enable)
155 {
156  const char *anon_mech[] = {"ANONYMOUS", 0};
157  const char *default_mech[] = {"EXTERNAL", "DBUS_COOKIE_SHA1", "ANONYMOUS", 0};
158  const char **used_mech = default_mech;
159 
160  if (enable)
161  used_mech = anon_mech;
162 
163  if (!dbus_server_set_auth_mechanisms(_pvt->server, used_mech))
164  throw ErrorSetAuthMechanisms();
165 
166  _pvt->anon_enabled = enable;
167 }
void enable_anon(bool)
Definition: server.cpp:154
Dispatcher * dispatcher
Definition: server_p.h:45
void enable_auth(bool)
Definition: server.cpp:149
virtual bool on_user_auth(unsigned long uid)
Definition: server.cpp:144
RefPtrI< Private > _pvt
Definition: server.h:76
Private(DBusServer *)
T * get() const
Definition: util.h:145
bool operator==(const Server &) const
Definition: server.cpp:130
DBusServer * server
Definition: server_p.h:43
DXXAPI LogFunction debug_log
Definition: debug.cpp:55
DXXAPI Dispatcher * default_dispatcher
Definition: dispatcher.cpp:36
bool listening() const
Definition: server.cpp:135
virtual void on_new_connection(Connection &c)=0
void disconnect()
Definition: server.cpp:139
virtual ~Server()
Definition: server.cpp:96
Dispatcher * setup(Dispatcher *)
Definition: server.cpp:101