summaryrefslogtreecommitdiff
path: root/src/include/lwip/apps/mdns_priv.h
blob: 5209ba00430902a57ca06ac45ad82efb8fc3c4f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
 * @file
 * MDNS responder private definitions
 */

 /*
 * Copyright (c) 2015 Verisure Innovation AB
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * This file is part of the lwIP TCP/IP stack.
 *
 * Author: Erik Ekman <erik@kryo.se>
 * Author: Jasper Verschueren <jasper.verschueren@apart-audio.com>
 *
 */
#ifndef LWIP_HDR_MDNS_PRIV_H
#define LWIP_HDR_MDNS_PRIV_H

#include "lwip/apps/mdns.h"
#include "lwip/apps/mdns_opts.h"
#include "lwip/pbuf.h"

#ifdef __cplusplus
extern "C" {
#endif

#if LWIP_MDNS_RESPONDER

#define MDNS_READNAME_ERROR 0xFFFF
#define NUM_DOMAIN_OFFSETS 10

#define SRV_PRIORITY 0
#define SRV_WEIGHT   0

/* mDNS TTL: (RFC6762 section 10)
 *  - 120 seconds if the hostname appears somewhere in the RR
 *  - 75 minutes if not (4500 seconds)
 *  - 10 seconds if responding to a legacy query
 */
#define MDNS_TTL_10    10
#define MDNS_TTL_120   120
#define MDNS_TTL_4500  4500

/* RFC6762 section 8.1: If fifteen conflicts occur within any ten-second period,
 * then the host MUST wait at least five seconds before each successive
 * additional probe attempt.
 */
#define MDNS_PROBE_MAX_CONFLICTS_BEFORE_RATE_LIMIT  15
#define MDNS_PROBE_MAX_CONFLICTS_TIME_WINDOW        10000
#define MDNS_PROBE_MAX_CONFLICTS_TIMEOUT            5000

#if LWIP_MDNS_SEARCH
/** Description of a search request */
struct mdns_request {
  /** Name of service, like 'myweb' */
  char name[MDNS_LABEL_MAXLEN + 1];
  /** Type of service, like '_http' or '_services._dns-sd' */
  struct mdns_domain service;
  /** Callback function called for each response */
  search_result_fn_t result_fn;
  void *arg;
  /** Protocol, TCP or UDP */
  u16_t proto;
  /** Query type (PTR, SRV, ...) */
  u8_t qtype;
  /** PTR only request. */
  u16_t only_ptr;
};
#endif

/** Description of a service */
struct mdns_service {
  /** TXT record to answer with */
  struct mdns_domain txtdata;
  /** Name of service, like 'myweb' */
  char name[MDNS_LABEL_MAXLEN + 1];
  /** Type of service, like '_http' */
  char service[MDNS_LABEL_MAXLEN + 1];
  /** Callback function and userdata
   * to update txtdata buffer */
  service_get_txt_fn_t txt_fn;
  void *txt_userdata;
  /** Protocol, TCP or UDP */
  u16_t proto;
  /** Port of the service */
  u16_t port;
};

/** mDNS output packet */
struct mdns_outpacket {
  /** Packet data */
  struct pbuf *pbuf;
  /** Current write offset in packet */
  u16_t write_offset;
  /** Number of questions written */
  u16_t questions;
  /** Number of normal answers written */
  u16_t answers;
  /** Number of authoritative answers written */
  u16_t authoritative;
  /** Number of additional answers written */
  u16_t additional;
  /** Offsets for written domain names in packet.
   *  Used for compression */
  u16_t domain_offsets[NUM_DOMAIN_OFFSETS];
};

/** mDNS output message */
struct mdns_outmsg {
  /** Identifier. Used in legacy queries */
  u16_t tx_id;
  /** dns flags */
  u8_t flags;
  /** Destination IP/port if sent unicast */
  ip_addr_t dest_addr;
  u16_t dest_port;
  /** If all answers in packet should set cache_flush bit */
  u8_t cache_flush;
  /** If reply should be sent unicast (as requested) */
  u8_t unicast_reply_requested;
  /** If legacy query. (tx_id needed, and write
   *  question again in reply before answer) */
  u8_t legacy_query;
  /** If the query is a probe msg we need to respond immediately. Independent of
   *  the QU or QM flag. */
  u8_t probe_query_recv;
  /* Question bitmask for host information */
  u8_t host_questions;
  /* Questions bitmask per service */
  u8_t serv_questions[MDNS_MAX_SERVICES];
  /* Reply bitmask for host information */
  u8_t host_replies;
  /* Bitmask for which reverse IPv6 hosts to answer */
  u8_t host_reverse_v6_replies;
  /* Reply bitmask per service */
  u8_t serv_replies[MDNS_MAX_SERVICES];
#ifdef LWIP_MDNS_SEARCH
  /** Search query to send */
  struct mdns_request *query;
#endif
};

/** Delayed msg info */
struct mdns_delayed_msg {
  /** Signals if a multicast msg needs to be send out */
  u8_t multicast_msg_waiting;
  /** Multicast timeout for all multicast traffic except probe answers */
  u8_t multicast_timeout;
  /** Multicast timeout only for probe answers */
  u8_t multicast_probe_timeout;
  /** Output msg used for delayed multicast responses */
  struct mdns_outmsg delayed_msg_multicast;
  /** Prefer multicast over unicast timeout -> 25% of TTL = we take 30s as
      general delay. */
  u8_t multicast_timeout_25TTL;
  /** Only send out new unicast message if previous was send */
  u8_t unicast_msg_in_use;
  /** Output msg used for delayed unicast responses */
  struct mdns_outmsg delayed_msg_unicast;
};

/* MDNS states */
typedef enum {
  /* MDNS module is off */
  MDNS_STATE_OFF,
  /* Waiting before probing can be started */
  MDNS_STATE_PROBE_WAIT,
  /* Probing the unique records */
  MDNS_STATE_PROBING,
  /* Waiting before announcing the probed unique records */
  MDNS_STATE_ANNOUNCE_WAIT,
  /* Announcing all records */
  MDNS_STATE_ANNOUNCING,
  /* Probing and announcing completed */
  MDNS_STATE_COMPLETE
} mdns_resp_state_enum_t;

/** Description of a host/netif */
struct mdns_host {
  /** Hostname */
  char name[MDNS_LABEL_MAXLEN + 1];
  /** Pointer to services */
  struct mdns_service *services[MDNS_MAX_SERVICES];
  /** Number of probes/announces sent for the current name */
  u8_t sent_num;
  /** State of the mdns responder */
  mdns_resp_state_enum_t state;
#if LWIP_IPV4
  /** delayed msg struct for IPv4 */
  struct mdns_delayed_msg ipv4;
#endif
#if LWIP_IPV6
  /** delayed msg struct for IPv6 */
  struct mdns_delayed_msg ipv6;
#endif
  /** Timestamp of probe conflict saved in list */
  u32_t conflict_time[MDNS_PROBE_MAX_CONFLICTS_BEFORE_RATE_LIMIT];
  /** Rate limit flag */
  u8_t rate_limit_activated;
  /** List index for timestamps */
  u8_t index;
  /** number of conflicts since startup */
  u8_t num_conflicts;
};

struct mdns_host* netif_mdns_data(struct netif *netif);
struct udp_pcb* get_mdns_pcb(void);

#endif /* LWIP_MDNS_RESPONDER */

#ifdef __cplusplus
}
#endif

#endif /* LWIP_HDR_MDNS_PRIV_H */