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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/*
* qpb.h
*
* @copyright Copyright (C) 2016 Sproute Networks, Inc.
*
* @author Avneesh Sachdev <avneesh@sproute.com>
*
* This file is part of Quagga.
*
* Quagga 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, or (at your option) any
* later version.
*
* Quagga 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; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Main public header file for the quagga protobuf library.
*/
#ifndef _QPB_H
#define _QPB_H
#include "prefix.h"
#include "qpb/qpb.pb-c.h"
#include "qpb/qpb_allocator.h"
/*
* qpb__address_family__set
*/
#define qpb_address_family_set qpb__address_family__set
static inline int qpb__address_family__set(Qpb__AddressFamily *pb_family,
uint8_t family)
{
switch (family) {
case AF_INET:
*pb_family = QPB__ADDRESS_FAMILY__IPV4;
return 1;
case AF_INET6:
*pb_family = QPB__ADDRESS_FAMILY__IPV6;
return 1;
default:
*pb_family = QPB__ADDRESS_FAMILY__UNKNOWN_AF;
}
return 0;
}
/*
* qpb__address_family__get
*/
#define qpb_address_family_get qpb__address_family__get
static inline int qpb__address_family__get(Qpb__AddressFamily pb_family,
uint8_t *family)
{
switch (pb_family) {
case QPB__ADDRESS_FAMILY__IPV4:
*family = AF_INET;
return 1;
case QPB__ADDRESS_FAMILY__IPV6:
*family = AF_INET6;
return 1;
case QPB__ADDRESS_FAMILY__UNKNOWN_AF:
return 0;
default: /* protobuf "magic value" _QPB__ADDRESS_FAMILY_IS_INT_SIZE */
return 0;
}
return 0;
}
/*
* qpb__l3_prefix__create
*/
#define qpb_l3_prefix_create qpb__l3_prefix__create
static inline Qpb__L3Prefix *qpb__l3_prefix__create(qpb_allocator_t *allocator,
struct prefix *p)
{
Qpb__L3Prefix *prefix;
prefix = QPB_ALLOC(allocator, typeof(*prefix));
if (!prefix) {
return NULL;
}
qpb__l3_prefix__init(prefix);
prefix->length = p->prefixlen;
prefix->bytes.len = (p->prefixlen + 7) / 8;
prefix->bytes.data = qpb_alloc(allocator, prefix->bytes.len);
if (!prefix->bytes.data) {
return NULL;
}
memcpy(prefix->bytes.data, &p->u.prefix, prefix->bytes.len);
return prefix;
}
/*
* qpb__l3_prefix__get
*/
#define qpb_l3_prefix_get qpb__l3_prefix__get
static inline int qpb__l3_prefix__get(const Qpb__L3Prefix *pb_prefix,
uint8_t family, struct prefix *prefix)
{
switch (family) {
case AF_INET:
memset((struct prefix_ipv4 *)prefix, 0,
sizeof(struct prefix_ipv4));
break;
case AF_INET6:
memset((struct prefix_ipv6 *)prefix, 0,
sizeof(struct prefix_ipv6));
break;
default:
memset(prefix, 0, sizeof(*prefix));
}
prefix->prefixlen = pb_prefix->length;
prefix->family = family;
memcpy(&prefix->u.prefix, pb_prefix->bytes.data, pb_prefix->bytes.len);
return 1;
}
/*
* qpb__protocol__set
*
* Translate a quagga route type to a protobuf protocol.
*/
#define qpb_protocol_set qpb__protocol__set
static inline int qpb__protocol__set(Qpb__Protocol *pb_proto, int route_type)
{
switch (route_type) {
case ZEBRA_ROUTE_KERNEL:
*pb_proto = QPB__PROTOCOL__KERNEL;
break;
case ZEBRA_ROUTE_CONNECT:
*pb_proto = QPB__PROTOCOL__CONNECTED;
break;
case ZEBRA_ROUTE_STATIC:
*pb_proto = QPB__PROTOCOL__STATIC;
break;
case ZEBRA_ROUTE_RIP:
*pb_proto = QPB__PROTOCOL__RIP;
break;
case ZEBRA_ROUTE_RIPNG:
*pb_proto = QPB__PROTOCOL__RIPNG;
break;
case ZEBRA_ROUTE_OSPF:
case ZEBRA_ROUTE_OSPF6:
*pb_proto = QPB__PROTOCOL__OSPF;
break;
case ZEBRA_ROUTE_ISIS:
*pb_proto = QPB__PROTOCOL__ISIS;
break;
case ZEBRA_ROUTE_BGP:
*pb_proto = QPB__PROTOCOL__BGP;
break;
case ZEBRA_ROUTE_HSLS:
case ZEBRA_ROUTE_OLSR:
case ZEBRA_ROUTE_MAX:
case ZEBRA_ROUTE_SYSTEM:
default:
*pb_proto = QPB__PROTOCOL__OTHER;
}
return 1;
}
/*
* qpb__ipv4_address__create
*/
static inline Qpb__Ipv4Address *
qpb__ipv4_address__create(qpb_allocator_t *allocator, struct in_addr *addr)
{
Qpb__Ipv4Address *v4;
v4 = QPB_ALLOC(allocator, typeof(*v4));
if (!v4) {
return NULL;
}
qpb__ipv4_address__init(v4);
v4->value = ntohl(addr->s_addr);
return v4;
}
/*
* qpb__ipv4_address__get
*/
static inline int qpb__ipv4_address__get(const Qpb__Ipv4Address *v4,
struct in_addr *addr)
{
addr->s_addr = htonl(v4->value);
return 1;
}
/*
* qpb__ipv6_address__create
*/
static inline Qpb__Ipv6Address *
qpb__ipv6_address__create(qpb_allocator_t *allocator, struct in6_addr *addr)
{
Qpb__Ipv6Address *v6;
v6 = QPB_ALLOC(allocator, typeof(*v6));
if (!v6)
return NULL;
qpb__ipv6_address__init(v6);
v6->bytes.len = 16;
v6->bytes.data = qpb_alloc(allocator, 16);
if (!v6->bytes.data)
return NULL;
memcpy(v6->bytes.data, addr->s6_addr, v6->bytes.len);
return v6;
}
/*
* qpb__ipv6_address__get
*
* Read out information from a protobuf ipv6 address structure.
*/
static inline int qpb__ipv6_address__get(const Qpb__Ipv6Address *v6,
struct in6_addr *addr)
{
if (v6->bytes.len != 16)
return 0;
memcpy(addr->s6_addr, v6->bytes.data, v6->bytes.len);
return 1;
}
/*
* qpb__l3_address__create
*/
#define qpb_l3_address_create qpb__l3_address__create
static inline Qpb__L3Address *
qpb__l3_address__create(qpb_allocator_t *allocator, union g_addr *addr,
uint8_t family)
{
Qpb__L3Address *l3_addr;
l3_addr = QPB_ALLOC(allocator, typeof(*l3_addr));
if (!l3_addr)
return NULL;
qpb__l3_address__init(l3_addr);
switch (family) {
case AF_INET:
l3_addr->v4 = qpb__ipv4_address__create(allocator, &addr->ipv4);
if (!l3_addr->v4)
return NULL;
break;
case AF_INET6:
l3_addr->v6 = qpb__ipv6_address__create(allocator, &addr->ipv6);
if (!l3_addr->v6)
return NULL;
break;
}
return l3_addr;
}
/*
* qpb__l3_address__get
*
* Read out a gateway address from a protobuf l3 address.
*/
#define qpb_l3_address_get qpb__l3_address__get
static inline int qpb__l3_address__get(const Qpb__L3Address *l3_addr,
uint8_t *family, union g_addr *addr)
{
if (l3_addr->v4) {
qpb__ipv4_address__get(l3_addr->v4, &addr->ipv4);
*family = AF_INET;
return 1;
}
if (l3_addr->v6) {
qpb__ipv6_address__get(l3_addr->v6, &addr->ipv6);
*family = AF_INET6;
return 1;
}
return 0;
}
/*
* qpb__if_identifier__create
*/
#define qpb_if_identifier_create qpb__if_identifier__create
static inline Qpb__IfIdentifier *
qpb__if_identifier__create(qpb_allocator_t *allocator, uint if_index)
{
Qpb__IfIdentifier *if_id;
if_id = QPB_ALLOC(allocator, typeof(*if_id));
if (!if_id) {
return NULL;
}
qpb__if_identifier__init(if_id);
if_id->has_index = 1;
if_id->index = if_index;
return if_id;
}
/*
* qpb__if_identifier__get
*
* Get interface name and/or if_index from an if identifier.
*/
#define qpb_if_identifier_get qpb__if_identifier__get
static inline int qpb__if_identifier__get(Qpb__IfIdentifier *if_id,
uint *if_index, char **name)
{
char *str;
uint ix;
if (!if_index)
if_index = &ix;
if (!name)
name = &str;
if (if_id->has_index)
*if_index = if_id->index;
else
*if_index = 0;
*name = if_id->name;
return 1;
}
#endif
|