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