move to new skeleton

This commit is contained in:
Sergio R. Caprile committed 2024-04-26 13:46:18 -03:00
1 parent 290d48e1aa
commit 8c5dfa4227
82 files changed
+34 -34

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
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 packed_fs.c # Source code files, packed_fs.c contains ca.pem, which contains CA certs for TLS
CFLAGS = -W -Wall -Wextra -g -I. # Build options
# Mongoose build options. See https://mongoose.ws/documentation/#build-options
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES=1 -DMG_ENABLE_PACKED_FS=1
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
MAKE += WINDOWS=1 CC=$(CC)
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 mbedtls
# see https://mongoose.ws/tutorials/tls/#how-to-build for TLS build options
mbedtls: # Pull and build mbedTLS library
git clone --depth 1 -b v2.28.2 https://github.com/mbed-tls/mbedtls $@
$(MAKE) -C mbedtls/library
+2
View File
@@ -0,0 +1,2 @@
- This example requires changing server name and user credentials in `main.c`
- This example requires building with TLS support; before running, please see its detailed tutorial at https://mongoose.ws/tutorials/smtp/smtp-client/
+82
View File
@@ -0,0 +1,82 @@
#include "mongoose.h"
static const char *server = "tcp://smtp.gmail.com:587";
static const char *user = "aaa@domain.com"; // Change this! Your mail account
static const char *pass = "xxxxxxxxxxxxxx"; // Change this! Your mail password
static const char *to = "bbb@domain.com"; // Change this! Destination email
static const char *from = "My Mail Sender";
static const char *subj = "Test email from Mongoose library!";
static const char *mesg = "Hi!\nThis is a test message.\nBye.";
static bool s_quit;
enum { EHLO, STARTTLS, STARTTLS_WAIT, AUTH, FROM, TO, DATA, BODY, QUIT, END };
static void fn(struct mg_connection *c, int ev, void *ev_data) {
uint8_t *state = (uint8_t *) c->data;
if (ev == MG_EV_OPEN) {
// c->is_hexdumping = 1;
} else if (ev == MG_EV_READ) {
if (c->recv.len > 0 && c->recv.buf[c->recv.len - 1] == '\n') {
MG_INFO(("<-- %.*s", (int) c->recv.len - 2, c->recv.buf));
c->recv.len = 0;
if (*state == EHLO) {
mg_printf(c, "EHLO myname\r\n");
*state = STARTTLS;
} else if (*state == STARTTLS) {
mg_printf(c, "STARTTLS\r\n");
*state = STARTTLS_WAIT;
} else if (*state == STARTTLS_WAIT) {
struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"),
.name = mg_url_host(server)};
mg_tls_init(c, &opts);
*state = AUTH;
} else if (*state == AUTH) {
char a[100], b[300] = "";
size_t n = mg_snprintf(a, sizeof(a), "%c%s%c%s", 0, user, 0, pass);
mg_base64_encode((uint8_t *) a, n, b, sizeof(b));
mg_printf(c, "AUTH PLAIN %s\r\n", b);
*state = FROM;
} else if (*state == FROM) {
mg_printf(c, "MAIL FROM: <%s>\r\n", user);
*state = TO;
} else if (*state == TO) {
mg_printf(c, "RCPT TO: <%s>\r\n", to);
*state = DATA;
} else if (*state == DATA) {
mg_printf(c, "DATA\r\n");
*state = BODY;
} else if (*state == BODY) {
mg_printf(c, "From: %s <%s>\r\n", from, user); // Mail header
mg_printf(c, "Subject: %s\r\n", subj); // Mail header
mg_printf(c, "\r\n"); // End of headers
mg_printf(c, "%s\r\n", mesg); // Mail body
mg_printf(c, ".\r\n"); // End of body
*state = QUIT;
} else if (*state == QUIT) {
mg_printf(c, "QUIT\r\n");
*state = END;
} else {
c->is_draining = 1;
MG_INFO(("end"));
}
MG_INFO(("--> %.*s", (int) c->send.len - 2, c->send.buf));
}
} else if (ev == MG_EV_CLOSE) {
s_quit = true;
} else if (ev == MG_EV_TLS_HS) {
MG_INFO(("TLS handshake done! Sending EHLO again"));
mg_printf(c, "EHLO myname\r\n");
}
(void) ev_data;
}
int main(void) {
struct mg_mgr mgr;
mg_mgr_init(&mgr);
mg_log_set(MG_LL_DEBUG);
mg_connect(&mgr, server, fn, NULL);
while (s_quit == false) mg_mgr_poll(&mgr, 1000);
return 0;
}
+1
View File
@@ -0,0 +1 @@
../../../mongoose.c
+1
View File
@@ -0,0 +1 @@
../../../mongoose.h
+1
View File
@@ -0,0 +1 @@
../../http/http-client/packed_fs.c