Files
miniroute/src/main.c

110 lines
4.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <forward/routetable.h>
#include <forward/route.h>
#include <forward/if/interface.h>
#include <iputils.h>
#include <unistd.h>
#include <log.h>
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) {
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", Route_Processv4, NULL);
interface_v6_t* iface6 = Interface_Create_v6("::", Route_Processv6, NULL);
if (!iface || !iface6) {
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) {
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) {
Log_Err("Failed to add route to IPv4 route table");
}
// Get said route
uint32_t nextHop;
if (RouteTable_GetNextHop_v4(0x01020005, &nextHop) == 0) { // 1.2.0.5 should match the /16 route, not the /8 route
char destStr[16], nextHopStr[16];
IPUtils_PrintIPv4(0x01020005, destStr);
IPUtils_PrintIPv4(nextHop, nextHopStr);
Log_Info("Next hop for destination %s is %s", destStr, nextHopStr);
} else {
char destStr[16];
IPUtils_PrintIPv4(0x01020005, 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);
Log_Info("Next hop for destination %s is %s", destStr, nextHopStr);
} else {
char destStr[16];
IPUtils_PrintIPv4(0x01010001, destStr);
Log_Err("Failed to get next hop for destination %s", destStr);
}
// Attempt to add class E route (should be rejected)
if (RouteTable_AddRoute_v4(0xF0000001, 0xF0000000, 0xC0A80101) < 0) {
Log_Info("Correctly rejected invalid class E route");
} else {
Log_Err("Incorrectly accepted invalid class E route");
}
// Some IPv6 routes
__uint128_t dest6 = (((__uint128_t)0x20010DB8 << 96) | ((__uint128_t)0x00000000 << 64) | ((__uint128_t)0x00000000 << 32) | (__uint128_t)0x00000001); // 2001:0db8::1
__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) {
Log_Err("Failed to add route to IPv6 route table");
}
// Get said IPv6 route
__uint128_t nextHop6Out;
if (RouteTable_GetNextHop_v6(dest6, &nextHop6Out) == 0) {
char destStr[40], nextHopStr[40];
IPUtils_PrintIPv6(dest6, destStr);
IPUtils_PrintIPv6(nextHop6Out, nextHopStr);
Log_Info("Next hop for destination %s is %s", destStr, nextHopStr);
} else {
char destStr[40];
IPUtils_PrintIPv6(dest6, destStr);
Log_Err("Failed to get next hop for destination %s", destStr);
}
while (1) {
// Main loop can be used for future tasks such as monitoring, CLI, etc.
// For now, it just keeps the program running until interrupted.
pause();
}
RouteTable_Cleanup();
Interface_Destroy_v4(iface);
Interface_Destroy_v6(iface6);
Log_Cleanup();
return 0;
}