rtnl.c 2.4 KB

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