Add reacher initial functions 00/190100/1
authorYu <jiung.yu@samsung.com>
Tue, 18 Sep 2018 08:43:36 +0000 (17:43 +0900)
committerYu <jiung.yu@samsung.com>
Tue, 18 Sep 2018 08:43:46 +0000 (17:43 +0900)
Change-Id: I28b281a8f5e62cdf64fc94f45586716a5e9bbe57
Signed-off-by: Yu Jiung <jiung.yu@samsung.com>
include/inm-reacher.h [new file with mode: 0644]
packaging/inm-manager.spec
src/CMakeLists.txt
src/inm-reacher.c [new file with mode: 0644]

diff --git a/include/inm-reacher.h b/include/inm-reacher.h
new file mode 100644 (file)
index 0000000..449129c
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Network Monitoring Module
+ *
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * 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.
+ *
+ */
+
+/**
+ * This file defines macros and declares functions get Internet reachability.
+ *
+ * @file        inm-reacher.h
+ * @author      Jiung Yu (jiung.yu@samsung.com)
+ * @version     0.1
+ */
+
+#ifndef __INM_REACHER_H__
+#define __INM_REACHER_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum {
+       INM_REACHER_ERROR_NONE = 0,
+       INM_REACHER_ERROR_NOT_INITIALIZED,
+       INM_REACHER_ERROR_INVALID_PARAM,
+       INM_REACHER_ERROR_OPERATION_FAILED,
+       INM_REACHER_ERROR_IN_PROGRESS,
+       INM_REACHER_ERROR_URL_NOT_EXIST,
+       INM_REACHER_ERROR_URL_NOT_FOUND,
+       INM_REACHER_ERROR_DNS_NOT_RESOLVED,
+       INM_REACHER_ERROR_TIMEOUT,
+       INM_REACHER_ERROR_CURL,
+
+} inm_reacher_error_e;
+
+typedef enum {
+       INM_REACHER_CHECK_DEFAULT_URLS,
+       INM_REACHER_CHECK_USER_URLS,
+}inm_reacher_check_type_e;
+
+int inm_reacher_init(void);
+int inm_reacher_deinit(void);
+
+
+typedef void (*reacher_callback) (
+               inm_reacher_error_e err,
+               gboolean found,
+               const char *url,
+               gpointer user_data);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __INM_REACHER_H__ */
index 635662cfe8a896ea8e8b6e7e4eceda2d5e8db509..8628fea46242bcca7eacd2fd84a7a13e50b0875d 100755 (executable)
@@ -18,6 +18,7 @@ BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(libtzplatform-config)
 BuildRequires: pkgconfig(vconf)
 BuildRequires: pkgconfig(libnl-2.0)
+BuildRequires: pkgconfig(libcurl)
 BuildRequires:  python
 BuildRequires:  python-xml
 Requires:       security-config
index 4176fdab1926d0c8d2f3c52d80b85352d0fe43fc..8f6597a33b9d31b229f5ccb502a3ba8884c0d4a5 100644 (file)
@@ -27,6 +27,7 @@ PKG_CHECK_MODULES(inm_pkgs REQUIRED
        dlog
        libnl-2.0
        vconf
+       libcurl
        )
 
 FOREACH(flag ${inm_pkgs_CFLAGS})
