mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-01 13:10:43 +00:00
Obtain IP adress for MDNS automatically
This commit is contained in:
1 parent
44ef3b43d2
commit
0c794f19d2
8 files changed
+193
-85
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user