ethernet_interface: Clean up object creation

This cleans up the loop and makes sure the interface parsing code
updates the local variables for each iteration of the loop.

Tested:
    Built and verified that objects were still being created as
    expected.

Change-Id: I5ba442538f6d2b5a4dc6a8a1261777c0303818d1
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/ethernet_interface.cpp b/ethernet_interface.cpp
index f2ad372..94bfecd 100644
--- a/ethernet_interface.cpp
+++ b/ethernet_interface.cpp
@@ -57,23 +57,31 @@
     }
 }
 
+static IP::Protocol convertFamily(int family)
+{
+    switch (family)
+    {
+        case AF_INET:
+            return IP::Protocol::IPv4;
+        case AF_INET6:
+            return IP::Protocol::IPv6;
+    }
+
+    throw std::invalid_argument("Bad address family");
+}
+
 void EthernetInterface::createIPAddressObjects()
 {
-    std::string gateway;
     addrs.clear();
 
     auto addrs = getInterfaceAddrs()[interfaceName()];
 
-    IP::Protocol addressType = IP::Protocol::IPv4;
-    IP::AddressOrigin origin = IP::AddressOrigin::Static;
     route::Table routingTable;
 
     for (auto& addr : addrs)
     {
-        if (addr.addrType == AF_INET6)
-        {
-            addressType = IP::Protocol::IPv6;
-        }
+        IP::Protocol addressType = convertFamily(addr.addrType);
+        IP::AddressOrigin origin = IP::AddressOrigin::Static;
         if (dHCPEnabled())
         {
             origin = IP::AddressOrigin::DHCP;
@@ -82,7 +90,7 @@
         {
             origin = IP::AddressOrigin::LinkLocal;
         }
-        gateway =
+        std::string gateway =
             routingTable.getGateway(addr.addrType, addr.ipaddress, addr.prefix);
 
         std::string ipAddressObjectPath = generateObjectPath(
@@ -93,8 +101,6 @@
                                 bus, ipAddressObjectPath.c_str(), *this,
                                 addressType, addr.ipaddress, origin,
                                 addr.prefix, gateway));
-
-        origin = IP::AddressOrigin::Static;
     }
 }