Added support to enable clatd service
[platform/core/connectivity/net-config.git] / src / clatd-handler.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2015 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 <unistd.h>
21 #include <stdlib.h>
22
23 #include "log.h"
24 #include "util.h"
25 #include "network-state.h"
26 #include "netdbus.h"
27 #include "clatd-handler.h"
28
29 #define CLATD_SERVICE   "com.samsung.clatd"
30 #define CLATD_PATH              "/com/samsung/clatd"
31 #define CLATD_INTERFACE "com.samsung.clatd.Control"
32
33 static gboolean g_is_running = FALSE;
34
35 static void __netconfig_clatd_async_callback(GObject *source_object,
36                 GAsyncResult *res, gpointer user_data)
37 {
38         GDBusConnection *conn = NULL;
39         GVariant *reply = NULL;
40         GError *error = NULL;
41         DBG("__netconfig_clatd_async_callback");
42
43         conn = G_DBUS_CONNECTION(source_object);
44         reply = g_dbus_connection_call_finish(conn, res, &error);
45         if (error) {
46                 ERR("Dbus error : %s", error->message);
47                 g_error_free(error);
48         } else {
49                 int is_clat_enabled = -1;
50                 g_variant_get(reply, "(i)", &is_clat_enabled);
51
52                 if (is_clat_enabled == 0) {
53                         DBG("OK");
54                 } else if (is_clat_enabled == 1) {
55                         INFO("already on going");
56                         g_is_running = TRUE;
57                 } else {
58                         ERR("Failed to clat enable, %d", is_clat_enabled);
59                 }
60         }
61
62         if (reply)
63                 g_variant_unref(reply);
64 }
65
66 int netconfig_clatd_enable(void)
67 {
68         DBG("");
69         GVariant *params = NULL;
70         GVariantBuilder *builder = NULL;
71         gboolean rv = FALSE;
72         struct clatd_ctrl_hint hint = {0 ,};
73         char ns_buff[256] = {0, };
74
75         if (g_is_running) {
76                 INFO("clatd is already ENABLED");
77                 return 0;
78         }
79
80         const char *if_name = netconfig_get_default_ifname();
81         if (if_name == NULL) {
82                 int idx;
83                 for (idx = 0; idx < MAX_DNS; idx++)
84                         g_free(hint.nameserver[idx]);
85
86                 ERR("There is no interface name");
87                 return -1;
88         } else {
89                 DBG("if_name : %s", if_name);
90         }
91
92         builder = g_variant_builder_new(G_VARIANT_TYPE ("a{is}"));
93
94         g_variant_builder_add(builder, "{is}", 'i', if_name);
95
96         // nameserver addresses
97         if (hint.nameserver[0] != NULL || hint.nameserver[1] != NULL) {
98                 g_snprintf(ns_buff, 256, "%s;%s",
99                                         ((hint.nameserver[0] != NULL) ? hint.nameserver[0] : "NULL"),
100                                         ((hint.nameserver[1] != NULL) ? hint.nameserver[1] : "NULL"));
101         }
102         g_variant_builder_add(builder, "{is}", 'd', ns_buff);
103
104         params = g_variant_builder_end(builder);
105         g_variant_builder_unref(builder);
106
107         rv = netconfig_invoke_dbus_method_nonblock(CLATD_SERVICE, CLATD_PATH,
108                         CLATD_INTERFACE, "Start", g_variant_new("(@a{is})", params), __netconfig_clatd_async_callback);
109
110         if (!rv) {
111                 DBG("Failed to dbus call");
112                 g_variant_unref(params);
113
114                 int idx;
115                 for (idx = 0; idx < MAX_DNS; idx++)
116                         g_free(hint.nameserver[idx]);
117                 return -1;
118         }
119
120         g_is_running = TRUE;
121         DBG("clatd enabled[%d]", g_is_running);
122         int idx;
123         for (idx = 0; idx < MAX_DNS; idx++)
124                 g_free(hint.nameserver[idx]);
125         return 0;
126 }
127
128 int netconfig_clatd_disable(void)
129 {
130         DBG("");
131         gboolean rv = FALSE;
132
133         if (!g_is_running) {
134                 ERR("clatd is already DISABLED");
135                 return 0;
136         }
137
138         rv = netconfig_invoke_dbus_method_nonblock(CLATD_SERVICE, CLATD_PATH,
139                         CLATD_INTERFACE, "Stop", NULL, NULL);
140         g_is_running = FALSE;
141         DBG("clatd disabled[%d]", g_is_running);
142         if (!rv) {
143                 DBG("Failed to dbus call");
144                 return -1;
145         }
146
147         return 0;
148 }