Initialize Tizen 2.3
[framework/connectivity/mobileap-agent.git] / src / mobileap_network.c
1 /*
2  * mobileap-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <net_connection.h>
21
22 #include "mobileap_agent.h"
23 #include "mobileap_common.h"
24 #include "mobileap_network.h"
25
26
27 extern int ref_agent;
28 static connection_h connection = NULL;
29 static connection_profile_h cprof = NULL;
30
31 static void __print_profile(connection_profile_h profile)
32 {
33         if (profile == NULL)
34                 return;
35
36         int conn_ret;
37         bool roaming;
38         char *apn = NULL;
39         char *home_url = NULL;
40         connection_cellular_network_type_e network_type;
41         connection_cellular_service_type_e service_type;
42
43         conn_ret = connection_profile_get_cellular_network_type(profile, &network_type);
44         if (conn_ret != CONNECTION_ERROR_NONE)
45                 ERR("connection API fail : 0x%X\n", conn_ret);
46         else
47                 DBG("Network type : %d\n", network_type);
48
49         conn_ret = connection_profile_get_cellular_service_type(profile, &service_type);
50         if (conn_ret != CONNECTION_ERROR_NONE)
51                 ERR("connection API fail : 0x%X\n", conn_ret);
52         else
53                 DBG("Service type : %d\n", service_type);
54
55         conn_ret = connection_profile_get_cellular_apn(profile, &apn);
56         if (conn_ret != CONNECTION_ERROR_NONE)
57                 ERR("connection API fail : 0x%X\n", conn_ret);
58         else {
59                 DBG("APN : %s\n", apn);
60                 free(apn);
61         }
62
63         conn_ret = connection_profile_get_cellular_home_url(profile, &home_url);
64         if (conn_ret != CONNECTION_ERROR_NONE)
65                 ERR("connection API fail : 0x%X\n", conn_ret);
66         else {
67                 DBG("Home url : %s\n", home_url);
68                 free(home_url);
69         }
70
71         conn_ret = connection_profile_is_cellular_roaming(profile, &roaming);
72         if (conn_ret != CONNECTION_ERROR_NONE)
73                 ERR("connection API fail : 0x%X\n", conn_ret);
74         else
75                 DBG("Roaming : %d\n", roaming);
76
77         return;
78 }
79
80 static gboolean __is_connected_profile(connection_profile_h profile)
81 {
82         if (profile == NULL) {
83                 ERR("profile is NULL\n");
84                 return FALSE;
85         }
86
87         int conn_ret;
88         connection_profile_state_e pstat = CONNECTION_PROFILE_STATE_DISCONNECTED;
89
90         conn_ret = connection_profile_get_state(profile, &pstat);
91         if (conn_ret != CONNECTION_ERROR_NONE) {
92                 ERR("connection_profile_get_state is failed: 0x%X\n", conn_ret);
93                 return FALSE;
94         }
95
96         if (pstat != CONNECTION_PROFILE_STATE_CONNECTED) {
97                 DBG("Profile is not connected\n");
98                 return FALSE;
99         }
100
101         DBG("Profile is connected\n");
102         return TRUE;
103 }
104
105
106 static gboolean __get_connected_profile(connection_profile_h *r_prof, connection_profile_type_e *r_net_type)
107 {
108         if (r_prof == NULL || r_net_type == NULL) {
109                 ERR("Invalid param [%p] [%p]\n", r_prof, r_net_type);
110                 return FALSE;
111         }
112
113         int conn_ret;
114         connection_profile_h profile = NULL;
115         connection_profile_type_e net_type = CONNECTION_PROFILE_TYPE_CELLULAR;
116
117         conn_ret = connection_get_current_profile(connection, &profile);
118         if (conn_ret != CONNECTION_ERROR_NONE) {
119                 ERR("connection_get_current_profile is failed : %d\n", conn_ret);
120                 return FALSE;
121         }
122
123         conn_ret = connection_profile_get_type(profile, &net_type);
124         if (conn_ret != CONNECTION_ERROR_NONE) {
125                 ERR("connection_profile_get_type is failed : 0x%X\n", conn_ret);
126                 connection_profile_destroy(profile);
127                 return FALSE;
128         }
129
130         *r_prof = profile;
131         *r_net_type = net_type;
132         return TRUE;
133 }
134
135 static gboolean __get_network_profile(connection_profile_h *r_prof)
136 {
137         if (r_prof == NULL) {
138                 ERR("r_prof is NULL\n");
139                 return FALSE;
140         }
141
142         connection_profile_h profile;
143         connection_profile_type_e net_type = CONNECTION_PROFILE_TYPE_CELLULAR;
144
145         if (__get_connected_profile(&profile, &net_type) == FALSE) {
146                 ERR("There is no available network\n");
147                 return FALSE;
148         }
149
150         DBG("Current connected net_type : %d\n", net_type);
151         if (net_type == CONNECTION_PROFILE_TYPE_WIFI) {
152                 *r_prof = profile;
153                 return TRUE;
154         }
155
156         if (net_type != CONNECTION_PROFILE_TYPE_CELLULAR) {
157                 ERR("Network type [%d] is not supported\n", net_type);
158                 return FALSE;
159         }
160         __print_profile(profile);
161
162         *r_prof = profile;
163         return TRUE;
164 }
165
166 static void __connection_type_changed_cb(connection_type_e type, void *user_data)
167 {
168         DBG("Changed connection type is %s\n",
169                         type == CONNECTION_TYPE_DISCONNECTED ? "DISCONNECTED" :
170                         type == CONNECTION_TYPE_WIFI ? "Wi-Fi" :
171                         type == CONNECTION_TYPE_CELLULAR ? "Cellular" :
172                         type == CONNECTION_TYPE_ETHERNET ? "Ethernet" :
173                         "Unknown");
174
175
176         if (_mobileap_is_disabled()) {
177                 DBG("Tethering is not enabled\n");
178                 return;
179         }
180
181         if (_unset_masquerade() == FALSE) {
182                 ERR("_unset_masquerade is failed\n");
183         }
184
185         if (cprof) {
186                 connection_profile_destroy(cprof);
187                 cprof = NULL;
188         }
189
190         if (type == CONNECTION_TYPE_DISCONNECTED) {
191                 return;
192         }
193
194         _open_network();
195
196         return;
197 }
198
199 gboolean _is_trying_network_operation(void)
200 {
201
202         return FALSE;
203 }
204
205 gboolean _get_network_interface_name(char **if_name)
206 {
207         if (if_name == NULL) {
208                 ERR("if_name is NULL\n");
209                 return FALSE;
210         }
211
212         if (cprof == NULL) {
213                 ERR("There is no connected profile\n");
214                 return FALSE;
215         }
216
217         int conn_ret = 0;
218
219         conn_ret = connection_profile_get_network_interface_name(cprof, if_name);
220         if (conn_ret != CONNECTION_ERROR_NONE) {
221                 ERR("connection_profile_get_network_interface_name is failed : 0x%X\n", conn_ret);
222                 return FALSE;
223         }
224
225         return TRUE;
226 }
227
228 gboolean _set_masquerade(void)
229 {
230         char *if_name = NULL;
231
232         if (_get_network_interface_name(&if_name) == FALSE) {
233                 ERR("_get_network_interface_name is failed\n");
234                 return FALSE;
235         }
236         DBG("Network interface : %s\n", if_name);
237
238         _mh_core_enable_masquerade(if_name);
239         free(if_name);
240
241         return TRUE;
242 }
243
244 gboolean _unset_masquerade(void)
245 {
246         if (cprof == NULL) {
247                 DBG("There is nothing to unset masquerading\n");
248                 return TRUE;
249         }
250
251         char *if_name = NULL;
252
253         if (_get_network_interface_name(&if_name) == FALSE) {
254                 ERR("_get_network_interface_name is failed\n");
255                 return FALSE;
256         }
257         DBG("Network interface : %s\n", if_name);
258
259         _mh_core_disable_masquerade(if_name);
260         free(if_name);
261
262         return TRUE;
263 }
264
265 gboolean _open_network(void)
266 {
267         connection_profile_h profile = NULL;
268
269         DBG("+\n");
270
271         if (__get_network_profile(&profile) == FALSE) {
272                 ERR("__get_network_profile is failed\n");
273                 return FALSE;
274         }
275
276         if (!__is_connected_profile(profile)) {
277                 connection_profile_destroy(profile);
278                 return TRUE;
279         }
280         cprof = profile;
281
282         if (_set_masquerade() == FALSE) {
283                 ERR("_set_masquerade is failed\n");
284                 _close_network();
285                 return FALSE;
286         }
287
288         DBG("-\n");
289
290         return TRUE;
291 }
292
293 gboolean _close_network(void)
294 {
295         gboolean ret;
296
297         DBG("+\n");
298
299         ret = _unset_masquerade();
300         if (ret == FALSE)
301                 ERR("_unset_masquerade is failed\n");
302
303         connection_profile_destroy(cprof);
304         cprof = NULL;
305
306         DBG("-\n");
307
308         return TRUE;
309 }
310
311 gboolean _init_network(void *user_data)
312 {
313         int conn_ret;
314
315         conn_ret = connection_create(&connection);
316         if (conn_ret != CONNECTION_ERROR_NONE) {
317                 ERR("connection_create is failed : 0x%X\n", conn_ret);
318                 return FALSE;
319         }
320
321         conn_ret = connection_set_type_changed_cb(connection,
322                         __connection_type_changed_cb, user_data);
323         if (conn_ret != CONNECTION_ERROR_NONE) {
324                 ERR("connection_set_type_changed cb is failed : 0x%X\n", conn_ret);
325                 connection_destroy(connection);
326                 connection = NULL;
327                 return FALSE;
328         }
329
330         return TRUE;
331 }
332
333 gboolean _deinit_network(void)
334 {
335         int conn_ret;
336
337         if (connection == NULL) {
338                 ERR("Connection handle is not initialized\n");
339                 return TRUE;
340         }
341
342         conn_ret = connection_unset_type_changed_cb(connection);
343         if (conn_ret != CONNECTION_ERROR_NONE) {
344                 ERR("connection_unset_type_changed_cb is failed : %d\n", conn_ret);
345         }
346
347         connection_destroy(connection);
348         connection = NULL;
349
350         return TRUE;
351 }