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
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
27 * Main public header file for the quagga protobuf library.
35 #include "qpb/qpb.pb-c.h"
37 #include "qpb/qpb_allocator.h"
40 * qpb__address_family__set
42 #define qpb_address_family_set qpb__address_family__set
44 qpb__address_family__set (Qpb__AddressFamily
*pb_family
, u_char family
)
48 *pb_family
= QPB__ADDRESS_FAMILY__IPV4
;
52 *pb_family
= QPB__ADDRESS_FAMILY__IPV6
;
56 *pb_family
= QPB__ADDRESS_FAMILY__UNKNOWN_AF
;
63 * qpb__address_family__get
65 #define qpb_address_family_get qpb__address_family__get
67 qpb__address_family__get (Qpb__AddressFamily pb_family
, u_char
*family
)
71 case QPB__ADDRESS_FAMILY__IPV4
:
75 case QPB__ADDRESS_FAMILY__IPV6
:
79 case QPB__ADDRESS_FAMILY__UNKNOWN_AF
:
81 default: /* protobuf "magic value" _QPB__ADDRESS_FAMILY_IS_INT_SIZE */
89 * qpb__l3_prefix__create
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
)
95 Qpb__L3Prefix
*prefix
;
97 prefix
= QPB_ALLOC(allocator
, typeof(*prefix
));
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
) {
109 memcpy(prefix
->bytes
.data
, &p
->u
.prefix
, prefix
->bytes
.len
);
115 * qpb__l3_prefix__get
117 #define qpb_l3_prefix_get qpb__l3_prefix__get
119 qpb__l3_prefix__get (const Qpb__L3Prefix
*pb_prefix
, u_char family
,
120 struct prefix
*prefix
)
127 memset(prefix
, 0, sizeof(struct prefix_ipv4
));
131 memset(prefix
, 0, sizeof(struct prefix_ipv6
));
135 memset(prefix
, 0, sizeof(*prefix
));
138 prefix
->prefixlen
= pb_prefix
->length
;
139 prefix
->family
= family
;
140 memcpy(&prefix
->u
.prefix
, pb_prefix
->bytes
.data
, pb_prefix
->bytes
.len
);
147 * Translate a quagga route type to a protobuf protocol.
149 #define qpb_protocol_set qpb__protocol__set
151 qpb__protocol__set (Qpb__Protocol
*pb_proto
, int route_type
)
153 switch (route_type
) {
154 case ZEBRA_ROUTE_KERNEL
:
155 *pb_proto
= QPB__PROTOCOL__KERNEL
;
158 case ZEBRA_ROUTE_CONNECT
:
159 *pb_proto
= QPB__PROTOCOL__CONNECTED
;
162 case ZEBRA_ROUTE_STATIC
:
163 *pb_proto
= QPB__PROTOCOL__STATIC
;
166 case ZEBRA_ROUTE_RIP
:
167 *pb_proto
= QPB__PROTOCOL__RIP
;
170 case ZEBRA_ROUTE_RIPNG
:
171 *pb_proto
= QPB__PROTOCOL__RIPNG
;
174 case ZEBRA_ROUTE_OSPF
:
175 case ZEBRA_ROUTE_OSPF6
:
176 *pb_proto
= QPB__PROTOCOL__OSPF
;
179 case ZEBRA_ROUTE_ISIS
:
180 *pb_proto
= QPB__PROTOCOL__ISIS
;
183 case ZEBRA_ROUTE_BGP
:
184 *pb_proto
= QPB__PROTOCOL__BGP
;
187 case ZEBRA_ROUTE_HSLS
:
188 case ZEBRA_ROUTE_OLSR
:
189 case ZEBRA_ROUTE_MAX
:
190 case ZEBRA_ROUTE_SYSTEM
:
192 *pb_proto
= QPB__PROTOCOL__OTHER
;
199 * qpb__ipv4_address__create
201 static inline Qpb__Ipv4Address
*
202 qpb__ipv4_address__create (qpb_allocator_t
*allocator
,
203 struct in_addr
*addr
)
205 Qpb__Ipv4Address
*v4
;
207 v4
= QPB_ALLOC(allocator
, typeof(*v4
));
211 qpb__ipv4_address__init(v4
);
213 v4
->value
= ntohl(addr
->s_addr
);
218 * qpb__ipv4_address__get
221 qpb__ipv4_address__get (const Qpb__Ipv4Address
*v4
, struct in_addr
*addr
)
223 addr
->s_addr
= htonl(v4
->value
);
228 * qpb__ipv6_address__create
230 static inline Qpb__Ipv6Address
*
231 qpb__ipv6_address__create (qpb_allocator_t
*allocator
, struct in6_addr
*addr
)
233 Qpb__Ipv6Address
*v6
;
235 v6
= QPB_ALLOC(allocator
, typeof(*v6
));
239 qpb__ipv6_address__init(v6
);
241 v6
->bytes
.data
= qpb_alloc(allocator
, 16);
245 memcpy(v6
->bytes
.data
, addr
->s6_addr
, v6
->bytes
.len
);
250 * qpb__ipv6_address__get
252 * Read out information from a protobuf ipv6 address structure.
255 qpb__ipv6_address__get (const Qpb__Ipv6Address
*v6
, struct in6_addr
*addr
)
257 if (v6
->bytes
.len
!= 16)
260 memcpy(addr
->s6_addr
, v6
->bytes
.data
, v6
->bytes
.len
);
265 * qpb__l3_address__create
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
,
272 Qpb__L3Address
*l3_addr
;
274 l3_addr
= QPB_ALLOC(allocator
, typeof(*l3_addr
));
278 qpb__l3_address__init(l3_addr
);
283 l3_addr
->v4
= qpb__ipv4_address__create (allocator
, &addr
->ipv4
);
290 l3_addr
->v6
= qpb__ipv6_address__create (allocator
, &addr
->ipv6
);
300 * qpb__l3_address__get
302 * Read out a gateway address from a protobuf l3 address.
304 #define qpb_l3_address_get qpb__l3_address__get
306 qpb__l3_address__get (const Qpb__L3Address
*l3_addr
,
307 u_char
*family
, union g_addr
*addr
)
311 qpb__ipv4_address__get (l3_addr
->v4
, &addr
->ipv4
);
318 qpb__ipv6_address__get(l3_addr
->v6
, &addr
->ipv6
);
327 * qpb__if_identifier__create
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
)
333 Qpb__IfIdentifier
*if_id
;
335 if_id
= QPB_ALLOC(allocator
, typeof(*if_id
));
339 qpb__if_identifier__init(if_id
);
340 if_id
->has_index
= 1;
341 if_id
->index
= if_index
;
346 * qpb__if_identifier__get
348 * Get interface name and/or if_index from an if identifier.
350 #define qpb_if_identifier_get qpb__if_identifier__get
352 qpb__if_identifier__get (Qpb__IfIdentifier
*if_id
, uint
*if_index
,
364 if (if_id
->has_index
)
365 *if_index
= if_id
->index
;