Bonjour/Zerconf library

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mDNSResponder.h Source File

mDNSResponder.h

00001 #ifndef MDNS_RESPONDER_H
00002 #define MDNS_RESPONDER_H
00003 
00004 
00005 #include "lwip/opt.h"
00006 
00007 #if LWIP_DNS /* don't build if not configured for use in lwipopts.h */
00008 
00009 #include "if/net/net.h"
00010 #include "api/UDPSocket.h"
00011 #include "api/DNSRequest.h"
00012 #include "mbed.h"
00013 
00014 #include "lwip/udp.h"
00015 #include "lwip/mem.h"
00016 #include "lwip/memp.h"
00017 #include "lwip/dns.h"
00018 
00019 // As defined by IANA.
00020 //
00021 #define MDNS_PORT (5353)
00022 #define MCAST 224,0,0,251
00023 
00024 // How long we announce our IP and URLs to be valid. Should
00025 // ideally be 1/3 or so of the DHCP lease time. But we do 
00026 // not know that.
00027 //
00028 #define MDNS_TTL ( 100 )
00029 
00030 // Rebroadcast our details more regularly than the TTL as 
00031 // we otherwise get dropped.
00032 //
00033 #ifndef MDNS_INTERVAL
00034 #define MDNS_INTERVAL ( MDNS_TTL * 2 / 3 )
00035 #endif
00036 
00037 #ifndef MDNS_QCACHE
00038 #define MDNS_QCACHE 1
00039 #endif
00040 
00041 #ifndef MDNS_MAXCACHEDNSLABELS
00042 #define MDNS_MAXCACHEDNSLABELS (10)
00043 #endif
00044 
00045 class mDNSResponder : protected NetService
00046 {
00047 public:
00048   mDNSResponder();
00049   virtual ~mDNSResponder();
00050   void close();
00051   void announce(IpAddr ip, const char * ldn, const char * proto, uint16_t port, const char * name, char ** txts);
00052   
00053 protected:
00054   virtual void poll(); //Called by NetServices - not actually needed.
00055   
00056 private:
00057   void process(); // Needed for timer, to fire off regular updates.
00058   
00059   // Main inbound packet handler
00060   void onUDPSocketEvent(UDPSocketEvent e);
00061 
00062   // 3 functions to keep a QName chace. If enabled with MDNS_QCACHE
00063   void initLabelCache(char * off);
00064   uint16_t checkLabelCache(char * name);
00065   void addLabelCache(char * p, char * name);
00066   
00067   // Handing functions
00068   char * breakname(char *p, char *name); // Break FQDNs at the dots, understanding QCaching.
00069   
00070   // reply building functions; see RFC for names/defintion.
00071   char * mRR(char *p, char * name, uint16_t tpe, uint16_t cls, uint32_t ttl);
00072   char * mRRLABEL(char *p, char * name, uint16_t tpe, char ** rr); 
00073   char * mPTR(char *p, char * name, char * r); 
00074   char * mTXT(char *p, char * name, char ** rr);
00075   char * mARR(char *p, char * name, IpAddr ip);
00076   char * mSRV(char *p, char * name, uint16_t port, char * rr);
00077   char * qANY(char *p, char * name, uint16_t tpe, uint16_t cls);
00078 
00079   // build query part of a reply.
00080   char * qPTR(char *p, char *name);
00081   char * qA(char *p, char *name);
00082   char * qSRV(char *p, char *name);
00083   
00084   // Sent out the actual DNS packet.
00085   void sendReply(uint16_t t, uint16_t tid, Host dst);
00086   
00087   // Decode the query part of a DNS inbound packet.
00088   char * decodeQBlock(char * udp, int len, char *p, char  * fqdn, int fqdn_size, uint32_t *Qclass, uint32_t *Qtype);
00089 
00090 PACK_STRUCT_BEGIN
00091 struct DNSPacket
00092   {
00093     PACK_STRUCT_FIELD(uint16_t tid);
00094     PACK_STRUCT_FIELD(uint16_t flags);
00095     PACK_STRUCT_FIELD(uint16_t question_count);
00096     PACK_STRUCT_FIELD(uint16_t answer_count);
00097     PACK_STRUCT_FIELD(uint16_t a_count);
00098     PACK_STRUCT_FIELD(uint16_t aa_count);
00099     PACK_STRUCT_FIELD(char answers);
00100   } PACK_STRUCT_STRUCT;
00101 PACK_STRUCT_END
00102   
00103   // Services
00104   char ** moi_txt; /* Null terminated list */
00105   
00106   // Endpoint
00107   uint16_t moi_port;
00108   IpAddr moi_ip;
00109   
00110   // Local name construction.
00111   const char * moi_name, * moi_proto;
00112   char * moi_local_proto, * moi_local_name, * moi_local_pglue;
00113   
00114   Timer announcer;
00115     
00116   UDPSocket* m_pUDPSocket;
00117 
00118   // For the QCache
00119 #ifdef MDNS_QCACHE
00120   int labelCacheCnt;
00121   char * labelCacheOffset;
00122   
00123   uint16_t _cache_off[MDNS_MAXCACHEDNSLABELS];
00124   char * _cache_och[MDNS_MAXCACHEDNSLABELS];
00125 #endif
00126 };
00127 
00128 
00129 
00130 #endif
00131 
00132 // Just in case - there is prolly some nice ARM6 assembler already linked in.
00133 //
00134 #ifndef htons
00135 #define htons( x ) ( (( x << 8 ) & 0xFF00) | (( x >> 8 ) & 0x00FF) )
00136 #define ntohs( x ) (htons(x))
00137 #endif
00138 
00139 #ifndef htonl
00140 #define htonl( x ) ( (( x << 24 ) & 0xff000000)  \
00141                    | (( x <<  8 ) & 0x00ff0000)  \
00142                    | (( x >>  8 ) & 0x0000ff00)  \
00143                    | (( x >> 24 ) & 0x000000ff)  )
00144 #define ntohl( x ) (htonl(x))
00145 #endif
00146 
00147 #endif