qpb.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. * Main public header file for the quagga protobuf library.
  27. */
  28. #ifndef _QPB_H
  29. #define _QPB_H
  30. #include "prefix.h"
  31. #include "qpb/qpb.pb-c.h"
  32. #include "qpb/qpb_allocator.h"
  33. /*
  34. * qpb__address_family__set
  35. */
  36. #define qpb_address_family_set qpb__address_family__set
  37. static inline int
  38. qpb__address_family__set (Qpb__AddressFamily *pb_family, u_char family)
  39. {
  40. switch (family) {
  41. case AF_INET:
  42. *pb_family = QPB__ADDRESS_FAMILY__IPV4;
  43. return 1;
  44. case AF_INET6:
  45. *pb_family = QPB__ADDRESS_FAMILY__IPV6;
  46. return 1;
  47. default:
  48. *pb_family = QPB__ADDRESS_FAMILY__UNKNOWN_AF;
  49. }
  50. return 0;
  51. }
  52. /*
  53. * qpb__address_family__get
  54. */
  55. #define qpb_address_family_get qpb__address_family__get
  56. static inline int
  57. qpb__address_family__get (Qpb__AddressFamily pb_family, u_char *family)
  58. {
  59. switch (pb_family) {
  60. case QPB__ADDRESS_FAMILY__IPV4:
  61. *family = AF_INET;
  62. return 1;
  63. case QPB__ADDRESS_FAMILY__IPV6:
  64. *family = AF_INET6;
  65. return 1;
  66. case QPB__ADDRESS_FAMILY__UNKNOWN_AF:
  67. return 0;
  68. }
  69. return 0;
  70. }
  71. /*
  72. * qpb__l3_prefix__create
  73. */
  74. #define qpb_l3_prefix_create qpb__l3_prefix__create
  75. static inline Qpb__L3Prefix *
  76. qpb__l3_prefix__create (qpb_allocator_t *allocator, struct prefix *p)
  77. {
  78. Qpb__L3Prefix *prefix;
  79. prefix = QPB_ALLOC(allocator, typeof(*prefix));
  80. if (!prefix) {
  81. return NULL;
  82. }
  83. qpb__l3_prefix__init(prefix);
  84. prefix->length = p->prefixlen;
  85. prefix->bytes.len = (p->prefixlen + 7)/8;
  86. prefix->bytes.data = qpb_alloc(allocator, prefix->bytes.len);
  87. if (!prefix->bytes.data) {
  88. return NULL;
  89. }
  90. memcpy(prefix->bytes.data, &p->u.prefix, prefix->bytes.len);
  91. return prefix;
  92. }
  93. /*
  94. * qpb__l3_prefix__get
  95. */
  96. #define qpb_l3_prefix_get qpb__l3_prefix__get
  97. static inline int
  98. qpb__l3_prefix__get (const Qpb__L3Prefix *pb_prefix, u_char family,
  99. struct prefix *prefix)
  100. {
  101. switch (family)
  102. {
  103. case AF_INET:
  104. memset(prefix, 0, sizeof(struct prefix_ipv4));
  105. break;
  106. case AF_INET6:
  107. memset(prefix, 0, sizeof(struct prefix_ipv6));
  108. break;
  109. default:
  110. memset(prefix, 0, sizeof(*prefix));
  111. }
  112. prefix->prefixlen = pb_prefix->length;
  113. prefix->family = family;
  114. memcpy(&prefix->u.prefix, pb_prefix->bytes.data, pb_prefix->bytes.len);
  115. return 1;
  116. }
  117. /*
  118. * qpb__protocol__set
  119. *
  120. * Translate a quagga route type to a protobuf protocol.
  121. */
  122. #define qpb_protocol_set qpb__protocol__set
  123. static inline int
  124. qpb__protocol__set (Qpb__Protocol *pb_proto, int route_type)
  125. {
  126. switch (route_type) {
  127. case ZEBRA_ROUTE_KERNEL:
  128. *pb_proto = QPB__PROTOCOL__KERNEL;
  129. break;
  130. case ZEBRA_ROUTE_CONNECT:
  131. *pb_proto = QPB__PROTOCOL__CONNECTED;
  132. break;
  133. case ZEBRA_ROUTE_STATIC:
  134. *pb_proto = QPB__PROTOCOL__STATIC;
  135. break;
  136. case ZEBRA_ROUTE_RIP:
  137. *pb_proto = QPB__PROTOCOL__RIP;
  138. break;
  139. case ZEBRA_ROUTE_RIPNG:
  140. *pb_proto = QPB__PROTOCOL__RIPNG;
  141. break;
  142. case ZEBRA_ROUTE_OSPF:
  143. case ZEBRA_ROUTE_OSPF6:
  144. *pb_proto = QPB__PROTOCOL__OSPF;
  145. break;
  146. case ZEBRA_ROUTE_ISIS:
  147. *pb_proto = QPB__PROTOCOL__ISIS;
  148. break;
  149. case ZEBRA_ROUTE_BGP:
  150. *pb_proto = QPB__PROTOCOL__BGP;
  151. break;
  152. case ZEBRA_ROUTE_HSLS:
  153. case ZEBRA_ROUTE_OLSR:
  154. case ZEBRA_ROUTE_BABEL:
  155. case ZEBRA_ROUTE_MAX:
  156. case ZEBRA_ROUTE_SYSTEM:
  157. default:
  158. *pb_proto = QPB__PROTOCOL__OTHER;
  159. }
  160. return 1;
  161. }
  162. /*
  163. * qpb__ipv4_address__create
  164. */
  165. static inline Qpb__Ipv4Address *
  166. qpb__ipv4_address__create (qpb_allocator_t *allocator,
  167. struct in_addr *addr)
  168. {
  169. Qpb__Ipv4Address *v4;
  170. v4 = QPB_ALLOC(allocator, typeof(*v4));
  171. if (!v4) {
  172. return NULL;
  173. }
  174. qpb__ipv4_address__init(v4);
  175. v4->value = ntohl(addr->s_addr);
  176. return v4;
  177. }
  178. /*
  179. * qpb__ipv4_address__get
  180. */
  181. static inline int
  182. qpb__ipv4_address__get (const Qpb__Ipv4Address *v4, struct in_addr *addr)
  183. {
  184. addr->s_addr = htonl(v4->value);
  185. return 1;
  186. }
  187. /*
  188. * qpb__ipv6_address__create
  189. */
  190. static inline Qpb__Ipv6Address *
  191. qpb__ipv6_address__create (qpb_allocator_t *allocator, struct in6_addr *addr)
  192. {
  193. Qpb__Ipv6Address *v6;
  194. v6 = QPB_ALLOC(allocator, typeof(*v6));
  195. if (!v6)
  196. return NULL;
  197. qpb__ipv6_address__init(v6);
  198. v6->bytes.len = 16;
  199. v6->bytes.data = qpb_alloc(allocator, 16);
  200. if (!v6->bytes.data)
  201. return NULL;
  202. memcpy(v6->bytes.data, addr->s6_addr, v6->bytes.len);
  203. return v6;
  204. }
  205. /*
  206. * qpb__ipv6_address__get
  207. *
  208. * Read out information from a protobuf ipv6 address structure.
  209. */
  210. static inline int
  211. qpb__ipv6_address__get (const Qpb__Ipv6Address *v6, struct in6_addr *addr)
  212. {
  213. if (v6->bytes.len != 16)
  214. return 0;
  215. memcpy(addr->s6_addr, v6->bytes.data, v6->bytes.len);
  216. return 1;
  217. }
  218. /*
  219. * qpb__l3_address__create
  220. */
  221. #define qpb_l3_address_create qpb__l3_address__create
  222. static inline Qpb__L3Address *
  223. qpb__l3_address__create (qpb_allocator_t *allocator, union g_addr *addr,
  224. u_char family)
  225. {
  226. Qpb__L3Address *l3_addr;
  227. l3_addr = QPB_ALLOC(allocator, typeof(*l3_addr));
  228. if (!l3_addr)
  229. return NULL;
  230. qpb__l3_address__init(l3_addr);
  231. switch (family) {
  232. case AF_INET:
  233. l3_addr->v4 = qpb__ipv4_address__create (allocator, &addr->ipv4);
  234. if (!l3_addr->v4)
  235. return NULL;
  236. break;
  237. case AF_INET6:
  238. l3_addr->v6 = qpb__ipv6_address__create (allocator, &addr->ipv6);
  239. if (!l3_addr->v6)
  240. return NULL;
  241. break;
  242. }
  243. return l3_addr;
  244. }
  245. /*
  246. * qpb__l3_address__get
  247. *
  248. * Read out a gateway address from a protobuf l3 address.
  249. */
  250. #define qpb_l3_address_get qpb__l3_address__get
  251. static inline int
  252. qpb__l3_address__get (const Qpb__L3Address *l3_addr,
  253. u_char *family, union g_addr *addr)
  254. {
  255. if (l3_addr->v4)
  256. {
  257. qpb__ipv4_address__get (l3_addr->v4, &addr->ipv4);
  258. *family = AF_INET;
  259. return 1;
  260. }
  261. if (l3_addr->v6)
  262. {
  263. qpb__ipv6_address__get(l3_addr->v6, &addr->ipv6);
  264. *family = AF_INET6;
  265. return 1;
  266. }
  267. return 0;
  268. }
  269. /*
  270. * qpb__if_identifier__create
  271. */
  272. #define qpb_if_identifier_create qpb__if_identifier__create
  273. static inline Qpb__IfIdentifier *
  274. qpb__if_identifier__create (qpb_allocator_t *allocator, uint if_index)
  275. {
  276. Qpb__IfIdentifier *if_id;
  277. if_id = QPB_ALLOC(allocator, typeof(*if_id));
  278. if (!if_id) {
  279. return NULL;
  280. }
  281. qpb__if_identifier__init(if_id);
  282. if_id->has_index = 1;
  283. if_id->index = if_index;
  284. return if_id;
  285. }
  286. /*
  287. * qpb__if_identifier__get
  288. *
  289. * Get interface name and/or if_index from an if identifier.
  290. */
  291. #define qpb_if_identifier_get qpb__if_identifier__get
  292. static inline int
  293. qpb__if_identifier__get (Qpb__IfIdentifier *if_id, uint *if_index,
  294. char **name)
  295. {
  296. char *str;
  297. uint ix;
  298. if (!if_index)
  299. if_index = &ix;
  300. if (!name)
  301. name = &str;
  302. if (if_id->has_index)
  303. *if_index = if_id->index;
  304. else
  305. *if_index = 0;
  306. *name = if_id->name;
  307. return 1;
  308. }
  309. #endif