4 * Copyright (C) 2011, 2012 Samsung Electronics Co., Ltd. All rights reserved.
7 * SeokYeon Hwang <syeon.hwang@samsung.com>
8 * MunKyu Im <munkyu.im@samsung.com>
9 * GiWoong Kim <giwoong.kim@samsung.com>
10 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
34 @brief collection of dialog function
39 #include "maru_common.h"
40 #if defined (CONFIG_LINUX)
41 #include <sys/ioctl.h>
42 #include <sys/types.h>
44 #include <sys/socket.h>
46 #include <netinet/in.h>
48 #elif defined (CONFIG_WIN32)
53 #elif defined (CONFIG_DARWIN)
54 #include <SystemConfiguration/SystemConfiguration.h>
55 CFDictionaryRef proxySettings;
57 #include <curl/curl.h>
61 #define HTTP_PROTOCOL "http="
62 #define HTTP_PREFIX "http://"
63 #define HTTPS_PROTOCOL "https="
64 #define FTP_PROTOCOL "ftp="
65 #define SOCKS_PROTOCOL "socks="
66 #define DIRECT "DIRECT"
69 MULTI_DEBUG_CHANNEL(tizen, option);
70 #if defined(CONFIG_WIN32)
73 const char *pactempfile = ".autoproxy";
76 @brief get host DNS server address
77 @param dns1: return value (first dns server address)
78 @param dns2: return value (second dns server address)
81 int gethostDNS(char *dns1, char *dns2)
86 memset(buf, 0, sizeof(char)*255);
88 resolv = fopen("/etc/resolv.conf", "r");
90 ERR( "Can't open \"/etc/resolv.conf.\"\n");
95 while(fscanf(resolv , "%s", buf) != EOF) {
96 if(strcmp(buf, "nameserver") == 0)
98 if(fscanf(resolv , "%s", dns1) <= 0) {
105 while(fscanf(resolv , "%s", buf) != EOF) {
106 if(strcmp(buf, "nameserver") == 0)
108 if(fscanf(resolv , "%s", dns2) <= 0) {
117 PIP_ADAPTER_ADDRESSES pAdapterAddr;
118 PIP_ADAPTER_ADDRESSES pAddr;
119 PIP_ADAPTER_DNS_SERVER_ADDRESS pDnsAddr;
120 unsigned long nBufferLength = sizeof(IP_ADAPTER_ADDRESSES);
121 pAdapterAddr = (PIP_ADAPTER_ADDRESSES)malloc(nBufferLength);
122 memset(pAdapterAddr, 0x00, nBufferLength);
124 while (GetAdaptersAddresses(AF_INET, 0, NULL, pAdapterAddr, &nBufferLength)
125 == ERROR_BUFFER_OVERFLOW) {
127 pAdapterAddr = (PIP_ADAPTER_ADDRESSES)malloc(nBufferLength);
128 memset(pAdapterAddr, 0x00, nBufferLength);
131 pAddr = pAdapterAddr;
132 for (; pAddr != NULL; pAddr = pAddr->Next) {
133 pDnsAddr = pAddr->FirstDnsServerAddress;
134 for (; pDnsAddr != NULL; pDnsAddr = pDnsAddr->Next) {
135 struct sockaddr_in *pSockAddr = (struct sockaddr_in*)pDnsAddr->Address.lpSockaddr;
137 strcpy(dns1, inet_ntoa(pSockAddr->sin_addr));
141 strcpy(dns2, inet_ntoa(pSockAddr->sin_addr));
149 // by caramis... change DNS address if localhost has DNS server or DNS cache.
150 if(!strncmp(dns1, "127.0.0.1", 9) || !strncmp(dns1, "localhost", 9)) {
151 strncpy(dns1, "10.0.2.2", 9);
153 if(!strncmp(dns2, "127.0.0.1", 9) || !strncmp(dns2, "localhost", 9)) {
154 strncpy(dns2, "10.0.2.2", 9);
160 #if defined (CONFIG_DARWIN)
161 static char *cfstring_to_cstring(CFStringRef str) {
166 CFIndex length = CFStringGetLength(str);
167 CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
168 char *buffer = (char *)malloc(maxSize);
169 if (CFStringGetCString(str, buffer, maxSize, kCFStringEncodingUTF8))
174 static int cfnumber_to_int(CFNumberRef num) {
179 CFNumberGetValue(num, kCFNumberIntType, &value);
184 static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
187 written = fwrite(ptr, size, nmemb, stream);
191 static void download_url(char *url)
197 curl = curl_easy_init();
199 fp = fopen(pactempfile,"wb");
200 curl_easy_setopt(curl, CURLOPT_URL, url);
201 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
202 //just in case network does not work.
203 curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 3000);
204 curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
205 res = curl_easy_perform(curl);
207 ERR("Fail to download pac file: %s\n", url);
209 curl_easy_cleanup(curl);
216 #if defined (CONFIG_DARWIN)
217 static void getmacproxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
221 CFNumberRef isEnable;
222 CFStringRef proxyHostname;
223 CFNumberRef proxyPort;
224 CFDictionaryRef proxySettings;
225 proxySettings = SCDynamicStoreCopyProxies(NULL);
227 isEnable = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesHTTPEnable);
228 if (cfnumber_to_int(isEnable)) {
229 // Get proxy hostname
230 proxyHostname = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesHTTPProxy);
231 hostname = cfstring_to_cstring(proxyHostname);
233 proxyPort = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesHTTPPort);
234 port = cfnumber_to_int(proxyPort);
235 // Save hostname & port
236 snprintf(http_proxy, MAXLEN, "%s:%d", hostname, port);
240 INFO("http proxy is null\n");
243 isEnable = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesHTTPSEnable);
244 if (cfnumber_to_int(isEnable)) {
245 // Get proxy hostname
246 proxyHostname = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesHTTPSProxy);
247 hostname = cfstring_to_cstring(proxyHostname);
249 proxyPort = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesHTTPSPort);
250 port = cfnumber_to_int(proxyPort);
251 // Save hostname & port
252 snprintf(https_proxy, MAXLEN, "%s:%d", hostname, port);
256 INFO("https proxy is null\n");
259 isEnable = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesFTPEnable);
260 if (cfnumber_to_int(isEnable)) {
261 // Get proxy hostname
262 proxyHostname = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesFTPProxy);
263 hostname = cfstring_to_cstring(proxyHostname);
265 proxyPort = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesFTPPort);
266 port = cfnumber_to_int(proxyPort);
267 // Save hostname & port
268 snprintf(ftp_proxy, MAXLEN, "%s:%d", hostname, port);
272 INFO("ftp proxy is null\n");
275 isEnable = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesSOCKSEnable);
276 if (cfnumber_to_int(isEnable)) {
277 // Get proxy hostname
278 proxyHostname = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesSOCKSProxy);
279 hostname = cfstring_to_cstring(proxyHostname);
281 proxyPort = CFDictionaryGetValue(proxySettings, kSCPropNetProxiesSOCKSPort);
282 port = cfnumber_to_int(proxyPort);
283 // Save hostname & port
284 snprintf(socks_proxy, MAXLEN, "%s:%d", hostname, port);
288 INFO("socks proxy is null\n");
290 CFRelease(proxySettings);
294 static void remove_string(char *src, char *dst, const char *toremove)
296 int len = strlen(toremove);
298 int max_len = strlen(src);
300 for(i = len, j = 0; i < max_len; i++)
308 #if defined (CONFIG_LINUX)
309 static void getlinuxproxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
311 char buf[MAXLEN] = {0,};
312 char buf_port[MAXPORTLEN] = {0,};
313 char buf_proxy[MAXLEN] = {0,};
317 int MAXPROXYLEN = MAXLEN + MAXPORTLEN;
319 output = popen("gconftool-2 --get /system/http_proxy/host", "r");
320 if(fscanf(output, "%s", buf) > 0) {
321 snprintf(buf_proxy, MAXLEN, "%s", buf);
325 output = popen("gconftool-2 --get /system/http_proxy/port", "r");
326 if(fscanf(output, "%s", buf_port) <= 0) {
327 //for abnormal case: if can't find the key of http port, get from environment value.
328 buf_proxy_bak = getenv("http_proxy");
329 INFO("http_proxy from env: %s\n", buf_proxy_bak);
330 if(buf_proxy_bak != NULL) {
331 proxy = malloc(MAXLEN);
332 remove_string(buf_proxy_bak, proxy, HTTP_PREFIX);
333 strncpy(http_proxy, proxy, strlen(proxy)-1);
334 INFO("final http_proxy value: %s\n", http_proxy);
338 INFO("http_proxy is not set on env.\n");
345 snprintf(http_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf_port);
346 memset(buf_proxy, 0, MAXLEN);
347 INFO("http_proxy: %s\n", http_proxy);
351 memset(buf, 0, MAXLEN);
353 output = popen("gconftool-2 --get /system/proxy/secure_host", "r");
354 if(fscanf(output, "%s", buf) > 0) {
355 snprintf(buf_proxy, MAXLEN, "%s", buf);
359 output = popen("gconftool-2 --get /system/proxy/secure_port", "r");
360 if(fscanf(output, "%s", buf) > 0) {
361 snprintf(https_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf);
364 memset(buf, 0, MAXLEN);
365 memset(buf_proxy, 0, MAXLEN);
366 INFO("https_proxy : %s\n", https_proxy);
368 output = popen("gconftool-2 --get /system/proxy/ftp_host", "r");
369 if(fscanf(output, "%s", buf) > 0) {
370 snprintf(buf_proxy, MAXLEN, "%s", buf);
374 output = popen("gconftool-2 --get /system/proxy/ftp_port", "r");
375 if(fscanf(output, "%s", buf) > 0) {
376 snprintf(ftp_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf);
379 memset(buf, 0, MAXLEN);
380 memset(buf_proxy, 0, MAXLEN);
381 INFO("ftp_proxy : %s\n", ftp_proxy);
383 output = popen("gconftool-2 --get /system/proxy/socks_host", "r");
384 if(fscanf(output, "%s", buf) > 0) {
385 snprintf(buf_proxy, MAXLEN, "%s", buf);
389 output = popen("gconftool-2 --get /system/proxy/socks_port", "r");
390 if(fscanf(output, "%s", buf) > 0) {
391 snprintf(socks_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf);
394 INFO("socks_proxy : %s\n", socks_proxy);
398 static int getautoproxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
405 #if defined(CONFIG_LINUX)
409 output = popen("gconftool-2 --get /system/proxy/autoconfig_url", "r");
410 if(fscanf(output, "%s", buf) > 0) {
411 INFO("pac address: %s\n", buf);
415 #elif defined(CONFIG_WIN32)
416 INFO("pac address: %s\n", (char*)url);
417 download_url((char*)url);
418 #elif defined(CONFIG_DARWIN)
419 CFStringRef pacURL = (CFStringRef)CFDictionaryGetValue(proxySettings,
420 kSCPropNetProxiesProxyAutoConfigURLString);
422 char url[MAXLEN] = {};
423 CFStringGetCString(pacURL, url, sizeof url, kCFStringEncodingASCII);
424 INFO("pac address: %s\n", (char*)url);
428 fp_pacfile = fopen(pactempfile, "r");
429 if(fp_pacfile != NULL) {
430 while(fgets(line, MAXLEN, fp_pacfile) != NULL) {
431 if( (strstr(line, "return") != NULL) && (strstr(line, "if") == NULL)) {
432 INFO("line found %s", line);
433 sscanf(line, "%*[^\"]\"%s %s", type, proxy);
437 if(g_str_has_prefix(type, DIRECT)) {
438 INFO("auto proxy is set to direct mode\n");
441 else if(g_str_has_prefix(type, PROXY)) {
442 INFO("auto proxy is set to proxy mode\n");
443 INFO("type: %s, proxy: %s\n", type, proxy);
444 p = strtok(proxy, "\";");
446 INFO("auto proxy to set: %s\n",p);
447 strcpy(http_proxy, p);
448 strcpy(https_proxy, p);
449 strcpy(ftp_proxy, p);
450 strcpy(socks_proxy, p);
456 ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n");
461 ERR("fail to get pacfile fp\n");
471 @brief get host proxy server address
472 @param proxy: return value (proxy server address)
475 int gethostproxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
477 #if defined(CONFIG_LINUX)
481 output = popen("gconftool-2 --get /system/proxy/mode", "r");
482 if(fscanf(output, "%s", buf) > 0) {
483 //priority : auto > manual > none
484 if (strcmp(buf, "auto") == 0) {
485 INFO("AUTO PROXY MODE\n");
486 getautoproxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
488 else if (strcmp(buf, "manual") == 0) {
489 INFO("MANUAL PROXY MODE\n");
490 getlinuxproxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
492 else if (strcmp(buf, "none") == 0) {
493 INFO("DIRECT PROXY MODE\n");
498 #elif defined(CONFIG_WIN32)
502 BYTE *proxyenable, *proxyserver;
507 nRet = RegOpenKeyEx(HKEY_CURRENT_USER,
508 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
509 0, KEY_QUERY_VALUE, &hKey);
510 if (nRet != ERROR_SUCCESS) {
511 ERR("Failed to open registry from %s\n",
512 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
515 //check auto proxy key exists
516 lRet = RegQueryValueEx(hKey, "AutoConfigURL", 0, NULL, NULL, &dwLength);
517 if (lRet != ERROR_SUCCESS && dwLength == 0) {
518 ERR("Failed to query value from %s\n",
519 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\AutoConfigURL");
523 url = (char*)malloc(dwLength);
525 ERR( "Failed to allocate a buffer\n");
528 memset(url, 0x00, dwLength);
529 lRet = RegQueryValueEx(hKey, "AutoConfigURL", 0, NULL, url, &dwLength);
530 if (lRet == ERROR_SUCCESS && dwLength != 0) {
531 getautoproxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
537 //check manual proxy key exists
538 lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, NULL, &dwLength);
539 if (lRet != ERROR_SUCCESS && dwLength == 0) {
540 ERR(stderr, "Failed to query value from %s\n",
541 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
545 proxyenable = (BYTE*)malloc(dwLength);
546 if (proxyenable == NULL) {
547 ERR( "Failed to allocate a buffer\n");
552 lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, proxyenable, &dwLength);
553 if (lRet != ERROR_SUCCESS) {
555 ERR("Failed to query value from %s\n",
556 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
560 if (*(char*)proxyenable == 0) {
567 lRet = RegQueryValueEx(hKey, "ProxyServer", 0, NULL, NULL, &dwLength);
568 if (lRet != ERROR_SUCCESS && dwLength == 0) {
569 ERR("Failed to query value from from %s\n",
570 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
575 proxyserver = (BYTE*)malloc(dwLength);
576 if (proxyserver == NULL) {
577 ERR( "Failed to allocate a buffer\n");
582 memset(proxyserver, 0x00, dwLength);
583 lRet = RegQueryValueEx(hKey, "ProxyServer", 0, NULL, proxyserver, &dwLength);
584 if (lRet != ERROR_SUCCESS) {
586 ERR( "Failed to query value from from %s\n",
587 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
592 if((char*)proxyserver != NULL) {
593 INFO("proxy value: %s\n", (char*)proxyserver);
594 real_proxy = malloc(MAXLEN);
596 for(p = strtok((char*)proxyserver, ";"); p; p = strtok(NULL, ";")){
597 if(strstr(p, HTTP_PROTOCOL)) {
598 remove_string(p, real_proxy, HTTP_PROTOCOL);
599 strcpy(http_proxy, real_proxy);
601 else if(strstr(p, HTTPS_PROTOCOL)) {
602 remove_string(p, real_proxy, HTTPS_PROTOCOL);
603 strcpy(https_proxy, real_proxy);
605 else if(strstr(p, FTP_PROTOCOL)) {
606 remove_string(p, real_proxy, FTP_PROTOCOL);
607 strcpy(ftp_proxy, real_proxy);
609 else if(strstr(p, SOCKS_PROTOCOL)) {
610 remove_string(p, real_proxy, SOCKS_PROTOCOL);
611 strcpy(socks_proxy, real_proxy);
614 INFO("all protocol uses the same proxy server: %s\n", p);
615 strcpy(http_proxy, p);
616 strcpy(https_proxy, p);
617 strcpy(ftp_proxy, p);
618 strcpy(socks_proxy, p);
624 INFO("proxy is null\n");
628 #elif defined (CONFIG_DARWIN)
630 proxySettings = SCDynamicStoreCopyProxies(NULL);
632 INFO("AUTO PROXY MODE\n");
633 ret = getautoproxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);
634 if(strlen(http_proxy) == 0 && ret < 0) {
635 INFO("MANUAL PROXY MODE\n");
636 getmacproxy(http_proxy, https_proxy, ftp_proxy, socks_proxy);