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 <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 "util/platform/netdev/NetDev.h"
  20. #include "util/platform/netdev/NetPlatform.h"
  21. static Er_DEFUN(void checkAddressAndPrefix(struct Sockaddr* sa,
  22. int* addrFam,
  23. char** printedAddr,
  24. void** addr,
  25. struct Allocator* alloc))
  26. {
  27. *printedAddr = Sockaddr_print(sa, alloc);
  28. *addrFam = Sockaddr_getFamily(sa);
  29. if (*addrFam != Sockaddr_AF_INET && *addrFam != Sockaddr_AF_INET6) {
  30. Er_raise(alloc, "Unknown address type for address [%s]", *printedAddr);
  31. }
  32. int prefixMax = (*addrFam == Sockaddr_AF_INET6) ? 128 : 32;
  33. if (!(sa->flags & Sockaddr_flags_PREFIX)) {
  34. sa->prefix = prefixMax;
  35. }
  36. if (sa->prefix > prefixMax) {
  37. Er_raise(alloc, "prefix [%u] must be less than %d", sa->prefix, prefixMax);
  38. }
  39. int len = Sockaddr_getAddress(sa, addr);
  40. if (len < 0 || len != prefixMax / 8) {
  41. Er_raise(alloc, "Invalid sockaddr [%s]", *printedAddr);
  42. }
  43. Er_ret();
  44. }
  45. Er_DEFUN(void NetDev_addAddress(const char* ifName,
  46. struct Sockaddr* sa,
  47. struct Log* logger,
  48. struct Allocator* alloc))
  49. {
  50. int addrFam;
  51. char* printedAddr;
  52. void* addr;
  53. Er(checkAddressAndPrefix(sa, &addrFam, &printedAddr, &addr, alloc));
  54. Log_info(logger, "Setting IP address [%s] on interface [%s]",
  55. printedAddr, ifName);
  56. Er(NetPlatform_addAddress(ifName, addr, sa->prefix, addrFam, logger, alloc));
  57. Er_ret();
  58. }
  59. Er_DEFUN(void NetDev_setMTU(const char* interfaceName,
  60. uint32_t mtu,
  61. struct Log* logger,
  62. struct Allocator* alloc))
  63. {
  64. Er(NetPlatform_setMTU(interfaceName, mtu, logger, alloc));
  65. Er_ret();
  66. }
  67. Er_DEFUN(void NetDev_flushAddresses(const char* deviceName, struct Allocator* alloc))
  68. {
  69. #ifdef win32
  70. Er(NetPlatform_flushAddresses(deviceName, alloc));
  71. #endif
  72. Er_ret();
  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. Er_DEFUN(void NetDev_setRoutes(const char* ifName,
  92. struct Sockaddr** prefixSet,
  93. int prefixCount,
  94. struct Log* logger,
  95. struct Allocator* tempAlloc))
  96. {
  97. for (int i = 0; i < prefixCount; i++) {
  98. struct Allocator* alloc = Allocator_child(tempAlloc);
  99. int addrFam;
  100. char* printedAddr;
  101. void* addr;
  102. Er(checkAddressAndPrefix(prefixSet[i], &addrFam, &printedAddr, &addr, alloc));
  103. Allocator_free(alloc);
  104. }
  105. Er(NetPlatform_setRoutes(ifName, prefixSet, prefixCount, logger, tempAlloc));
  106. Er_ret();
  107. }