2
0

DNS.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. #ifndef ZT_DNS_HPP
  9. #define ZT_DNS_HPP
  10. #include "../include/ZeroTierOne.h"
  11. #include "Buffer.hpp"
  12. #include "InetAddress.hpp"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. namespace ZeroTier {
  17. /**
  18. * DNS data serialization methods
  19. */
  20. class DNS {
  21. public:
  22. template <unsigned int C> static inline void serializeDNS(Buffer<C>& b, const ZT_VirtualNetworkDNS* dns)
  23. {
  24. b.append(dns->domain, 128);
  25. for (unsigned int j = 0; j < ZT_MAX_DNS_SERVERS; ++j) {
  26. InetAddress tmp(dns->server_addr[j]);
  27. tmp.serialize(b);
  28. }
  29. }
  30. template <unsigned int C> static inline void deserializeDNS(const Buffer<C>& b, unsigned int& p, ZT_VirtualNetworkDNS* dns)
  31. {
  32. char* d = (char*)b.data() + p;
  33. memset(dns, 0, sizeof(ZT_VirtualNetworkDNS));
  34. memcpy(dns->domain, d, 128);
  35. dns->domain[127] = 0;
  36. p += 128;
  37. for (unsigned int j = 0; j < ZT_MAX_DNS_SERVERS; ++j) {
  38. p += reinterpret_cast<InetAddress*>(&(dns->server_addr[j]))->deserialize(b, p);
  39. }
  40. }
  41. };
  42. } // namespace ZeroTier
  43. #endif // ZT_DNS_HPP