]> git.karo-electronics.de Git - mdnsd.git/blob - rfc1035.h
Add futher files to .gitignore
[mdnsd.git] / rfc1035.h
1 #ifndef _RFC1035_H_
2 #define _RFC1035_H_
3
4 /*
5  * Copyright (C) 2003 Jer <jer@jabber.org>
6  * Copyright (c) 2009 Simon Budig <simon@budig.org>
7  * Copyright (C) 2013 Ole Reinhardt <ole.reinhardt@embedded-it.de>
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the copyright holders nor the names of
21  *    contributors may be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
31  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
34  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * For additional information see http://www.ethernut.de/
38  */
39
40 /* This code is based on
41  * Based on BSD licensed mdnsd implementation by Jer <jer@jabber.org>
42  * http://dotlocal.org/mdnsd/
43  *
44  * Unfortunately this site is now longer alive. You can still find it at:
45  * http://web.archive.org/web/20080705131510/http://dotlocal.org/mdnsd/
46  *
47  * mdnsd - embeddable Multicast DNS Daemon
48  * =======================================
49  *
50  * "mdnsd" is a very lightweight, simple, portable, and easy to integrate
51  * open source implementation of Multicast DNS (part of Zeroconf, also called
52  * Rendezvous by Apple) for developers. It supports both acting as a Query and
53  * a Responder, allowing any software to participate fully on the .localnetwork
54  * just by including a few files and calling a few functions.  All of the
55  * complexity of handling the Multicast DNS retransmit timing, duplicate
56  * suppression, probing, conflict detection, and other facets of the DNS
57  * protocol is hidden behind a very simple and very easy to use interface,
58  * described in the header file. The single small c source file has almost no
59  * dependencies, and is portable to almost any embedded platform.
60  * Multiple example applications and usages are included in the download,
61  * including a simple persistent query browser and a tool to advertise .local
62  * web sites.
63  *
64  * The code is licensed under both the GPL and BSD licenses, for use in any
65  * free software or commercial application. If there is a licensing need not
66  * covered by either of those, alternative licensing is available upon request.
67  *
68  */
69
70 /*!
71  * \file include/pro/rfc1035.h
72  * \brief Standalone DNS parsing functions
73  *
74  * Implementation follows RF1035 [http://www.rfc-editor.org/rfc/rfc1035.txt] and
75  * includes decoding functions for svr rr's code type 33. See RFC2782
76  * [http://www.rfc-editor.org/rfc/rfc2782.txt]*
77  *
78  * \verbatim
79  *
80  * $Id$
81  *
82  * \endverbatim
83  */
84
85 #include <stdint.h>
86
87 /*!
88  * \addtogroup xgMulticastDns
89  */
90 /*@{*/
91
92 #define PACKET_BUFFER_LEN 4096
93 #define MAX_PACKET_LEN    4000
94 #define MAX_LABEL_SIZE    256
95 #define MAX_LABEL         20
96
97 #define QTYPE_A           1
98 #define QTYPE_NS          2
99 #define QTYPE_CNAME       5
100 #define QTYPE_PTR         12
101 #define QTYPE_TXT         16
102 #define QTYPE_SRV         33
103 #define QTYPE_ANY         255
104
105 #define RRTYPE_A          QTYPE_A
106 #define RRTYPE_NS         QTYPE_NS
107 #define RRTYPE_CNAME      QTYPE_CNAME
108 #define RRTYPE_PTR        QTYPE_PTR
109 #define RRTYPE_TXT        QTYPE_TXT
110 #define RRTYPE_SRV        QTYPE_SRV
111
112
113 /*!
114  * \brief DNS question structure type.
115  *
116  * Contains the name, query type and class
117  */
118 typedef struct
119 {
120     char    *name;
121     uint16_t type;
122     uint16_t class;
123 } DNSQUESTION;
124
125
126 /*!
127  * \brief DNS ressource record structure type.
128  */
129 typedef struct
130 {
131     char    *name;
132     uint16_t type;
133     uint16_t class;
134     uint32_t ttl;
135     uint16_t rdlength;
136     uint8_t *rdata;
137     union {
138         struct {
139             uint32_t ip;
140             char *name;
141         } a;
142         struct {
143             char *name;
144         } ns;
145         struct {
146             char *name;
147         } cname;
148         struct {
149             char *name;
150         } ptr;
151         struct {
152             uint16_t priority;
153             uint16_t weight;
154             uint16_t port;
155             char *name;
156         } srv;
157     } known;
158 } DNSRESOURCE;
159
160
161 /*!
162  * \brief DNS message structure type.
163  */
164 typedef struct
165 {
166     /* external data */
167     uint16_t id;
168     struct {
169         uint16_t qr:1;
170         uint16_t opcode:4;
171         uint16_t aa:1;
172         uint16_t tc:1;
173         uint16_t rd:1;
174         uint16_t ra:1;
175         uint16_t z:3;
176         uint16_t rcode:4;
177     } header;
178     uint16_t     qdcount;
179     uint16_t     ancount;
180     uint16_t     nscount;
181     uint16_t     arcount;
182     DNSQUESTION *qd;
183     DNSRESOURCE *an;
184     DNSRESOURCE *ns;
185     DNSRESOURCE *ar;
186
187     /* internal variables */
188     uint8_t *buf;
189     uint8_t *labels[MAX_LABEL];
190     int len;
191     int label;
192
193     /* Padding data */
194     uint8_t packet[MAX_PACKET_LEN];
195 } DNSMESSAGE;
196
197 /* Conversion of the network byte order buffer content into uint16_t or uint32_t values.
198  * Buffer pointer is incremented accordingly
199  */
200 uint16_t DnsNet2Short(uint8_t **buf);
201 uint32_t DnsNet2Long(uint8_t **buf);
202
203 /* Copies a short or long value in host byte order to the buffer and convert it to network byte order */
204 void DnsShort2Net(uint16_t val, uint8_t **buf);
205 void DnsLong2net(uint32_t val, uint8_t **buf);
206
207 /* Parsing function to parse a DNS packet into message format. Therfor the packet
208    must be at least MAX_PACKET_LEN bytes in size and must be allocated clean (zeroed)
209  */
210 void DnsParseMsg(DNSMESSAGE *msg, uint8_t *packet);
211
212 /* Create a message for sending out on the network. */
213 DNSMESSAGE *DnsCreateMsg(void);
214
215 /* Append a question to the message buffer */
216 void DnsMsgAdd_qd(DNSMESSAGE *msg, char *name, uint16_t type, uint16_t class);
217
218 /* Append a resource record to the message. Call these functions in the below order. */
219 void DnsMsgAdd_an(DNSMESSAGE *msg, char *name, uint16_t type, uint16_t class, uint32_t ttl);
220 void DnsMsgAdd_ns(DNSMESSAGE *msg, char *name, uint16_t type, uint16_t class, uint32_t ttl);
221 void DnsMsgAdd_ar(DNSMESSAGE *msg, char *name, uint16_t type, uint16_t class, uint32_t ttl);
222
223 /* Append resource data types blocks */
224 void DnsMsgAdd_rdata_long(DNSMESSAGE *msg, uint32_t val);
225 void DnsMsgAdd_rdata_name(DNSMESSAGE *msg, char *name);
226 void DnsMsgAdd_rdata_srv(DNSMESSAGE *msg, uint16_t priority, uint16_t weight, uint16_t port, char *name);
227 void DnsMsgAdd_rdata_raw(DNSMESSAGE *msg, uint8_t *rdata, uint16_t rdlength);
228
229 /* Generate the message packet to be send out and return the length */
230 uint8_t *DnsMsg2Pkt(DNSMESSAGE *msg);
231 int DnsMsgLen(DNSMESSAGE *msg);
232
233
234 /*@}*/
235 #endif