]> git.proxmox.com Git - mirror_frr.git/blobdiff - lib/ipaddr.h
Merge pull request #5793 from ton31337/fix/formatting_show_bgp_summary_failed
[mirror_frr.git] / lib / ipaddr.h
index 98c28008dc60a36ea6ea76cdf6244a84475ba08e..6bd614044c768d27d9979429b6ca39d2651e0712 100644 (file)
 
 #include <zebra.h>
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /*
  * Generic IP address - union of IPv4 and IPv6 address.
  */
@@ -37,7 +41,7 @@ enum ipaddr_type_t {
 struct ipaddr {
        enum ipaddr_type_t ipa_type;
        union {
-               u_char addr;
+               uint8_t addr;
                struct in_addr _v4_addr;
                struct in6_addr _v6_addr;
        } ip;
@@ -52,6 +56,9 @@ struct ipaddr {
 #define SET_IPADDR_V4(p)  (p)->ipa_type = IPADDR_V4
 #define SET_IPADDR_V6(p)  (p)->ipa_type = IPADDR_V6
 
+#define IPADDRSZ(p)                                                            \
+       (IS_IPADDR_V4((p)) ? sizeof(struct in_addr) : sizeof(struct in6_addr))
+
 static inline int str2ipaddr(const char *str, struct ipaddr *ip)
 {
        int ret;
@@ -74,7 +81,7 @@ static inline int str2ipaddr(const char *str, struct ipaddr *ip)
        return -1;
 }
 
-static inline char *ipaddr2str(struct ipaddr *ip, char *buf, int size)
+static inline char *ipaddr2str(const struct ipaddr *ip, char *buf, int size)
 {
        buf[0] = '\0';
        if (ip) {
@@ -85,4 +92,41 @@ static inline char *ipaddr2str(struct ipaddr *ip, char *buf, int size)
        }
        return buf;
 }
+
+/*
+ * Convert IPv4 address to IPv4-mapped IPv6 address which is of the
+ * form ::FFFF:<IPv4 address> (RFC 4291). This IPv6 address can then
+ * be used to represent the IPv4 address, wherever only an IPv6 address
+ * is required.
+ */
+static inline void ipv4_to_ipv4_mapped_ipv6(struct in6_addr *in6,
+                                           struct in_addr in)
+{
+       uint32_t addr_type = htonl(0xFFFF);
+
+       memset(in6, 0, sizeof(struct in6_addr));
+       memcpy((char *)in6 + 8, &addr_type, sizeof(addr_type));
+       memcpy((char *)in6 + 12, &in, sizeof(struct in_addr));
+}
+
+/*
+ * convert an ipv4 mapped ipv6 address back to ipv4 address
+ */
+static inline void ipv4_mapped_ipv6_to_ipv4(struct in6_addr *in6,
+                                           struct in_addr *in)
+{
+       memset(in, 0, sizeof(struct in_addr));
+       memcpy(in, (char *)in6 + 12, sizeof(struct in_addr));
+}
+
+static inline bool ipaddr_isset(struct ipaddr *ip)
+{
+       static struct ipaddr a = {};
+       return (0 == memcmp(&a, ip, sizeof(struct ipaddr)));
+}
+
+#ifdef __cplusplus
+}
+#endif
+
 #endif /* __IPADDR_H__ */