cache: fix host length calculation
[project/mdnsd.git] / dns.h
1 /*
2  * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #ifndef _DNS_H__
15 #define _DNS_H__
16
17 #define FLAG_RESPONSE           0x8000
18 #define FLAG_AUTHORATIVE        0x0400
19
20 #define TYPE_A                  0x0001
21 #define TYPE_PTR                0x000C
22 #define TYPE_TXT                0x0010
23 #define TYPE_AAAA               0x001c
24 #define TYPE_SRV                0x0021
25 #define TYPE_ANY                0x00ff
26
27 #define IS_COMPRESSED(x)        ((x & 0xc0) == 0xc0)
28
29 #define MCAST_ADDR              "224.0.0.251"
30 #define MCAST_PORT              5353
31
32 #define CLASS_FLUSH             0x8000
33 #define CLASS_IN                0x0001
34
35 #define MAX_NAME_LEN            8096
36 #define MAX_DATA_LEN            8096
37
38 #define C_DNS_SD                "_services._dns-sd._udp.local"
39
40 struct dns_header {
41         uint16_t id;
42         uint16_t flags;
43         uint16_t questions;
44         uint16_t answers;
45         uint16_t authority;
46         uint16_t additional;
47 };
48
49 struct dns_srv_data {
50         uint16_t priority;
51         uint16_t weight;
52         uint16_t port;
53 } __attribute__((packed, aligned(2)));
54
55 struct dns_answer {
56         uint16_t type;
57         uint16_t class;
58         uint32_t ttl;
59         uint16_t rdlength;
60 } __attribute__((packed, aligned(2)));
61
62 struct dns_question {
63         uint16_t type;
64         uint16_t class;
65 } __attribute__((packed, aligned(2)));
66
67 extern char rdata_buffer[MAX_DATA_LEN + 1];
68
69 extern void dns_send_question(struct uloop_fd *u, const char *question, int type);
70 extern void dns_init_answer(void);
71 extern void dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength);
72 extern void dns_send_answer(struct uloop_fd *u, const char *answer);
73 extern char* dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len);
74 extern struct dns_answer* dns_consume_answer(uint8_t **data, int *len);
75 extern struct dns_question* dns_consume_question(uint8_t **data, int *len);
76 extern struct dns_header* dns_consume_header(uint8_t **data, int *len);
77 extern const char* dns_type_string(uint16_t type);
78
79 #endif