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