emul_state: guest IP and host IP getters/setters are refined
authorSeokYeon Hwang <syeon.hwang@samsung.com>
Wed, 6 Jan 2016 06:49:53 +0000 (15:49 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Thu, 7 Jan 2016 11:02:03 +0000 (20:02 +0900)
Change-Id: I2a7b3bed1fb653855c21e61da79a85b35dfae524
Signed-off-by: SeokYeon Hwang <syeon.hwang@samsung.com>
net/tap.c
tizen/src/ecs/ecs_msg_injector.c
tizen/src/emul_state.c
tizen/src/emul_state.h
tizen/src/emulator.c
tizen/src/ui/menu/shellopener.cpp
tizen/src/util/net_helper.c

index 3a67a3e..e377aab 100644 (file)
--- a/net/tap.c
+++ b/net/tap.c
@@ -403,7 +403,7 @@ static char *get_interface_name_from_ip(void)
         if (ifaddr->ifa_addr->sa_family == AF_INET) {
             sa = (struct sockaddr_in *) ifaddr->ifa_addr;
             addr = inet_ntoa(sa->sin_addr);
-            if (0 == strcmp(addr, get_emul_host_ip())) {
+            if (0 == strcmp(addr, get_host_ip())) {
                 freeifaddrs(ifaddrs);
                 if (0 == strcmp(addr, BRIDGE_NAME)) {
                     fprintf(stdout, "interface is bridge\n");
index 84a2797..c98fbd2 100644 (file)
@@ -459,7 +459,7 @@ static bool injector_req_handle(char* cat, type_action action, const char* data)
     } else if (!strcmp(cat, MSG_TYPE_GUESTIP)) {
         if (data != NULL && strlen(data) > 0) {
             LOG_INFO("guest ip: %s\n", data);
-            set_emul_guest_ip((char*)data);
+            set_guest_ip((char*)data);
         } else {
             LOG_SEVERE("guest ip is null!\n");
         }
index ef382b5..f313877 100644 (file)
@@ -63,50 +63,10 @@ extern bool hax_allowed;
 DECLARE_DEBUG_CHANNEL(emul_state);
 
 #define MAXLEN  512
-#define HTTP_PROXY_PREFIX "http_proxy="
-#define HOST_IP_PREFIX "host_ip="
 
 static EmulatorConfigInfo _emul_info;
 static EmulatorConfigState _emul_state;
 
-void set_emul_guest_ip(char *ip)
-{
-    strncpy(_emul_info.guest_ip, ip, strlen(ip));
-}
-
-char* get_emul_guest_ip(void)
-{
-    LOG_INFO("guest ip: %s\n", _emul_info.guest_ip);
-    return _emul_info.guest_ip;
-}
-
-// FIXME: should be cleaned up
-void set_emul_host_ip(char *kernel_cmdline)
-{
-    char *buf = strstr(kernel_cmdline, HOST_IP_PREFIX);
-
-    if (buf) {
-        char buf_host_ip[MAXLEN] = {0,};
-        int len = strlen(HOST_IP_PREFIX);
-        int i, j;
-        int max_len = strlen(buf);
-        for(i = len, j = 0; i < max_len; i++) {
-            if (buf[i] == ' ' || buf[i] == '\0')
-                break;
-            buf_host_ip[j++] = buf[i];
-        }
-
-        buf_host_ip[j] = '\0';
-        LOG_INFO("host_ip: %s\n", buf_host_ip);
-        strncpy(_emul_info.host_ip, buf_host_ip, sizeof(_emul_info.host_ip));
-    }
-}
-
-char* get_emul_host_ip(void)
-{
-    return _emul_info.host_ip;
-}
-
 int get_max_touch_point(void)
 {
     if (ts) {
@@ -426,10 +386,11 @@ const char *get_http_proxy_addr(void)
         return http_proxy_addr;
     }
 
-    const char *kernel_cmdline = qemu_opt_get(qemu_get_machine_opts(), "append");
+    const char *kernel_cmdline =
+        qemu_opt_get(qemu_get_machine_opts(), "append");
 
     // kernel cmdline always contains proxy information
-    char *buf = g_strstr_len(kernel_cmdline, -1, HTTP_PROXY_PREFIX);
+    char *buf = g_strstr_len(kernel_cmdline, -1, "http_proxy=");
     if (buf) {
         char **token = g_strsplit_set(buf, "= ", 3);
         if (token[0] && token[1] && g_strcmp0(token[1], "")) {
@@ -557,12 +518,8 @@ uint64_t get_ram_size(void)
 }
 
 // VM base port
-static int vm_base_port = 0;
-
-void set_vm_base_port(int port)
-{
-    vm_base_port = port;
-}
+// only set by net_helper.c when start up
+int vm_base_port = -1;
 
 int get_vm_base_port(void)
 {
@@ -699,7 +656,7 @@ const char *get_sdcard_image_path(void)
 
     char *ptr = realpath(sdcard_rel_path, sdcard_abs_path);
     if (!ptr) {
-        LOG_WARNING("fail to get absolute path!\n");
+        LOG_WARNING("Fail to get absolute path !!!\n");
         return sdcard_rel_path;
     }
 #ifndef CONFIG_WIN32
@@ -711,6 +668,65 @@ const char *get_sdcard_image_path(void)
     return sdcard_path;
 }
 
+// guest IP
+static const char *guest_ip = NULL;
+
+void set_guest_ip(const char *ip)
+{
+    if (guest_ip) {
+        LOG_WARNING("Guest IP is aleady set !!!\n");
+
+        return;
+    }
+    guest_ip = g_strdup(ip);
+}
+
+const char* get_guest_ip(void)
+{
+    if (!guest_ip) {
+        LOG_WARNING("Guest IP is NULL !!!\n");
+    }
+
+    return guest_ip;
+}
+
+// host IP
+static const char *host_ip = NULL;
+
+const char* get_host_ip(void)
+{
+    if (host_ip) {
+        return host_ip;
+    }
+
+    // try to get from conf
+    const char *host_ip_var = get_variable("host_ip");
+    if (host_ip_var) {
+        host_ip = g_strdup(host_ip_var);
+        return host_ip;
+    }
+
+    // failsafe for legacy
+    // try to parse from kernel commandline
+    const char *kernel_cmdline =
+        qemu_opt_get(qemu_get_machine_opts(), "append");
+    char *buf = g_strstr_len(kernel_cmdline, -1, "host_ip=");
+
+    char **splitted;
+    if (buf && (splitted = g_strsplit(buf, " ", 2))) {
+        if (splitted[0]) {
+            host_ip = g_strdup(splitted[0]);
+        }
+        g_strfreev(splitted);
+
+        return host_ip;
+    }
+
+    g_assert(host_ip != NULL);
+
+    return NULL;
+}
+
 // display resolution
 // only set by vl.c when start up
 int initial_resolution_width = -1;
@@ -762,6 +778,12 @@ static void emul_state_notify_exit(Notifier *notifier, void *data)
 
     g_free((void *)vm_data_path);
     vm_data_path = NULL;
+
+    g_free((void *)guest_ip);
+    guest_ip = NULL;
+
+    g_free((void *)host_ip);
+    host_ip = NULL;
 }
 
 static Notifier emul_state_exit = { .notify = emul_state_notify_exit };
index dcaad44..0ad1e94 100644 (file)
@@ -82,9 +82,6 @@ enum {
 };
 
 typedef  struct EmulatorConfigInfo {
-    char guest_ip[16];
-    char host_ip[16];
-
     const char *skin_path;
     const char *file_sharing_path;
     /* add here */
@@ -103,9 +100,6 @@ typedef struct EmulatorConfigState {
 } EmulatorConfigState;
 
 /* setter */
-void set_emul_guest_ip(char *ip);
-void set_emul_host_ip(char *ip);
-
 void set_emulator_condition(int state);
 void set_emul_caps_lock_state(int state);
 void set_emul_num_lock_state(int state);
@@ -122,8 +116,6 @@ int get_host_lock_key_state(int key);
 int get_host_lock_key_state_darwin(int key);
 int get_emul_caps_lock_state(void);
 int get_emul_num_lock_state(void);
-char* get_emul_guest_ip(void);
-char* get_emul_host_ip(void);
 
 bool get_emuld_connection(void);
 bool get_sdb_connection(void);
@@ -138,6 +130,7 @@ extern const char *launch_conf_file;
 extern const char *bin_path;
 extern int initial_resolution_width;
 extern int initial_resolution_height;
+extern int vm_base_port;
 
 char const *get_bin_path(void);
 char const *get_log_redirect_file(void);
@@ -161,11 +154,15 @@ bool is_netclient_tap_attached(void);
 
 uint64_t get_ram_size(void);
 
-void set_vm_base_port(int port);
 int get_vm_base_port(void);
 int get_vm_device_serial_number(void);
 int get_vm_spice_port(void);
 
+void set_guest_ip(const char *ip);
+void set_host_ip(const char *ip);
+const char* get_guest_ip(void);
+const char* get_host_ip(void);
+
 void set_initial_display_resolution(int width, int height);
 int get_display_resolution_width(void);
 int get_display_resolution_height(void);
index bbb3726..da032f2 100644 (file)
@@ -231,7 +231,7 @@ const char *prepare_maru(const char * const kernel_cmdline)
     maru_kernel_cmdline = g_strdup_printf("%s sdb_port=%d, "
             "vm_resolution=%dx%d", kernel_cmdline, get_vm_base_port(),
             get_display_resolution_width(), get_display_resolution_height());
-    set_emul_host_ip(maru_kernel_cmdline);
+
     LOG_INFO("kernel commandline : %s\n", maru_kernel_cmdline);
 
     return maru_kernel_cmdline;
index c4f5249..20a95a4 100644 (file)
@@ -60,7 +60,7 @@ void ShellOpener::openShell(QString title)
     QString sdbPort = QString::number(get_vm_device_serial_number());
     QString sdbSerialName;
     if (is_netclient_tap_attached()) {
-        sdbSerialName = QString(get_emul_guest_ip());
+        sdbSerialName = QString(get_guest_ip());
     } else {
         sdbSerialName = "emulator-" + sdbPort;
     }
index 1f65afc..82d0382 100644 (file)
@@ -206,7 +206,7 @@ static void start_sdb_noti_server(int server_port);
 
 void init_sdb_and_vm_base_port(void)
 {
-    uint32_t port = 0;
+    uint32_t port = -1;
 
     for (port = START_VM_BASE_PORT ; port < END_VM_BASE_PORT; port += 10) {
         if (prepare_network_port(port) < 0) {
@@ -226,7 +226,7 @@ void init_sdb_and_vm_base_port(void)
 
     start_sdb_noti_server(port + SDB_UDP_NOTI_PORT_INDEX);
 
-    set_vm_base_port(port);
+    vm_base_port = port;
 }
 
 /*