diff --git a/include/log.h b/include/log.h new file mode 100644 index 0000000..d2665c6 --- /dev/null +++ b/include/log.h @@ -0,0 +1,19 @@ +#ifndef LOG_H +#define LOG_H + +#include +#include +#include +#include +#include +#include +#include + +void Log_Init(); +void Log_Cleanup(); + +void Log_Info(const char* fmt, ...) __attribute__((format(printf, 1, 2))); +void Log_Warn(const char* fmt, ...) __attribute__((format(printf, 1, 2))); +void Log_Err(const char* fmt, ...) __attribute__((format(printf, 1, 2))); + +#endif diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..a596210 --- /dev/null +++ b/src/log.c @@ -0,0 +1,56 @@ +#include + +// UNIX socket for CONOUT (Console Output) - sends log datagrams to a listener on /tmp/miniroute-log.sock +static int log_socket_fd = -1; +static struct sockaddr_un log_dest; + +void Log_Init() { + log_socket_fd = socket(AF_UNIX, SOCK_DGRAM, 0); + if (log_socket_fd < 0) { + perror("Log_Init: socket"); + return; + } + + memset(&log_dest, 0, sizeof(log_dest)); + log_dest.sun_family = AF_UNIX; + strncpy(log_dest.sun_path, "/tmp/miniroute-log.sock", sizeof(log_dest.sun_path) - 1); +} + +void Log_Cleanup() { + if (log_socket_fd >= 0) { + close(log_socket_fd); + log_socket_fd = -1; + } +} + +static void log_emit(FILE* stream, const char* level, const char* fmt, va_list ap) { + char msg[1024]; + vsnprintf(msg, sizeof(msg), fmt, ap); + + char buf[1024]; + int len = snprintf(buf, sizeof(buf), "[%s] %s\n", level, msg); + fputs(buf, stream); + if (log_socket_fd >= 0) + sendto(log_socket_fd, buf, len, 0, (struct sockaddr*)&log_dest, sizeof(log_dest)); +} + +void Log_Info(const char* fmt, ...) { + va_list ap; + va_start(ap, fmt); + log_emit(stdout, "INFO", fmt, ap); + va_end(ap); +} + +void Log_Warn(const char* fmt, ...) { + va_list ap; + va_start(ap, fmt); + log_emit(stdout, "WARN", fmt, ap); + va_end(ap); +} + +void Log_Err(const char* fmt, ...) { + va_list ap; + va_start(ap, fmt); + log_emit(stderr, "ERROR", fmt, ap); + va_end(ap); +} diff --git a/src/main.c b/src/main.c index 272504e..26e9b55 100644 --- a/src/main.c +++ b/src/main.c @@ -6,29 +6,38 @@ #include #include +#include int main(void) { + Log_Init(); + + Log_Info("Welcome to MiniRoute!\nMiniRoute is licensed under the GPLv2-only license.\nMiniRoute is the copyright of its contributors.\nVersion alpha 0.1"); + if (RouteTable_Init() < 0) { - fprintf(stderr, "Failed to initialize route tables\n"); + Log_Err("Failed to initialize route tables"); return EXIT_FAILURE; + } else { + Log_Info("Route tables initialized successfully"); } interface_v4_t* iface = Interface_Create_v4("0.0.0.0", NULL, NULL); interface_v6_t* iface6 = Interface_Create_v6("::", NULL, NULL); if (!iface || !iface6) { - fprintf(stderr, "Failed to create interfaces\n"); + Log_Err("Failed to create interfaces"); RouteTable_Cleanup(); return EXIT_FAILURE; + } else { + Log_Info("Interfaces created successfully"); } route_table_v4_entry_t entry = {.destination = 0x01000000, .mask = 0xFF000000, .nextHop = 0xC0A80101}; // Example route: 1.0.0.0/8 via 192.168.1.1 if (RouteTable_AddRoute_v4(entry.destination, entry.mask, entry.nextHop) < 0) { - fprintf(stderr, "Failed to add route to IPv4 route table\n"); + Log_Err("Failed to add route to IPv4 route table"); } route_table_v4_entry_t entry2 = {.destination = 0x01020000, .mask = 0xFFFF0000, .nextHop = 0xC0A80102}; // Example route: 1.2.0.0/16 via 192.168.1.1 - more specific than the previous one if (RouteTable_AddRoute_v4(entry2.destination, entry2.mask, entry2.nextHop) < 0) { - fprintf(stderr, "Failed to add route to IPv4 route table\n"); + Log_Err("Failed to add route to IPv4 route table"); } // Get said route @@ -37,22 +46,22 @@ int main(void) { char destStr[16], nextHopStr[16]; IPUtils_PrintIPv4(0x01020005, destStr); IPUtils_PrintIPv4(nextHop, nextHopStr); - printf("Next hop for destination %s is %s\n", destStr, nextHopStr); + Log_Info("Next hop for destination %s is %s", destStr, nextHopStr); } else { char destStr[16]; IPUtils_PrintIPv4(0x01020005, destStr); - fprintf(stderr, "Failed to get next hop for destination %s\n", destStr); + Log_Err("Failed to get next hop for destination %s", destStr); } if (RouteTable_GetNextHop_v4(0x01010001, &nextHop) == 0) { // 1.1.0.1 should match the /8 route, not the /16 route char destStr[16], nextHopStr[16]; IPUtils_PrintIPv4(0x01010001, destStr); IPUtils_PrintIPv4(nextHop, nextHopStr); - printf("Next hop for destination %s is %s\n", destStr, nextHopStr); + Log_Info("Next hop for destination %s is %s", destStr, nextHopStr); } else { char destStr[16]; IPUtils_PrintIPv4(0x01010001, destStr); - fprintf(stderr, "Failed to get next hop for destination %s\n", destStr); + Log_Err("Failed to get next hop for destination %s", destStr); } // Some IPv6 routes @@ -60,7 +69,7 @@ int main(void) { __uint128_t mask6 = IPUtils_CIDRToMaskv6(64); __uint128_t nextHop6 = (((__uint128_t)0x20010DB8 << 96) | ((__uint128_t)0x00000000 << 64) | ((__uint128_t)0x00000000 << 32) | (__uint128_t)0x00000002); // 2001:0db8::2 if (RouteTable_AddRoute_v6(dest6, mask6, nextHop6) < 0) { - fprintf(stderr, "Failed to add route to IPv6 route table\n"); + Log_Err("Failed to add route to IPv6 route table"); } // Get said IPv6 route @@ -69,11 +78,11 @@ int main(void) { char destStr[40], nextHopStr[40]; IPUtils_PrintIPv6(dest6, destStr); IPUtils_PrintIPv6(nextHop6Out, nextHopStr); - printf("Next hop for destination %s is %s\n", destStr, nextHopStr); + Log_Info("Next hop for destination %s is %s", destStr, nextHopStr); } else { char destStr[40]; IPUtils_PrintIPv6(dest6, destStr); - fprintf(stderr, "Failed to get next hop for destination %s\n", destStr); + Log_Err("Failed to get next hop for destination %s", destStr); } while (1) { @@ -85,5 +94,7 @@ int main(void) { RouteTable_Cleanup(); Interface_Cleanup(); + Log_Cleanup(); + return 0; }