4 * @copyright Copyright (C) 2016 Sproute Networks, Inc.
6 * @author Avneesh Sachdev <avneesh@sproute.com>
8 * This file is part of Quagga.
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
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.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; see the file COPYING; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 * Main public header file for the quagga protobuf library.
34 #include "qpb/qpb.pb-c.h"
36 #include "qpb/qpb_allocator.h"
39 * qpb__address_family__set
41 #define qpb_address_family_set qpb__address_family__set
42 static inline int qpb__address_family__set(Qpb__AddressFamily
*pb_family
,
47 *pb_family
= QPB__ADDRESS_FAMILY__IPV4
;
51 *pb_family
= QPB__ADDRESS_FAMILY__IPV6
;
55 *pb_family
= QPB__ADDRESS_FAMILY__UNKNOWN_AF
;
62 * qpb__address_family__get
64 #define qpb_address_family_get qpb__address_family__get
65 static inline int qpb__address_family__get(Qpb__AddressFamily pb_family
,
70 case QPB__ADDRESS_FAMILY__IPV4
:
74 case QPB__ADDRESS_FAMILY__IPV6
:
78 case QPB__ADDRESS_FAMILY__UNKNOWN_AF
:
80 default: /* protobuf "magic value" _QPB__ADDRESS_FAMILY_IS_INT_SIZE */
88 * qpb__l3_prefix__create
90 #define qpb_l3_prefix_create qpb__l3_prefix__create
91 static inline Qpb__L3Prefix
*qpb__l3_prefix__create(qpb_allocator_t
*allocator
,
94 Qpb__L3Prefix
*prefix
;
96 prefix
= QPB_ALLOC(allocator
, typeof(*prefix
));
100 qpb__l3_prefix__init(prefix
);
101 prefix
->length
= p
->prefixlen
;
102 prefix
->bytes
.len
= (p
->prefixlen
+ 7) / 8;
103 prefix
->bytes
.data
= qpb_alloc(allocator
, prefix
->bytes
.len
);
104 if (!prefix
->bytes
.data
) {
108 memcpy(prefix
->bytes
.data
, &p
->u
.prefix
, prefix
->bytes
.len
);
114 * qpb__l3_prefix__get
116 #define qpb_l3_prefix_get qpb__l3_prefix__get
117 static inline int qpb__l3_prefix__get(const Qpb__L3Prefix
*pb_prefix
,
118 uint8_t family
, struct prefix
*prefix
)
124 memset((struct prefix_ipv4
*)prefix
, 0,
125 sizeof(struct prefix_ipv4
));
129 memset((struct prefix_ipv6
*)prefix
, 0,
130 sizeof(struct prefix_ipv6
));
134 memset(prefix
, 0, sizeof(*prefix
));
137 prefix
->prefixlen
= pb_prefix
->length
;
138 prefix
->family
= family
;
139 memcpy(&prefix
->u
.prefix
, pb_prefix
->bytes
.data
, pb_prefix
->bytes
.len
);
146 * Translate a quagga route type to a protobuf protocol.
148 #define qpb_protocol_set qpb__protocol__set
149 static inline int qpb__protocol__set(Qpb__Protocol
*pb_proto
, int route_type
)
151 switch (route_type
) {
152 case ZEBRA_ROUTE_KERNEL
:
153 *pb_proto
= QPB__PROTOCOL__KERNEL
;
156 case ZEBRA_ROUTE_CONNECT
:
157 *pb_proto
= QPB__PROTOCOL__CONNECTED
;
160 case ZEBRA_ROUTE_STATIC
:
161 *pb_proto
= QPB__PROTOCOL__STATIC
;
164 case ZEBRA_ROUTE_RIP
:
165 *pb_proto
= QPB__PROTOCOL__RIP
;
168 case ZEBRA_ROUTE_RIPNG
:
169 *pb_proto
= QPB__PROTOCOL__RIPNG
;
172 case ZEBRA_ROUTE_OSPF
:
173 case ZEBRA_ROUTE_OSPF6
:
174 *pb_proto
= QPB__PROTOCOL__OSPF
;
177 case ZEBRA_ROUTE_ISIS
:
178 *pb_proto
= QPB__PROTOCOL__ISIS
;
181 case ZEBRA_ROUTE_BGP
:
182 *pb_proto
= QPB__PROTOCOL__BGP
;
185 case ZEBRA_ROUTE_HSLS
:
186 case ZEBRA_ROUTE_OLSR
:
187 case ZEBRA_ROUTE_MAX
:
188 case ZEBRA_ROUTE_SYSTEM
:
190 *pb_proto
= QPB__PROTOCOL__OTHER
;
197 * qpb__ipv4_address__create
199 static inline Qpb__Ipv4Address
*
200 qpb__ipv4_address__create(qpb_allocator_t
*allocator
, struct in_addr
*addr
)
202 Qpb__Ipv4Address
*v4
;
204 v4
= QPB_ALLOC(allocator
, typeof(*v4
));
208 qpb__ipv4_address__init(v4
);
210 v4
->value
= ntohl(addr
->s_addr
);
215 * qpb__ipv4_address__get
217 static inline int qpb__ipv4_address__get(const Qpb__Ipv4Address
*v4
,
218 struct in_addr
*addr
)
220 addr
->s_addr
= htonl(v4
->value
);
225 * qpb__ipv6_address__create
227 static inline Qpb__Ipv6Address
*
228 qpb__ipv6_address__create(qpb_allocator_t
*allocator
, struct in6_addr
*addr
)
230 Qpb__Ipv6Address
*v6
;
232 v6
= QPB_ALLOC(allocator
, typeof(*v6
));
236 qpb__ipv6_address__init(v6
);
238 v6
->bytes
.data
= qpb_alloc(allocator
, 16);
242 memcpy(v6
->bytes
.data
, addr
->s6_addr
, v6
->bytes
.len
);
247 * qpb__ipv6_address__get
249 * Read out information from a protobuf ipv6 address structure.
251 static inline int qpb__ipv6_address__get(const Qpb__Ipv6Address
*v6
,
252 struct in6_addr
*addr
)
254 if (v6
->bytes
.len
!= 16)
257 memcpy(addr
->s6_addr
, v6
->bytes
.data
, v6
->bytes
.len
);
262 * qpb__l3_address__create
264 #define qpb_l3_address_create qpb__l3_address__create
265 static inline Qpb__L3Address
*
266 qpb__l3_address__create(qpb_allocator_t
*allocator
, union g_addr
*addr
,
269 Qpb__L3Address
*l3_addr
;
271 l3_addr
= QPB_ALLOC(allocator
, typeof(*l3_addr
));
275 qpb__l3_address__init(l3_addr
);
280 l3_addr
->v4
= qpb__ipv4_address__create(allocator
, &addr
->ipv4
);
287 l3_addr
->v6
= qpb__ipv6_address__create(allocator
, &addr
->ipv6
);
297 * qpb__l3_address__get
299 * Read out a gateway address from a protobuf l3 address.
301 #define qpb_l3_address_get qpb__l3_address__get
302 static inline int qpb__l3_address__get(const Qpb__L3Address
*l3_addr
,
303 uint8_t *family
, union g_addr
*addr
)
306 qpb__ipv4_address__get(l3_addr
->v4
, &addr
->ipv4
);
312 qpb__ipv6_address__get(l3_addr
->v6
, &addr
->ipv6
);
321 * qpb__if_identifier__create
323 #define qpb_if_identifier_create qpb__if_identifier__create
324 static inline Qpb__IfIdentifier
*
325 qpb__if_identifier__create(qpb_allocator_t
*allocator
, uint if_index
)
327 Qpb__IfIdentifier
*if_id
;
329 if_id
= QPB_ALLOC(allocator
, typeof(*if_id
));
333 qpb__if_identifier__init(if_id
);
334 if_id
->has_index
= 1;
335 if_id
->index
= if_index
;
340 * qpb__if_identifier__get
342 * Get interface name and/or if_index from an if identifier.
344 #define qpb_if_identifier_get qpb__if_identifier__get
345 static inline int qpb__if_identifier__get(Qpb__IfIdentifier
*if_id
,
346 uint
*if_index
, char **name
)
357 if (if_id
->has_index
)
358 *if_index
= if_id
->index
;