diff --git a/vrrp_ip4.c b/vrrp_ip4.c index 9a454d6..50028d9 100644 --- a/vrrp_ip4.c +++ b/vrrp_ip4.c @@ -106,7 +106,7 @@ static int vrrp_ip4_mgroup(struct vrrp_net *vnet) * Return 0 if the list is the same, * the number of different VIP else */ -static int vrrp_ip4_cmp(struct vrrp_net *vnet, struct vrrphdr *vrrpkt) +static int vrrp_ip4_viplist_cmp(struct vrrp_net *vnet, struct vrrphdr *vrrpkt) { /* compare IP address(es) */ uint32_t *vip_addr = @@ -222,6 +222,30 @@ static uint16_t vrrp_ip4_chksum(const struct vrrp_net *vnet, return 0; } +/** + * vrrp_ip4_ntop() - network to string representation + */ +static const char *vrrp_ip4_ntop(union vrrp_ipx_addr *ipx, char *dst) +{ + return inet_ntop(AF_INET, &ipx->addr, dst, INET_ADDRSTRLEN); +} + +/** + * vrrp_ip4_pton() - string representation to network + */ +static int vrrp_ip4_pton(union vrrp_ipx_addr *dst, const char *src) +{ + return inet_pton(AF_INET, src, &dst->addr); +} + + +/** + * vrrp_ip4_cmp() - compare two vipx + */ +int vrrp_ip4_cmp(union vrrp_ipx_addr *s1, union vrrp_ipx_addr *s2) +{ + return ntohl(s1->addr.s_addr) - ntohl(s2->addr.s_addr); +} /** * vrrp_ip4_setsockopt() - no need to setsockopt() in IPv4 @@ -238,8 +262,11 @@ struct vrrp_ipx VRRP_IP4 = { .family = AF_INET, .setsockopt = vrrp_ip4_setsockopt, .mgroup = vrrp_ip4_mgroup, - .cmp = vrrp_ip4_cmp, .recv = vrrp_ip4_recv, + .cmp = vrrp_ip4_cmp, .chksum = vrrp_ip4_chksum, + .viplist_cmp = vrrp_ip4_viplist_cmp, .getsize = vrrp_ip4_getsize, + .ipx_pton = vrrp_ip4_pton, + .ipx_ntop = vrrp_ip4_ntop, }; diff --git a/vrrp_ip6.c b/vrrp_ip6.c index 03de274..22c1dfd 100644 --- a/vrrp_ip6.c +++ b/vrrp_ip6.c @@ -133,7 +133,7 @@ static int vrrp_ip6_mgroup(struct vrrp_net *vnet) * Return 0 if the list is the same, * the number of differente VIP else */ -static int vrrp_ip6_cmp(struct vrrp_net *vnet, struct vrrphdr *vrrpkt) +static int vrrp_ip6_viplist_cmp(struct vrrp_net *vnet, struct vrrphdr *vrrpkt) { uint32_t *vip_addr = (uint32_t *) ((unsigned char *) vrrpkt + VRRP_PKTHDR_SIZE); @@ -312,13 +312,40 @@ uint16_t vrrp_ip6_chksum(const struct vrrp_net *vnet, struct vrrphdr *pkt, return cksum(buf, psh_size); } +/** + * vrrp_ip6_ntop() - network to string representation + */ +static const char *vrrp_ip6_ntop(union vrrp_ipx_addr *ipx, char *dst) +{ + return inet_ntop(AF_INET6, &ipx->addr6, dst, INET6_ADDRSTRLEN); +} + +/** + * vrrp_ip6_pton() - string representation to network + */ +static int vrrp_ip6_pton(union vrrp_ipx_addr *dst, const char *src) +{ + return inet_pton(AF_INET6, src, &dst->addr6); +} + +/** + * vrrp_ip6_cmp() - compare two vipx + */ +int vrrp_ip6_cmp(union vrrp_ipx_addr *s1, union vrrp_ipx_addr *s2) +{ + return memcmp(&s1->addr6, &s2->addr6, sizeof(struct in6_addr)); +} + /* exported VRRP_IP6 helper */ struct vrrp_ipx VRRP_IP6 = { .family = AF_INET6, .setsockopt = vrrp_ip6_setsockopt, .mgroup = vrrp_ip6_mgroup, - .cmp = vrrp_ip6_cmp, .recv = vrrp_ip6_recv, + .cmp = vrrp_ip6_cmp, .chksum = vrrp_ip6_chksum, .getsize = vrrp_ip6_getsize, + .viplist_cmp = vrrp_ip6_viplist_cmp, + .ipx_pton = vrrp_ip6_pton, + .ipx_ntop = vrrp_ip6_ntop }; diff --git a/vrrp_ipx.h b/vrrp_ipx.h index dac198c..dd59683 100644 --- a/vrrp_ipx.h +++ b/vrrp_ipx.h @@ -59,18 +59,24 @@ struct vrrp_ipx { int family; int (*setsockopt) (int, int); int (*mgroup) (struct vrrp_net *); - int (*cmp) (struct vrrp_net *, struct vrrphdr *); + int (*cmp) (union vrrp_ipx_addr *, union vrrp_ipx_addr *); int (*recv) (int, struct vrrp_recv *, unsigned char *, ssize_t, int *); + int (*getsize) (const struct vrrp_net *); + int (*viplist_cmp) (struct vrrp_net *, struct vrrphdr *); uint16_t(*chksum) (const struct vrrp_net *, struct vrrphdr *, union vrrp_ipx_addr *, union vrrp_ipx_addr *); - int (*getsize) (const struct vrrp_net *); + const char *(*ipx_ntop) (union vrrp_ipx_addr *, char *); + int (*ipx_pton) (union vrrp_ipx_addr *, const char *); }; #define set_sockopt ipx_helper->setsockopt #define join_mgroup ipx_helper->mgroup -#define vip_compare ipx_helper->cmp +#define vip_compare ipx_helper->viplist_cmp +#define ipx_cmp ipx_helper->cmp #define pkt_receive ipx_helper->recv #define adv_checksum ipx_helper->chksum #define adv_getsize ipx_helper->getsize +#define ipx_to_str ipx_helper->ipx_ntop +#define str_to_ipx ipx_helper->ipx_pton extern struct vrrp_ipx VRRP_IP4; /* IPv4 module */ extern struct vrrp_ipx VRRP_IP6; /* IPv6 module */