Obtain IP adress for MDNS automatically

This commit is contained in:
cpq
2026-03-13 13:47:25 -03:00
committed by Sergio R. Caprile
parent 44ef3b43d2
commit 0c794f19d2
8 changed files with 193 additions and 85 deletions
-5
View File
@@ -15,7 +15,6 @@ static void mdns_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
}
int main(void) {
uint32_t response_ip = inet_addr("192.168.69.11");
struct mg_mgr mgr;
static struct mg_connection *c;
mg_log_set(MG_LL_DEBUG); // Set log level
@@ -24,10 +23,6 @@ int main(void) {
// Desired name must NOT have any dots in it, nor a domain
c = mg_mdns_listen(&mgr, mdns_ev_handler, "Mongoose"); // Start mDNS server
if (c == NULL) return 1;
// if not using our built-in TCP/IP stack, pass the IP address you want to
// use as a response, this depends on your underlying TCP/IP stack and number
// of interfaces available
memcpy(c->data, &response_ip, sizeof(response_ip));
if (!mg_mdns_query(c, s_theone, MG_DNS_RTYPE_A)) return 1;
for (;;) mg_mgr_poll(&mgr, 1000); // Event loop
+6 -7
View File
@@ -15,7 +15,10 @@
// Desired name must NOT have any dots in it, nor a domain
#define MDNS_NAME "mongoose-device"
#define WEB_SERVER_ADDR "http://0.0.0.0:8000" // port is hardcoded below
#define RESPONSE_IP "192.168.69.11"
// When not using our built-in TCP/IP stack, Mongoose responds with the address
// of the interface in which the request came in. If, for some reason, you want
// to use some other address, fill req->addr. See: mdns-client advanced example
#ifdef ADVANCED_EXAMPLE
// A TXT record data containing JSON, if actually needed.
@@ -80,7 +83,8 @@ static void mdns_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
req->rr->atype == MG_DNS_RTYPE_TXT) {
// Mongoose already checked the host name for us, otherwise we need to do
// that ourselves first, then check for service name in db
struct mg_dnssd_record *r = (struct mg_dnssd_record *) dnssd_lookup(req->reqname);
struct mg_dnssd_record *r =
(struct mg_dnssd_record *) dnssd_lookup(req->reqname);
if (r == NULL) return;
req->r = r;
req->is_resp = true;
@@ -107,17 +111,12 @@ static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
int main(void) {
struct mg_mgr mgr;
static struct mg_connection *c;
uint32_t response_ip = inet_addr(RESPONSE_IP);
mg_log_set(MG_LL_DEBUG); // Set log level
mg_mgr_init(&mgr); // Initialise event manager
c = mg_mdns_listen(&mgr, mdns_ev_handler, MDNS_NAME);
if (c == NULL) return 1;
// if not using our built-in TCP/IP stack, pass the IP address you want to
// use as a response, this depends on your underlying TCP/IP stack and number
// of interfaces available
memcpy(c->data, &response_ip, sizeof(response_ip));
mg_http_listen(&mgr, WEB_SERVER_ADDR, http_ev_handler, NULL);
+27 -5
View File
@@ -3,23 +3,45 @@
#include "mongoose.h"
#ifdef ADVANCED_EXAMPLE
static void mdns_ev_handler(struct mg_connection *c, int ev, void *ev_data);
#endif
int main(void) {
uint32_t response_ip = inet_addr("192.168.69.11");
struct mg_mgr mgr;
static struct mg_connection *c;
mg_log_set(MG_LL_DEBUG); // Set log level
mg_mgr_init(&mgr); // Initialise event manager
// Desired name must NOT have any dots in it, nor a domain
#ifndef ADVANCED_EXAMPLE
c = mg_mdns_listen(&mgr, NULL, "Mongoose"); // Start mDNS server
#else
c = mg_mdns_listen(&mgr, mdns_ev_handler, "Mongoose"); // Start mDNS server
#endif
if (c == NULL) return 1;
// if not using our built-in TCP/IP stack, pass the IP address you want to
// use as a response, this depends on your underlying TCP/IP stack and number
// of interfaces available
memcpy(c->data, &response_ip, sizeof(response_ip));
for (;;) mg_mgr_poll(&mgr, 1000); // Event loop
mg_mgr_free(&mgr);
return 0;
}
// When not using our built-in TCP/IP stack, Mongoose responds with the address
// of the interface in which the request came in. If, for some reason, you want
// to use some other address, catch the MG_EV_MDNS_REQ event for MG_DNS_RTYPE_A
// requests, and fill your desired address
#ifdef ADVANCED_EXAMPLE
static void mdns_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_MDNS_REQ) {
struct mg_mdns_req *req = (struct mg_mdns_req *) ev_data;
if (req->rr->atype == MG_DNS_RTYPE_A) {
struct mg_addr myaddr;
memset(&myaddr, 0, sizeof(myaddr));
myaddr.addr.ip4 = MG_IPV4(1, 2, 3, 4);
req->addr = &myaddr;
}
}
(void) c;
}
#endif