Bonjour/mDNS lib, works with the new network stack

Fork of BonjourLib by Dirk-Willem van Gulik (NXP/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 #include "lwip/lwipopts.h"
00005 
00006 #if LWIP_DNS /* don't build if not configured for use in lwipopts.h */
00007 
00008 #include "EthernetInterface.h"
00009 #include "mbed.h"
00010 #include "rtos.h"
00011 
00012 #include "lwip/dns.h"
00013 
00014 // As defined by IANA.
00015 //
00016 #define MDNS_PORT (5353)
00017 #define MCAST "224.0.0.251"
00018 
00019 // How long we announce our IP and URLs to be valid. Should
00020 // ideally be 1/3 or so of the DHCP lease time. But we do 
00021 // not know that.
00022 //
00023 #define MDNS_TTL ( 100 )
00024 
00025 // Rebroadcast our details more regularly than the TTL as 
00026 // we otherwise get dropped.
00027 //
00028 #ifndef MDNS_INTERVAL
00029 #define MDNS_INTERVAL ( MDNS_TTL * 2 / 3 )
00030 #endif
00031 
00032 #ifndef MDNS_QCACHE
00033 #define MDNS_QCACHE 1
00034 #endif
00035 
00036 #ifndef MDNS_MAXCACHEDNSLABELS
00037 #define MDNS_MAXCACHEDNSLABELS (10)
00038 #endif
00039 
00040 class mDNSResponder {
00041     
00042 public:
00043   mDNSResponder();
00044   virtual ~mDNSResponder();
00045   void close();
00046   void announce(ip_addr_t ip, const char * ldn, const char * proto, uint16_t port, const char * name, char ** txts);
00047 
00048   void Listen(void const *args);
00049   void periodic(void const *n);
00050  
00051 private:
00052   void process(); // Needed for timer, to fire off regular updates.
00053   
00054   // Main inbound packet handler
00055   Thread * mdns_listener;
00056   RtosTimer * mdns_announcer;
00057 
00058 
00059   // 3 functions to keep a QName chace. If enabled with MDNS_QCACHE
00060   void initLabelCache(char * off);
00061   uint16_t checkLabelCache(char * name);
00062   void addLabelCache(char * p, char * name);
00063   
00064   // Handing functions
00065   char * breakname(char *p, char *name); // Break FQDNs at the dots, understanding QCaching.
00066   
00067   // reply building functions; see RFC for names/defintion.
00068   char * mRR(char *p, char * name, uint16_t tpe, uint16_t cls, uint32_t ttl);
00069   char * mRRLABEL(char *p, char * name, uint16_t tpe, char ** rr); 
00070   char * mPTR(char *p, char * name, char * r); 
00071   char * mTXT(char *p, char * name, char ** rr);
00072   char * mARR(char *p, char * name, ip_addr_t ip);
00073   char * mSRV(char *p, char * name, uint16_t port, char * rr);
00074   char * qANY(char *p, char * name, uint16_t tpe, uint16_t cls);
00075 
00076   // build query part of a reply.
00077   char * qPTR(char *p, char *name);
00078   char * qA(char *p, char *name);
00079   char * qSRV(char *p, char *name);
00080   
00081   // Periodic announcements
00082   void sendAnnounce(void);
00083   
00084   // Sent out the actual DNS packet.
00085   void sendReply(uint16_t t, uint16_t tid, Endpoint *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   ip_addr_t 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   // buffer;
00115   char space[1500];
00116 
00117 //  Timer announcer;
00118     
00119   UDPSocket* m_pUDPSocket;
00120   Endpoint* multicast_group;
00121 
00122   // For the QCache
00123 #ifdef MDNS_QCACHE
00124   int labelCacheCnt;
00125   char * labelCacheOffset;
00126   
00127   uint16_t _cache_off[MDNS_MAXCACHEDNSLABELS];
00128   char * _cache_och[MDNS_MAXCACHEDNSLABELS];
00129 #endif
00130 };
00131 
00132 
00133 
00134 #endif
00135 
00136 // Just in case - there is prolly some nice ARM6 assembler already linked in.
00137 //
00138 #ifndef htons
00139 #define htons( x ) ( (( x << 8 ) & 0xFF00) | (( x >> 8 ) & 0x00FF) )
00140 #define ntohs( x ) (htons(x))
00141 #endif
00142 
00143 #ifndef htonl
00144 #define htonl( x ) ( (( x << 24 ) & 0xff000000)  \
00145                    | (( x <<  8 ) & 0x00ff0000)  \
00146                    | (( x >>  8 ) & 0x0000ff00)  \
00147                    | (( x >> 24 ) & 0x000000ff)  )
00148 #define ntohl( x ) (htonl(x))
00149 #endif
00150 
00151 #endif