4 * Copyright (c) 2011-2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
7 * Genie Kim <daejins.kim@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
35 #include <sys/socket.h>
40 #include "data_connection.h"
41 #include <network-cm-intf.h>
42 #include "debug_util.h"
44 int noti_resp_init(int *noti_pipe_fds);
45 int noti_resp_wait(int *noti_pipe_fds);
46 int noti_resp_check(int *noti_pipe_fds);
47 int noti_resp_noti(int *noti_pipe_fds, int result);
48 int noti_resp_deinit(int *noti_pipe_fds);
50 unsigned int g_ipaddr;
51 static int pdp_pipe_fds[2];
60 char profile_name[NET_PROFILE_NAME_LEN_MAX + 1];
64 char *PdpStat[] = { "Deactivated", "Activating", "Activated", "Deactivating" };
66 static void set_connection_status(pdp_status i)
69 LOG_GPS(DBG_LOW, "==Status: %s\n", PdpStat[i]);
72 static pdp_status get_connection_status()
77 static void pdp_proxy_conf()
81 ret = net_get_active_proxy(&proxy);
83 if (ret == NET_ERR_NONE) {
84 if (strncmp(proxy.proxy_addr, "0.0.0.0", 7)) {
86 snprintf(buf, sizeof(buf), "http://%s/", proxy.proxy_addr);
87 setenv("http_proxy", buf, 1);
89 unsetenv("http_proxy");
92 LOG_GPS(DBG_ERR, "Fail to get proxy\n");
96 void pdp_evt_cb(net_event_info_t *event_cb, void *user_data)
98 switch (event_cb->Event) {
99 case NET_EVENT_OPEN_RSP: {
100 LOG_GPS(DBG_LOW, "event_cb->Error : [%d]\n", event_cb->Error);
101 if (get_connection_status() != ACTIVATING) {
102 LOG_GPS(DBG_LOW, "Not Activating status\n");
103 } else if (event_cb->Error == NET_ERR_NONE || event_cb->Error == NET_ERR_ACTIVE_CONNECTION_EXISTS) {
104 LOG_GPS(DBG_LOW, "Successful PDP Activation\n");
105 net_profile_info_t *prof_info = NULL;
106 net_dev_info_t *net_info = NULL;
108 prof_info = (net_profile_info_t *) event_cb->Data;
109 net_info = &(prof_info->ProfileInfo.Pdp.net_info);
111 strncpy(profile_name, net_info->ProfileName, NET_PROFILE_NAME_LEN_MAX);
113 set_connection_status(ACTIVATED);
115 noti_resp_noti(pdp_pipe_fds, TRUE);
117 LOG_GPS(DBG_ERR, " PDP Activation Failed - PDP Error[%d]\n", event_cb->Error);
118 set_connection_status(DEACTIVATED);
119 noti_resp_noti(pdp_pipe_fds, FALSE);
124 case NET_EVENT_CLOSE_RSP:
125 if (get_connection_status() != ACTIVATED && get_connection_status() != DEACTIVATING) {
126 LOG_GPS(DBG_ERR, "Not Activated && Deactivating status\n");
127 } else if (event_cb->Error == NET_ERR_NONE || event_cb->Error == NET_ERR_UNKNOWN) {
128 LOG_GPS(DBG_LOW, "Successful PDP De-Activation\n");
129 set_connection_status(DEACTIVATED);
130 noti_resp_noti(pdp_pipe_fds, TRUE);
132 LOG_GPS(DBG_ERR, " PDP DeActivation Failed - PDP Error[%d]\n", event_cb->Error);
133 noti_resp_noti(pdp_pipe_fds, FALSE);
137 case NET_EVENT_CLOSE_IND:
138 if (get_connection_status() != ACTIVATED && get_connection_status() != DEACTIVATING) {
139 LOG_GPS(DBG_ERR, "Not Activated && Deactivating status\n");
140 } else if (event_cb->Error == NET_ERR_NONE || event_cb->Error == NET_ERR_UNKNOWN) {
141 LOG_GPS(DBG_LOW, "Successful PDP De-Activation\n");
142 set_connection_status(DEACTIVATED);
143 noti_resp_noti(pdp_pipe_fds, TRUE);
145 LOG_GPS(DBG_ERR, " PDP DeActivation Failed - PDP Error[%d]\n", event_cb->Error);
146 noti_resp_noti(pdp_pipe_fds, FALSE);
149 case NET_EVENT_OPEN_IND:
156 unsigned int start_pdp_connection(void)
160 set_connection_status(DEACTIVATED);
161 if (noti_resp_init(pdp_pipe_fds)) {
162 LOG_GPS(DBG_LOW, "Success start_pdp_connection\n");
164 LOG_GPS(DBG_ERR, "ERROR in noti_resp_init()\n");
168 err = net_register_client((net_event_cb_t) pdp_evt_cb, NULL);
170 if (err == NET_ERR_NONE || err == NET_ERR_APP_ALREADY_REGISTERED) {
171 LOG_GPS(DBG_LOW, "Client registration is succeed\n");
173 LOG_GPS(DBG_WARN, "Error in net_register_client [%d]\n", err);
174 noti_resp_deinit(pdp_pipe_fds);
178 set_connection_status(ACTIVATING);
179 err = net_open_connection_with_preference(NET_SERVICE_INTERNET);
181 if (err == NET_ERR_NONE) {
182 LOG_GPS(DBG_LOW, "Sent PDP Activation.\n");
184 LOG_GPS(DBG_WARN, "Error in net_open_connection_with_preference() [%d]\n", err);
185 set_connection_status(DEACTIVATED);
186 err = net_deregister_client();
187 if (err == NET_ERR_NONE)
188 LOG_GPS(DBG_LOW, "Client deregistered successfully\n");
190 LOG_GPS(DBG_ERR, "Error deregistering the client\n");
192 noti_resp_deinit(pdp_pipe_fds);
196 if (noti_resp_wait(pdp_pipe_fds) > 0) {
197 if (noti_resp_check(pdp_pipe_fds)) {
198 LOG_GPS(DBG_LOW, "PDP Activation Successful\n");
199 noti_resp_deinit(pdp_pipe_fds);
202 LOG_GPS(DBG_ERR, "PDP failed\n");
203 noti_resp_deinit(pdp_pipe_fds);
205 err = net_deregister_client();
206 if (err == NET_ERR_NONE)
207 LOG_GPS(DBG_LOW, "Client deregistered successfully\n");
209 LOG_GPS(DBG_ERR, "Error deregistering the client\n");
214 LOG_GPS(DBG_ERR, "NO Pdp Act Response or Some error in select.\n");
215 noti_resp_deinit(pdp_pipe_fds);
217 err = net_deregister_client();
218 if (err == NET_ERR_NONE)
219 LOG_GPS(DBG_LOW, "Client deregistered successfully\n");
221 LOG_GPS(DBG_ERR, "Error deregistering the client\n");
227 unsigned int stop_pdp_connection(void)
230 pdp_status pStatus = get_connection_status();
231 if (pStatus == DEACTIVATED || pStatus == DEACTIVATING) {
232 LOG_GPS(DBG_ERR, "pdp stop progressing already. pStatus[%d] \n", pStatus);
236 if (noti_resp_init(pdp_pipe_fds)) {
237 LOG_GPS(DBG_LOW, "Success stop_pdp_connection\n");
239 LOG_GPS(DBG_ERR, "ERROR in noti_resp_init()\n");
243 set_connection_status(DEACTIVATING);
244 err = net_close_connection(profile_name);
245 if (err == NET_ERR_NONE) {
246 LOG_GPS(DBG_LOW, "Success net_close_connection\n");
248 LOG_GPS(DBG_WARN, "Error in sending net_close_connection error=%d\n", err);
249 set_connection_status(pStatus);
250 noti_resp_deinit(pdp_pipe_fds);
253 if (noti_resp_wait(pdp_pipe_fds) > 0) {
254 if (noti_resp_check(pdp_pipe_fds))
255 LOG_GPS(DBG_LOW, "Close Connection success\n");
257 LOG_GPS(DBG_ERR, "Close connection failed\n");
260 noti_resp_deinit(pdp_pipe_fds);
262 set_connection_status(DEACTIVATED);
264 err = net_deregister_client();
265 if (err == NET_ERR_NONE) {
266 LOG_GPS(DBG_LOW, "Client deregistered successfully\n");
268 LOG_GPS(DBG_WARN, "Error deregistering the client\n");
275 unsigned int query_dns(char *pdns_lookup_addr, unsigned int *ipaddr, int *port)
277 FUNC_ENTRANCE_SERVER;
279 if (!pdns_lookup_addr || !ipaddr || !port) return FALSE;
282 unsigned int ret = 0;
283 struct hostent hostbuf, *he = NULL;
288 char *colon = strchr(pdns_lookup_addr, ':');
291 char *ptr = (char *)strtok_r(pdns_lookup_addr, ":", &last);
292 pdns_lookup_addr = ptr;
294 ptr = (char *)strtok_r(NULL, ":", &last);
300 tmpbuf = malloc(tmplen);
301 if (!tmpbuf) return FALSE;
303 while ((res = gethostbyname_r(pdns_lookup_addr, &hostbuf, tmpbuf, tmplen, &he, &herr)) == ERANGE) {
304 /* Enlarge the buffer. */
306 void *tmp = realloc(tmpbuf, tmplen);
309 LOG_GPS(DBG_ERR, "Failed to reallocate memories.");
318 LOG_GPS(DBG_LOW, "g_agps_ipaddr: %u\n", g_ipaddr);
323 LOG_GPS(DBG_ERR, "//gethostbyname : Error getting host information\n");
331 int noti_resp_init(int *noti_pipe_fds)
333 if (pipe(noti_pipe_fds) < 0)
339 int noti_resp_wait(int *noti_pipe_fds)
345 FD_SET(*noti_pipe_fds, &rfds);
346 return select(*noti_pipe_fds + 1, &rfds, &wfds, NULL, NULL);
349 int noti_resp_noti(int *noti_pipe_fds, int result)
351 return write(*(noti_pipe_fds + 1), &result, sizeof(int));
354 int noti_resp_check(int *noti_pipe_fds)
358 ret_val = read(*noti_pipe_fds, &activation, sizeof(int));
360 LOG_GPS(DBG_ERR, "No data");
365 int noti_resp_deinit(int *noti_pipe_fds)
368 err = close(*noti_pipe_fds);
370 LOG_GPS(DBG_ERR, "ERROR closing fds1.\n");
372 err = close(*(noti_pipe_fds + 1));
374 LOG_GPS(DBG_ERR, "ERROR closing fds2.\n");