VOL-2854: Add command line option to specify interface name in openolt on which gRPC server will run
          - OLT management interface or inband interface can be given as command line input and gRPC
            server will listen on given interface's IP address
          - If no interface name given gRPC server will listen on "0.0.0.0:9191"

Change-Id: I506fabf7ae81bf4d4ed8f6199107801daff62941
diff --git a/agent/src/core_utils.cc b/agent/src/core_utils.cc
index 32de09d..6570c2f 100644
--- a/agent/src/core_utils.cc
+++ b/agent/src/core_utils.cc
@@ -1267,3 +1267,28 @@
     return Status::OK;
 }
 
+std::string get_ip_address(const char* nw_intf){
+    std::string ipAddress = "0.0.0.0";
+    struct ifaddrs *interfaces = NULL;
+    struct ifaddrs *temp_addr = NULL;
+    int success = 0;
+   /* retrieve the current interfaces - returns 0 on success */
+    success = getifaddrs(&interfaces);
+    if (success == 0) {
+        /* Loop through linked list of interfaces */
+        temp_addr = interfaces;
+        while(temp_addr != NULL) {
+            if(temp_addr->ifa_addr->sa_family == AF_INET) {
+                /* Check if interface given present in OLT, if yes return its IP Address */
+                if(strcmp(temp_addr->ifa_name, nw_intf) == 0){
+                    ipAddress=inet_ntoa(((struct sockaddr_in*)temp_addr->ifa_addr)->sin_addr);
+                    break;
+                }
+            }
+            temp_addr = temp_addr->ifa_next;
+        }
+    }
+    /* Free memory */
+    freeifaddrs(interfaces);
+    return ipAddress;
+}
diff --git a/agent/src/core_utils.h b/agent/src/core_utils.h
index ad48474..b686867 100644
--- a/agent/src/core_utils.h
+++ b/agent/src/core_utils.h
@@ -17,6 +17,8 @@
 #define OPENOLT_CORE_UTILS_H_
 #include <string>
 #include <unistd.h>
+#include <ifaddrs.h>
+#include <arpa/inet.h>
 
 #include "core.h"
 #include "core_data.h"
@@ -98,6 +100,5 @@
 Status handle_acl_rule_cleanup(int16_t acl_id, int32_t gemport_id, int32_t intf_id, const std::string flow_type);
 Status check_bal_ready();
 Status check_connection();
+std::string get_ip_address(const char* nw_intf);
 #endif // OPENOLT_CORE_UTILS_H_
-
-