Modified to use /dev/urandom instead of random()
[platform/core/connectivity/net-config.git] / src / dbus / netdbus.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 #include "log.h"
25 #include "netdbus.h"
26
27 #define DBUS_PARAM_TYPE_STRING          "string"
28 #define DBUS_PARAM_TYPE_INT16           "int16"
29 #define DBUS_PARAM_TYPE_UINT16          "uint16"
30 #define DBUS_PARAM_TYPE_INT32           "int32"
31 #define DBUS_PARAM_TYPE_UINT32          "uint32"
32 #define DBUS_PARAM_TYPE_INT64           "int64"
33 #define DBUS_PARAM_TYPE_UINT64          "uint64"
34 #define DBUS_PARAM_TYPE_DOUBLE          "double"
35 #define DBUS_PARAM_TYPE_BYTE            "byte"
36 #define DBUS_PARAM_TYPE_BOOLEAN         "boolean"
37 #define DBUS_PARAM_TYPE_OBJECT_PATH     "objpath"
38 #define DBUS_PARAM_TYPE_VARIANT         "variant"
39 #define DBUS_PARAM_TYPE_ARRAY           "array"
40
41 static GDBusObjectManagerServer *manager_server_wifi = NULL;
42 static GDBusObjectManagerServer *manager_server_state = NULL;
43 static GDBusObjectManagerServer *manager_server_statistics = NULL;
44 static GDBusObjectManagerServer *manager_server_vpn = NULL;
45 static GDBusObjectManagerServer *manager_server_mptcp = NULL;
46 static GDBusObjectManagerServer *manager_server_ethernet = NULL;
47 static guint owner_id = 0;
48 static got_name_cb g_callback = NULL;
49
50 struct gdbus_conn_data {
51         GDBusConnection *connection;
52         int conn_ref_count;
53         GCancellable *cancellable;
54 };
55
56 static struct gdbus_conn_data gconn_data = {NULL, 0, NULL};
57
58 GDBusObjectManagerServer *netdbus_get_wifi_manager(void)
59 {
60         return manager_server_wifi;
61 }
62
63 GDBusObjectManagerServer *netdbus_get_state_manager(void)
64 {
65         return manager_server_state;
66 }
67
68 GDBusObjectManagerServer *netdbus_get_statistics_manager(void)
69 {
70         return manager_server_statistics;
71 }
72
73 GDBusObjectManagerServer *netdbus_get_vpn_manager(void)
74 {
75         return manager_server_vpn;
76 }
77
78 GDBusObjectManagerServer *netdbus_get_mptcp_manager(void)
79 {
80         return manager_server_mptcp;
81 }
82
83 GDBusObjectManagerServer *netdbus_get_ethernet_manager(void)
84 {
85         return manager_server_ethernet;
86 }
87
88 GDBusConnection *netdbus_get_connection(void)
89 {
90         return gconn_data.connection;
91 }
92
93 GCancellable *netdbus_get_cancellable(void)
94 {
95         return gconn_data.cancellable;
96 }
97
98 void netconfig_gdbus_pending_call_ref(void)
99 {
100         g_object_ref(gconn_data.connection);
101
102         __sync_fetch_and_add(&gconn_data.conn_ref_count, 1);
103 }
104
105 void netconfig_gdbus_pending_call_unref(void)
106 {
107         if (gconn_data.conn_ref_count < 1)
108                 return;
109
110         g_object_unref(gconn_data.connection);
111
112         if (__sync_sub_and_fetch(&gconn_data.conn_ref_count, 1) < 1) {
113                 /* TODO: Check this
114                  * gconn_data.connection = NULL;
115                  */
116         }
117 }
118
119 int _create_gdbus_call(GDBusConnection *conn)
120 {
121         if (gconn_data.connection != NULL) {
122                 ERR("Connection already set");
123                 return -1;
124         }
125
126         gconn_data.connection = conn;
127         if (gconn_data.connection == NULL) {
128                 ERR("Failed to connect to the D-BUS daemon");
129                 return -1;
130         }
131
132         gconn_data.cancellable = g_cancellable_new();
133
134         return 0;
135 }
136
137 gboolean netconfig_is_cellular_internet_profile(const char *profile)
138 {
139         const char internet_suffix[] = "_1";
140         char *suffix = NULL;
141
142         if (profile == NULL)
143                 return FALSE;
144
145         if (g_str_has_prefix(profile, CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX)
146                         == TRUE) {
147                 suffix = strrchr(profile, '_');
148                 if (g_strcmp0(suffix, internet_suffix) == 0)
149                         return TRUE;
150         }
151
152         return FALSE;
153 }
154
155 gboolean netconfig_is_cellular_profile(const char *profile)
156 {
157         if (profile == NULL)
158                 return FALSE;
159
160         return g_str_has_prefix(profile, CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX);
161 }
162
163 gboolean netconfig_is_wifi_profile(const char *profile)
164 {
165         if (profile == NULL)
166                 return FALSE;
167
168         return g_str_has_prefix(profile, CONNMAN_WIFI_SERVICE_PROFILE_PREFIX);
169 }
170
171 gboolean netconfig_is_ethernet_profile(const char *profile)
172 {
173         if (profile == NULL)
174                 return FALSE;
175
176         return g_str_has_prefix(profile, CONNMAN_ETHERNET_SERVICE_PROFILE_PREFIX);
177 }
178
179 gboolean netconfig_is_bluetooth_profile(const char *profile)
180 {
181         if (profile == NULL)
182                 return FALSE;
183
184         return g_str_has_prefix(profile, CONNMAN_BLUETOOTH_SERVICE_PROFILE_PREFIX);
185 }
186
187 gboolean netconfig_invoke_dbus_method_nonblock(const char *dest, const char *path,
188                 const char *interface_name, const char *method, GVariant *params,
189                 GAsyncReadyCallback notify_func, void *user_data)
190 {
191         GDBusConnection *connection = NULL;
192
193         DBG("[GDBUS Async] %s %s %s", interface_name, method, path);
194
195         connection = netdbus_get_connection();
196         if (connection == NULL) {
197                 ERR("Failed to get gdbus connection");
198                 return FALSE;
199         }
200
201         g_dbus_connection_call(connection,
202                         dest,
203                         path,
204                         interface_name,
205                         method,
206                         params,
207                         NULL,
208                         G_DBUS_CALL_FLAGS_NONE,
209                         NETCONFIG_DBUS_REPLY_TIMEOUT,
210                         netdbus_get_cancellable(),
211                         (GAsyncReadyCallback) notify_func,
212                         user_data);
213         if (notify_func)
214                 netconfig_gdbus_pending_call_ref();
215
216         return TRUE;
217 }
218
219 GVariant *netconfig_invoke_dbus_method(const char *dest, const char *path,
220                 const char *interface_name, const char *method, GVariant *params)
221 {
222
223         GError *error = NULL;
224         GVariant *reply = NULL;
225         GDBusConnection *connection;
226
227         connection = netdbus_get_connection();
228         if (connection == NULL) {
229                 ERR("Failed to get GDBusconnection");
230                 return reply;
231         }
232
233         reply = g_dbus_connection_call_sync(
234                         connection,
235                         dest,
236                         path,
237                         interface_name,
238                         method,
239                         params,
240                         NULL,
241                         G_DBUS_CALL_FLAGS_NONE,
242                         NETCONFIG_DBUS_REPLY_TIMEOUT,
243                         netdbus_get_cancellable(),
244                         &error);
245
246         if (reply == NULL) {
247                 if (error != NULL) {
248                         ERR("g_dbus_connection_call_sync() failed"
249                                                 "error [%d: %s]", error->code, error->message);
250                         g_error_free(error);
251                 } else {
252                         ERR("g_dbus_connection_call_sync() failed");
253                 }
254
255                 return NULL;
256         }
257
258         return reply;
259 }
260
261 gboolean netconfig_dbus_emit_signal(const gchar *destination_bus_name,
262                                                                         const gchar *object_path,
263                                                                         const gchar *interface_name,
264                                                                         const gchar *signal_name,
265                                                                         GVariant *params)
266 {
267         gboolean rv = FALSE;
268         GError *Error = NULL;
269         GDBusConnection *connection;
270
271         connection = netdbus_get_connection();
272         if (connection == NULL) {
273                 ERR("[NET_DBUS] GDBusconnection is NULL");
274                 return 0;
275         }
276
277         rv = g_dbus_connection_emit_signal(connection, destination_bus_name,
278                 object_path, interface_name, signal_name, params, &Error);
279         if (rv != TRUE) {
280                 ERR("[NET_DBUS] Failed to emit signal, Error: %s", Error->message);
281                 g_clear_error(&Error);
282                 return rv;
283         }
284
285         INFO("Sent signal (%s), Interface (%s)", signal_name, interface_name);
286
287         return rv;
288 }
289
290 static void _got_bus_cb(GDBusConnection *conn, const gchar *name,
291                 gpointer user_data)
292 {
293         _create_gdbus_call(conn);
294 }
295
296 static void _got_name_cb(GDBusConnection *conn, const gchar *name,
297                 gpointer user_data)
298 {
299         INFO("Got gdbus name: [%s] and gdbus connection: [%p]", name, conn);
300
301         if (g_callback != NULL)
302                 g_callback();
303 }
304
305 static void _lost_name_cb(GDBusConnection *conn, const gchar *name,
306                 gpointer user_data)
307 {
308         /* May service name is already in use */
309         ERR("_lost_name_cb [%s]", name);
310
311         /* The result of DBus name request is only permitted,
312          *  such as DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER.
313          */
314         exit(2);
315 }
316
317 int setup_gdbus(got_name_cb cb)
318 {
319         g_callback = cb;
320
321         manager_server_wifi = g_dbus_object_manager_server_new(NETCONFIG_WIFI_PATH);
322         if (manager_server_wifi == NULL) {
323                 ERR("Manager server for WIFI_PATH not created.");
324                 exit(1);
325         }
326
327         manager_server_state = g_dbus_object_manager_server_new(NETCONFIG_NETWORK_STATE_PATH);
328         if (manager_server_state == NULL) {
329                 ERR("Manager server for STATE_PATH not created.");
330                 exit(1);
331         }
332
333         manager_server_statistics = g_dbus_object_manager_server_new(NETCONFIG_NETWORK_STATISTICS_PATH);
334         if (manager_server_statistics == NULL) {
335                 ERR("Manager server for STATISTICS_PATH not created.");
336                 exit(1);
337         }
338
339         manager_server_vpn = g_dbus_object_manager_server_new(NETCONFIG_VPNSVC_PATH);
340         if (manager_server_vpn == NULL) {
341                 ERR("Manager server for VPNSVC_PATH not created.");
342                 exit(1);
343         }
344
345         manager_server_mptcp = g_dbus_object_manager_server_new(NETCONFIG_MPTCP_PATH);
346         if (manager_server_mptcp == NULL) {
347                 ERR("Manager server for MPTCP_PATH not created.");
348                 exit(1);
349         }
350
351         manager_server_ethernet = g_dbus_object_manager_server_new(NETCONFIG_ETHERNET_PATH);
352         if (manager_server_ethernet == NULL) {
353                 ERR("Manager server for NETCONFIG_ETHERNET_PATH not created.");
354                 exit(1);
355         }
356
357         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, NETCONFIG_SERVICE,
358                                                           G_BUS_NAME_OWNER_FLAGS_NONE,
359                                                           _got_bus_cb, _got_name_cb, _lost_name_cb,
360                                                           NULL, NULL);
361         if (!owner_id) {
362                 ERR("Could not get system bus!");
363                 return -EIO;
364         }
365
366         INFO("Got system bus!");
367         return 0;
368 }
369
370 void cleanup_gdbus(void)
371 {
372         g_bus_unown_name(owner_id);
373         g_object_unref(manager_server_wifi);
374         g_object_unref(manager_server_state);
375         g_object_unref(manager_server_statistics);
376 }