mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-06 16:10:13 +07:00
move to new skeleton
This commit is contained in:
@@ -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_IO_SIZE=8192 -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
|
||||
@@ -0,0 +1 @@
|
||||
See detailed tutorial at https://mongoose.ws/tutorials/udp/captive-dns-server/
|
||||
@@ -0,0 +1,57 @@
|
||||
// Copyright (c) 2020 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// Example captive DNS server (captive portal).
|
||||
// 1. Run `make`. This builds and starts a server on port 5533
|
||||
// 2. Run `dig -t A www.google.com -4 @localhost -p 5533`
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
static const char *s_listen_url = "udp://0.0.0.0:5533";
|
||||
|
||||
// DNS answer section. We response with IP 1.2.3.4 - you can change it
|
||||
// in the last 4 bytes of this array
|
||||
uint8_t answer[] = {
|
||||
0xc0, 0x0c, // Point to the name in the DNS question
|
||||
0, 1, // 2 bytes - record type, A
|
||||
0, 1, // 2 bytes - address class, INET
|
||||
0, 0, 0, 120, // 4 bytes - TTL
|
||||
0, 4, // 2 bytes - address length
|
||||
1, 2, 3, 4 // 4 bytes - IP address
|
||||
};
|
||||
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_OPEN) {
|
||||
c->is_hexdumping = 1;
|
||||
} else if (ev == MG_EV_READ) {
|
||||
struct mg_dns_rr rr; // Parse first question, offset 12 is header size
|
||||
size_t n = mg_dns_parse_rr(c->recv.buf, c->recv.len, 12, true, &rr);
|
||||
MG_INFO(("DNS request parsed, result=%d", (int) n));
|
||||
if (n > 0) {
|
||||
char buf[512];
|
||||
struct mg_dns_header *h = (struct mg_dns_header *) buf;
|
||||
memset(buf, 0, sizeof(buf)); // Clear the whole datagram
|
||||
h->txnid = ((struct mg_dns_header *) c->recv.buf)->txnid; // Copy tnxid
|
||||
h->num_questions = mg_htons(1); // We use only the 1st question
|
||||
h->num_answers = mg_htons(1); // And only one answer
|
||||
h->flags = mg_htons(0x8400); // Authoritative response
|
||||
memcpy(buf + sizeof(*h), c->recv.buf + sizeof(*h), n); // Copy question
|
||||
memcpy(buf + sizeof(*h) + n, answer, sizeof(answer)); // And answer
|
||||
mg_send(c, buf, 12 + n + sizeof(answer)); // And send it!
|
||||
}
|
||||
mg_iobuf_del(&c->recv, 0, c->recv.len);
|
||||
}
|
||||
(void) ev_data;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_mgr mgr;
|
||||
|
||||
mg_log_set(MG_LL_DEBUG); // Set log level
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
mg_listen(&mgr, s_listen_url, fn, NULL); // Start DNS server
|
||||
for (;;) mg_mgr_poll(&mgr, 1000); // Event loop
|
||||
mg_mgr_free(&mgr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
../../../mongoose.c
|
||||
@@ -0,0 +1 @@
|
||||
../../../mongoose.h
|
||||
@@ -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_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
|
||||
@@ -0,0 +1 @@
|
||||
See detailed tutorial at https://mongoose.ws/tutorials/udp/sntp-time-sync/
|
||||
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) 2022 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// Synchronize time with remote SNTP server
|
||||
// A working time() call is required by TLS, so an embedded device without
|
||||
// a clock that wants to use TLS, must sync time via SNTP.
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
// The UNIX epoch of the boot time. Initially, we set it to 0. But then after
|
||||
// SNTP response, we update it to the correct value, which will allow us to
|
||||
// use time(). Uptime in milliseconds is returned by mg_millis().
|
||||
static time_t s_boot_timestamp = 0;
|
||||
|
||||
// SNTP client connection
|
||||
static struct mg_connection *s_sntp_conn = NULL;
|
||||
|
||||
// On embedded systems, rename to time()
|
||||
time_t my_time(time_t *tp) {
|
||||
time_t t = s_boot_timestamp + mg_millis() / 1000;
|
||||
if (tp != NULL) *tp = t;
|
||||
return t;
|
||||
}
|
||||
|
||||
// SNTP client callback
|
||||
static void sfn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_SNTP_TIME) {
|
||||
int64_t t = *(int64_t *) ev_data;
|
||||
MG_INFO(("Got SNTP time: %lld ms from epoch", t));
|
||||
s_boot_timestamp = (time_t) ((t - mg_millis()) / 1000);
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
s_sntp_conn = NULL;
|
||||
}
|
||||
(void) c;
|
||||
}
|
||||
|
||||
// Called every 5 seconds. Increase that for production case.
|
||||
static void timer_fn(void *arg) {
|
||||
struct mg_mgr *mgr = (struct mg_mgr *) arg;
|
||||
if (s_sntp_conn == NULL) s_sntp_conn = mg_sntp_connect(mgr, NULL, sfn, NULL);
|
||||
if (s_sntp_conn != NULL) mg_sntp_request(s_sntp_conn);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_mgr mgr; // Event manager
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
mg_log_set(MG_LL_DEBUG); // Set log level
|
||||
mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr);
|
||||
for (;;) mg_mgr_poll(&mgr, 300); // Infinite event loop
|
||||
mg_mgr_free(&mgr); // Free manager resources
|
||||
return 0;
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../../mongoose.c
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../../mongoose.h
|
||||
@@ -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_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
|
||||
@@ -0,0 +1 @@
|
||||
See detailed tutorial at https://mongoose.ws/tutorials/udp/ssdp-search/
|
||||
@@ -0,0 +1,64 @@
|
||||
// Copyright (c) 2022 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
static const char *s_ssdp_url = "udp://239.255.255.250:1900";
|
||||
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
MG_DEBUG(("%p got event: %d %p", c, ev, ev_data));
|
||||
if (ev == MG_EV_OPEN) {
|
||||
// c->is_hexdumping = 1;
|
||||
} else if (ev == MG_EV_RESOLVE) {
|
||||
// c->rem gets populated with multicast address. Store it in c->data
|
||||
memcpy(c->data, &c->rem, sizeof(c->rem));
|
||||
} else if (ev == MG_EV_READ) {
|
||||
MG_INFO(("Got a response"));
|
||||
struct mg_http_message hm;
|
||||
if (mg_http_parse((const char *) c->recv.buf, c->recv.len, &hm) > 0) {
|
||||
size_t i, max = sizeof(hm.headers) / sizeof(hm.headers[0]);
|
||||
// Iterate over request headers
|
||||
for (i = 0; i < max && hm.headers[i].name.len > 0; i++) {
|
||||
struct mg_str *k = &hm.headers[i].name, *v = &hm.headers[i].value;
|
||||
if ((mg_vcasecmp(k, "SERVER") == 0) ||
|
||||
(mg_vcasecmp(k, "LOCATION") == 0)) {
|
||||
printf("\t%.*s -> %.*s\n", (int) k->len, k->buf, (int) v->len,
|
||||
v->buf);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
// Each response to the SSDP socket will change c->rem.
|
||||
// We can now do mg_printf(c, "haha"); to respond back to the remote side.
|
||||
// But in our case, we should restore the multicast address in order
|
||||
// to have next search to go to the multicast address
|
||||
memcpy(&c->rem, c->data, sizeof(c->rem));
|
||||
// Discard the content of this response as we expect each SSDP response
|
||||
// to generate at most one MG_EV_READ event.
|
||||
c->recv.len = 0UL;
|
||||
}
|
||||
}
|
||||
|
||||
static void tfn(void *param) {
|
||||
struct mg_connection *c = param;
|
||||
if (c == NULL) return;
|
||||
MG_INFO(("Sending M-SEARCH"));
|
||||
mg_printf(c, "%s",
|
||||
"M-SEARCH * HTTP/1.1\r\n"
|
||||
"HOST: 239.255.255.250:1900\r\n"
|
||||
"MAN: \"ssdp:discover\"\r\n"
|
||||
"ST: urn:schemas-upnp-org:device:MediaRenderer:1\r\n"
|
||||
"MX: 5\r\n"
|
||||
"\r\n");
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_mgr mgr;
|
||||
static struct mg_connection *c;
|
||||
mg_mgr_init(&mgr);
|
||||
c = mg_connect(&mgr, s_ssdp_url, fn, NULL);
|
||||
mg_timer_add(&mgr, 2000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, tfn, c);
|
||||
while (true) mg_mgr_poll(&mgr, 200);
|
||||
mg_mgr_free(&mgr);
|
||||
return 0;
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../../mongoose.c
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../../mongoose.h
|
||||
Reference in New Issue
Block a user