NetDev.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "util/platform/Sockaddr.h"
  16. #include "util/log/Log.h"
  17. #include "exception/Except.h"
  18. #include "memory/Allocator.h"
  19. #include "memory/BufferAllocator.h"
  20. #include "util/platform/netdev/NetDev.h"
  21. #include "util/platform/netdev/NetPlatform.h"
  22. static void checkAddressAndPrefix(struct Sockaddr* sa,
  23. int* addrFam,
  24. char** printedAddr,
  25. void** addr,
  26. struct Allocator* alloc,
  27. struct Except* eh)
  28. {
  29. *printedAddr = Sockaddr_print(sa, alloc);
  30. *addrFam = Sockaddr_getFamily(sa);
  31. if (*addrFam != Sockaddr_AF_INET && *addrFam != Sockaddr_AF_INET6) {
  32. Except_throw(eh, "Unknown address type for address [%s]", *printedAddr);
  33. }
  34. int prefixMax = (*addrFam == Sockaddr_AF_INET6) ? 128 : 32;
  35. if (!(sa->flags & Sockaddr_flags_PREFIX)) {
  36. sa->prefix = prefixMax;
  37. }
  38. if (sa->prefix > prefixMax) {
  39. Except_throw(eh, "prefix [%u] must be less than %d", sa->prefix, prefixMax);
  40. }
  41. int len = Sockaddr_getAddress(sa, addr);
  42. if (len < 0 || len != prefixMax / 8) {
  43. Except_throw(eh, "Invalid sockaddr [%s]", *printedAddr);
  44. }
  45. }
  46. void NetDev_addAddress(const char* ifName,
  47. struct Sockaddr* sa,
  48. struct Log* logger,
  49. struct Except* eh)
  50. {
  51. int addrFam;
  52. char* printedAddr;
  53. void* addr;
  54. struct Allocator* alloc;
  55. BufferAllocator_STACK(alloc, 4096);
  56. checkAddressAndPrefix(sa, &addrFam, &printedAddr, &addr, alloc, eh);
  57. Log_info(logger, "Setting IP address [%s/%d] on interface [%s]",
  58. printedAddr, sa->prefix, ifName);
  59. NetPlatform_addAddress(ifName, addr, sa->prefix, addrFam, logger, alloc, eh);
  60. }
  61. void NetDev_setMTU(const char* interfaceName,
  62. uint32_t mtu,
  63. struct Log* logger,
  64. struct Except* eh)
  65. {
  66. NetPlatform_setMTU(interfaceName, mtu, logger, eh);
  67. }
  68. void NetDev_flushAddresses(const char* deviceName, struct Except* eh)
  69. {
  70. #ifdef win32
  71. NetPlatform_flushAddresses(deviceName, eh);
  72. #endif
  73. }
  74. /*
  75. void NetDev_addRoute(const char* ifName,
  76. struct Sockaddr* sa,
  77. int prefixLen,
  78. struct Log* logger,
  79. struct Except* eh)
  80. {
  81. int addrFam;
  82. char* printedAddr;
  83. void* addr;
  84. struct Allocator* alloc;
  85. BufferAllocator_STACK(alloc, 4096);
  86. checkAddressAndPrefix(sa, prefixLen, &addrFam, &printedAddr, &addr, alloc, eh);
  87. Log_info(logger, "Setting route [%s/%d on interface [%s]",
  88. printedAddr, prefixLen, ifName);
  89. NetPlatform_addRoute(ifName, addr, prefixLen, addrFam, logger, eh);
  90. }*/
  91. void NetDev_setRoutes(const char* ifName,
  92. struct Sockaddr** prefixSet,
  93. int prefixCount,
  94. struct Log* logger,
  95. struct Allocator* tempAlloc,
  96. struct Except* eh)
  97. {
  98. for (int i = 0; i < prefixCount; i++) {
  99. struct Allocator* alloc = Allocator_child(tempAlloc);
  100. int addrFam;
  101. char* printedAddr;
  102. void* addr;
  103. checkAddressAndPrefix(prefixSet[i], &addrFam, &printedAddr, &addr, alloc, eh);
  104. Allocator_free(alloc);
  105. }
  106. NetPlatform_setRoutes(ifName, prefixSet, prefixCount, logger, tempAlloc, eh);
  107. }