From dfcfdfd0f77023672746bc0cef7987cbaf64ba6f Mon Sep 17 00:00:00 2001 From: cpq Date: Thu, 26 Feb 2026 14:49:48 +0000 Subject: [PATCH] fix email --- README.md | 2 +- tutorials/udp/mdns-sd-server/main.c | 82 ++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index d2d510bd..9adeef03 100644 --- a/README.md +++ b/README.md @@ -200,7 +200,7 @@ We take security seriously: ## How to report security vulnerabilities -Please send an email to support at mongoose dot ws, with the full information. +Please send an email to support@cesanta.com, with the full information. Do NOT create a github issue. ## Articles diff --git a/tutorials/udp/mdns-sd-server/main.c b/tutorials/udp/mdns-sd-server/main.c index 0fbf37a6..fd3b07f9 100644 --- a/tutorials/udp/mdns-sd-server/main.c +++ b/tutorials/udp/mdns-sd-server/main.c @@ -1,52 +1,78 @@ -// Copyright (c) 2022 Cesanta Software Limited +// Copyright (c) 2022-2026 Cesanta Software Limited // All rights reserved +// +// This example runs MDNS server, auto-discoverable as MDNS_NAME +// Also it runs as web server #include "mongoose.h" +#define MDNS_NAME "mongoose-device" +#define WEB_SERVER_ADDR "http://0.0.0.0:8000" + +//static struct mg_connection *s_mdns_conn; +//static struct mg_connection *s_http_conn; + +#if 0 // A TXT record data containing JSON, if actually needed. // The whole record must not exceed 256 bytes, so keep this small char txt[] = "\x15{\"property\": \"value\"}"; // string length followed by string data, data length = 21 + 1 = 22 bytes // DNS-SD service records -static const struct mg_dnssd_record srvcs[] = { +static const struct mg_dnssd_record s_records[] = { {{"_myservice._tcp", 15}, {"", 0}, 9876}, {{"_myservice._udp", 15}, {txt, 22}, 9876}}; +#if 0 // DNS-SD service records database struct dnssd_db { struct mg_dnssd_record *srvcs; // service record data uint8_t num; // number of records in db -}; - -static const struct dnssd_db db_ = { +} db_ = { (struct mg_dnssd_record *) srvcs, - sizeof(srvcs) / sizeof(struct mg_dnssd_record)}; + sizeof(srvcs) / sizeof(struct mg_dnssd_record), +}; +#endif -static struct mg_dnssd_record *dnssd_lookup(const struct dnssd_db *db, - struct mg_str *name) { - unsigned int i; - for (i = 0; i < db->num; i++) { - if (mg_strcasecmp(db->srvcs[i].srvcproto, *name) == 0) break; +static struct mg_dnssd_record *dnssd_lookup(struct mg_str name) { + size_t i; + for (i = 0; i < sizeof(s_records) / sizeof(s_records[0]); i++) { + if (mg_strcasecmp(s_records[i].srvcproto, name) == 0) return &s_records[i]; } - if (i == db->num) return NULL; - return &db->srvcs[i]; + return NULL; } +#endif // we can do more complex stuff, this is a simple implementation -static void fn(struct mg_connection *c, int ev, void *ev_data) { +static void mdns_ev_handler(struct mg_connection *c, int ev, void *ev_data) { + struct mg_mdns_req *req = (struct mg_mdns_req *) ev_data; + // MDNS resolver expects response IP address in the c->data + // memcpy(c->data, &response_ip, sizeof(response_ip)); + MG_INFO(("---> %lu %d %p %p", c->id, ev, ev_data, req)); + if (ev == MG_EV_MDNS_PTR) { + // do we serve this service ? + //MG_INFO(("PTR")); + } else if (ev == MG_EV_MDNS_SRV) { + //MG_INFO(("SrV")); + } else if (ev == MG_EV_MDNS_TXT) { + //MG_INFO(("TXT")); + } else if (ev == MG_EV_MDNS_A) { // is this us ? + //MG_INFO(("A, ADDR: %M", mg_print_ip, &req->addr)); + } + +#if 0 struct mg_mdns_req *req = (struct mg_mdns_req *) ev_data; if (ev == MG_EV_MDNS_PTR) { // do we serve this service ? struct mg_dnssd_record *r; if (req->is_listing) return; // TODO(): handle services listing - r = dnssd_lookup(&db_, &req->reqname); + r = dnssd_lookup(req->reqname); if (r == NULL) return; req->r = r; req->is_resp = true; } else if (ev == MG_EV_MDNS_SRV || ev == MG_EV_MDNS_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 = dnssd_lookup(&db_, &req->reqname); + struct mg_dnssd_record *r = dnssd_lookup(req->reqname); if (r == NULL) return; req->r = r; req->is_resp = true; @@ -55,22 +81,28 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { // that ourselves } (void) c; +#endif +} + +// Simple web server, responds with a JSON like this +// {"uri":"/","addr":"192.168.0.2:1234"} +static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) { + if (ev == MG_EV_HTTP_MSG) { + struct mg_http_message *hm = (struct mg_http_message *) ev_data; + mg_http_reply(c, 200, "", "{%m:%m,%m:%m}\n", // Print JSON response + MG_ESC("uri"), mg_print_esc, hm->uri.len, hm->uri.buf, // URI + MG_ESC("addr"), mg_print_ip, &c->rem); // And remote addr + } } 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 - c = mg_mdns_listen(&mgr, fn, "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)); + mg_mdns_listen(&mgr, mdns_ev_handler, MDNS_NAME); + mg_http_listen(&mgr, WEB_SERVER_ADDR, http_ev_handler, NULL); for (;;) mg_mgr_poll(&mgr, 1000); // Event loop