nexthop.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * lib/route/nexthop.c Routing Nexthop
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup route_obj
  13. * @defgroup nexthop Nexthop
  14. * @{
  15. */
  16. #include <netlink-local.h>
  17. #include <netlink/netlink.h>
  18. #include <netlink/utils.h>
  19. #include <netlink/route/rtnl.h>
  20. #include <netlink/route/route.h>
  21. /**
  22. * @name Allocation/Freeing
  23. * @{
  24. */
  25. struct rtnl_nexthop *rtnl_route_nh_alloc(void)
  26. {
  27. struct rtnl_nexthop *nh;
  28. nh = calloc(1, sizeof(*nh));
  29. if (!nh) {
  30. nl_errno(ENOMEM);
  31. return NULL;
  32. }
  33. nl_init_list_head(&nh->rtnh_list);
  34. return nh;
  35. }
  36. struct rtnl_nexthop *rtnl_route_nh_clone(struct rtnl_nexthop *src)
  37. {
  38. struct rtnl_nexthop *nh;
  39. nh = rtnl_route_nh_alloc();
  40. if (!nh)
  41. return NULL;
  42. nh->rtnh_flags = src->rtnh_flags;
  43. nh->rtnh_flag_mask = src->rtnh_flag_mask;
  44. nh->rtnh_weight = src->rtnh_weight;
  45. nh->rtnh_ifindex = src->rtnh_ifindex;
  46. nh->rtnh_mask = src->rtnh_mask;
  47. if (src->rtnh_gateway) {
  48. nh->rtnh_gateway = nl_addr_clone(src->rtnh_gateway);
  49. if (!nh->rtnh_gateway) {
  50. free(nh);
  51. return NULL;
  52. }
  53. }
  54. return nh;
  55. }
  56. void rtnl_route_nh_free(struct rtnl_nexthop *nh)
  57. {
  58. nl_addr_put(nh->rtnh_gateway);
  59. free(nh);
  60. }
  61. /** @} */
  62. /**
  63. * @name Attributes
  64. */
  65. void rtnl_route_nh_set_weight(struct rtnl_nexthop *nh, int weight)
  66. {
  67. nh->rtnh_weight = weight;
  68. nh->rtnh_mask |= NEXTHOP_HAS_WEIGHT;
  69. }
  70. int rtnl_route_nh_get_weight(struct rtnl_nexthop *nh)
  71. {
  72. if (nh->rtnh_mask & NEXTHOP_HAS_WEIGHT)
  73. return nh->rtnh_weight;
  74. else
  75. return 0;
  76. }
  77. void rtnl_route_nh_set_ifindex(struct rtnl_nexthop *nh, int ifindex)
  78. {
  79. nh->rtnh_ifindex = ifindex;
  80. nh->rtnh_mask |= NEXTHOP_HAS_IFINDEX;
  81. }
  82. int rtnl_route_nh_get_ifindex(struct rtnl_nexthop *nh)
  83. {
  84. if (nh->rtnh_mask & NEXTHOP_HAS_IFINDEX)
  85. return nh->rtnh_ifindex;
  86. else
  87. return -1;
  88. }
  89. void rtnl_route_nh_set_gateway(struct rtnl_nexthop *nh, struct nl_addr *addr)
  90. {
  91. struct nl_addr *old = nh->rtnh_gateway;
  92. nh->rtnh_gateway = nl_addr_get(addr);
  93. if (old)
  94. nl_addr_put(old);
  95. nh->rtnh_mask |= NEXTHOP_HAS_GATEWAY;
  96. }
  97. struct nl_addr *rtnl_route_nh_get_gateway(struct rtnl_nexthop *nh)
  98. {
  99. if (nh->rtnh_mask & NEXTHOP_HAS_GATEWAY)
  100. return nh->rtnh_gateway;
  101. else
  102. return NULL;
  103. }
  104. void rtnl_route_nh_set_flags(struct rtnl_nexthop *nh, unsigned int flags)
  105. {
  106. nh->rtnh_flag_mask |= flags;
  107. nh->rtnh_flags |= flags;
  108. nh->rtnh_mask |= NEXTHOP_HAS_FLAGS;
  109. }
  110. void rtnl_route_nh_unset_flags(struct rtnl_nexthop *nh, unsigned int flags)
  111. {
  112. nh->rtnh_flag_mask |= flags;
  113. nh->rtnh_flags &= ~flags;
  114. nh->rtnh_mask |= NEXTHOP_HAS_FLAGS;
  115. }
  116. unsigned int rtnl_route_nh_get_flags(struct rtnl_nexthop *nh)
  117. {
  118. if (nh->rtnh_mask & NEXTHOP_HAS_FLAGS)
  119. return nh->rtnh_flags;
  120. else
  121. return 0;
  122. }
  123. /** @} */
  124. /** @} */