Webserver+3d print

Dependents:   Nucleo

Committer:
Sergunb
Date:
Sat Feb 04 18:15:49 2017 +0000
Revision:
0:8918a71cdbe9
nothing else

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sergunb 0:8918a71cdbe9 1 /**
Sergunb 0:8918a71cdbe9 2 * @file mdns_responder.h
Sergunb 0:8918a71cdbe9 3 * @brief mDNS responder (Multicast DNS)
Sergunb 0:8918a71cdbe9 4 *
Sergunb 0:8918a71cdbe9 5 * @section License
Sergunb 0:8918a71cdbe9 6 *
Sergunb 0:8918a71cdbe9 7 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
Sergunb 0:8918a71cdbe9 8 *
Sergunb 0:8918a71cdbe9 9 * This file is part of CycloneTCP Open.
Sergunb 0:8918a71cdbe9 10 *
Sergunb 0:8918a71cdbe9 11 * This program is free software; you can redistribute it and/or
Sergunb 0:8918a71cdbe9 12 * modify it under the terms of the GNU General Public License
Sergunb 0:8918a71cdbe9 13 * as published by the Free Software Foundation; either version 2
Sergunb 0:8918a71cdbe9 14 * of the License, or (at your option) any later version.
Sergunb 0:8918a71cdbe9 15 *
Sergunb 0:8918a71cdbe9 16 * This program is distributed in the hope that it will be useful,
Sergunb 0:8918a71cdbe9 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Sergunb 0:8918a71cdbe9 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Sergunb 0:8918a71cdbe9 19 * GNU General Public License for more details.
Sergunb 0:8918a71cdbe9 20 *
Sergunb 0:8918a71cdbe9 21 * You should have received a copy of the GNU General Public License
Sergunb 0:8918a71cdbe9 22 * along with this program; if not, write to the Free Software Foundation,
Sergunb 0:8918a71cdbe9 23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Sergunb 0:8918a71cdbe9 24 *
Sergunb 0:8918a71cdbe9 25 * @author Oryx Embedded SARL (www.oryx-embedded.com)
Sergunb 0:8918a71cdbe9 26 * @version 1.7.6
Sergunb 0:8918a71cdbe9 27 **/
Sergunb 0:8918a71cdbe9 28
Sergunb 0:8918a71cdbe9 29 #ifndef _MDNS_RESPONDER_H
Sergunb 0:8918a71cdbe9 30 #define _MDNS_RESPONDER_H
Sergunb 0:8918a71cdbe9 31
Sergunb 0:8918a71cdbe9 32 //Dependencies
Sergunb 0:8918a71cdbe9 33 #include "core/net.h"
Sergunb 0:8918a71cdbe9 34 #include "core/udp.h"
Sergunb 0:8918a71cdbe9 35 #include "dns/dns_common.h"
Sergunb 0:8918a71cdbe9 36 #include "mdns/mdns_common.h"
Sergunb 0:8918a71cdbe9 37
Sergunb 0:8918a71cdbe9 38 //mDNS responder support
Sergunb 0:8918a71cdbe9 39 #ifndef MDNS_RESPONDER_SUPPORT
Sergunb 0:8918a71cdbe9 40 #define MDNS_RESPONDER_SUPPORT DISABLED
Sergunb 0:8918a71cdbe9 41 #elif (MDNS_RESPONDER_SUPPORT != ENABLED && MDNS_RESPONDER_SUPPORT != DISABLED)
Sergunb 0:8918a71cdbe9 42 #error MDNS_RESPONDER_SUPPORT parameter is not valid
Sergunb 0:8918a71cdbe9 43 #endif
Sergunb 0:8918a71cdbe9 44
Sergunb 0:8918a71cdbe9 45 //mDNS responder tick interval
Sergunb 0:8918a71cdbe9 46 #ifndef MDNS_RESPONDER_TICK_INTERVAL
Sergunb 0:8918a71cdbe9 47 #define MDNS_RESPONDER_TICK_INTERVAL 250
Sergunb 0:8918a71cdbe9 48 #elif (MDNS_RESPONDER_TICK_INTERVAL < 10)
Sergunb 0:8918a71cdbe9 49 #error MDNS_RESPONDER_TICK_INTERVAL parameter is not valid
Sergunb 0:8918a71cdbe9 50 #endif
Sergunb 0:8918a71cdbe9 51
Sergunb 0:8918a71cdbe9 52 //Maximum length of host name
Sergunb 0:8918a71cdbe9 53 #ifndef MDNS_RESPONDER_MAX_HOSTNAME_LEN
Sergunb 0:8918a71cdbe9 54 #define MDNS_RESPONDER_MAX_HOSTNAME_LEN 32
Sergunb 0:8918a71cdbe9 55 #elif (MDNS_RESPONDER_MAX_HOSTNAME_LEN < 1)
Sergunb 0:8918a71cdbe9 56 #error MDNS_RESPONDER_MAX_HOSTNAME_LEN parameter is not valid
Sergunb 0:8918a71cdbe9 57 #endif
Sergunb 0:8918a71cdbe9 58
Sergunb 0:8918a71cdbe9 59 //Maximum waiting delay
Sergunb 0:8918a71cdbe9 60 #ifndef MDNS_MAX_WAITING_DELAY
Sergunb 0:8918a71cdbe9 61 #define MDNS_MAX_WAITING_DELAY 10000
Sergunb 0:8918a71cdbe9 62 #elif (MDNS_MAX_WAITING_DELAY < 0)
Sergunb 0:8918a71cdbe9 63 #error MDNS_MAX_WAITING_DELAY parameter is not valid
Sergunb 0:8918a71cdbe9 64 #endif
Sergunb 0:8918a71cdbe9 65
Sergunb 0:8918a71cdbe9 66 //Initial random delay (minimum value)
Sergunb 0:8918a71cdbe9 67 #ifndef MDNS_RAND_DELAY_MIN
Sergunb 0:8918a71cdbe9 68 #define MDNS_RAND_DELAY_MIN 0
Sergunb 0:8918a71cdbe9 69 #elif (MDNS_RAND_DELAY_MIN < 0)
Sergunb 0:8918a71cdbe9 70 #error MDNS_RAND_DELAY_MIN parameter is not valid
Sergunb 0:8918a71cdbe9 71 #endif
Sergunb 0:8918a71cdbe9 72
Sergunb 0:8918a71cdbe9 73 //Initial random delay (maximum value)
Sergunb 0:8918a71cdbe9 74 #ifndef MDNS_RAND_DELAY_MAX
Sergunb 0:8918a71cdbe9 75 #define MDNS_RAND_DELAY_MAX 250
Sergunb 0:8918a71cdbe9 76 #elif (MDNS_RAND_DELAY_MAX < 0)
Sergunb 0:8918a71cdbe9 77 #error MDNS_RAND_DELAY_MAX parameter is not valid
Sergunb 0:8918a71cdbe9 78 #endif
Sergunb 0:8918a71cdbe9 79
Sergunb 0:8918a71cdbe9 80 //Number of probe packets
Sergunb 0:8918a71cdbe9 81 #ifndef MDNS_PROBE_NUM
Sergunb 0:8918a71cdbe9 82 #define MDNS_PROBE_NUM 3
Sergunb 0:8918a71cdbe9 83 #elif (MDNS_PROBE_NUM < 1)
Sergunb 0:8918a71cdbe9 84 #error MDNS_PROBE_NUM parameter is not valid
Sergunb 0:8918a71cdbe9 85 #endif
Sergunb 0:8918a71cdbe9 86
Sergunb 0:8918a71cdbe9 87 //Time interval between subsequent probe packets
Sergunb 0:8918a71cdbe9 88 #ifndef MDNS_PROBE_DELAY
Sergunb 0:8918a71cdbe9 89 #define MDNS_PROBE_DELAY 250
Sergunb 0:8918a71cdbe9 90 #elif (MDNS_PROBE_DELAY < 100)
Sergunb 0:8918a71cdbe9 91 #error MDNS_PROBE_DELAY parameter is not valid
Sergunb 0:8918a71cdbe9 92 #endif
Sergunb 0:8918a71cdbe9 93
Sergunb 0:8918a71cdbe9 94 //Delay before probing again when deferring to the winning host
Sergunb 0:8918a71cdbe9 95 #ifndef MDNS_PROBE_DEFER
Sergunb 0:8918a71cdbe9 96 #define MDNS_PROBE_DEFER 1000
Sergunb 0:8918a71cdbe9 97 #elif (MDNS_PROBE_DEFER < 100)
Sergunb 0:8918a71cdbe9 98 #error MDNS_PROBE_DEFER parameter is not valid
Sergunb 0:8918a71cdbe9 99 #endif
Sergunb 0:8918a71cdbe9 100
Sergunb 0:8918a71cdbe9 101 //Number of announcement packets
Sergunb 0:8918a71cdbe9 102 #ifndef MDNS_ANNOUNCE_NUM
Sergunb 0:8918a71cdbe9 103 #define MDNS_ANNOUNCE_NUM 2
Sergunb 0:8918a71cdbe9 104 #elif (MDNS_ANNOUNCE_NUM < 1)
Sergunb 0:8918a71cdbe9 105 #error MDNS_ANNOUNCE_NUM parameter is not valid
Sergunb 0:8918a71cdbe9 106 #endif
Sergunb 0:8918a71cdbe9 107
Sergunb 0:8918a71cdbe9 108 //Time interval between subsequent announcement packets
Sergunb 0:8918a71cdbe9 109 #ifndef MDNS_ANNOUNCE_DELAY
Sergunb 0:8918a71cdbe9 110 #define MDNS_ANNOUNCE_DELAY 1000
Sergunb 0:8918a71cdbe9 111 #elif (MDNS_ANNOUNCE_DELAY < 100)
Sergunb 0:8918a71cdbe9 112 #error MDNS_ANNOUNCE_DELAY parameter is not valid
Sergunb 0:8918a71cdbe9 113 #endif
Sergunb 0:8918a71cdbe9 114
Sergunb 0:8918a71cdbe9 115 //Forward declaration of DnsSdContext structure
Sergunb 0:8918a71cdbe9 116 struct _MdnsResponderContext;
Sergunb 0:8918a71cdbe9 117 #define MdnsResponderContext struct _MdnsResponderContext
Sergunb 0:8918a71cdbe9 118
Sergunb 0:8918a71cdbe9 119
Sergunb 0:8918a71cdbe9 120 /**
Sergunb 0:8918a71cdbe9 121 * @brief mDNS responder states
Sergunb 0:8918a71cdbe9 122 **/
Sergunb 0:8918a71cdbe9 123
Sergunb 0:8918a71cdbe9 124 typedef enum
Sergunb 0:8918a71cdbe9 125 {
Sergunb 0:8918a71cdbe9 126 MDNS_STATE_INIT,
Sergunb 0:8918a71cdbe9 127 MDNS_STATE_WAITING,
Sergunb 0:8918a71cdbe9 128 MDNS_STATE_PROBING,
Sergunb 0:8918a71cdbe9 129 MDNS_STATE_ANNOUNCING,
Sergunb 0:8918a71cdbe9 130 MDNS_STATE_IDLE
Sergunb 0:8918a71cdbe9 131 } MdnsState;
Sergunb 0:8918a71cdbe9 132
Sergunb 0:8918a71cdbe9 133
Sergunb 0:8918a71cdbe9 134 /**
Sergunb 0:8918a71cdbe9 135 * @brief FSM state change callback
Sergunb 0:8918a71cdbe9 136 **/
Sergunb 0:8918a71cdbe9 137
Sergunb 0:8918a71cdbe9 138 typedef void (*MdnsResponderStateChangeCallback)(MdnsResponderContext *context,
Sergunb 0:8918a71cdbe9 139 NetInterface *interface, MdnsState state);
Sergunb 0:8918a71cdbe9 140
Sergunb 0:8918a71cdbe9 141
Sergunb 0:8918a71cdbe9 142 /**
Sergunb 0:8918a71cdbe9 143 * @brief mDNS responder settings
Sergunb 0:8918a71cdbe9 144 **/
Sergunb 0:8918a71cdbe9 145
Sergunb 0:8918a71cdbe9 146 typedef struct
Sergunb 0:8918a71cdbe9 147 {
Sergunb 0:8918a71cdbe9 148 NetInterface *interface; ///<Underlying network interface
Sergunb 0:8918a71cdbe9 149 uint_t numAnnouncements; ///<Number of announcement packets
Sergunb 0:8918a71cdbe9 150 uint32_t ttl; ///<TTL resource record
Sergunb 0:8918a71cdbe9 151 MdnsResponderStateChangeCallback stateChangeEvent; ///<FSM state change event
Sergunb 0:8918a71cdbe9 152 } MdnsResponderSettings;
Sergunb 0:8918a71cdbe9 153
Sergunb 0:8918a71cdbe9 154
Sergunb 0:8918a71cdbe9 155 /**
Sergunb 0:8918a71cdbe9 156 * @brief mDNS responder context
Sergunb 0:8918a71cdbe9 157 **/
Sergunb 0:8918a71cdbe9 158
Sergunb 0:8918a71cdbe9 159 struct _MdnsResponderContext
Sergunb 0:8918a71cdbe9 160 {
Sergunb 0:8918a71cdbe9 161 MdnsResponderSettings settings; ///<DNS-SD settings
Sergunb 0:8918a71cdbe9 162 bool_t running; ///<mDNS responder is currently running
Sergunb 0:8918a71cdbe9 163 MdnsState state; ///<FSM state
Sergunb 0:8918a71cdbe9 164 bool_t conflict; ///<Conflict detected
Sergunb 0:8918a71cdbe9 165 bool_t tieBreakLost; ///<Tie-break lost
Sergunb 0:8918a71cdbe9 166 systime_t timestamp; ///<Timestamp to manage retransmissions
Sergunb 0:8918a71cdbe9 167 systime_t timeout; ///<Timeout value
Sergunb 0:8918a71cdbe9 168 uint_t retransmitCount; ///<Retransmission counter
Sergunb 0:8918a71cdbe9 169 char_t hostname[MDNS_RESPONDER_MAX_HOSTNAME_LEN + 1]; ///<Hostname
Sergunb 0:8918a71cdbe9 170 #if (IPV4_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 171 char_t ipv4ReverseName[DNS_MAX_IPV4_REVERSE_NAME_LEN + 1]; ///<Reverse DNS lookup for IPv4
Sergunb 0:8918a71cdbe9 172 MdnsMessage ipv4Response; ///<IPv4 response message
Sergunb 0:8918a71cdbe9 173 #endif
Sergunb 0:8918a71cdbe9 174 #if (IPV6_SUPPORT == ENABLED)
Sergunb 0:8918a71cdbe9 175 char_t ipv6ReverseName[DNS_MAX_IPV6_REVERSE_NAME_LEN + 1]; ///<Reverse DNS lookup for IPv6
Sergunb 0:8918a71cdbe9 176 MdnsMessage ipv6Response; ///<IPv6 response message
Sergunb 0:8918a71cdbe9 177 #endif
Sergunb 0:8918a71cdbe9 178 };
Sergunb 0:8918a71cdbe9 179
Sergunb 0:8918a71cdbe9 180
Sergunb 0:8918a71cdbe9 181 //Tick counter to handle periodic operations
Sergunb 0:8918a71cdbe9 182 extern systime_t mdnsResponderTickCounter;
Sergunb 0:8918a71cdbe9 183
Sergunb 0:8918a71cdbe9 184 //mDNS related functions
Sergunb 0:8918a71cdbe9 185 void mdnsResponderGetDefaultSettings(MdnsResponderSettings *settings);
Sergunb 0:8918a71cdbe9 186
Sergunb 0:8918a71cdbe9 187 error_t mdnsResponderInit(MdnsResponderContext *context,
Sergunb 0:8918a71cdbe9 188 const MdnsResponderSettings *settings);
Sergunb 0:8918a71cdbe9 189
Sergunb 0:8918a71cdbe9 190 error_t mdnsResponderStart(MdnsResponderContext *context);
Sergunb 0:8918a71cdbe9 191 error_t mdnsResponderStop(MdnsResponderContext *context);
Sergunb 0:8918a71cdbe9 192 MdnsState mdnsResponderGetState(MdnsResponderContext *context);
Sergunb 0:8918a71cdbe9 193
Sergunb 0:8918a71cdbe9 194 error_t mdnsResponderSetHostname(MdnsResponderContext *context,
Sergunb 0:8918a71cdbe9 195 const char_t *hostname);
Sergunb 0:8918a71cdbe9 196
Sergunb 0:8918a71cdbe9 197 error_t mdnsResponderSetIpv4ReverseName(MdnsResponderContext *context);
Sergunb 0:8918a71cdbe9 198 error_t mdnsResponderSetIpv6ReverseName(MdnsResponderContext *context);
Sergunb 0:8918a71cdbe9 199
Sergunb 0:8918a71cdbe9 200 error_t mdnsResponderStartProbing(MdnsResponderContext *context);
Sergunb 0:8918a71cdbe9 201
Sergunb 0:8918a71cdbe9 202 void mdnsResponderTick(MdnsResponderContext *context);
Sergunb 0:8918a71cdbe9 203 void mdnsResponderLinkChangeEvent(MdnsResponderContext *context);
Sergunb 0:8918a71cdbe9 204
Sergunb 0:8918a71cdbe9 205 void mdnsResponderChangeState(MdnsResponderContext *context,
Sergunb 0:8918a71cdbe9 206 MdnsState newState, systime_t delay);
Sergunb 0:8918a71cdbe9 207
Sergunb 0:8918a71cdbe9 208 void mdnsResponderChangeHostname(MdnsResponderContext *context);
Sergunb 0:8918a71cdbe9 209
Sergunb 0:8918a71cdbe9 210 error_t mdnsResponderSendProbe(MdnsResponderContext *context);
Sergunb 0:8918a71cdbe9 211 error_t mdnsResponderSendAnnouncement(MdnsResponderContext *context);
Sergunb 0:8918a71cdbe9 212 error_t mdnsResponderSendGoodbye(MdnsResponderContext *context);
Sergunb 0:8918a71cdbe9 213
Sergunb 0:8918a71cdbe9 214 void mdnsResponderProcessQuery(NetInterface *interface, MdnsMessage *query);
Sergunb 0:8918a71cdbe9 215
Sergunb 0:8918a71cdbe9 216 error_t mdnsResponderParseQuestion(NetInterface *interface, const MdnsMessage *query,
Sergunb 0:8918a71cdbe9 217 size_t offset, const DnsQuestion *question, MdnsMessage *response);
Sergunb 0:8918a71cdbe9 218
Sergunb 0:8918a71cdbe9 219 void mdnsResponderParseKnownAnRecord(NetInterface *interface, const MdnsMessage *query,
Sergunb 0:8918a71cdbe9 220 size_t queryOffset, const DnsResourceRecord *queryRecord, MdnsMessage *response);
Sergunb 0:8918a71cdbe9 221
Sergunb 0:8918a71cdbe9 222 void mdnsResponderParseNsRecord(NetInterface *interface,
Sergunb 0:8918a71cdbe9 223 const MdnsMessage *query, size_t offset, const DnsResourceRecord *record);
Sergunb 0:8918a71cdbe9 224
Sergunb 0:8918a71cdbe9 225 void mdnsResponderParseAnRecord(NetInterface *interface,
Sergunb 0:8918a71cdbe9 226 const MdnsMessage *response, size_t offset, const DnsResourceRecord *record);
Sergunb 0:8918a71cdbe9 227
Sergunb 0:8918a71cdbe9 228 void mdnsResponderGenerateAdditionalRecords(NetInterface *interface,
Sergunb 0:8918a71cdbe9 229 MdnsMessage *response, bool_t legacyUnicast);
Sergunb 0:8918a71cdbe9 230
Sergunb 0:8918a71cdbe9 231 error_t mdnsResponderAddIpv4AddrRecord(NetInterface *interface,
Sergunb 0:8918a71cdbe9 232 MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
Sergunb 0:8918a71cdbe9 233
Sergunb 0:8918a71cdbe9 234 error_t mdnsResponderAddIpv6AddrRecord(NetInterface *interface,
Sergunb 0:8918a71cdbe9 235 MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
Sergunb 0:8918a71cdbe9 236
Sergunb 0:8918a71cdbe9 237 error_t mdnsResponderAddIpv4ReversePtrRecord(NetInterface *interface,
Sergunb 0:8918a71cdbe9 238 MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
Sergunb 0:8918a71cdbe9 239
Sergunb 0:8918a71cdbe9 240 error_t mdnsResponderAddIpv6ReversePtrRecord(NetInterface *interface,
Sergunb 0:8918a71cdbe9 241 MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
Sergunb 0:8918a71cdbe9 242
Sergunb 0:8918a71cdbe9 243 error_t mdnsResponderAddNsecRecord(NetInterface *interface,
Sergunb 0:8918a71cdbe9 244 MdnsMessage *message, bool_t cacheFlush, uint32_t ttl);
Sergunb 0:8918a71cdbe9 245
Sergunb 0:8918a71cdbe9 246 #endif
Sergunb 0:8918a71cdbe9 247