4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 /******************************************************************************
25 * File: email-network.c
26 * Desc: Data network interface
31 * 2006.11.17 : created
32 *****************************************************************************/
36 #include <sys/types.h>
37 #include <sys/socket.h>
44 #include <email-storage.h>
51 #include "email-debug-log.h"
52 #include "email-types.h"
53 #include "email-core-utils.h"
54 #include "email-core-mailbox.h"
56 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
57 #include "email-core-event.h"
60 /* _get_network_status - Get the data network status from vconf */
61 static int _get_network_status(int *network_status)
63 EM_DEBUG_FUNC_BEGIN("network_status [%p]", network_status);
68 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
69 return EMAIL_ERROR_INVALID_PARAM;
72 if (vconf_get_int(VCONFKEY_NETWORK_STATUS, &value)) {
73 EM_DEBUG_EXCEPTION("Failed vconf_get_int [VCONFKEY_NETWORK_STATUS]");
74 return EMAIL_ERROR_SYSTEM_FAILURE;
77 *network_status = value;
79 EM_DEBUG_FUNC_END("network_status [%d]", value);
80 return EMAIL_ERROR_NONE;
83 /* Check code for SIM status */
84 static int _get_sim_status(int *sim_status)
86 EM_DEBUG_FUNC_BEGIN();
90 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
91 return EMAIL_ERROR_INVALID_PARAM;
94 if (vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT, &value) != 0) {
95 EM_DEBUG_EXCEPTION("Failed vconf_get_int [VCONFKEY_TELEPHONY_SIM_SLOT]");
96 return EMAIL_ERROR_SYSTEM_FAILURE;
101 EM_DEBUG_FUNC_END("status[%d]", value);
102 return EMAIL_ERROR_NONE;
105 static int _get_wifi_status(int *wifi_status)
107 EM_DEBUG_FUNC_BEGIN();
112 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
113 return EMAIL_ERROR_INVALID_PARAM;
116 if (vconf_get_int(VCONFKEY_WIFI_STATE, &value) != 0) {
117 EM_DEBUG_EXCEPTION("vconf_get_int failed");
118 return EMAIL_ERROR_SYSTEM_FAILURE;
121 *wifi_status = value;
123 EM_DEBUG_FUNC_END("status[%d]", *wifi_status);
124 return EMAIL_ERROR_NONE;
128 INTERNAL_FUNC int emnetwork_check_network_status(int *err_code)
130 EM_DEBUG_FUNC_BEGIN();
131 int network_status = 0;
132 int sim_status = VCONFKEY_TELEPHONY_SIM_UNKNOWN;
134 int err = EMAIL_ERROR_NONE;
137 if ( (err = _get_network_status(&network_status)) != EMAIL_ERROR_NONE) {
138 EM_DEBUG_EXCEPTION("_get_network_status failed [%d]", err);
142 if(network_status == 0) {
143 EM_DEBUG_LOG("VCONFKEY_NETWORK_STATUS is 0");
145 if ( (err = _get_wifi_status(&wifi_status)) != EMAIL_ERROR_NONE) {
146 EM_DEBUG_EXCEPTION("_get_wifi_status failed [%d]", err);
150 if (wifi_status == 0) {
151 EM_DEBUG_LOG("Furthermore, WIFI is off");
153 if ( (err = _get_sim_status(&sim_status)) != EMAIL_ERROR_NONE) {
154 EM_DEBUG_EXCEPTION("_get_sim_status failed [%d]", err);
158 if (sim_status != VCONFKEY_TELEPHONY_SIM_INSERTED) {
159 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NO_SIM_INSERTED");
160 err = EMAIL_ERROR_NO_SIM_INSERTED;
164 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NETWORK_NOT_AVAILABLE");
165 err = EMAIL_ERROR_NETWORK_NOT_AVAILABLE;
170 EM_DEBUG_LOG("Data Network Mode is ON");
175 emcore_set_network_error(err);
185 INTERNAL_FUNC long emnetwork_callback_ssl_cert_query(char *reason, char *host, char *cert)
187 EM_DEBUG_FUNC_BEGIN("reason[%s], host[%s], cert[%s]", reason, host, cert);
190 /* FIXME : add user interface for Accept or Reject */
191 /* 1:Accept - ignore this certificate error */
192 /* 0:Reject - stop ssl connection */
195 EM_DEBUG_FUNC_END("ret[%s]", ret ? "ignore error" : "stop connection");
199 /* ------ socket read/write handle ---------------------------------------- */
201 /* TCPSTREAM = SENDSTREAM->netstream->stream; socket-id = TCPSTREAM->tcpsi, tcpso; */
202 /* sockid_in = ((TCPSTREAM*)send_stream->netstream->stream)->tcpsi; */
203 /* sockid_out = ((TCPSTREAM*)send_stream->netstream->stream)->tcpso; */
205 INTERNAL_FUNC long tcp_getbuffer_lnx(TCPSTREAM *stream, unsigned long size, char *s)
207 struct timeval tmout;
209 int nleave, nread, sret, sockid, maxfd;
212 sockid = stream->tcpsi;
214 /* EM_DEBUG_LOG("start sockid %d", sockid); */
215 if (sockid < 0) return 0;
217 if (stream->ictr > 0) {
219 if (stream->ictr >= size) {
220 memcpy(p, stream->iptr, size);
224 memcpy(p, stream->iptr, stream->ictr);
225 copy_sz = stream->ictr;
228 nleave = size - copy_sz;
229 stream->iptr += copy_sz;
230 stream->ictr -= copy_sz;
234 /* EM_DEBUG_LOG("end"); */
243 #ifdef TEST_CANCEL_JOB
244 if (!emcore_check_thread_status()) {
245 EM_DEBUG_EXCEPTION("thread canceled");
250 /* if (_g_canceled){ */
251 /* EM_DEBUG_LOG1("thread canceled\n"); */
252 /* tcp_abort(stream); */
259 FD_SET(sockid, &readfds);
261 sret = select(maxfd, &readfds, NULL, NULL, &tmout);
263 EM_DEBUG_EXCEPTION("select error[%d]\n", errno);
268 if (max_timeout >= 5) {
269 EM_DEBUG_EXCEPTION("max select timeout %d", max_timeout);
270 emcore_set_network_error(EMAIL_ERROR_NO_RESPONSE);
273 EM_DEBUG_EXCEPTION("%d select timeout", max_timeout);
278 nread = read(sockid, p, nleave);
280 EM_DEBUG_EXCEPTION("socket read error");
281 /* if (errno == EINTR) continue; */
287 EM_DEBUG_EXCEPTION("socket read no data");
299 EM_DEBUG_EXCEPTION("thread canceled");
305 /* EM_DEBUG_LOG("end"); */
306 #ifdef TEST_CANCEL_JOB
312 long tcp_getdata_lnx(TCPSTREAM *stream)
314 struct timeval tmout;
316 int nread, sret, sockid, maxfd;
319 sockid = stream->tcpsi;
322 /* EM_DEBUG_LOG("start sockid %d", sockid); */
323 if (sockid < 0) return false;
325 while (stream->ictr < 1) {
326 #ifdef TEST_CANCEL_JOB
327 if (!emcore_check_thread_status()) {
328 EM_DEBUG_EXCEPTION("thread canceled...");
334 /* if (_g_canceled){ */
335 /* EM_DEBUG_LOG1("thread canceled\n"); */
336 /* tcp_abort(stream); */
340 tmout.tv_usec = 0;/* 1000*10; */
344 FD_SET(sockid, &readfds);
346 sret = select(maxfd, &readfds, NULL, NULL, &tmout);
349 EM_DEBUG_LOG("select error[%d]", errno);
355 if (max_timeout >= 50) {
356 EM_DEBUG_EXCEPTION("max select timeout %d", max_timeout);
358 emcore_set_network_error(EMAIL_ERROR_NO_RESPONSE);
362 EM_DEBUG_EXCEPTION("%d select timeout", max_timeout);
368 if ((nread = read(sockid, stream->ibuf, BUFLEN)) < 0) {
369 EM_DEBUG_EXCEPTION("socket read failed...");
371 emcore_set_network_error(EMAIL_ERROR_SOCKET_FAILURE);
373 /* if (errno == EINTR) contine; */
379 EM_DEBUG_EXCEPTION("socket read no data...");
381 emcore_set_network_error(EMAIL_ERROR_INVALID_RESPONSE);
387 stream->ictr = nread;
388 stream->iptr = stream->ibuf;
392 EM_DEBUG_EXCEPTION("\t thread canceled...\n");
398 /* EM_DEBUG_LOG("end"); */
401 #ifdef TEST_CANCEL_JOB
407 INTERNAL_FUNC long tcp_sout_lnx(TCPSTREAM *stream, char *string, unsigned long size)
409 struct timeval tmout;
411 int sret, nwrite, maxfd, sockid;
414 sockid = stream->tcpso;
416 /* EM_DEBUG_LOG("start sockid %d", sockid); */
417 if (sockid < 0) return 0;
420 #ifdef TEST_CANCEL_JOB
421 if (!emcore_check_thread_status()) {
422 EM_DEBUG_EXCEPTION("thread canceled");
427 /* if (_g_canceled){ */
428 /* EM_DEBUG_LOG1("thread canceled"); */
429 /* tcp_abort(stream); */
436 FD_SET(sockid, &writefds);
438 sret = select(maxfd, NULL, &writefds, NULL, &tmout);
440 EM_DEBUG_LOG("select error[%d]", errno);
445 if (max_timeout >= 50) {
446 EM_DEBUG_EXCEPTION("max select timeout %d", max_timeout);
447 emcore_set_network_error(EMAIL_ERROR_NO_RESPONSE);
450 EM_DEBUG_EXCEPTION("%d select timeout", max_timeout);
455 if ((nwrite = write(sockid, string, size)) < 0) { /*prevent 22857*/
456 EM_DEBUG_EXCEPTION("socket write error");
457 /* if (errno == EINTR) continue; */
463 EM_DEBUG_EXCEPTION("socket write no data");
473 EM_DEBUG_EXCEPTION("thread canceled");
477 /* EM_DEBUG_LOG("end"); */
480 #ifdef TEST_CANCEL_JOB
486 INTERNAL_FUNC long tcp_soutr_lnx(TCPSTREAM *stream, char *string)
488 return tcp_sout_lnx(stream, string, EM_SAFE_STRLEN(string));