Bonjour/Zerconf library

Dependencies:   mbed

Revision:
1:59820ca5c83a
Parent:
0:355018f44c9f
Child:
2:816cbd922d3e
--- a/services/mDNS/mDNSResponder.cpp	Wed Jul 21 19:25:56 2010 +0000
+++ b/services/mDNS/mDNSResponder.cpp	Thu Jul 22 00:08:38 2010 +0000
@@ -26,12 +26,15 @@
  *   printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]);
  *
  *   // Announce ourselves.
- *   mdns.announce(ip, "_http._tcp", 80, "The Little Server that Could", "path=/demo");
+ *   mdns.announce(ip, "fred", "_http._tcp", 80, "The Little Server that Could", "path=/demo");
  *
  *   while()... enter some run loop
- *   
- *   Or as another example: (http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt)
- *   and the various RFCs:
+ *   ...
+ *
+ *  This will cause http://fred.local./demo to be announced as 'The Little Server that Could'.
+ *
+ *  Or as another example: (http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt)
+ *  and the various RFCs:
  *
  *     mdns.announce(ip, "_ssh._tcp", 22, SSH to Serial Gateway", NULL);
  *
@@ -59,7 +62,7 @@
       close();
 }
 
-void mDNSResponder::announce(IpAddr ip, const char * proto, uint16_t port, const char * name, const char * txt) {
+void mDNSResponder::announce(IpAddr ip, const char * ldn, const char * proto, uint16_t port, const char * name, char ** txt) {
     Host localhost(IpAddr(MCAST), MDNS_PORT, NULL /* fqdn */);
 
     m_pUDPSocket = new UDPSocket;
@@ -79,7 +82,7 @@
 
     #define LOCAL "local"
     snprintf(moi_local_proto,128,"%s.%s.", moi_proto, LOCAL);
-    snprintf(moi_local_name,128,"%s.%s.", moi_name, LOCAL);
+    snprintf(moi_local_name,128,"%s.%s.", ldn, LOCAL);
     snprintf(moi_local_pglue,128,"%s.%s.%s.", moi_name,moi_proto, LOCAL);
     
     // Gratuis intro - and repeat such regularly..
@@ -140,7 +143,7 @@
     _cache_och[labelCacheCnt] = name;
 
     labelCacheCnt++;
-    // we intentionally do not wack the first two - as they are the most
+    // we intentionally do not wack the first entries - as they are the most
     // likely ones to be used.
     if (labelCacheCnt>=MAXCACHEDNSLABELS)
         labelCacheCnt = MAXCACHEDNSLABELS / 3;
@@ -207,25 +210,28 @@
     return p;
 }
 
-char * mRRLABEL(char *p, char * name, uint16_t tpe, char * rr) {
+char * mRRLABEL(char *p, char * name, uint16_t tpe, char ** rr) {
     uint16_t i;
 
     p = mRR(p, name, tpe, 1, MDNS_TTL); // Type, IN, TTL
 
     // RR String
     char * q = p + 2;
-    q = breakname(q, rr);
-
+    
+    for(;*rr;rr++) 
+        q = breakname(q, *rr);
+    
     i = htons(q - p - 2); // RDLEN
     memcpy(p, &i, 2);
     return q;
 }    
 
-char * mPTR(char *p, char * name, char * rr) {
-    return mRRLABEL(p, name, 12 /* PTR */, rr);
+char * mPTR(char *p, char * name, char * r) {
+    char *rr[] = { r, NULL };
+    return mRRLABEL(p, name, 12 /* PTR */, rr );
 }
     
-char * mTXT(char *p, char * name, char * rr) {
+char * mTXT(char *p, char * name, char ** rr) {
     return mRRLABEL(p, name, 16 /* TXT */, rr);
 }
 
@@ -309,8 +315,8 @@
     p = qPTR(p,moi_local_proto);
     p = mPTR(p,moi_local_proto, moi_local_pglue);
     p = mSRV(p,moi_local_pglue,80, moi_local_name);
-    if (moi_txt)
-         p = mTXT(p,moi_local_pglue,(char *)moi_txt);
+    if (moi_txt && * moi_txt)
+         p = mTXT(p,moi_local_pglue,moi_txt);
     p = mARR(p,moi_local_name,moi_ip); // fill out my own IP address.
 
     m_pUDPSocket->sendto(out,p-out,&dst);
@@ -337,7 +343,7 @@
 
                 #define MAXDEPTH 64
                 #define MAXRR 192 /* including dot */
-
+                
                 // assume nQ zero terminated fields followed by Qtype & Qclass
                 char * p = udp + 12;
                 for (int i = 0; i < nQ; i++) {
@@ -388,6 +394,8 @@
                     unsigned int Qt = htons(q[0] + q[1]*256);
                     unsigned int Qc = htons(q[2] + q[3]*256);
                     p = q + 4;
+                    
+                    printf("Q for %s\r\n", buff);
                     // We want PTR records on the INternet of our type
                     if ((Qt == 12) && (Qc == 1) && !(strcmp(buff,moi_local_proto))) 
                         sendReply(tid,from);