From ea01b4d84b9978f7d10e92868f43c75f2b52f36a Mon Sep 17 00:00:00 2001 From: SeokYeon Hwang Date: Tue, 22 Jan 2013 18:17:46 +0900 Subject: [PATCH] osutil: Migration "get host proxy" to osutil on linux Signed-off-by: SeokYeon Hwang --- tizen/src/Makefile.tizen | 1 + tizen/src/emulator.c | 12 +++- tizen/src/maru_common.h | 4 +- tizen/src/osutil-linux.c | 179 +++++++++++++++++++++++++++++++++++++++++++++++ tizen/src/osutil.c | 91 ++++++++++++++++++++++++ tizen/src/osutil.h | 17 +++++ 6 files changed, 302 insertions(+), 2 deletions(-) create mode 100644 tizen/src/osutil.c diff --git a/tizen/src/Makefile.tizen b/tizen/src/Makefile.tizen index de40722..98c7050 100755 --- a/tizen/src/Makefile.tizen +++ b/tizen/src/Makefile.tizen @@ -72,6 +72,7 @@ endif #CONFIG_GL obj-y += emulator.o emul_state.o option.o maru_err_table.o # osutil +obj-y += osutil.o ifdef CONFIG_LINUX obj-y += osutil-linux.o endif diff --git a/tizen/src/emulator.c b/tizen/src/emulator.c index fb39554..e35c52c 100644 --- a/tizen/src/emulator.c +++ b/tizen/src/emulator.c @@ -57,7 +57,7 @@ #include #endif -#ifndef CONFIG_WIN32 +#ifdef CONFIG_DARWIN #include "ns_event.h" #endif @@ -164,6 +164,11 @@ static void parse_options(int argc, char *argv[], int *skin_argc, } } +static void get_host_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy) +{ + get_host_proxy_os(http_proxy, https_proxy, ftp_proxy, socks_proxy); +} + static void set_bin_path(gchar * exec_argv) { set_bin_path_os(exec_argv); @@ -339,7 +344,12 @@ static void prepare_basic_features(void) tizen_base_port = get_sdb_base_port(); + // TODO: Codes about Getting host proxy migration is processing... +#if defined(CONFIG_LINUX) + get_host_proxy(http_proxy, https_proxy, ftp_proxy, socks_proxy); +#else gethostproxy(http_proxy, https_proxy, ftp_proxy, socks_proxy); +#endif // using "DNS" provided by default QEMU g_strlcpy(dns, DEFAULT_QEMU_DNS_IP, strlen(DEFAULT_QEMU_DNS_IP) + 1); diff --git a/tizen/src/maru_common.h b/tizen/src/maru_common.h index 63dc8c0..96c9104 100644 --- a/tizen/src/maru_common.h +++ b/tizen/src/maru_common.h @@ -49,7 +49,9 @@ #include "config-host.h" -#ifndef PATH_MAX +#if defined(MAX_PATH) +#define PATH_MAX MAX_PATH +#elif !defined(PATH_MAX) #define PATH_MAX 256 #endif diff --git a/tizen/src/osutil-linux.c b/tizen/src/osutil-linux.c index 03b8f97..9d0fe6d 100644 --- a/tizen/src/osutil-linux.c +++ b/tizen/src/osutil-linux.c @@ -41,6 +41,16 @@ #include "maru_err_table.h" #include "sdb.h" +#if 0 +#include +#include +#include +#include +#include +#include +#include +#endif + #include #include #include @@ -179,3 +189,172 @@ void print_system_info_os(void) returns error !", lspci_cmd); } } + +static int get_auto_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy) +{ + char type[MAXLEN]; + char proxy[MAXLEN]; + char line[MAXLEN]; + FILE *fp_pacfile; + char *p = NULL; + FILE *output; + char buf[MAXLEN]; + + output = popen("gconftool-2 --get /system/proxy/autoconfig_url", "r"); + if(fscanf(output, "%s", buf) > 0) { + INFO("pac address: %s\n", buf); + download_url(buf); + } + pclose(output); + fp_pacfile = fopen(pac_tempfile, "r"); + if(fp_pacfile != NULL) { + while(fgets(line, MAXLEN, fp_pacfile) != NULL) { + if( (strstr(line, "return") != NULL) && (strstr(line, "if") == NULL)) { + INFO("line found %s", line); + sscanf(line, "%*[^\"]\"%s %s", type, proxy); + } + } + + if(g_str_has_prefix(type, DIRECT)) { + INFO("auto proxy is set to direct mode\n"); + fclose(fp_pacfile); + } + else if(g_str_has_prefix(type, PROXY)) { + INFO("auto proxy is set to proxy mode\n"); + INFO("type: %s, proxy: %s\n", type, proxy); + p = strtok(proxy, "\";"); + if(p != NULL) { + INFO("auto proxy to set: %s\n",p); + strcpy(http_proxy, p); + strcpy(https_proxy, p); + strcpy(ftp_proxy, p); + strcpy(socks_proxy, p); + } + fclose(fp_pacfile); + } + else + { + ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n"); + fclose(fp_pacfile); + } + } + else { + ERR("fail to get pacfile fp\n"); + return -1; + } + + remove(pac_tempfile); + return 0; +} + +static void get_proxy(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy) +{ + char buf[MAXLEN] = {0,}; + char buf_port[MAXPORTLEN] = {0,}; + char buf_proxy[MAXLEN] = {0,}; + char *buf_proxy_bak; + char *proxy; + FILE *output; + int MAXPROXYLEN = MAXLEN + MAXPORTLEN; + + output = popen("gconftool-2 --get /system/http_proxy/host", "r"); + if(fscanf(output, "%s", buf) > 0) { + snprintf(buf_proxy, MAXLEN, "%s", buf); + } + pclose(output); + + output = popen("gconftool-2 --get /system/http_proxy/port", "r"); + if(fscanf(output, "%s", buf_port) <= 0) { + //for abnormal case: if can't find the key of http port, get from environment value. + buf_proxy_bak = getenv("http_proxy"); + INFO("http_proxy from env: %s\n", buf_proxy_bak); + if(buf_proxy_bak != NULL) { + proxy = malloc(MAXLEN); + remove_string(buf_proxy_bak, proxy, HTTP_PREFIX); + strncpy(http_proxy, proxy, strlen(proxy)-1); + INFO("final http_proxy value: %s\n", http_proxy); + free(proxy); + } + else { + INFO("http_proxy is not set on env.\n"); + pclose(output); + return; + } + + } + else { + snprintf(http_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf_port); + memset(buf_proxy, 0, MAXLEN); + INFO("http_proxy: %s\n", http_proxy); + } + pclose(output); + + memset(buf, 0, MAXLEN); + + output = popen("gconftool-2 --get /system/proxy/secure_host", "r"); + if(fscanf(output, "%s", buf) > 0) { + snprintf(buf_proxy, MAXLEN, "%s", buf); + } + pclose(output); + + output = popen("gconftool-2 --get /system/proxy/secure_port", "r"); + if(fscanf(output, "%s", buf) > 0) { + snprintf(https_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf); + } + pclose(output); + memset(buf, 0, MAXLEN); + memset(buf_proxy, 0, MAXLEN); + INFO("https_proxy : %s\n", https_proxy); + + output = popen("gconftool-2 --get /system/proxy/ftp_host", "r"); + if(fscanf(output, "%s", buf) > 0) { + snprintf(buf_proxy, MAXLEN, "%s", buf); + } + pclose(output); + + output = popen("gconftool-2 --get /system/proxy/ftp_port", "r"); + if(fscanf(output, "%s", buf) > 0) { + snprintf(ftp_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf); + } + pclose(output); + memset(buf, 0, MAXLEN); + memset(buf_proxy, 0, MAXLEN); + INFO("ftp_proxy : %s\n", ftp_proxy); + + output = popen("gconftool-2 --get /system/proxy/socks_host", "r"); + if(fscanf(output, "%s", buf) > 0) { + snprintf(buf_proxy, MAXLEN, "%s", buf); + } + pclose(output); + + output = popen("gconftool-2 --get /system/proxy/socks_port", "r"); + if(fscanf(output, "%s", buf) > 0) { + snprintf(socks_proxy, MAXPROXYLEN, "%s:%s", buf_proxy, buf); + } + pclose(output); + INFO("socks_proxy : %s\n", socks_proxy); +} + + +void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy) +{ + char buf[MAXLEN]; + FILE *output; + + output = popen("gconftool-2 --get /system/proxy/mode", "r"); + if(fscanf(output, "%s", buf) > 0) { + //priority : auto > manual > none + if (strcmp(buf, "auto") == 0) { + INFO("AUTO PROXY MODE\n"); + get_auto_proxy(http_proxy, https_proxy, ftp_proxy, socks_proxy); + } + else if (strcmp(buf, "manual") == 0) { + INFO("MANUAL PROXY MODE\n"); + get_proxy(http_proxy, https_proxy, ftp_proxy, socks_proxy); + } + else if (strcmp(buf, "none") == 0) { + INFO("DIRECT PROXY MODE\n"); + } + } + pclose(output); +} diff --git a/tizen/src/osutil.c b/tizen/src/osutil.c new file mode 100644 index 0000000..10877c6 --- /dev/null +++ b/tizen/src/osutil.c @@ -0,0 +1,91 @@ +/* + * Emulator + * + * Copyright (C) 2012, 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * SeokYeon Hwang + * MunKyu Im + * GiWoong Kim + * YeongKyoon Lee + * HyunJun Son + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +/** + @file osutil.c + @brief Common functions for osutil + */ + +#include "osutil.h" +#include "debug_ch.h" + +#include +#include + +MULTI_DEBUG_CHANNEL(emulator, osutil); + +const char *pac_tempfile = ".autoproxy"; + +inline size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + size_t written; + written = fwrite(ptr, size, nmemb, stream); + return written; +} + +inline void download_url(char *url) +{ + CURL *curl; + FILE *fp; + CURLcode res; + + curl = curl_easy_init(); + if (curl) { + fp = fopen(pac_tempfile,"wb"); + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + //just in case network does not work. + curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 3000); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); + res = curl_easy_perform(curl); + if(res != 0) { + ERR("Fail to download pac file: %s\n", url); + } + curl_easy_cleanup(curl); + fclose(fp); + } + + return; +} + +inline void remove_string(char *src, char *dst, const char *toremove) +{ + int len = strlen(toremove); + int i, j; + int max_len = strlen(src); + + for(i = len, j = 0; i < max_len; i++) + { + dst[j++] = src[i]; + } + + dst[j] = '\0'; +} diff --git a/tizen/src/osutil.h b/tizen/src/osutil.h index 3bd9474..a0acb9b 100644 --- a/tizen/src/osutil.h +++ b/tizen/src/osutil.h @@ -34,6 +34,17 @@ #include "maru_common.h" +#define HTTP_PROTOCOL "http=" +#define HTTP_PREFIX "http://" +#define HTTPS_PROTOCOL "https=" +#define FTP_PROTOCOL "ftp=" +#define SOCKS_PROTOCOL "socks=" +#define DIRECT "DIRECT" +#define PROXY "PROXY" +#define MAXPORTLEN 6 + +extern const char *pac_tempfile; + void check_vm_lock_os(void); void make_vm_lock_os(void); @@ -41,5 +52,11 @@ void set_bin_path_os(gchar *); void print_system_info_os(void); +void get_host_proxy_os(char *, char *, char *, char *); + +inline void download_url(char *); +inline size_t write_data(void *, size_t, size_t, FILE *); +inline void remove_string(char *, char *, const char *); + #endif // __OS_UTIL_H__ -- 2.7.4