vpn: New vpn daemon that handles vpn connections and clients
[platform/upstream/connman.git] / vpn / vpn-manager.c
1 /*
2  *
3  *  ConnMan VPN daemon
4  *
5  *  Copyright (C) 2012  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
31 #include "../src/connman.h"
32
33 #include "vpn.h"
34 #include "connman/vpn-dbus.h"
35
36 static int vpn_connect_count;
37 static DBusConnection *connection;
38
39 static DBusMessage *create(DBusConnection *conn, DBusMessage *msg, void *data)
40 {
41         int err;
42
43         DBG("conn %p", conn);
44
45         err = __vpn_provider_create_and_connect(msg);
46         if (err < 0) {
47                 if (err == -EINPROGRESS) {
48                         connman_error("Invalid return code (%d) "
49                                         "from connect", err);
50                         err = -EINVAL;
51                 }
52
53                 return __connman_error_failed(msg, -err);
54         }
55
56         return NULL;
57 }
58
59 static DBusMessage *remove(DBusConnection *conn, DBusMessage *msg, void *data)
60 {
61         const char *path;
62         int err;
63
64         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
65                                                         DBUS_TYPE_INVALID);
66
67         DBG("conn %p path %s", conn, path);
68
69         err = __vpn_provider_remove(path);
70         if (err < 0)
71                 return __connman_error_failed(msg, -err);
72
73         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
74 }
75
76 static DBusMessage *get_connections(DBusConnection *conn, DBusMessage *msg,
77                                                                 void *data)
78 {
79         DBusMessage *reply;
80
81         DBG("conn %p", conn);
82
83         reply = __vpn_provider_get_connections(msg);
84         if (reply == NULL)
85                 return __connman_error_failed(msg, -EINVAL);
86
87         return reply;
88 }
89
90 static const GDBusMethodTable manager_methods[] = {
91         { GDBUS_ASYNC_METHOD("Create",
92                         GDBUS_ARGS({ "properties", "a{sv}" }),
93                         GDBUS_ARGS({ "path", "o" }),
94                         create) },
95         { GDBUS_ASYNC_METHOD("Remove",
96                         GDBUS_ARGS({ "identifier", "o" }), NULL,
97                         remove) },
98         { GDBUS_METHOD("GetConnections", NULL,
99                         GDBUS_ARGS({ "connections", "a(oa{sv})" }),
100                         get_connections) },
101         { },
102 };
103
104 static const GDBusSignalTable manager_signals[] = {
105         { GDBUS_SIGNAL("ConnectionAdded",
106                         GDBUS_ARGS({ "identifier", "o" },
107                                 { "properties", "a{sv}" })) },
108         { GDBUS_SIGNAL("ConnectionRemoved",
109                         GDBUS_ARGS({ "identifier", "o" })) },
110         { },
111 };
112
113 int __vpn_manager_init(void)
114 {
115         DBG("");
116
117         connection = connman_dbus_get_connection();
118         if (connection == NULL)
119                 return -1;
120
121         g_dbus_register_interface(connection, VPN_MANAGER_PATH,
122                                         VPN_MANAGER_INTERFACE,
123                                         manager_methods,
124                                         manager_signals, NULL, NULL, NULL);
125
126         vpn_connect_count = 0;
127
128         return 0;
129 }
130
131 void __vpn_manager_cleanup(void)
132 {
133         DBG("");
134
135         if (connection == NULL)
136                 return;
137
138         g_dbus_unregister_interface(connection, VPN_MANAGER_PATH,
139                                                 VPN_MANAGER_INTERFACE);
140
141         dbus_connection_unref(connection);
142 }