]> git.proxmox.com Git - mirror_frr.git/blob - qpb/qpb.h
Merge branch 'cmaster-next' into vtysh-grammar
[mirror_frr.git] / qpb / qpb.h
1 /*
2 * qpb.h
3 *
4 * @copyright Copyright (C) 2016 Sproute Networks, Inc.
5 *
6 * @author Avneesh Sachdev <avneesh@sproute.com>
7 *
8 * This file is part of Quagga.
9 *
10 * Quagga is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
13 * later version.
14 *
15 * Quagga is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Quagga; see the file COPYING. If not, write to the Free
22 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 * 02111-1307, USA.
24 */
25
26 /*
27 * Main public header file for the quagga protobuf library.
28 */
29
30 #ifndef _QPB_H
31 #define _QPB_H
32
33 #include "prefix.h"
34
35 #include "qpb/qpb.pb-c.h"
36
37 #include "qpb/qpb_allocator.h"
38
39 /*
40 * qpb__address_family__set
41 */
42 #define qpb_address_family_set qpb__address_family__set
43 static inline int
44 qpb__address_family__set (Qpb__AddressFamily *pb_family, u_char family)
45 {
46 switch (family) {
47 case AF_INET:
48 *pb_family = QPB__ADDRESS_FAMILY__IPV4;
49 return 1;
50
51 case AF_INET6:
52 *pb_family = QPB__ADDRESS_FAMILY__IPV6;
53 return 1;
54
55 default:
56 *pb_family = QPB__ADDRESS_FAMILY__UNKNOWN_AF;
57 }
58
59 return 0;
60 }
61
62 /*
63 * qpb__address_family__get
64 */
65 #define qpb_address_family_get qpb__address_family__get
66 static inline int
67 qpb__address_family__get (Qpb__AddressFamily pb_family, u_char *family)
68 {
69
70 switch (pb_family) {
71 case QPB__ADDRESS_FAMILY__IPV4:
72 *family = AF_INET;
73 return 1;
74
75 case QPB__ADDRESS_FAMILY__IPV6:
76 *family = AF_INET6;
77 return 1;
78
79 case QPB__ADDRESS_FAMILY__UNKNOWN_AF:
80 return 0;
81 default: /* protobuf "magic value" _QPB__ADDRESS_FAMILY_IS_INT_SIZE */
82 return 0;
83 }
84
85 return 0;
86 }
87
88 /*
89 * qpb__l3_prefix__create
90 */
91 #define qpb_l3_prefix_create qpb__l3_prefix__create
92 static inline Qpb__L3Prefix *
93 qpb__l3_prefix__create (qpb_allocator_t *allocator, struct prefix *p)
94 {
95 Qpb__L3Prefix *prefix;
96
97 prefix = QPB_ALLOC(allocator, typeof(*prefix));
98 if (!prefix) {
99 return NULL;
100 }
101 qpb__l3_prefix__init(prefix);
102 prefix->length = p->prefixlen;
103 prefix->bytes.len = (p->prefixlen + 7)/8;
104 prefix->bytes.data = qpb_alloc(allocator, prefix->bytes.len);
105 if (!prefix->bytes.data) {
106 return NULL;
107 }
108
109 memcpy(prefix->bytes.data, &p->u.prefix, prefix->bytes.len);
110
111 return prefix;
112 }
113
114 /*
115 * qpb__l3_prefix__get
116 */
117 #define qpb_l3_prefix_get qpb__l3_prefix__get
118 static inline int
119 qpb__l3_prefix__get (const Qpb__L3Prefix *pb_prefix, u_char family,
120 struct prefix *prefix)
121 {
122
123 switch (family)
124 {
125
126 case AF_INET:
127 memset(prefix, 0, sizeof(struct prefix_ipv4));
128 break;
129
130 case AF_INET6:
131 memset(prefix, 0, sizeof(struct prefix_ipv6));
132 break;
133
134 default:
135 memset(prefix, 0, sizeof(*prefix));
136 }
137
138 prefix->prefixlen = pb_prefix->length;
139 prefix->family = family;
140 memcpy(&prefix->u.prefix, pb_prefix->bytes.data, pb_prefix->bytes.len);
141 return 1;
142 }
143
144 /*
145 * qpb__protocol__set
146 *
147 * Translate a quagga route type to a protobuf protocol.
148 */
149 #define qpb_protocol_set qpb__protocol__set
150 static inline int
151 qpb__protocol__set (Qpb__Protocol *pb_proto, int route_type)
152 {
153 switch (route_type) {
154 case ZEBRA_ROUTE_KERNEL:
155 *pb_proto = QPB__PROTOCOL__KERNEL;
156 break;
157
158 case ZEBRA_ROUTE_CONNECT:
159 *pb_proto = QPB__PROTOCOL__CONNECTED;
160 break;
161
162 case ZEBRA_ROUTE_STATIC:
163 *pb_proto = QPB__PROTOCOL__STATIC;
164 break;
165
166 case ZEBRA_ROUTE_RIP:
167 *pb_proto = QPB__PROTOCOL__RIP;
168 break;
169
170 case ZEBRA_ROUTE_RIPNG:
171 *pb_proto = QPB__PROTOCOL__RIPNG;
172 break;
173
174 case ZEBRA_ROUTE_OSPF:
175 case ZEBRA_ROUTE_OSPF6:
176 *pb_proto = QPB__PROTOCOL__OSPF;
177 break;
178
179 case ZEBRA_ROUTE_ISIS:
180 *pb_proto = QPB__PROTOCOL__ISIS;
181 break;
182
183 case ZEBRA_ROUTE_BGP:
184 *pb_proto = QPB__PROTOCOL__BGP;
185 break;
186
187 case ZEBRA_ROUTE_HSLS:
188 case ZEBRA_ROUTE_OLSR:
189 case ZEBRA_ROUTE_MAX:
190 case ZEBRA_ROUTE_SYSTEM:
191 default:
192 *pb_proto = QPB__PROTOCOL__OTHER;
193 }
194
195 return 1;
196 }
197
198 /*
199 * qpb__ipv4_address__create
200 */
201 static inline Qpb__Ipv4Address *
202 qpb__ipv4_address__create (qpb_allocator_t *allocator,
203 struct in_addr *addr)
204 {
205 Qpb__Ipv4Address *v4;
206
207 v4 = QPB_ALLOC(allocator, typeof(*v4));
208 if (!v4) {
209 return NULL;
210 }
211 qpb__ipv4_address__init(v4);
212
213 v4->value = ntohl(addr->s_addr);
214 return v4;
215 }
216
217 /*
218 * qpb__ipv4_address__get
219 */
220 static inline int
221 qpb__ipv4_address__get (const Qpb__Ipv4Address *v4, struct in_addr *addr)
222 {
223 addr->s_addr = htonl(v4->value);
224 return 1;
225 }
226
227 /*
228 * qpb__ipv6_address__create
229 */
230 static inline Qpb__Ipv6Address *
231 qpb__ipv6_address__create (qpb_allocator_t *allocator, struct in6_addr *addr)
232 {
233 Qpb__Ipv6Address *v6;
234
235 v6 = QPB_ALLOC(allocator, typeof(*v6));
236 if (!v6)
237 return NULL;
238
239 qpb__ipv6_address__init(v6);
240 v6->bytes.len = 16;
241 v6->bytes.data = qpb_alloc(allocator, 16);
242 if (!v6->bytes.data)
243 return NULL;
244
245 memcpy(v6->bytes.data, addr->s6_addr, v6->bytes.len);
246 return v6;
247 }
248
249 /*
250 * qpb__ipv6_address__get
251 *
252 * Read out information from a protobuf ipv6 address structure.
253 */
254 static inline int
255 qpb__ipv6_address__get (const Qpb__Ipv6Address *v6, struct in6_addr *addr)
256 {
257 if (v6->bytes.len != 16)
258 return 0;
259
260 memcpy(addr->s6_addr, v6->bytes.data, v6->bytes.len);
261 return 1;
262 }
263
264 /*
265 * qpb__l3_address__create
266 */
267 #define qpb_l3_address_create qpb__l3_address__create
268 static inline Qpb__L3Address *
269 qpb__l3_address__create (qpb_allocator_t *allocator, union g_addr *addr,
270 u_char family)
271 {
272 Qpb__L3Address *l3_addr;
273
274 l3_addr = QPB_ALLOC(allocator, typeof(*l3_addr));
275 if (!l3_addr)
276 return NULL;
277
278 qpb__l3_address__init(l3_addr);
279
280 switch (family) {
281
282 case AF_INET:
283 l3_addr->v4 = qpb__ipv4_address__create (allocator, &addr->ipv4);
284 if (!l3_addr->v4)
285 return NULL;
286
287 break;
288
289 case AF_INET6:
290 l3_addr->v6 = qpb__ipv6_address__create (allocator, &addr->ipv6);
291 if (!l3_addr->v6)
292 return NULL;
293
294 break;
295 }
296 return l3_addr;
297 }
298
299 /*
300 * qpb__l3_address__get
301 *
302 * Read out a gateway address from a protobuf l3 address.
303 */
304 #define qpb_l3_address_get qpb__l3_address__get
305 static inline int
306 qpb__l3_address__get (const Qpb__L3Address *l3_addr,
307 u_char *family, union g_addr *addr)
308 {
309 if (l3_addr->v4)
310 {
311 qpb__ipv4_address__get (l3_addr->v4, &addr->ipv4);
312 *family = AF_INET;
313 return 1;
314 }
315
316 if (l3_addr->v6)
317 {
318 qpb__ipv6_address__get(l3_addr->v6, &addr->ipv6);
319 *family = AF_INET6;
320 return 1;
321 }
322
323 return 0;
324 }
325
326 /*
327 * qpb__if_identifier__create
328 */
329 #define qpb_if_identifier_create qpb__if_identifier__create
330 static inline Qpb__IfIdentifier *
331 qpb__if_identifier__create (qpb_allocator_t *allocator, uint if_index)
332 {
333 Qpb__IfIdentifier *if_id;
334
335 if_id = QPB_ALLOC(allocator, typeof(*if_id));
336 if (!if_id) {
337 return NULL;
338 }
339 qpb__if_identifier__init(if_id);
340 if_id->has_index = 1;
341 if_id->index = if_index;
342 return if_id;
343 }
344
345 /*
346 * qpb__if_identifier__get
347 *
348 * Get interface name and/or if_index from an if identifier.
349 */
350 #define qpb_if_identifier_get qpb__if_identifier__get
351 static inline int
352 qpb__if_identifier__get (Qpb__IfIdentifier *if_id, uint *if_index,
353 char **name)
354 {
355 char *str;
356 uint ix;
357
358 if (!if_index)
359 if_index = &ix;
360
361 if (!name)
362 name = &str;
363
364 if (if_id->has_index)
365 *if_index = if_id->index;
366 else
367 *if_index = 0;
368
369 *name = if_id->name;
370 return 1;
371 }
372
373 #endif