1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * @copyright Copyright (C) 2016 Sproute Networks, Inc.
7 * @author Avneesh Sachdev <avneesh@sproute.com>
11 * Main public header file for the quagga protobuf library.
19 #include "qpb/qpb.pb-c.h"
21 #include "qpb/qpb_allocator.h"
24 * qpb__address_family__set
26 #define qpb_address_family_set qpb__address_family__set
27 static inline int qpb__address_family__set(Qpb__AddressFamily
*pb_family
,
32 *pb_family
= QPB__ADDRESS_FAMILY__IPV4
;
36 *pb_family
= QPB__ADDRESS_FAMILY__IPV6
;
40 *pb_family
= QPB__ADDRESS_FAMILY__UNKNOWN_AF
;
47 * qpb__address_family__get
49 #define qpb_address_family_get qpb__address_family__get
50 static inline int qpb__address_family__get(Qpb__AddressFamily pb_family
,
55 case QPB__ADDRESS_FAMILY__IPV4
:
59 case QPB__ADDRESS_FAMILY__IPV6
:
63 case QPB__ADDRESS_FAMILY__UNKNOWN_AF
:
65 case _QPB__ADDRESS_FAMILY_IS_INT_SIZE
:
73 * qpb__l3_prefix__create
75 #define qpb_l3_prefix_create qpb__l3_prefix__create
76 static inline Qpb__L3Prefix
*qpb__l3_prefix__create(qpb_allocator_t
*allocator
,
79 Qpb__L3Prefix
*prefix
;
81 prefix
= QPB_ALLOC(allocator
, typeof(*prefix
));
85 qpb__l3_prefix__init(prefix
);
86 prefix
->length
= p
->prefixlen
;
87 prefix
->bytes
.len
= (p
->prefixlen
+ 7) / 8;
88 prefix
->bytes
.data
= qpb_alloc(allocator
, prefix
->bytes
.len
);
89 if (!prefix
->bytes
.data
) {
93 memcpy(prefix
->bytes
.data
, &p
->u
.prefix
, prefix
->bytes
.len
);
101 #define qpb_l3_prefix_get qpb__l3_prefix__get
102 static inline int qpb__l3_prefix__get(const Qpb__L3Prefix
*pb_prefix
,
103 uint8_t family
, struct prefix
*prefix
)
109 memset((struct prefix_ipv4
*)prefix
, 0,
110 sizeof(struct prefix_ipv4
));
114 memset((struct prefix_ipv6
*)prefix
, 0,
115 sizeof(struct prefix_ipv6
));
119 memset(prefix
, 0, sizeof(*prefix
));
122 prefix
->prefixlen
= pb_prefix
->length
;
123 prefix
->family
= family
;
124 memcpy(&prefix
->u
.prefix
, pb_prefix
->bytes
.data
, pb_prefix
->bytes
.len
);
131 * Translate a quagga route type to a protobuf protocol.
133 #define qpb_protocol_set qpb__protocol__set
134 static inline int qpb__protocol__set(Qpb__Protocol
*pb_proto
, int route_type
)
136 switch (route_type
) {
137 case ZEBRA_ROUTE_KERNEL
:
138 *pb_proto
= QPB__PROTOCOL__KERNEL
;
141 case ZEBRA_ROUTE_CONNECT
:
142 *pb_proto
= QPB__PROTOCOL__CONNECTED
;
145 case ZEBRA_ROUTE_STATIC
:
146 *pb_proto
= QPB__PROTOCOL__STATIC
;
149 case ZEBRA_ROUTE_RIP
:
150 *pb_proto
= QPB__PROTOCOL__RIP
;
153 case ZEBRA_ROUTE_RIPNG
:
154 *pb_proto
= QPB__PROTOCOL__RIPNG
;
157 case ZEBRA_ROUTE_OSPF
:
158 case ZEBRA_ROUTE_OSPF6
:
159 *pb_proto
= QPB__PROTOCOL__OSPF
;
162 case ZEBRA_ROUTE_ISIS
:
163 *pb_proto
= QPB__PROTOCOL__ISIS
;
166 case ZEBRA_ROUTE_BGP
:
167 *pb_proto
= QPB__PROTOCOL__BGP
;
170 case ZEBRA_ROUTE_HSLS
:
171 case ZEBRA_ROUTE_OLSR
:
172 case ZEBRA_ROUTE_MAX
:
173 case ZEBRA_ROUTE_SYSTEM
:
175 *pb_proto
= QPB__PROTOCOL__OTHER
;
182 * qpb__ipv4_address__create
184 static inline Qpb__Ipv4Address
*
185 qpb__ipv4_address__create(qpb_allocator_t
*allocator
, struct in_addr
*addr
)
187 Qpb__Ipv4Address
*v4
;
189 v4
= QPB_ALLOC(allocator
, typeof(*v4
));
193 qpb__ipv4_address__init(v4
);
195 v4
->value
= ntohl(addr
->s_addr
);
200 * qpb__ipv4_address__get
202 static inline int qpb__ipv4_address__get(const Qpb__Ipv4Address
*v4
,
203 struct in_addr
*addr
)
205 addr
->s_addr
= htonl(v4
->value
);
210 * qpb__ipv6_address__create
212 static inline Qpb__Ipv6Address
*
213 qpb__ipv6_address__create(qpb_allocator_t
*allocator
, struct in6_addr
*addr
)
215 Qpb__Ipv6Address
*v6
;
217 v6
= QPB_ALLOC(allocator
, typeof(*v6
));
221 qpb__ipv6_address__init(v6
);
223 v6
->bytes
.data
= qpb_alloc(allocator
, 16);
227 memcpy(v6
->bytes
.data
, addr
->s6_addr
, v6
->bytes
.len
);
232 * qpb__ipv6_address__get
234 * Read out information from a protobuf ipv6 address structure.
236 static inline int qpb__ipv6_address__get(const Qpb__Ipv6Address
*v6
,
237 struct in6_addr
*addr
)
239 if (v6
->bytes
.len
!= 16)
242 memcpy(addr
->s6_addr
, v6
->bytes
.data
, v6
->bytes
.len
);
247 * qpb__l3_address__create
249 #define qpb_l3_address_create qpb__l3_address__create
250 static inline Qpb__L3Address
*
251 qpb__l3_address__create(qpb_allocator_t
*allocator
, union g_addr
*addr
,
254 Qpb__L3Address
*l3_addr
;
256 l3_addr
= QPB_ALLOC(allocator
, typeof(*l3_addr
));
260 qpb__l3_address__init(l3_addr
);
265 l3_addr
->v4
= qpb__ipv4_address__create(allocator
, &addr
->ipv4
);
272 l3_addr
->v6
= qpb__ipv6_address__create(allocator
, &addr
->ipv6
);
282 * qpb__l3_address__get
284 * Read out a gateway address from a protobuf l3 address.
286 #define qpb_l3_address_get qpb__l3_address__get
287 static inline int qpb__l3_address__get(const Qpb__L3Address
*l3_addr
,
288 uint8_t *family
, union g_addr
*addr
)
291 qpb__ipv4_address__get(l3_addr
->v4
, &addr
->ipv4
);
297 qpb__ipv6_address__get(l3_addr
->v6
, &addr
->ipv6
);
306 * qpb__if_identifier__create
308 #define qpb_if_identifier_create qpb__if_identifier__create
309 static inline Qpb__IfIdentifier
*
310 qpb__if_identifier__create(qpb_allocator_t
*allocator
, uint if_index
)
312 Qpb__IfIdentifier
*if_id
;
314 if_id
= QPB_ALLOC(allocator
, typeof(*if_id
));
318 qpb__if_identifier__init(if_id
);
319 if_id
->has_index
= 1;
320 if_id
->index
= if_index
;
325 * qpb__if_identifier__get
327 * Get interface name and/or if_index from an if identifier.
329 #define qpb_if_identifier_get qpb__if_identifier__get
330 static inline int qpb__if_identifier__get(Qpb__IfIdentifier
*if_id
,
331 uint
*if_index
, char **name
)
342 if (if_id
->has_index
)
343 *if_index
= if_id
->index
;