Commit 286fa90e authored by Marko Mikulicic's avatar Marko Mikulicic Committed by Cesanta Bot

Simple mDNS + DNS-SD impl

Main TODOs:
1. the spec requires the impl to resend the reply once and
   to send gratuitous updates

2. advertise our clubby services as DNS-SD sub-services.

---

Tested with:

1. https://play.google.com/store/apps/details?id=com.grokkt.android.bonjour&hl=en
2. https://play.google.com/store/apps/details?id=com.druk.bonjour.browser&hl=en
3. osx `dns-sd`

Works well enough; it doesn't have some glitches due the fact that we
don't retransmit yet and we don't send gratuitous updates.

PUBLISHED_FROM=38d7a50beb274d4aeeda5b0b58e44dfa0789da84
parent d63fb70c
...@@ -8,6 +8,7 @@ items: ...@@ -8,6 +8,7 @@ items:
- { name: mg_dns_insert_header.md } - { name: mg_dns_insert_header.md }
- { name: mg_dns_copy_questions.md } - { name: mg_dns_copy_questions.md }
- { name: mg_dns_encode_record.md } - { name: mg_dns_encode_record.md }
- { name: mg_dns_encode_name.md }
- { name: mg_parse_dns.md } - { name: mg_parse_dns.md }
- { name: mg_dns_uncompress_name.md } - { name: mg_dns_uncompress_name.md }
- { name: mg_set_protocol_dns.md } - { name: mg_set_protocol_dns.md }
......
---
title: "mg_dns_encode_name()"
decl_name: "mg_dns_encode_name"
symbol_kind: "func"
signature: |
int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len);
---
Encodes a DNS name.
...@@ -15,11 +15,11 @@ are taken from the parameters, encoded in the appropriate format depending on ...@@ -15,11 +15,11 @@ are taken from the parameters, encoded in the appropriate format depending on
record type and stored in the IO buffer. The encoded values might contain record type and stored in the IO buffer. The encoded values might contain
offsets within the IO buffer. It's thus important that the IO buffer doesn't offsets within the IO buffer. It's thus important that the IO buffer doesn't
get trimmed while a sequence of records are encoded while preparing a DNS get trimmed while a sequence of records are encoded while preparing a DNS
*reply. reply.
This function doesn't update the `name` and `rdata` pointers in the `rr` This function doesn't update the `name` and `rdata` pointers in the `rr`
*struct struct because they might be invalidated as soon as the IO buffer grows
because they might be invalidated as soon as the IO buffer grows again. again.
Returns the number of bytes appened or -1 in case of error. Returns the number of bytes appened or -1 in case of error.
...@@ -28,6 +28,13 @@ static void ev_handler(struct mg_connection *nc, int ev, void *p) { ...@@ -28,6 +28,13 @@ static void ev_handler(struct mg_connection *nc, int ev, void *p) {
struct mbuf *io = &nc->recv_mbuf; struct mbuf *io = &nc->recv_mbuf;
(void) p; (void) p;
switch (ev) { switch (ev) {
case MG_EV_POLL: {
const char *peer;
peer = inet_ntoa(nc->sa.sin.sin_addr);
printf("nc->sa: %s %d\n", peer, ntohs(nc->sa.sin.sin_port));
break;
}
case MG_EV_RECV: case MG_EV_RECV:
printf("Received (%zu bytes): '%.*s'\n", io->len, (int) io->len, io->buf); printf("Received (%zu bytes): '%.*s'\n", io->len, (int) io->len, io->buf);
mbuf_remove(io, io->len); mbuf_remove(io, io->len);
......
...@@ -8455,7 +8455,7 @@ int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg) { ...@@ -8455,7 +8455,7 @@ int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg) {
return mbuf_append(io, begin, end - begin); return mbuf_append(io, begin, end - begin);
} }
static int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len) { int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len) {
const char *s; const char *s;
unsigned char n; unsigned char n;
size_t pos = io->len; size_t pos = io->len;
......
...@@ -3223,7 +3223,10 @@ extern "C" { ...@@ -3223,7 +3223,10 @@ extern "C" {
#define MG_DNS_A_RECORD 0x01 /* Lookup IP address */ #define MG_DNS_A_RECORD 0x01 /* Lookup IP address */
#define MG_DNS_CNAME_RECORD 0x05 /* Lookup CNAME */ #define MG_DNS_CNAME_RECORD 0x05 /* Lookup CNAME */
#define MG_DNS_PTR_RECORD 0x0c /* Lookup PTR */
#define MG_DNS_TXT_RECORD 0x10 /* Lookup TXT */
#define MG_DNS_AAAA_RECORD 0x1c /* Lookup IPv6 address */ #define MG_DNS_AAAA_RECORD 0x1c /* Lookup IPv6 address */
#define MG_DNS_SRV_RECORD 0x21 /* Lookup SRV */
#define MG_DNS_MX_RECORD 0x0f /* Lookup mail server for domain */ #define MG_DNS_MX_RECORD 0x0f /* Lookup mail server for domain */
#define MG_MAX_DNS_QUESTIONS 32 #define MG_MAX_DNS_QUESTIONS 32
...@@ -3308,11 +3311,11 @@ int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg); ...@@ -3308,11 +3311,11 @@ int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg);
* record type and stored in the IO buffer. The encoded values might contain * record type and stored in the IO buffer. The encoded values might contain
* offsets within the IO buffer. It's thus important that the IO buffer doesn't * offsets within the IO buffer. It's thus important that the IO buffer doesn't
* get trimmed while a sequence of records are encoded while preparing a DNS * get trimmed while a sequence of records are encoded while preparing a DNS
*reply. * reply.
* *
* This function doesn't update the `name` and `rdata` pointers in the `rr` * This function doesn't update the `name` and `rdata` pointers in the `rr`
*struct * struct because they might be invalidated as soon as the IO buffer grows
* because they might be invalidated as soon as the IO buffer grows again. * again.
* *
* Returns the number of bytes appened or -1 in case of error. * Returns the number of bytes appened or -1 in case of error.
*/ */
...@@ -3320,6 +3323,11 @@ int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr, ...@@ -3320,6 +3323,11 @@ int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr,
const char *name, size_t nlen, const void *rdata, const char *name, size_t nlen, const void *rdata,
size_t rlen); size_t rlen);
/*
* Encodes a DNS name.
*/
int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len);
/* Low-level: parses a DNS response. */ /* Low-level: parses a DNS response. */
int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg); int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment