NetDev.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <https://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 Er_DEFUN(void checkAddressAndPrefix(struct Sockaddr* sa,
  23. int* addrFam,
  24. char** printedAddr,
  25. void** addr,
  26. struct Allocator* alloc))
  27. {
  28. *printedAddr = Sockaddr_print(sa, alloc);
  29. *addrFam = Sockaddr_getFamily(sa);
  30. if (*addrFam != Sockaddr_AF_INET && *addrFam != Sockaddr_AF_INET6) {
  31. Er_raise(alloc, "Unknown address type for address [%s]", *printedAddr);
  32. }
  33. int prefixMax = (*addrFam == Sockaddr_AF_INET6) ? 128 : 32;
  34. if (!(sa->flags & Sockaddr_flags_PREFIX)) {
  35. sa->prefix = prefixMax;
  36. }
  37. if (sa->prefix > prefixMax) {
  38. Er_raise(alloc, "prefix [%u] must be less than %d", sa->prefix, prefixMax);
  39. }
  40. int len = Sockaddr_getAddress(sa, addr);
  41. if (len < 0 || len != prefixMax / 8) {
  42. Er_raise(alloc, "Invalid sockaddr [%s]", *printedAddr);
  43. }
  44. Er_ret();
  45. }
  46. Er_DEFUN(void NetDev_addAddress(const char* ifName,
  47. struct Sockaddr* sa,
  48. struct Log* logger,
  49. struct Allocator* alloc))
  50. {
  51. int addrFam;
  52. char* printedAddr;
  53. void* addr;
  54. Er(checkAddressAndPrefix(sa, &addrFam, &printedAddr, &addr, alloc));
  55. Log_info(logger, "Setting IP address [%s] on interface [%s]",
  56. printedAddr, ifName);
  57. Er(NetPlatform_addAddress(ifName, addr, sa->prefix, addrFam, logger, alloc));
  58. Er_ret();
  59. }
  60. Er_DEFUN(void NetDev_setMTU(const char* interfaceName,
  61. uint32_t mtu,
  62. struct Log* logger,
  63. struct Allocator* alloc))
  64. {
  65. Er(NetPlatform_setMTU(interfaceName, mtu, logger, alloc));
  66. Er_ret();
  67. }
  68. Er_DEFUN(void NetDev_flushAddresses(const char* deviceName, struct Allocator* alloc))
  69. {
  70. #ifdef win32
  71. Er(NetPlatform_flushAddresses(deviceName, alloc));
  72. #endif
  73. Er_ret();
  74. }
  75. /*
  76. void NetDev_addRoute(const char* ifName,
  77. struct Sockaddr* sa,
  78. int prefixLen,
  79. struct Log* logger,
  80. struct Except* eh)
  81. {
  82. int addrFam;
  83. char* printedAddr;
  84. void* addr;
  85. struct Allocator* alloc;
  86. BufferAllocator_STACK(alloc, 4096);
  87. checkAddressAndPrefix(sa, prefixLen, &addrFam, &printedAddr, &addr, alloc, eh);
  88. Log_info(logger, "Setting route [%s/%d on interface [%s]",
  89. printedAddr, prefixLen, ifName);
  90. NetPlatform_addRoute(ifName, addr, prefixLen, addrFam, logger, eh);
  91. }*/
  92. Er_DEFUN(void NetDev_setRoutes(const char* ifName,
  93. struct Sockaddr** prefixSet,
  94. int prefixCount,
  95. struct Log* logger,
  96. struct Allocator* tempAlloc))
  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. Er(checkAddressAndPrefix(prefixSet[i], &addrFam, &printedAddr, &addr, alloc));
  104. Allocator_free(alloc);
  105. }
  106. Er(NetPlatform_setRoutes(ifName, prefixSet, prefixCount, logger, tempAlloc));
  107. Er_ret();
  108. }