Bonjour/mDNS lib, works with the new network stack
Fork of BonjourLib by
services/mDNS/mDNSResponder.h@0:1a198985f183, 2010-08-11 (annotated)
- Committer:
- dirkx
- Date:
- Wed Aug 11 09:49:48 2010 +0000
- Revision:
- 0:1a198985f183
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dirkx | 0:1a198985f183 | 1 | #ifndef MDNS_RESPONDER_H |
dirkx | 0:1a198985f183 | 2 | #define MDNS_RESPONDER_H |
dirkx | 0:1a198985f183 | 3 | |
dirkx | 0:1a198985f183 | 4 | |
dirkx | 0:1a198985f183 | 5 | #include "lwip/opt.h" |
dirkx | 0:1a198985f183 | 6 | |
dirkx | 0:1a198985f183 | 7 | #if LWIP_DNS /* don't build if not configured for use in lwipopts.h */ |
dirkx | 0:1a198985f183 | 8 | |
dirkx | 0:1a198985f183 | 9 | #include "if/net/net.h" |
dirkx | 0:1a198985f183 | 10 | #include "api/UDPSocket.h" |
dirkx | 0:1a198985f183 | 11 | #include "api/DNSRequest.h" |
dirkx | 0:1a198985f183 | 12 | #include "mbed.h" |
dirkx | 0:1a198985f183 | 13 | |
dirkx | 0:1a198985f183 | 14 | #include "lwip/udp.h" |
dirkx | 0:1a198985f183 | 15 | #include "lwip/mem.h" |
dirkx | 0:1a198985f183 | 16 | #include "lwip/memp.h" |
dirkx | 0:1a198985f183 | 17 | #include "lwip/dns.h" |
dirkx | 0:1a198985f183 | 18 | |
dirkx | 0:1a198985f183 | 19 | // As defined by IANA. |
dirkx | 0:1a198985f183 | 20 | // |
dirkx | 0:1a198985f183 | 21 | #define MDNS_PORT (5353) |
dirkx | 0:1a198985f183 | 22 | #define MCAST 224,0,0,251 |
dirkx | 0:1a198985f183 | 23 | |
dirkx | 0:1a198985f183 | 24 | // How long we announce our IP and URLs to be valid. Should |
dirkx | 0:1a198985f183 | 25 | // ideally be 1/3 or so of the DHCP lease time. But we do |
dirkx | 0:1a198985f183 | 26 | // not know that. |
dirkx | 0:1a198985f183 | 27 | // |
dirkx | 0:1a198985f183 | 28 | #define MDNS_TTL ( 100 ) |
dirkx | 0:1a198985f183 | 29 | |
dirkx | 0:1a198985f183 | 30 | // Rebroadcast our details more regularly than the TTL as |
dirkx | 0:1a198985f183 | 31 | // we otherwise get dropped. |
dirkx | 0:1a198985f183 | 32 | // |
dirkx | 0:1a198985f183 | 33 | #ifndef MDNS_INTERVAL |
dirkx | 0:1a198985f183 | 34 | #define MDNS_INTERVAL ( MDNS_TTL * 2 / 3 ) |
dirkx | 0:1a198985f183 | 35 | #endif |
dirkx | 0:1a198985f183 | 36 | |
dirkx | 0:1a198985f183 | 37 | #ifndef MDNS_QCACHE |
dirkx | 0:1a198985f183 | 38 | #define MDNS_QCACHE 1 |
dirkx | 0:1a198985f183 | 39 | #endif |
dirkx | 0:1a198985f183 | 40 | |
dirkx | 0:1a198985f183 | 41 | #ifndef MDNS_MAXCACHEDNSLABELS |
dirkx | 0:1a198985f183 | 42 | #define MDNS_MAXCACHEDNSLABELS (10) |
dirkx | 0:1a198985f183 | 43 | #endif |
dirkx | 0:1a198985f183 | 44 | |
dirkx | 0:1a198985f183 | 45 | class mDNSResponder : protected NetService |
dirkx | 0:1a198985f183 | 46 | { |
dirkx | 0:1a198985f183 | 47 | public: |
dirkx | 0:1a198985f183 | 48 | mDNSResponder(); |
dirkx | 0:1a198985f183 | 49 | virtual ~mDNSResponder(); |
dirkx | 0:1a198985f183 | 50 | void close(); |
dirkx | 0:1a198985f183 | 51 | void announce(IpAddr ip, const char * ldn, const char * proto, uint16_t port, const char * name, char ** txts); |
dirkx | 0:1a198985f183 | 52 | |
dirkx | 0:1a198985f183 | 53 | protected: |
dirkx | 0:1a198985f183 | 54 | virtual void poll(); //Called by NetServices - not actually needed. |
dirkx | 0:1a198985f183 | 55 | |
dirkx | 0:1a198985f183 | 56 | private: |
dirkx | 0:1a198985f183 | 57 | void process(); // Needed for timer, to fire off regular updates. |
dirkx | 0:1a198985f183 | 58 | |
dirkx | 0:1a198985f183 | 59 | // Main inbound packet handler |
dirkx | 0:1a198985f183 | 60 | void onUDPSocketEvent(UDPSocketEvent e); |
dirkx | 0:1a198985f183 | 61 | |
dirkx | 0:1a198985f183 | 62 | // 3 functions to keep a QName chace. If enabled with MDNS_QCACHE |
dirkx | 0:1a198985f183 | 63 | void initLabelCache(char * off); |
dirkx | 0:1a198985f183 | 64 | uint16_t checkLabelCache(char * name); |
dirkx | 0:1a198985f183 | 65 | void addLabelCache(char * p, char * name); |
dirkx | 0:1a198985f183 | 66 | |
dirkx | 0:1a198985f183 | 67 | // Handing functions |
dirkx | 0:1a198985f183 | 68 | char * breakname(char *p, char *name); // Break FQDNs at the dots, understanding QCaching. |
dirkx | 0:1a198985f183 | 69 | |
dirkx | 0:1a198985f183 | 70 | // reply building functions; see RFC for names/defintion. |
dirkx | 0:1a198985f183 | 71 | char * mRR(char *p, char * name, uint16_t tpe, uint16_t cls, uint32_t ttl); |
dirkx | 0:1a198985f183 | 72 | char * mRRLABEL(char *p, char * name, uint16_t tpe, char ** rr); |
dirkx | 0:1a198985f183 | 73 | char * mPTR(char *p, char * name, char * r); |
dirkx | 0:1a198985f183 | 74 | char * mTXT(char *p, char * name, char ** rr); |
dirkx | 0:1a198985f183 | 75 | char * mARR(char *p, char * name, IpAddr ip); |
dirkx | 0:1a198985f183 | 76 | char * mSRV(char *p, char * name, uint16_t port, char * rr); |
dirkx | 0:1a198985f183 | 77 | char * qANY(char *p, char * name, uint16_t tpe, uint16_t cls); |
dirkx | 0:1a198985f183 | 78 | |
dirkx | 0:1a198985f183 | 79 | // build query part of a reply. |
dirkx | 0:1a198985f183 | 80 | char * qPTR(char *p, char *name); |
dirkx | 0:1a198985f183 | 81 | char * qA(char *p, char *name); |
dirkx | 0:1a198985f183 | 82 | char * qSRV(char *p, char *name); |
dirkx | 0:1a198985f183 | 83 | |
dirkx | 0:1a198985f183 | 84 | // Sent out the actual DNS packet. |
dirkx | 0:1a198985f183 | 85 | void sendReply(uint16_t t, uint16_t tid, Host 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; |
dirkx | 0:1a198985f183 | 108 | IpAddr 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 | |
dirkx | 0:1a198985f183 | 114 | Timer announcer; |
dirkx | 0:1a198985f183 | 115 | |
dirkx | 0:1a198985f183 | 116 | UDPSocket* m_pUDPSocket; |
dirkx | 0:1a198985f183 | 117 | |
dirkx | 0:1a198985f183 | 118 | // For the QCache |
dirkx | 0:1a198985f183 | 119 | #ifdef MDNS_QCACHE |
dirkx | 0:1a198985f183 | 120 | int labelCacheCnt; |
dirkx | 0:1a198985f183 | 121 | char * labelCacheOffset; |
dirkx | 0:1a198985f183 | 122 | |
dirkx | 0:1a198985f183 | 123 | uint16_t _cache_off[MDNS_MAXCACHEDNSLABELS]; |
dirkx | 0:1a198985f183 | 124 | char * _cache_och[MDNS_MAXCACHEDNSLABELS]; |
dirkx | 0:1a198985f183 | 125 | #endif |
dirkx | 0:1a198985f183 | 126 | }; |
dirkx | 0:1a198985f183 | 127 | |
dirkx | 0:1a198985f183 | 128 | |
dirkx | 0:1a198985f183 | 129 | |
dirkx | 0:1a198985f183 | 130 | #endif |
dirkx | 0:1a198985f183 | 131 | |
dirkx | 0:1a198985f183 | 132 | // Just in case - there is prolly some nice ARM6 assembler already linked in. |
dirkx | 0:1a198985f183 | 133 | // |
dirkx | 0:1a198985f183 | 134 | #ifndef htons |
dirkx | 0:1a198985f183 | 135 | #define htons( x ) ( (( x << 8 ) & 0xFF00) | (( x >> 8 ) & 0x00FF) ) |
dirkx | 0:1a198985f183 | 136 | #define ntohs( x ) (htons(x)) |
dirkx | 0:1a198985f183 | 137 | #endif |
dirkx | 0:1a198985f183 | 138 | |
dirkx | 0:1a198985f183 | 139 | #ifndef htonl |
dirkx | 0:1a198985f183 | 140 | #define htonl( x ) ( (( x << 24 ) & 0xff000000) \ |
dirkx | 0:1a198985f183 | 141 | | (( x << 8 ) & 0x00ff0000) \ |
dirkx | 0:1a198985f183 | 142 | | (( x >> 8 ) & 0x0000ff00) \ |
dirkx | 0:1a198985f183 | 143 | | (( x >> 24 ) & 0x000000ff) ) |
dirkx | 0:1a198985f183 | 144 | #define ntohl( x ) (htonl(x)) |
dirkx | 0:1a198985f183 | 145 | #endif |
dirkx | 0:1a198985f183 | 146 | |
dirkx | 0:1a198985f183 | 147 | #endif |