Merge branch 'upstream' into tizen
[platform/upstream/connman.git] / vpn / vpn-manager.c
1 /*
2  *
3  *  ConnMan VPN daemon
4  *
5  *  Copyright (C) 2012-2013  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27
28 #include <gdbus.h>
29 #include <connman/log.h>
30 #include <connman/agent.h>
31
32 #include "../src/connman.h"
33
34 #include "vpn.h"
35 #include "connman/vpn-dbus.h"
36
37 static int vpn_connect_count;
38 static DBusConnection *connection;
39
40 static DBusMessage *create(DBusConnection *conn, DBusMessage *msg, void *data)
41 {
42         int err;
43
44         DBG("conn %p", conn);
45
46         err = __vpn_provider_create(msg);
47         if (err < 0) {
48                 if (err == -EINPROGRESS) {
49                         connman_error("Invalid return code (%d) "
50                                         "from connect", err);
51                         err = -EINVAL;
52                 }
53
54                 return __connman_error_failed(msg, -err);
55         }
56
57         return NULL;
58 }
59
60 static DBusMessage *remove(DBusConnection *conn, DBusMessage *msg, void *data)
61 {
62         const char *path;
63         int err;
64
65         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
66                                                         DBUS_TYPE_INVALID);
67
68         DBG("conn %p path %s", conn, path);
69
70         err = __vpn_provider_remove(path);
71         if (err < 0)
72                 return __connman_error_failed(msg, -err);
73
74         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
75 }
76
77 static DBusMessage *get_connections(DBusConnection *conn, DBusMessage *msg,
78                                                                 void *data)
79 {
80         DBusMessage *reply;
81
82         DBG("conn %p", conn);
83
84         reply = __vpn_provider_get_connections(msg);
85         if (!reply)
86                 return __connman_error_failed(msg, EINVAL);
87
88         return reply;
89 }
90
91 static DBusMessage *register_agent(DBusConnection *conn,
92                                         DBusMessage *msg, void *data)
93 {
94         const char *sender, *path;
95         int err;
96
97         DBG("conn %p", conn);
98
99         sender = dbus_message_get_sender(msg);
100
101         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
102                                                         DBUS_TYPE_INVALID);
103
104         err = connman_agent_register(sender, path);
105         if (err < 0)
106                 return __connman_error_failed(msg, -err);
107
108         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
109 }
110
111 static DBusMessage *unregister_agent(DBusConnection *conn,
112                                         DBusMessage *msg, void *data)
113 {
114         const char *sender, *path;
115         int err;
116
117         DBG("conn %p", conn);
118
119         sender = dbus_message_get_sender(msg);
120
121         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
122                                                         DBUS_TYPE_INVALID);
123
124         err = connman_agent_unregister(sender, path);
125         if (err < 0)
126                 return __connman_error_failed(msg, -err);
127
128         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
129 }
130
131 static const GDBusMethodTable manager_methods[] = {
132         { GDBUS_ASYNC_METHOD("Create",
133                         GDBUS_ARGS({ "properties", "a{sv}" }),
134                         GDBUS_ARGS({ "path", "o" }),
135                         create) },
136         { GDBUS_ASYNC_METHOD("Remove",
137                         GDBUS_ARGS({ "identifier", "o" }), NULL,
138                         remove) },
139         { GDBUS_METHOD("GetConnections", NULL,
140                         GDBUS_ARGS({ "connections", "a(oa{sv})" }),
141                         get_connections) },
142         { GDBUS_METHOD("RegisterAgent",
143                         GDBUS_ARGS({ "path", "o" }), NULL,
144                         register_agent) },
145         { GDBUS_METHOD("UnregisterAgent",
146                         GDBUS_ARGS({ "path", "o" }), NULL,
147                         unregister_agent) },
148         { },
149 };
150
151 static const GDBusSignalTable manager_signals[] = {
152         { GDBUS_SIGNAL("ConnectionAdded",
153                         GDBUS_ARGS({ "identifier", "o" },
154                                 { "properties", "a{sv}" })) },
155         { GDBUS_SIGNAL("ConnectionRemoved",
156                         GDBUS_ARGS({ "identifier", "o" })) },
157         { },
158 };
159
160 int __vpn_manager_init(void)
161 {
162         DBG("");
163
164         connection = connman_dbus_get_connection();
165         if (!connection)
166                 return -1;
167
168         g_dbus_register_interface(connection, VPN_MANAGER_PATH,
169                                         VPN_MANAGER_INTERFACE,
170                                         manager_methods,
171                                         manager_signals, NULL, NULL, NULL);
172
173         vpn_connect_count = 0;
174
175         return 0;
176 }
177
178 void __vpn_manager_cleanup(void)
179 {
180         DBG("");
181
182         if (!connection)
183                 return;
184
185         g_dbus_unregister_interface(connection, VPN_MANAGER_PATH,
186                                                 VPN_MANAGER_INTERFACE);
187
188         dbus_connection_unref(connection);
189 }