summaryrefslogtreecommitdiffstats
path: root/bgpd/rfapi/rfapi_nve_addr.c
blob: eabec2fdde3b667e1ed03250c4a187bc42786134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *
 * Copyright 2009-2016, LabN Consulting, L.L.C.
 *
 */


#include "lib/zebra.h"
#include "lib/prefix.h"
#include "lib/agg_table.h"
#include "lib/vty.h"
#include "lib/memory.h"
#include "lib/skiplist.h"


#include "bgpd/bgpd.h"

#include "bgpd/rfapi/bgp_rfapi_cfg.h"
#include "bgpd/rfapi/rfapi.h"
#include "bgpd/rfapi/rfapi_backend.h"

#include "bgpd/rfapi/rfapi_import.h"
#include "bgpd/rfapi/rfapi_private.h"
#include "bgpd/rfapi/rfapi_nve_addr.h"
#include "bgpd/rfapi/rfapi_vty.h"
#include "bgpd/rfapi/vnc_debug.h"

#define DEBUG_NVE_ADDR 0

void rfapiNveAddr2Str(struct rfapi_nve_addr *, char *, int);


#if DEBUG_NVE_ADDR
static void logdifferent(const char *tag, struct rfapi_nve_addr *a,
			 struct rfapi_nve_addr *b)
{
	char a_str[BUFSIZ];
	char b_str[BUFSIZ];

	rfapiNveAddr2Str(a, a_str, BUFSIZ);
	rfapiNveAddr2Str(b, b_str, BUFSIZ);
	vnc_zlog_debug_verbose("%s: [%s] [%s]", tag, a_str, b_str);
}
#endif


int rfapi_nve_addr_cmp(const void *k1, const void *k2)
{
	const struct rfapi_nve_addr *a = (struct rfapi_nve_addr *)k1;
	const struct rfapi_nve_addr *b = (struct rfapi_nve_addr *)k2;
	int ret = 0;

	if (!a || !b) {
#if DEBUG_NVE_ADDR
		vnc_zlog_debug_verbose("%s: missing address a=%p b=%p",
				       __func__, a, b);
#endif
		return (a - b);
	}
	if (a->un.addr_family != b->un.addr_family) {
#if DEBUG_NVE_ADDR
		vnc_zlog_debug_verbose(
			"diff: UN addr fam a->un.af=%d, b->un.af=%d",
			a->un.addr_family, b->un.addr_family);
#endif
		return (a->un.addr_family - b->un.addr_family);
	}
	if (a->un.addr_family == AF_INET) {
		ret = IPV4_ADDR_CMP(&a->un.addr.v4, &b->un.addr.v4);
		if (ret != 0) {
#if DEBUG_NVE_ADDR
			logdifferent("diff: UN addr", a, b);
#endif
			return ret;
		}
	} else if (a->un.addr_family == AF_INET6) {
		ret = IPV6_ADDR_CMP(&a->un.addr.v6, &b->un.addr.v6);
		if (ret == 0) {
#if DEBUG_NVE_ADDR
			logdifferent("diff: UN addr", a, b);
#endif
			return ret;
		}
	} else {
		assert(0);
	}
	if (a->vn.addr_family != b->vn.addr_family) {
#if DEBUG_NVE_ADDR
		vnc_zlog_debug_verbose(
			"diff: pT addr fam a->vn.af=%d, b->vn.af=%d",
			a->vn.addr_family, b->vn.addr_family);
#endif
		return (a->vn.addr_family - b->vn.addr_family);
	}
	if (a->vn.addr_family == AF_INET) {
		ret = IPV4_ADDR_CMP(&a->vn.addr.v4, &b->vn.addr.v4);
		if (ret != 0) {
#if DEBUG_NVE_ADDR
			logdifferent("diff: VN addr", a, b);
#endif
			return ret;
		}
	} else if (a->vn.addr_family == AF_INET6) {
		ret = IPV6_ADDR_CMP(&a->vn.addr.v6, &b->vn.addr.v6);
		if (ret == 0) {
#if DEBUG_NVE_ADDR
			logdifferent("diff: VN addr", a, b);
#endif
			return ret;
		}
	} else {
		assert(0);
	}
	return 0;
}

void rfapiNveAddr2Str(struct rfapi_nve_addr *na, char *buf, int bufsize)
{
	char *p = buf;
	int r;

#define REMAIN (bufsize - (p-buf))
#define INCP {p += (r > REMAIN)? REMAIN: r;}

	if (bufsize < 1)
		return;

	r = snprintf(p, REMAIN, "VN=");
	INCP;

	if (!rfapiRfapiIpAddr2Str(&na->vn, p, REMAIN))
		goto done;

	buf[bufsize - 1] = 0;
	p = buf + strlen(buf);

	r = snprintf(p, REMAIN, ", UN=");
	INCP;

	rfapiRfapiIpAddr2Str(&na->un, p, REMAIN);

done:
	buf[bufsize - 1] = 0;
}