jwbensley / Etherate

Linux CLI Ethernet and MPLS Testing Tool
MIT License
176 stars 19 forks source link

Diff submitted via email to verify interface #20

Closed jwbensley closed 7 years ago

jwbensley commented 7 years ago

Evalute if this code can be pulled into Etherate, submitted by Christophe via email, to check if an interface is connected:

diff -urpN Etherate/defaults.c Etherate-clucas/defaults.c
--- Etherate/defaults.c 2017-06-12 13:53:17.446600433 +0200
+++ Etherate-clucas/defaults.c  2017-06-15 09:22:39.639698487 +0200
@@ -283,6 +283,11 @@ int16_t setup_socket_interface(struct fr

     }

+    if (verify_interface_connectivity(test_interface) != 1) {
+        printf("Error: Interace '%s' is DOWN.\n", (char*)test_interface->IF_NAME);
+            return EX_SOFTWARE;
+    }
+

     // Link layer socket setup
     test_interface->SOCKET_ADDRESS.sll_family   = PF_PACKET;    
diff -urpN Etherate/functions.c Etherate-clucas/functions.c
--- Etherate/functions.c    2017-06-12 13:53:17.456600422 +0200
+++ Etherate-clucas/functions.c 2017-06-15 09:23:27.819647543 +0200
@@ -38,6 +38,7 @@
  * print_usage()
  * remove_interface_promisc()
  * reset_app()
+ * verify_interface_connectivity()
  * set_interface_promisc()
  * set_sock_interface_index()
  * set_sock_interface_name()
@@ -1212,6 +1219,24 @@ void reset_app(struct frame_headers *fra

 }

+int16_t verify_interface_connectivity(struct test_interface *test_interface) 
+{
+    struct ifreq if_req;
+
+    int socId = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
+    if (socId < 0) 
+        fprintf(stderr, "Socket failed. Errno = %d\n", errno);
+
+
+    (void) strncpy(if_req.ifr_name, (char*)test_interface->IF_NAME, sizeof(if_req.ifr_name));
+    int rv = ioctl(socId, SIOCGIFFLAGS, &if_req);
+    close(socId);
+
+    if (rv == -1) 
+        fprintf(stderr, "Ioctl failed. Errno = %d\n", errno);
+
+    return (if_req.ifr_flags & IFF_UP) && (if_req.ifr_flags & IFF_RUNNING);
+}

 int16_t set_interface_promisc(struct test_interface *test_interface)
jwbensley commented 7 years ago

I have in-lined this in Beta 0.13 rather than add a new function as it's just two lines of code this way rather than defining a new socket et al. to run the ioctl().

// Is this interface even up?
if (ioctl(test_interface->SOCKET_FD, SIOCGIFFLAGS, &ifreq) == -1) break;
if (!(ifreq.ifr_flags & IFF_UP && ifreq.ifr_flags & IFF_RUNNING)) break;