net: support bridged network
authorMunkyu Im <munkyu.im@samsung.com>
Tue, 28 Apr 2015 12:49:38 +0000 (21:49 +0900)
committerMunkyu Im <munkyu.im@samsung.com>
Thu, 30 Apr 2015 08:37:05 +0000 (17:37 +0900)
get IP information from kernel parameter and set to guest network system.

Change-Id: I1270a334bcac9ed7d62d852d8570f8750a0fd999
Signed-off-by: Munkyu Im <munkyu.im@samsung.com>
CMakeLists.txt
include/emuld.h
packaging/emuld.spec
src/emuld.cpp
src/net.cpp [new file with mode: 0644]

index 201fb11c0d671ebbc50e641dd016dc216e5f48e3..cd93b6d8f455901c496fdec24255a1e1c11d87c4 100644 (file)
@@ -26,6 +26,7 @@ ENDIF()
 
 SET(SRCS
     src/emuld.cpp
+    src/net.cpp
     src/evdi.cpp
     src/client.cpp
     src/common.cpp
index 73b2bdd21e4abb0bde0b73cd427652397bcca335..e93bf16424ded1aba899ca40dbb26d849dcb7dbc 100644 (file)
@@ -185,6 +185,7 @@ bool read_ijcmd(const int fd, ijcommand* ijcmd);
 int recv_data(int event_fd, char** r_databuf, int size);
 void recv_from_evdi(evdi_fd fd);
 bool accept_proc(const int server_fd);
+void get_host_addr(void);
 
 void set_vconf_cb(void);
 void send_to_ecs(const char* cat, int group, int action, char* data);
index 3d3185c42d3ca4859fcbf6fc52894fafa4adf148..3119fb267b27ae57c955c7ff23000c7824d322a3 100644 (file)
@@ -1,5 +1,5 @@
 Name: emuld
-Version: 0.9.1
+Version: 0.9.2
 Release: 0
 Summary: Emulator daemon
 License: Apache-2.0
index c7122fd514a28495c9ca72451b2bc0a90d656c64..fef86ad0548155a443485cff09194169774bf1fb 100644 (file)
@@ -362,6 +362,7 @@ int main( int argc , char *argv[])
 
     LOGINFO("[START] epoll & device init success");
 
+    get_host_addr();
     add_vconf_map_common();
     add_vconf_map_profile();
     set_vconf_cb();
diff --git a/src/net.cpp b/src/net.cpp
new file mode 100644 (file)
index 0000000..a78f684
--- /dev/null
@@ -0,0 +1,178 @@
+/*
+ * emulator-daemon
+ *
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Munkyu Im <munkyu.im@samsnung.com>
+ * Sangho Park <sangho1206.park@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <netdb.h>
+#include <string.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <ctype.h>
+#include <fcntl.h>
+#include "emuld.h"
+
+#define PROC_CMDLINE_PATH "/proc/cmdline"
+#define IP_SUFFIX "ip="
+#define IJTYPE_GUESTIP "guest_ip"
+char g_guest_ip[64];
+static void send_guest_ip_req(void)
+{
+    LXT_MESSAGE *packet = (LXT_MESSAGE *)calloc(1 ,sizeof(LXT_MESSAGE));
+    if (packet == NULL) {
+        return;
+    }
+
+    packet->length = strlen(g_guest_ip);
+    packet->group = 0;
+    packet->action = STATUS;
+
+    const int tmplen = HEADER_SIZE + packet->length;
+    char *tmp = (char *)malloc(tmplen);
+    if (!tmp) {
+        if (packet)
+            free(packet);
+        return;
+    }
+
+    memcpy(tmp, packet, HEADER_SIZE);
+    memcpy(tmp + HEADER_SIZE, g_guest_ip, packet->length);
+
+    ijmsg_send_to_evdi(g_fd[fdtype_device], IJTYPE_GUESTIP, (const char*) tmp, tmplen);
+
+
+    if (tmp)
+        free(tmp);
+    if (packet)
+        free(packet);
+}
+
+static char *s_strncpy(char *dest, const char *source, size_t n)
+{
+    char *start = dest;
+
+    while (n && (*dest++ = *source++)) {
+        n--;
+    }
+    if (n) {
+        while (--n) {
+            *dest++ = '\0';
+        }
+    }
+    return start;
+}
+
+
+static int get_str_cmdline(char *src, const char *dest, char str[], int str_size)
+{
+    char *s = strstr(src, dest);
+    if (s == NULL) {
+        return -1;
+    }
+    char *e = strstr(s, " ");
+    if (e == NULL) {
+        return -1;
+    }
+
+    int len = e-s-strlen(dest);
+
+    if (len >= str_size) {
+        LOGERR("buffer size(%d) should be bigger than %d\n", str_size, len+1);
+        return -1;
+    }
+
+    s_strncpy(str, s + strlen(dest), len);
+    return len;
+}
+
+static int get_network_info(char str[], int str_size)
+{
+    size_t len = 0;
+    ssize_t read;
+    char *line = NULL;
+    FILE *fp = fopen(PROC_CMDLINE_PATH, "r");
+
+    if (fp == NULL) {
+        LOGERR("fail to read /proc/cmdline\n");
+        return -1;
+    }
+    while((read = getline(&line, &len, fp)) != -1) {
+        LOGERR("line: %s\n", line);
+        LOGERR("len: %d\n", len);
+    }
+    if (get_str_cmdline(line, IP_SUFFIX, str, str_size) < 1) {
+        LOGERR("could not get the (%s) value from cmdline\n", IP_SUFFIX);
+        fclose(fp);
+        return -1;
+    }
+    fclose(fp);
+    return 0;
+}
+
+void get_host_addr()
+{
+    int fd;
+    struct ifreq ifrq;
+    struct sockaddr_in *sin;
+    char guest_net[256] = {0,};
+    if (get_network_info(guest_net, sizeof guest_net) == 0) {
+        char *token;
+        int i = 0;
+        char *str = strdup(guest_net);
+        while ((token = strsep(&str, ":"))) {
+            if (i == 0) {
+                strncpy(g_guest_ip, token, strlen(token));
+                LOGDEBUG("set guest_ip: %s\n", g_guest_ip);
+            }
+            LOGDEBUG("token[%d]: %s\n",i++, token);
+        }
+        free(str);
+    } else {
+        fd = socket(AF_INET, SOCK_DGRAM, 0);
+        if (fd < 0) {
+            perror("Socket open error");
+            return;
+        }
+        strcpy(ifrq.ifr_name, "eth0");
+        if (ioctl(fd, SIOCGIFADDR, &ifrq) < 0) {
+            perror("IPADDR Error");
+            close(fd);
+            return;
+        }
+        sin = (struct sockaddr_in *)&ifrq.ifr_addr;
+        LOGDEBUG("IPADDR : %s\n", inet_ntoa(sin->sin_addr));
+        strncpy(g_guest_ip,  inet_ntoa(sin->sin_addr), strlen(inet_ntoa(sin->sin_addr)));
+        LOGDEBUG("set guest_ip: %s\n", g_guest_ip);
+
+        close(fd);
+    }
+    send_guest_ip_req();
+}
+