NetDev.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. void NetDev_addAddress(const char* ifName,
  23. struct Sockaddr* sa,
  24. int prefixLen,
  25. struct Log* logger,
  26. struct Except* eh)
  27. {
  28. int addrFam = Sockaddr_getFamily(sa);
  29. struct Allocator* alloc;
  30. BufferAllocator_STACK(alloc, 4096);
  31. char* printedAddr = Sockaddr_print(sa, alloc);
  32. if (addrFam != Sockaddr_AF_INET && addrFam != Sockaddr_AF_INET6) {
  33. Except_throw(eh, "Unknown address type for address [%s]", printedAddr);
  34. }
  35. int prefixMax = (addrFam == Sockaddr_AF_INET6) ? 128 : 32;
  36. if (prefixLen < 0 || prefixLen > prefixMax) {
  37. Except_throw(eh, "prefixLen [%d] must be greater than 0 and less than %d",
  38. prefixLen, prefixMax);
  39. }
  40. void* addr;
  41. int len = Sockaddr_getAddress(sa, &addr);
  42. if (len < 0 || len != prefixMax / 8) {
  43. Except_throw(eh, "Invalid sockaddr [%s]", printedAddr);
  44. }
  45. Log_info(logger, "Setting IP address [%s/%d] on interface [%s]",
  46. printedAddr, prefixLen, ifName);
  47. NetPlatform_addAddress(ifName, addr, prefixLen, addrFam, logger, eh);
  48. }
  49. void NetDev_setMTU(const char* interfaceName,
  50. uint32_t mtu,
  51. struct Log* logger,
  52. struct Except* eh)
  53. {
  54. NetPlatform_setMTU(interfaceName, mtu, logger, eh);
  55. }
  56. void NetDev_flushAddresses(const char* deviceName, struct Except* eh)
  57. {
  58. #ifdef win32
  59. NetPlatform_flushAddresses(deviceName, eh);
  60. #endif
  61. }