xref: /haiku/src/preferences/mail/DNSQuery.h (revision 58481f0f6ef1a61ba07283f012cafbc2ed874ead)
1 #ifndef DNS_QUERY_H
2 #define DNS_QUERY_H
3 
4 
5 #include <DataIO.h>
6 #include <NetBuffer.h>
7 #include <ObjectList.h>
8 #include <String.h>
9 
10 
11 #include <arpa/inet.h>
12 
13 #define MX_RECORD		15
14 
15 struct mx_record {
16 	uint16	priority;
17 	BString	serverName;
18 };
19 
20 
21 class BRawNetBuffer {
22 public:
23 						BRawNetBuffer();
24 						BRawNetBuffer(off_t size);
25 						BRawNetBuffer(const void* buf, size_t size);
26 
27 		// functions like in BNetBuffer but no type and size info is writen.
28 		// functions return B_NO_MEMORY or B_OK
29 		status_t		AppendUint16(uint16 value);
30 		status_t		AppendString(const char* string);
31 
32 		status_t		ReadUint16(uint16& value);
33 		status_t		ReadUint32(uint32& value);
34 		status_t		ReadString(BString& string);
35 
36 		status_t		SkipReading(off_t off);
37 
38 		void			*Data(void) const { return (void*)fBuffer.Buffer(); }
39 		size_t			Size(void) const { return fBuffer.BufferLength(); }
40 		size_t			SetSize(off_t size) { return fBuffer.SetSize(size); }
41 		void			SetWritePosition(off_t pos) { fWritePosition = pos; }
42 
43 private:
44 		void			_Init(const void* buf, size_t size);
45 		void			_ReadSubString(BString& string, off_t pos);
46 		off_t 			fWritePosition;
47 		off_t 			fReadPosition;
48 		BMallocIO		fBuffer;
49 };
50 
51 
52 class DNSTools {
53 public:
54 		static void		GetDNSServers(BObjectList<BString>* serverList);
55 		static BString	ConvertToDNSName(const BString& string);
56 		static BString	ConvertFromDNSName(const BString& string);
57 };
58 
59 // see also http://prasshhant.blogspot.com/2007/03/dns-query.html
60 struct dns_header {
61 	uint16 id;						// A 16 bit identifier
62 
63 	unsigned	char qr     :1;		// query (0), or a response (1)
64 	unsigned	char opcode :4;	    // A four bit field
65 	unsigned	char aa     :1;		// Authoritative Answer
66 	unsigned	char tc     :1;		// Truncated Message
67 	unsigned	char rd     :1;		// Recursion Desired
68 	unsigned	char ra     :1;		// Recursion Available
69 	unsigned	char z      :3;		// Reserved for future use
70 	unsigned	char rcode  :4;	    // Response code
71 
72 	uint16		q_count;			// number of question entries
73 	uint16		ans_count;			// number of answer entries
74 	uint16		auth_count;			// number of authority entries
75 	uint16		add_count;			// number of resource entries
76 };
77 
78 // resource record without resource data
79 struct resource_record_head {
80 	BString	name;
81 	uint16	type;
82 	uint16	dataClass;
83 	uint32	ttl;
84 	uint16	dataLength;
85 };
86 
87 
88 class DNSQuery {
89 public:
90 						DNSQuery();
91 						~DNSQuery();
92 		status_t		ReadDNSServer(in_addr* add);
93 		status_t		GetMXRecords(BString serverName,
94 							BObjectList<mx_record>* mxList,
95 							bigtime_t timeout = 500000);
96 
97 private:
98 		uint16			_GetUniqueID();
99 		void			_SetMXHeader(dns_header* header);
100 		void			_AppendQueryHeader(BRawNetBuffer& buffer,
101 							const dns_header* header);
102 		void			_ReadQueryHeader(BRawNetBuffer& buffer,
103 							dns_header* header);
104 		void			_ReadMXRecord(BRawNetBuffer& buffer,
105 							mx_record* mxRecord);
106 
107 		void			_ReadResourceRecord(BRawNetBuffer& buffer,
108 							resource_record_head* rrHead);
109 };
110 
111 
112 #endif // DNS_QUERY_H
113