DNS.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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: 2025-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 <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "Buffer.hpp"
  19. #include "InetAddress.hpp"
  20. #include "../include/ZeroTierOne.h"
  21. namespace ZeroTier {
  22. /**
  23. * DNS data serialization methods
  24. */
  25. class DNS {
  26. public:
  27. template<unsigned int C>
  28. static inline void serializeDNS(Buffer<C> &b, const ZT_VirtualNetworkDNS *dns)
  29. {
  30. b.append(dns->domain, 128);
  31. for(unsigned int j = 0; j < ZT_MAX_DNS_SERVERS; ++j) {
  32. InetAddress tmp(dns->server_addr[j]);
  33. tmp.serialize(b);
  34. }
  35. }
  36. template<unsigned int C>
  37. static inline void deserializeDNS(const Buffer<C> &b, unsigned int &p, ZT_VirtualNetworkDNS *dns)
  38. {
  39. char *d = (char*)b.data()+p;
  40. memset(dns, 0, sizeof(ZT_VirtualNetworkDNS));
  41. memcpy(dns->domain, d, 128);
  42. dns->domain[127] = 0;
  43. p += 128;
  44. for (unsigned int j = 0; j < ZT_MAX_DNS_SERVERS; ++j) {
  45. p += reinterpret_cast<InetAddress *>(&(dns->server_addr[j]))->deserialize(b, p);
  46. }
  47. }
  48. };
  49. }
  50. #endif // ZT_DNS_HPP