rtnl.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * lib/route/rtnl.c Routing Netlink
  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-2012 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @defgroup rtnl Routing Library (libnl-route)
  13. * @{
  14. */
  15. #include <netlink-private/netlink.h>
  16. #include <netlink/netlink.h>
  17. #include <netlink/utils.h>
  18. #include <netlink/route/rtnl.h>
  19. /**
  20. * @name Sending
  21. * @{
  22. */
  23. /**
  24. * Send routing netlink request message
  25. * @arg sk Netlink socket.
  26. * @arg type Netlink message type.
  27. * @arg family Address family.
  28. * @arg flags Additional netlink message flags.
  29. *
  30. * Fills out a routing netlink request message and sends it out
  31. * using nl_send_simple().
  32. *
  33. * @return 0 on success or a negative error code. Due to a bug in
  34. * older versions, this returned the number of bytes sent. So for
  35. * compatibility, treat positive return values as success too.
  36. */
  37. int nl_rtgen_request(struct nl_sock *sk, int type, int family, int flags)
  38. {
  39. int err;
  40. struct rtgenmsg gmsg = {
  41. .rtgen_family = family,
  42. };
  43. err = nl_send_simple(sk, type, flags, &gmsg, sizeof(gmsg));
  44. return err >= 0 ? 0 : err;
  45. }
  46. /** @} */
  47. /**
  48. * @name Routing Type Translations
  49. * @{
  50. */
  51. static const struct trans_tbl rtntypes[] = {
  52. __ADD(RTN_UNSPEC,unspec)
  53. __ADD(RTN_UNICAST,unicast)
  54. __ADD(RTN_LOCAL,local)
  55. __ADD(RTN_BROADCAST,broadcast)
  56. __ADD(RTN_ANYCAST,anycast)
  57. __ADD(RTN_MULTICAST,multicast)
  58. __ADD(RTN_BLACKHOLE,blackhole)
  59. __ADD(RTN_UNREACHABLE,unreachable)
  60. __ADD(RTN_PROHIBIT,prohibit)
  61. __ADD(RTN_THROW,throw)
  62. __ADD(RTN_NAT,nat)
  63. __ADD(RTN_XRESOLVE,xresolve)
  64. };
  65. char *nl_rtntype2str(int type, char *buf, size_t size)
  66. {
  67. return __type2str(type, buf, size, rtntypes, ARRAY_SIZE(rtntypes));
  68. }
  69. int nl_str2rtntype(const char *name)
  70. {
  71. return __str2type(name, rtntypes, ARRAY_SIZE(rtntypes));
  72. }
  73. /** @} */
  74. /**
  75. * @name Scope Translations
  76. * @{
  77. */
  78. static const struct trans_tbl scopes[] = {
  79. __ADD(255,nowhere)
  80. __ADD(254,host)
  81. __ADD(253,link)
  82. __ADD(200,site)
  83. __ADD(0,global)
  84. };
  85. char *rtnl_scope2str(int scope, char *buf, size_t size)
  86. {
  87. return __type2str(scope, buf, size, scopes, ARRAY_SIZE(scopes));
  88. }
  89. int rtnl_str2scope(const char *name)
  90. {
  91. return __str2type(name, scopes, ARRAY_SIZE(scopes));
  92. }
  93. /** @} */
  94. /**
  95. * @name Realms Translations
  96. * @{
  97. */
  98. char * rtnl_realms2str(uint32_t realms, char *buf, size_t len)
  99. {
  100. int from = RTNL_REALM_FROM(realms);
  101. int to = RTNL_REALM_TO(realms);
  102. snprintf(buf, len, "%d/%d", from, to);
  103. return buf;
  104. }
  105. /** @} */
  106. /** @} */