initial
This commit is contained in:
89
src/main.c
Normal file
89
src/main.c
Normal file
@@ -0,0 +1,89 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <forward/routetable.h>
|
||||
#include <forward/if/interface.h>
|
||||
#include <iputils.h>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void) {
|
||||
if (RouteTable_Init() < 0) {
|
||||
fprintf(stderr, "Failed to initialize route tables\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
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");
|
||||
RouteTable_Cleanup();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
// 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);
|
||||
printf("Next hop for destination %s is %s\n", destStr, nextHopStr);
|
||||
} else {
|
||||
char destStr[16];
|
||||
IPUtils_PrintIPv4(0x01020005, destStr);
|
||||
fprintf(stderr, "Failed to get next hop for destination %s\n", 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);
|
||||
} else {
|
||||
char destStr[16];
|
||||
IPUtils_PrintIPv4(0x01010001, destStr);
|
||||
fprintf(stderr, "Failed to get next hop for destination %s\n", destStr);
|
||||
}
|
||||
|
||||
// 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) {
|
||||
fprintf(stderr, "Failed to add route to IPv6 route table\n");
|
||||
}
|
||||
|
||||
// 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);
|
||||
printf("Next hop for destination %s is %s\n", destStr, nextHopStr);
|
||||
} else {
|
||||
char destStr[40];
|
||||
IPUtils_PrintIPv6(dest6, destStr);
|
||||
fprintf(stderr, "Failed to get next hop for destination %s\n", 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_Cleanup();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user