API: Change ip4_address and ip6_address to use type alias.
[vpp.git] / src / vnet / ip / ip_types_api.c
index 7fa8e40..168e615 100644 (file)
 #include <vnet/vnet_all_api_h.h>
 #undef vl_printfun
 
-
-void
-ip_address_decode (const vl_api_address_t * in, ip46_address_t * out)
+static ip46_type_t
+ip_address_union_decode (const vl_api_address_union_t * in,
+                        vl_api_address_family_t af, ip46_address_t * out)
 {
-  switch (in->af)
+  ip46_type_t type;
+
+  switch (clib_net_to_host_u32 (af))
     {
     case ADDRESS_IP4:
-      memset (out, 0, sizeof (*out));
-      clib_memcpy (&out->ip4, &in->un.ip4, sizeof (out->ip4));
+      clib_memset (out, 0, sizeof (*out));
+      clib_memcpy (&out->ip4, &in->ip4, sizeof (out->ip4));
+      type = IP46_TYPE_IP4;
       break;
     case ADDRESS_IP6:
-      clib_memcpy (&out->ip6, &in->un.ip6, sizeof (out->ip6));
+      clib_memcpy (&out->ip6, &in->ip6, sizeof (out->ip6));
+      type = IP46_TYPE_IP6;
+      break;
+    default:
+      ASSERT (!"Unkown address family in API address type");
+      type = IP46_TYPE_ANY;
       break;
     }
+
+  return type;
 }
 
-void
-ip_address_encode (const ip46_address_t * in, vl_api_address_t * out)
+ip46_type_t
+ip_address_decode (const vl_api_address_t * in, ip46_address_t * out)
 {
-  if (ip46_address_is_ip4 (in))
-    {
-      memset (out, 0, sizeof (*out));
-      out->af = ADDRESS_IP4;
-      clib_memcpy (&out->un.ip4, &in->ip4, sizeof (out->un.ip4));
-    }
+  return (ip_address_union_decode (&in->un, in->af, out));
+}
+
+static void
+ip_address_union_encode (const ip46_address_t * in,
+                        vl_api_address_family_t af,
+                        vl_api_address_union_t * out)
+{
+  if (ADDRESS_IP6 == clib_net_to_host_u32 (af))
+    memcpy (out->ip6, &in->ip6, sizeof (out->ip6));
   else
+    memcpy (out->ip4, &in->ip4, sizeof (out->ip4));
+}
+
+void
+ip_address_encode (const ip46_address_t * in,
+                  ip46_type_t type, vl_api_address_t * out)
+{
+  switch (type)
     {
-      out->af = ADDRESS_IP6;
-      clib_memcpy (&out->un.ip6, &in->ip6, sizeof (out->un.ip6));
+    case IP46_TYPE_IP4:
+      out->af = clib_net_to_host_u32 (ADDRESS_IP4);
+      break;
+    case IP46_TYPE_IP6:
+      out->af = clib_net_to_host_u32 (ADDRESS_IP6);
+      break;
+    case IP46_TYPE_ANY:
+      if (ip46_address_is_ip4 (in))
+       out->af = clib_net_to_host_u32 (ADDRESS_IP4);
+      else
+       out->af = clib_net_to_host_u32 (ADDRESS_IP6);
+      break;
     }
+  ip_address_union_encode (in, out->af, &out->un);
 }
 
 void
 ip_prefix_decode (const vl_api_prefix_t * in, fib_prefix_t * out)
 {
-  switch (in->address.af)
+  switch (clib_net_to_host_u32 (in->address.af))
     {
     case ADDRESS_IP4:
       out->fp_proto = FIB_PROTOCOL_IP4;
@@ -74,26 +107,38 @@ ip_prefix_decode (const vl_api_prefix_t * in, fib_prefix_t * out)
       break;
     }
   out->fp_len = in->address_length;
+  out->___fp___pad = 0;
   ip_address_decode (&in->address, &out->fp_addr);
 }
 
 void
 ip_prefix_encode (const fib_prefix_t * in, vl_api_prefix_t * out)
 {
-  switch (in->fp_proto)
-    {
-    case FIB_PROTOCOL_IP4:
-      out->address.af = ADDRESS_IP4;
-      break;
-    case FIB_PROTOCOL_IP6:
-      out->address.af = ADDRESS_IP6;
-      break;
-    case FIB_PROTOCOL_MPLS:
-      ASSERT (0);
-      break;
-    }
   out->address_length = in->fp_len;
-  ip_address_encode (&in->fp_addr, &out->address);
+  ip_address_encode (&in->fp_addr,
+                    fib_proto_to_ip46 (in->fp_proto), &out->address);
+}
+
+void
+ip_mprefix_encode (const mfib_prefix_t * in, vl_api_mprefix_t * out)
+{
+  out->af = (FIB_PROTOCOL_IP6 == in->fp_proto ? ADDRESS_IP6 : ADDRESS_IP4);
+  out->af = clib_host_to_net_u32 (out->af);
+  out->grp_address_length = clib_host_to_net_u16 (in->fp_len);
+
+  ip_address_union_encode (&in->fp_grp_addr, out->af, &out->grp_address);
+  ip_address_union_encode (&in->fp_src_addr, out->af, &out->src_address);
+}
+
+void
+ip_mprefix_decode (const vl_api_mprefix_t * in, mfib_prefix_t * out)
+{
+  out->fp_proto = (ADDRESS_IP6 == clib_net_to_host_u32 (in->af) ?
+                  FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
+  out->fp_len = clib_net_to_host_u16 (in->grp_address_length);
+
+  ip_address_union_decode (&in->grp_address, in->af, &out->fp_grp_addr);
+  ip_address_union_decode (&in->src_address, in->af, &out->fp_src_addr);
 }
 
 /*