udp: refactor udp code

Change-Id: I44d5c9df7c49b8d4d5677c6d319033b2da3e6b80
Signed-off-by: Florin Coras <fcoras@cisco.com>
diff --git a/src/vnet/ip/ip.c b/src/vnet/ip/ip.c
index caa553d..bd9706b 100644
--- a/src/vnet/ip/ip.c
+++ b/src/vnet/ip/ip.c
@@ -63,6 +63,24 @@
   return (flags & FIB_ENTRY_FLAG_LOCAL);
 }
 
+void
+ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4)
+{
+  if (is_ip4)
+    dst->ip4.as_u32 = src->ip4.as_u32;
+  else
+    clib_memcpy (&dst->ip6, &src->ip6, sizeof (ip6_address_t));
+}
+
+void
+ip_set (ip46_address_t * dst, void *src, u8 is_ip4)
+{
+  if (is_ip4)
+    dst->ip4.as_u32 = ((ip4_address_t *) src)->as_u32;
+  else
+    clib_memcpy (&dst->ip6, (ip6_address_t *) src, sizeof (ip6_address_t));
+}
+
 u8
 ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4)
 {
@@ -97,22 +115,37 @@
   return 0;
 }
 
-void
-ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4)
+void *
+ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
 {
-  if (is_ip4)
-    dst->ip4.as_u32 = src->ip4.as_u32;
-  else
-    clib_memcpy (&dst->ip6, &src->ip6, sizeof (ip6_address_t));
-}
+  ip_lookup_main_t *lm4 = &ip4_main.lookup_main;
+  ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
+  ip_interface_address_t *ia = 0;
 
-void
-ip_set (ip46_address_t * dst, void *src, u8 is_ip4)
-{
   if (is_ip4)
-    dst->ip4.as_u32 = ((ip4_address_t *) src)->as_u32;
+    {
+      /* *INDENT-OFF* */
+      foreach_ip_interface_address (lm4, ia, sw_if_index, 1 /* unnumbered */ ,
+      ({
+        return ip_interface_address_get_address (lm4, ia);
+      }));
+      /* *INDENT-ON* */
+    }
   else
-    clib_memcpy (&dst->ip6, (ip6_address_t *) src, sizeof (ip6_address_t));
+    {
+      /* *INDENT-OFF* */
+      foreach_ip_interface_address (lm6, ia, sw_if_index, 1 /* unnumbered */ ,
+      ({
+        ip6_address_t *rv;
+        rv = ip_interface_address_get_address (lm6, ia);
+        /* Trying to use a link-local ip6 src address is a fool's errand */
+        if (!ip6_address_is_link_local_unicast (rv))
+          return rv;
+      }));
+      /* *INDENT-ON* */
+    }
+
+  return 0;
 }
 
 /*
diff --git a/src/vnet/ip/ip.h b/src/vnet/ip/ip.h
index 3b3a465..9387ba3 100644
--- a/src/vnet/ip/ip.h
+++ b/src/vnet/ip/ip.h
@@ -198,6 +198,7 @@
 u8 ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4);
 void ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4);
 void ip_set (ip46_address_t * dst, void *src, u8 is_ip4);
+void *ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4);
 
 #endif /* included_ip_main_h */