net: Add check availability 27/228127/3
authorDongwoo Lee <dwoo08.lee@samsung.com>
Wed, 18 Mar 2020 03:32:08 +0000 (12:32 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Thu, 19 Mar 2020 13:57:55 +0000 (13:57 +0000)
If the target has NIC named eth0 or wlan0, then it considers that
network is available.

Change-Id: I6cb0bc48a9bb8154a8d02fd183db105554ea9862
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
src/net.c

index 6cd8587..e4da0ca 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -23,6 +23,8 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <arpa/inet.h>
+#include <netdb.h>
+#include <ifaddrs.h>
 
 #include "interface.h"
 
@@ -96,6 +98,33 @@ static int net_disconnect(struct tfm_interface *intf)
        return close(intf->rxd);
 }
 
+static int net_available(void)
+{
+       struct ifaddrs *ifs, *cur;
+       char host[NI_MAXHOST];
+       int ret, s;
+
+       ret = getifaddrs(&ifs);
+       if (ret < 0)
+               return 0;
+
+       /*
+        * TODO:
+        * On RPI-family targets it works well checking network availabilty
+        * via presence of NIC 'eth0' or 'wlan0', but it looks quite hack
+        * anyway. So, it should be changed if there is more generic way.
+        */
+       for (cur = ifs; cur != NULL; cur = cur->ifa_next) {
+               if (!strcmp(cur->ifa_name, "eth0"))
+                       return 1;
+
+               if (!strcmp(cur->ifa_name, "wlan0"))
+                       return 1;
+       }
+
+       return 0;
+}
+
 static struct tfm_interface_driver network = {
        .name = "net",
        .ops = {
@@ -103,6 +132,7 @@ static struct tfm_interface_driver network = {
                .disconnect = net_disconnect,
                .rx_data = net_rx_data,
                .tx_data = net_tx_data,
+               .available = net_available,
        },
 };
 INTERFACE_REGISTER(network)