DNS.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c)2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_DNS_HPP
  14. #define ZT_DNS_HPP
  15. #include "../include/ZeroTierOne.h"
  16. #include "Buffer.hpp"
  17. #include "InetAddress.hpp"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. namespace ZeroTier {
  22. /**
  23. * DNS data serialization methods
  24. */
  25. class DNS {
  26. public:
  27. template <unsigned int C> static inline void serializeDNS(Buffer<C>& b, const ZT_VirtualNetworkDNS* dns)
  28. {
  29. b.append(dns->domain, 128);
  30. for (unsigned int j = 0; j < ZT_MAX_DNS_SERVERS; ++j) {
  31. InetAddress tmp(dns->server_addr[j]);
  32. tmp.serialize(b);
  33. }
  34. }
  35. template <unsigned int C> static inline void deserializeDNS(const Buffer<C>& b, unsigned int& p, ZT_VirtualNetworkDNS* dns)
  36. {
  37. char* d = (char*)b.data() + p;
  38. memset(dns, 0, sizeof(ZT_VirtualNetworkDNS));
  39. memcpy(dns->domain, d, 128);
  40. dns->domain[127] = 0;
  41. p += 128;
  42. for (unsigned int j = 0; j < ZT_MAX_DNS_SERVERS; ++j) {
  43. p += reinterpret_cast<InetAddress*>(&(dns->server_addr[j]))->deserialize(b, p);
  44. }
  45. }
  46. };
  47. } // namespace ZeroTier
  48. #endif // ZT_DNS_HPP