Introduce new mDNS API, add mDNS client example

This commit is contained in:
Sergio R. Caprile committed 2026-03-06 11:27:22 -03:00
1 parent abe19d2367
commit 2a7fa4cb68
11 files changed
+303 -92

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
PROG ?= example # Program we are building
DELETE = rm -rf # Command to remove files
OUT ?= -o $(PROG) # Compiler argument for output file
SOURCES = main.c mongoose.c # Source code files
CFLAGS = -W -Wall -Wextra -g -I. # Build options
# Mongoose build options. See https://mongoose.ws/documentation/#build-options
CFLAGS_MONGOOSE += -DMG_ENABLE_MDNS=1 -DMG_ENABLE_LINES
ifeq ($(OS),Windows_NT) # Windows settings. Assume MinGW compiler. To use VC: make CC=cl CFLAGS=/MD OUT=/Feprog.exe
PROG ?= example.exe # Use .exe suffix for the binary
CC = gcc # Use MinGW gcc compiler
CFLAGS += -lws2_32 # Link against Winsock library
DELETE = cmd /C del /Q /F /S # Command prompt command to delete files
OUT ?= -o $(PROG) # Build output
endif
all: $(PROG) # Default target. Build and run program
$(RUN) ./$(PROG) $(ARGS)
$(PROG): $(SOURCES) # Build program from sources
$(CC) $(SOURCES) $(CFLAGS) $(CFLAGS_MONGOOSE) $(CFLAGS_EXTRA) $(OUT)
clean: # Cleanup. Delete built program and all build artifacts
$(DELETE) $(PROG) *.o *.obj *.exe *.dSYM
View File
Whitespace-only changes.
+37
View File
@@ -0,0 +1,37 @@
// Copyright (c) 2022 Cesanta Software Limited
// All rights reserved
#include "mongoose.h"
char s_theone[] = "Server.local";
static void mdns_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_MDNS_RESP) { // is this the one ?
struct mg_mdns_resp *resp = (struct mg_mdns_resp *) ev_data;
if (mg_strcasecmp(mg_str(s_theone), resp->name) == 0)
MG_INFO(("%.*s is: %M", resp->name.len, resp->name.buf, mg_print_ip, &resp->addr));
}
(void) c;
}
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, 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
mg_mgr_free(&mgr);
return 0;
}
+1
View File
@@ -0,0 +1 @@
../../../mongoose.c
+1
View File
@@ -0,0 +1 @@
../../../mongoose.h