2022-09-11 15:18:25 -03:00
|
|
|
// Copyright (c) 2022 Cesanta Software Limited
|
|
|
|
// All rights reserved
|
|
|
|
//
|
2023-05-26 14:43:36 -03:00
|
|
|
// example using built-in TCP/IP stack and TUN/TAP interface
|
2022-12-09 18:48:05 +00:00
|
|
|
|
2022-11-15 15:08:09 -03:00
|
|
|
#include <sys/socket.h>
|
2022-12-09 18:48:05 +00:00
|
|
|
#ifndef __OpenBSD__
|
2022-09-11 15:18:25 -03:00
|
|
|
#include <linux/if.h>
|
|
|
|
#include <linux/if_tun.h>
|
2022-12-09 18:48:05 +00:00
|
|
|
#else
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_tun.h>
|
|
|
|
#include <net/if_types.h>
|
|
|
|
#endif
|
2022-09-11 15:18:25 -03:00
|
|
|
#include <sys/ioctl.h>
|
2023-05-26 14:43:36 -03:00
|
|
|
|
2022-11-09 13:11:22 +00:00
|
|
|
#include "mongoose.h"
|
2023-05-26 14:43:36 -03:00
|
|
|
#include "net.h"
|
2022-09-11 15:18:25 -03:00
|
|
|
|
|
|
|
static int s_signo;
|
|
|
|
void signal_handler(int signo) {
|
|
|
|
s_signo = signo;
|
|
|
|
}
|
|
|
|
|
2023-02-07 21:16:42 +00:00
|
|
|
static size_t tap_tx(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
|
2023-05-26 14:43:36 -03:00
|
|
|
ssize_t res = write(*(int *) ifp->driver_data, buf, len);
|
2022-09-11 15:18:25 -03:00
|
|
|
if (res < 0) {
|
|
|
|
MG_ERROR(("tap_tx failed: %d", errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (size_t) res;
|
|
|
|
}
|
|
|
|
|
2023-02-07 21:16:42 +00:00
|
|
|
static bool tap_up(struct mg_tcpip_if *ifp) {
|
2022-12-19 11:43:25 +00:00
|
|
|
return ifp->driver_data ? true : false;
|
2022-09-11 15:18:25 -03:00
|
|
|
}
|
|
|
|
|
2023-02-07 21:16:42 +00:00
|
|
|
static size_t tap_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) {
|
2022-12-19 11:43:25 +00:00
|
|
|
ssize_t received = read(*(int *) ifp->driver_data, buf, len);
|
2022-09-11 15:18:25 -03:00
|
|
|
usleep(1); // This is to avoid 100% CPU
|
|
|
|
if (received < 0) return 0;
|
|
|
|
return (size_t) received;
|
|
|
|
}
|
|
|
|
|
2024-06-04 16:19:13 -03:00
|
|
|
char *s_dns = NULL, *s_sntp = NULL;
|
|
|
|
|
|
|
|
static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
|
|
|
|
if (ev == MG_TCPIP_EV_ST_CHG) {
|
|
|
|
MG_INFO(("State change: %u", *(uint8_t *) ev_data));
|
|
|
|
} else if (ev == MG_TCPIP_EV_DHCP_DNS) {
|
|
|
|
free(s_dns);
|
|
|
|
s_dns = mg_mprintf("udp://%M:53", mg_print_ip4, (uint32_t *) ev_data);
|
|
|
|
ifp->mgr->dns4.url = s_dns;
|
|
|
|
MG_INFO(("Set DNS to %s", ifp->mgr->dns4.url));
|
|
|
|
} else if (ev == MG_TCPIP_EV_DHCP_SNTP) {
|
|
|
|
free(s_sntp);
|
|
|
|
s_sntp = mg_mprintf("udp://%M:123", mg_print_ip4, (uint32_t *) ev_data);
|
|
|
|
MG_INFO(("Set SNTP to %s", s_sntp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 15:18:25 -03:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
const char *iface = "tap0"; // Network iface
|
2022-11-15 14:52:43 -03:00
|
|
|
const char *mac = "02:00:01:02:03:77"; // MAC address
|
2022-09-11 15:18:25 -03:00
|
|
|
|
|
|
|
// Parse options
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], "-i") == 0 && i + 1 < argc) {
|
|
|
|
iface = argv[++i];
|
|
|
|
} else if (strcmp(argv[i], "-mac") == 0 && i + 1 < argc) {
|
|
|
|
mac = argv[++i];
|
|
|
|
} else if (strcmp(argv[i], "-v") == 0 && i + 1 < argc) {
|
|
|
|
mg_log_set(atoi(argv[++i]));
|
|
|
|
} else {
|
|
|
|
MG_ERROR(("unknown option %s", argv[i]));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open network interface
|
2022-12-09 18:48:05 +00:00
|
|
|
#ifndef __OpenBSD__
|
2023-05-26 14:43:36 -03:00
|
|
|
const char *tuntap_device = "/dev/net/tun";
|
2022-12-09 18:48:05 +00:00
|
|
|
#else
|
2023-05-26 14:43:36 -03:00
|
|
|
const char *tuntap_device = "/dev/tap0";
|
2022-12-09 18:48:05 +00:00
|
|
|
#endif
|
|
|
|
int fd = open(tuntap_device, O_RDWR);
|
2022-09-11 15:18:25 -03:00
|
|
|
struct ifreq ifr;
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
strncpy(ifr.ifr_name, iface, IFNAMSIZ);
|
2022-12-09 18:48:05 +00:00
|
|
|
#ifndef __OpenBSD__
|
2022-09-11 15:18:25 -03:00
|
|
|
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
|
|
|
|
if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
|
|
|
|
MG_ERROR(("Failed to setup TAP interface: %s", ifr.ifr_name));
|
2022-12-09 18:48:05 +00:00
|
|
|
abort(); // return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
#else
|
2023-05-26 14:43:36 -03:00
|
|
|
ifr.ifr_flags = (short) (IFF_UP | IFF_BROADCAST | IFF_MULTICAST);
|
2022-12-09 18:48:05 +00:00
|
|
|
if (ioctl(fd, TUNSIFMODE, (void *) &ifr) < 0) {
|
|
|
|
MG_ERROR(("Failed to setup TAP interface: %s", ifr.ifr_name));
|
|
|
|
abort(); // return EXIT_FAILURE;
|
2022-09-11 15:18:25 -03:00
|
|
|
}
|
2022-12-09 18:48:05 +00:00
|
|
|
#endif
|
2022-09-11 15:18:25 -03:00
|
|
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); // Non-blocking mode
|
|
|
|
|
|
|
|
MG_INFO(("Opened TAP interface: %s", iface));
|
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
|
|
|
|
struct mg_mgr mgr; // Event manager
|
|
|
|
mg_mgr_init(&mgr); // Initialise event manager
|
|
|
|
|
2023-02-07 21:16:42 +00:00
|
|
|
struct mg_tcpip_driver driver = {.tx = tap_tx, .up = tap_up, .rx = tap_rx};
|
2024-06-04 16:19:13 -03:00
|
|
|
struct mg_tcpip_if mif = {.driver = &driver,
|
|
|
|
.driver_data = &fd,
|
|
|
|
.enable_req_dns = true,
|
|
|
|
.enable_req_sntp = true,
|
|
|
|
.fn = mif_fn};
|
2022-11-09 13:11:22 +00:00
|
|
|
sscanf(mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &mif.mac[0], &mif.mac[1],
|
|
|
|
&mif.mac[2], &mif.mac[3], &mif.mac[4], &mif.mac[5]);
|
2023-02-07 21:16:42 +00:00
|
|
|
mg_tcpip_init(&mgr, &mif);
|
2022-09-11 15:18:25 -03:00
|
|
|
MG_INFO(("Init done, starting main loop"));
|
|
|
|
|
2022-12-09 18:48:05 +00:00
|
|
|
// Start infinite event loop
|
|
|
|
MG_INFO(("Mongoose version : v%s", MG_VERSION));
|
2023-05-26 14:43:36 -03:00
|
|
|
MG_INFO(("Listening on : %s", HTTP_URL));
|
|
|
|
MG_INFO(("Listening on : %s", HTTPS_URL));
|
2022-09-11 15:18:25 -03:00
|
|
|
|
2023-05-26 14:43:36 -03:00
|
|
|
web_init(&mgr);
|
2022-09-11 15:18:25 -03:00
|
|
|
while (s_signo == 0) mg_mgr_poll(&mgr, 100); // Infinite event loop
|
|
|
|
|
2024-06-04 16:19:13 -03:00
|
|
|
free(s_dns);
|
|
|
|
free(s_sntp);
|
2022-09-11 15:18:25 -03:00
|
|
|
mg_mgr_free(&mgr);
|
|
|
|
close(fd);
|
|
|
|
printf("Exiting on signal %d\n", s_signo);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|