diff --git a/src/inm-reacher.c b/src/inm-reacher.c
new file mode 100644 (file)
index 0000000..8254af3
--- /dev/null
@@ -0,0 +1,172 @@
+#include <string.h>
+#include <unistd.h>
+
+#include <errno.h>
+
+#include <curl/curl.h>
+#include <curl/multi.h>
+
+#include <glib.h>
+
+#include <linux/if.h>
+
+#include "inm-manager-log.h"
+#include "inm-reacher.h"
+#include "inm-util.h"
+
+#define SHOW_VERBOSE 0
+
+#define RET_ERR_IF_NOT_INIT()\
+       do {\
+               if (!g_reacher) {\
+                       __INM_FUNC_EXIT__;\
+                       return INM_REACHER_ERROR_NOT_INITIALIZED;\
+               }\
+       } while (0)
+#define RET_ERR_IF_INVALID_PARAM(param)\
+       do {\
+               if (!(param)) {\
+                       __INM_FUNC_EXIT__;\
+                       return INM_REACHER_ERROR_INVALID_PARAM;\
+               }\
+       } while (0)
+
+/* Information associated with a specific easy handle */
+typedef struct {
+       CURLM *p_mh;
+       CURL *p_eh;
+       char *url;
+       gboolean found;
+       double resolved_time;
+       double total_time;
+       double download_byte;
+       double speed;
+       char error[CURL_ERROR_SIZE];
+} conn_info_s;
+
+/* Information associated with a specific socket */
+typedef struct {
+       curl_socket_t sockfd;
+       CURL *p_eh;
+       int action;
+       long timeout;
+       GIOChannel *ch;
+       guint source_id;
+} sock_data_s;
+
+/* Global information, common to all connections */
+typedef struct {
+       CURLM *p_mh;
+       GSList *user_added_url;
+
+       guint timer_source_id;
+       int active_connection;
+       gboolean running;
+       conn_info_s *current_conn;
+       sock_data_s *current_sock;
+
+       GSList *current;
+       int current_index;
+
+       inm_reacher_check_type_e type;
+       gboolean once;
+       reacher_callback cb;
+       gpointer cb_user_data;
+} reacher_data_s;
+
+reacher_data_s *g_reacher;
+
+static const char *default_urls[] = {
+       "http://www.microsoft.com",
+       "http://www.opensource.org",
+       "http://www.google.com",
+       "http://www.yahoo.com",
+       "http://www.ibm.com",
+       "http://www.mysql.com",
+       "http://www.oracle.com",
+       "http://www.ripe.net",
+       "http://www.iana.org",
+       "http://www.amazon.com",
+       "http://www.netcraft.com",
+       "http://www.heise.de",
+       "http://www.chip.de",
+       "http://www.ca.com",
+       "http://www.cnet.com",
+       "http://www.news.com",
+       "http://www.cnn.com",
+       "http://www.wikipedia.org",
+       "http://www.dell.com",
+       "http://www.hp.com",
+       "http://www.cert.org",
+       "http://www.mit.edu",
+       "http://www.nist.gov",
+       "http://www.ebay.com",
+       "http://www.playstation.com",
+       "http://www.uefa.com",
+       "http://www.ieee.org",
+       "http://www.apple.com",
+       "http://www.symantec.com",
+       "http://www.zdnet.com",
+       "http://www.fujitsu.com",
+       "http://www.supermicro.com",
+       "http://www.hotmail.com",
+       "http://www.ecma.com",
+       "http://www.bbc.co.uk",
+       "http://news.google.com",
+       "http://www.foxnews.com",
+       "http://www.msn.com",
+       "http://www.wired.com",
+       "http://www.sky.com",
+       "http://www.usatoday.com",
+       "http://www.cbs.com",
+       "http://www.nbc.com",
+       "http://slashdot.org",
+       "http://www.techweb.com",
+       "http://www.newslink.org",
+       "http://www.un.org",
+};
+
+#define URL_CNT sizeof(default_urls)/sizeof(char *)
+
+int inm_reacher_init(void)
+{
+       gint ret = INM_REACHER_ERROR_NONE;
+
+       __INM_FUNC_ENTER__;
+
+       if (g_reacher) {
+               __INM_FUNC_EXIT__;
+               return INM_REACHER_ERROR_NONE;
+       }
+
+       g_reacher = g_try_malloc0(sizeof(reacher_data_s));
+       if (!g_reacher) {
+               __INM_FUNC_EXIT__;
+               return INM_REACHER_ERROR_OPERATION_FAILED;
+       }
+
+       __INM_FUNC_EXIT__;
+       return ret;
+}
+
+int inm_reacher_deinit(void)
+{
+       gint ret = INM_REACHER_ERROR_NONE;
+
+       __INM_FUNC_ENTER__;
+
+       RET_ERR_IF_NOT_INIT();
+
+       g_slist_free_full(g_reacher->user_added_url, g_free);
+       g_reacher->user_added_url = NULL;
+
+       g_reacher->active_connection = 0;
+       g_reacher->cb = NULL;
+       g_reacher->cb_user_data = NULL;
+       g_reacher->current = NULL;
+       g_reacher->current_index = 0;
+       g_reacher->running = FALSE;
+
+       __INM_FUNC_EXIT__;
+       return ret;
+}