summaryrefslogtreecommitdiffstats
path: root/bgpd/rfapi/rfapi_nve_addr.c
blob: e00ff30312597b06cc6920e477224f0550b7e3de (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* 
 *
 * Copyright 2009-2016, LabN Consulting, L.L.C.
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */


#include "lib/zebra.h"
#include "lib/prefix.h"
#include "lib/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 (void *k1, void *k2)
{
  struct rfapi_nve_addr *a = (struct rfapi_nve_addr *) k1;
  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;
}