Get cellular IPv6 profile and then enable clatd.
[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         if (!netconfig_get_connected_cellular_internet_ipv6only_profile(&hint)) {
81                 ERR("Failed to get cellular profile");
82                 return -1;
83         }
84
85         const char *if_name = netconfig_get_default_ifname();
86         if (if_name == NULL) {
87                 int idx;
88                 for (idx = 0; idx < MAX_DNS; idx++)
89                         g_free(hint.nameserver[idx]);
90
91                 ERR("There is no interface name");
92                 return -1;
93         } else {
94                 DBG("if_name : %s", if_name);
95         }
96
97         builder = g_variant_builder_new(G_VARIANT_TYPE ("a{is}"));
98
99         g_variant_builder_add(builder, "{is}", 'i', if_name);
100
101         // nameserver addresses
102         if (hint.nameserver[0] != NULL || hint.nameserver[1] != NULL) {
103                 g_snprintf(ns_buff, 256, "%s;%s",
104                                         ((hint.nameserver[0] != NULL) ? hint.nameserver[0] : "NULL"),
105                                         ((hint.nameserver[1] != NULL) ? hint.nameserver[1] : "NULL"));
106         }
107         g_variant_builder_add(builder, "{is}", 'd', ns_buff);
108
109         params = g_variant_builder_end(builder);
110         g_variant_builder_unref(builder);
111
112         rv = netconfig_invoke_dbus_method_nonblock(CLATD_SERVICE, CLATD_PATH,
113                         CLATD_INTERFACE, "Start", g_variant_new("(@a{is})", params), __netconfig_clatd_async_callback);
114
115         if (!rv) {
116                 DBG("Failed to dbus call");
117                 g_variant_unref(params);
118
119                 int idx;
120                 for (idx = 0; idx < MAX_DNS; idx++)
121                         g_free(hint.nameserver[idx]);
122                 return -1;
123         }
124
125         g_is_running = TRUE;
126         DBG("clatd enabled[%d]", g_is_running);
127         int idx;
128         for (idx = 0; idx < MAX_DNS; idx++)
129                 g_free(hint.nameserver[idx]);
130         return 0;
131 }
132
133 int netconfig_clatd_disable(void)
134 {
135         DBG("");
136         gboolean rv = FALSE;
137
138         if (!g_is_running) {
139                 ERR("clatd is already DISABLED");
140                 return 0;
141         }
142
143         rv = netconfig_invoke_dbus_method_nonblock(CLATD_SERVICE, CLATD_PATH,
144                         CLATD_INTERFACE, "Stop", NULL, NULL);
145         g_is_running = FALSE;
146         DBG("clatd disabled[%d]", g_is_running);
147         if (!rv) {
148                 DBG("Failed to dbus call");
149                 return -1;
150         }
151
152         return 0;
153 }