Bonjour/mDNS lib, works with the new network stack

Fork of BonjourLib by Dirk-Willem van Gulik (NXP/mbed)

Committer:
Jasper
Date:
Fri May 30 09:43:56 2014 +0000
Revision:
3:b8a78d6da192
Parent:
2:18d443d86057
fix A records. some more debugging.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dirkx 0:1a198985f183 1 #ifndef MDNS_RESPONDER_H
dirkx 0:1a198985f183 2 #define MDNS_RESPONDER_H
dirkx 0:1a198985f183 3
Jasper 2:18d443d86057 4 #include "lwip/lwipopts.h"
dirkx 0:1a198985f183 5
dirkx 0:1a198985f183 6 #if LWIP_DNS /* don't build if not configured for use in lwipopts.h */
dirkx 0:1a198985f183 7
Jasper 2:18d443d86057 8 #include "EthernetInterface.h"
dirkx 0:1a198985f183 9 #include "mbed.h"
Jasper 2:18d443d86057 10 #include "rtos.h"
dirkx 0:1a198985f183 11
dirkx 0:1a198985f183 12 #include "lwip/dns.h"
dirkx 0:1a198985f183 13
dirkx 0:1a198985f183 14 // As defined by IANA.
dirkx 0:1a198985f183 15 //
dirkx 0:1a198985f183 16 #define MDNS_PORT (5353)
Jasper 2:18d443d86057 17 #define MCAST "224.0.0.251"
dirkx 0:1a198985f183 18
dirkx 0:1a198985f183 19 // How long we announce our IP and URLs to be valid. Should
dirkx 0:1a198985f183 20 // ideally be 1/3 or so of the DHCP lease time. But we do
dirkx 0:1a198985f183 21 // not know that.
dirkx 0:1a198985f183 22 //
dirkx 0:1a198985f183 23 #define MDNS_TTL ( 100 )
dirkx 0:1a198985f183 24
dirkx 0:1a198985f183 25 // Rebroadcast our details more regularly than the TTL as
dirkx 0:1a198985f183 26 // we otherwise get dropped.
dirkx 0:1a198985f183 27 //
dirkx 0:1a198985f183 28 #ifndef MDNS_INTERVAL
dirkx 0:1a198985f183 29 #define MDNS_INTERVAL ( MDNS_TTL * 2 / 3 )
dirkx 0:1a198985f183 30 #endif
dirkx 0:1a198985f183 31
dirkx 0:1a198985f183 32 #ifndef MDNS_QCACHE
dirkx 0:1a198985f183 33 #define MDNS_QCACHE 1
dirkx 0:1a198985f183 34 #endif
dirkx 0:1a198985f183 35
dirkx 0:1a198985f183 36 #ifndef MDNS_MAXCACHEDNSLABELS
dirkx 0:1a198985f183 37 #define MDNS_MAXCACHEDNSLABELS (10)
dirkx 0:1a198985f183 38 #endif
dirkx 0:1a198985f183 39
Jasper 2:18d443d86057 40 class mDNSResponder {
Jasper 2:18d443d86057 41
dirkx 0:1a198985f183 42 public:
dirkx 0:1a198985f183 43 mDNSResponder();
dirkx 0:1a198985f183 44 virtual ~mDNSResponder();
dirkx 0:1a198985f183 45 void close();
Jasper 2:18d443d86057 46 void announce(ip_addr_t ip, const char * ldn, const char * proto, uint16_t port, const char * name, char ** txts);
Jasper 2:18d443d86057 47
Jasper 2:18d443d86057 48 void Listen(void const *args);
Jasper 2:18d443d86057 49 void periodic(void const *n);
Jasper 2:18d443d86057 50
dirkx 0:1a198985f183 51 private:
dirkx 0:1a198985f183 52 void process(); // Needed for timer, to fire off regular updates.
dirkx 0:1a198985f183 53
dirkx 0:1a198985f183 54 // Main inbound packet handler
Jasper 2:18d443d86057 55 Thread * mdns_listener;
Jasper 2:18d443d86057 56 RtosTimer * mdns_announcer;
Jasper 2:18d443d86057 57
dirkx 0:1a198985f183 58
dirkx 0:1a198985f183 59 // 3 functions to keep a QName chace. If enabled with MDNS_QCACHE
dirkx 0:1a198985f183 60 void initLabelCache(char * off);
dirkx 0:1a198985f183 61 uint16_t checkLabelCache(char * name);
dirkx 0:1a198985f183 62 void addLabelCache(char * p, char * name);
dirkx 0:1a198985f183 63
dirkx 0:1a198985f183 64 // Handing functions
dirkx 0:1a198985f183 65 char * breakname(char *p, char *name); // Break FQDNs at the dots, understanding QCaching.
dirkx 0:1a198985f183 66
dirkx 0:1a198985f183 67 // reply building functions; see RFC for names/defintion.
dirkx 0:1a198985f183 68 char * mRR(char *p, char * name, uint16_t tpe, uint16_t cls, uint32_t ttl);
dirkx 0:1a198985f183 69 char * mRRLABEL(char *p, char * name, uint16_t tpe, char ** rr);
dirkx 0:1a198985f183 70 char * mPTR(char *p, char * name, char * r);
dirkx 0:1a198985f183 71 char * mTXT(char *p, char * name, char ** rr);
Jasper 2:18d443d86057 72 char * mARR(char *p, char * name, ip_addr_t ip);
dirkx 0:1a198985f183 73 char * mSRV(char *p, char * name, uint16_t port, char * rr);
dirkx 0:1a198985f183 74 char * qANY(char *p, char * name, uint16_t tpe, uint16_t cls);
dirkx 0:1a198985f183 75
dirkx 0:1a198985f183 76 // build query part of a reply.
dirkx 0:1a198985f183 77 char * qPTR(char *p, char *name);
dirkx 0:1a198985f183 78 char * qA(char *p, char *name);
dirkx 0:1a198985f183 79 char * qSRV(char *p, char *name);
dirkx 0:1a198985f183 80
Jasper 2:18d443d86057 81 // Periodic announcements
Jasper 2:18d443d86057 82 void sendAnnounce(void);
Jasper 2:18d443d86057 83
dirkx 0:1a198985f183 84 // Sent out the actual DNS packet.
Jasper 2:18d443d86057 85 void sendReply(uint16_t t, uint16_t tid, Endpoint *dst);
dirkx 0:1a198985f183 86
dirkx 0:1a198985f183 87 // Decode the query part of a DNS inbound packet.
dirkx 0:1a198985f183 88 char * decodeQBlock(char * udp, int len, char *p, char * fqdn, int fqdn_size, uint32_t *Qclass, uint32_t *Qtype);
dirkx 0:1a198985f183 89
dirkx 0:1a198985f183 90 PACK_STRUCT_BEGIN
dirkx 0:1a198985f183 91 struct DNSPacket
dirkx 0:1a198985f183 92 {
dirkx 0:1a198985f183 93 PACK_STRUCT_FIELD(uint16_t tid);
dirkx 0:1a198985f183 94 PACK_STRUCT_FIELD(uint16_t flags);
dirkx 0:1a198985f183 95 PACK_STRUCT_FIELD(uint16_t question_count);
dirkx 0:1a198985f183 96 PACK_STRUCT_FIELD(uint16_t answer_count);
dirkx 0:1a198985f183 97 PACK_STRUCT_FIELD(uint16_t a_count);
dirkx 0:1a198985f183 98 PACK_STRUCT_FIELD(uint16_t aa_count);
dirkx 0:1a198985f183 99 PACK_STRUCT_FIELD(char answers);
dirkx 0:1a198985f183 100 } PACK_STRUCT_STRUCT;
dirkx 0:1a198985f183 101 PACK_STRUCT_END
dirkx 0:1a198985f183 102
dirkx 0:1a198985f183 103 // Services
dirkx 0:1a198985f183 104 char ** moi_txt; /* Null terminated list */
dirkx 0:1a198985f183 105
dirkx 0:1a198985f183 106 // Endpoint
dirkx 0:1a198985f183 107 uint16_t moi_port;
Jasper 2:18d443d86057 108 ip_addr_t moi_ip;
dirkx 0:1a198985f183 109
dirkx 0:1a198985f183 110 // Local name construction.
dirkx 0:1a198985f183 111 const char * moi_name, * moi_proto;
dirkx 0:1a198985f183 112 char * moi_local_proto, * moi_local_name, * moi_local_pglue;
dirkx 0:1a198985f183 113
Jasper 2:18d443d86057 114 // buffer;
Jasper 2:18d443d86057 115 char space[1500];
Jasper 2:18d443d86057 116
Jasper 2:18d443d86057 117 // Timer announcer;
dirkx 0:1a198985f183 118
dirkx 0:1a198985f183 119 UDPSocket* m_pUDPSocket;
Jasper 2:18d443d86057 120 Endpoint* multicast_group;
dirkx 0:1a198985f183 121
dirkx 0:1a198985f183 122 // For the QCache
dirkx 0:1a198985f183 123 #ifdef MDNS_QCACHE
dirkx 0:1a198985f183 124 int labelCacheCnt;
dirkx 0:1a198985f183 125 char * labelCacheOffset;
dirkx 0:1a198985f183 126
dirkx 0:1a198985f183 127 uint16_t _cache_off[MDNS_MAXCACHEDNSLABELS];
dirkx 0:1a198985f183 128 char * _cache_och[MDNS_MAXCACHEDNSLABELS];
dirkx 0:1a198985f183 129 #endif
dirkx 0:1a198985f183 130 };
dirkx 0:1a198985f183 131
dirkx 0:1a198985f183 132
dirkx 0:1a198985f183 133
dirkx 0:1a198985f183 134 #endif
dirkx 0:1a198985f183 135
dirkx 0:1a198985f183 136 // Just in case - there is prolly some nice ARM6 assembler already linked in.
dirkx 0:1a198985f183 137 //
dirkx 0:1a198985f183 138 #ifndef htons
dirkx 0:1a198985f183 139 #define htons( x ) ( (( x << 8 ) & 0xFF00) | (( x >> 8 ) & 0x00FF) )
dirkx 0:1a198985f183 140 #define ntohs( x ) (htons(x))
dirkx 0:1a198985f183 141 #endif
dirkx 0:1a198985f183 142
dirkx 0:1a198985f183 143 #ifndef htonl
dirkx 0:1a198985f183 144 #define htonl( x ) ( (( x << 24 ) & 0xff000000) \
dirkx 0:1a198985f183 145 | (( x << 8 ) & 0x00ff0000) \
dirkx 0:1a198985f183 146 | (( x >> 8 ) & 0x0000ff00) \
dirkx 0:1a198985f183 147 | (( x >> 24 ) & 0x000000ff) )
dirkx 0:1a198985f183 148 #define ntohl( x ) (htonl(x))
dirkx 0:1a198985f183 149 #endif
dirkx 0:1a198985f183 150
dirkx 0:1a198985f183 151 #endif