mDNS

Dependents:   OS

This is a mDNS server.

Committer:
JBRYU
Date:
Tue Aug 11 17:05:41 2015 +0000
Revision:
0:0df3300689d2
Child:
2:2b00b659497a
2015; JB Ryu - KOR; mdns-SD

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JBRYU 0:0df3300689d2 1 /*
JBRYU 0:0df3300689d2 2 Made by JB RYU, KOR - 2015
JBRYU 0:0df3300689d2 3
JBRYU 0:0df3300689d2 4 class mDNSResponder
JBRYU 0:0df3300689d2 5 */
JBRYU 0:0df3300689d2 6
JBRYU 0:0df3300689d2 7 #ifndef MDNS_RESPONDER_H
JBRYU 0:0df3300689d2 8 #define MDNS_RESPONDER_H
JBRYU 0:0df3300689d2 9
JBRYU 0:0df3300689d2 10 #include "lwip/opt.h"
JBRYU 0:0df3300689d2 11 #include "mbed.h"
JBRYU 0:0df3300689d2 12 #include "EthernetInterface.h"
JBRYU 0:0df3300689d2 13 #include "dns-sd.h"
JBRYU 0:0df3300689d2 14
JBRYU 0:0df3300689d2 15 #if LWIP_DNS /* don't build if not configured for use in lwipopts.h */
JBRYU 0:0df3300689d2 16
JBRYU 0:0df3300689d2 17 #include "mbed.h"
JBRYU 0:0df3300689d2 18 #include "mem.h"
JBRYU 0:0df3300689d2 19 #include "memp.h"
JBRYU 0:0df3300689d2 20 #include "dns.h"
JBRYU 0:0df3300689d2 21
JBRYU 0:0df3300689d2 22 // As defined by IANA.
JBRYU 0:0df3300689d2 23 //
JBRYU 0:0df3300689d2 24 #define MDNS_PORT (5353)
JBRYU 0:0df3300689d2 25 #define MCAST "224.0.0.251"
JBRYU 0:0df3300689d2 26
JBRYU 0:0df3300689d2 27 // How long we announce our IP and URLs to be valid. Should
JBRYU 0:0df3300689d2 28 // ideally be 1/3 or so of the DHCP lease time. But we do
JBRYU 0:0df3300689d2 29 // not know that.
JBRYU 0:0df3300689d2 30 //
JBRYU 0:0df3300689d2 31 #define MDNS_TTL ( 100 )
JBRYU 0:0df3300689d2 32
JBRYU 0:0df3300689d2 33 // Rebroadcast our details more regularly than the TTL as
JBRYU 0:0df3300689d2 34 // we otherwise get dropped.
JBRYU 0:0df3300689d2 35 //
JBRYU 0:0df3300689d2 36 #ifndef MDNS_INTERVAL
JBRYU 0:0df3300689d2 37 #define MDNS_INTERVAL ( MDNS_TTL * 2 / 3 )
JBRYU 0:0df3300689d2 38 #endif
JBRYU 0:0df3300689d2 39
JBRYU 0:0df3300689d2 40 #ifndef MDNS_QCACHE
JBRYU 0:0df3300689d2 41 #define MDNS_QCACHE 1
JBRYU 0:0df3300689d2 42 #endif
JBRYU 0:0df3300689d2 43
JBRYU 0:0df3300689d2 44 #ifndef MDNS_MAXCACHEDNSLABELS
JBRYU 0:0df3300689d2 45 #define MDNS_MAXCACHEDNSLABELS (10)
JBRYU 0:0df3300689d2 46 #endif
JBRYU 0:0df3300689d2 47
JBRYU 0:0df3300689d2 48 class mDNSResponder
JBRYU 0:0df3300689d2 49 {
JBRYU 0:0df3300689d2 50 public:
JBRYU 0:0df3300689d2 51 mDNSResponder();
JBRYU 0:0df3300689d2 52 virtual ~mDNSResponder();
JBRYU 0:0df3300689d2 53 void close();
JBRYU 0:0df3300689d2 54 void announce(char* ip);
JBRYU 0:0df3300689d2 55 void MDNS_process(void const *args);
JBRYU 0:0df3300689d2 56
JBRYU 0:0df3300689d2 57 private:
JBRYU 0:0df3300689d2 58 void IPstringToByte(char* IPstr);
JBRYU 0:0df3300689d2 59 char* skip_name(char* query);
JBRYU 0:0df3300689d2 60 void query_domain(void);
JBRYU 0:0df3300689d2 61 void register_service(char* number);
JBRYU 0:0df3300689d2 62 char* decode_name(char* query, char* packet);
JBRYU 0:0df3300689d2 63 void send_dns_ans(struct dns_hdr* hdr);
JBRYU 0:0df3300689d2 64
JBRYU 0:0df3300689d2 65 /*static objs from my uip-mdns */
JBRYU 0:0df3300689d2 66 static MY_SD_DOMAINS SD_domains;
JBRYU 0:0df3300689d2 67 static char PTR_query_name[PTR_QUERY_NAME_LEN];
JBRYU 0:0df3300689d2 68 static struct dns_hdr SD_res_hdr;
JBRYU 0:0df3300689d2 69
JBRYU 0:0df3300689d2 70 static char query_buf[1500];
JBRYU 0:0df3300689d2 71 static QR_MAP g_queries;
JBRYU 0:0df3300689d2 72 /* end - from my dns */
JBRYU 0:0df3300689d2 73
JBRYU 0:0df3300689d2 74 char* IP_str;
JBRYU 0:0df3300689d2 75 uint8_t IP_byte[4];
JBRYU 0:0df3300689d2 76
JBRYU 0:0df3300689d2 77 UDPSocket mdns_sock;
JBRYU 0:0df3300689d2 78 Endpoint rcv_endpt; //initial value := ip addr any
JBRYU 0:0df3300689d2 79 Endpoint send_endpt;
JBRYU 0:0df3300689d2 80 };
JBRYU 0:0df3300689d2 81
JBRYU 0:0df3300689d2 82 #endif
JBRYU 0:0df3300689d2 83
JBRYU 0:0df3300689d2 84 #ifndef htons
JBRYU 0:0df3300689d2 85 #define htons( x ) ( (( x << 8 ) & 0xFF00) | (( x >> 8 ) & 0x00FF) )
JBRYU 0:0df3300689d2 86 #define ntohs( x ) (htons(x))
JBRYU 0:0df3300689d2 87 #endif
JBRYU 0:0df3300689d2 88
JBRYU 0:0df3300689d2 89 #ifndef htonl
JBRYU 0:0df3300689d2 90 #define htonl( x ) ( (( x << 24 ) & 0xff000000) \
JBRYU 0:0df3300689d2 91 | (( x << 8 ) & 0x00ff0000) \
JBRYU 0:0df3300689d2 92 | (( x >> 8 ) & 0x0000ff00) \
JBRYU 0:0df3300689d2 93 | (( x >> 24 ) & 0x000000ff) )
JBRYU 0:0df3300689d2 94 #define ntohl( x ) (htonl(x))
JBRYU 0:0df3300689d2 95 #endif
JBRYU 0:0df3300689d2 96
JBRYU 0:0df3300689d2 97 #endif