From bcbaea33c43143d50bef24216a1d1575218c61e2 Mon Sep 17 00:00:00 2001 From: Daniel Collins Date: Mon, 29 Aug 2011 13:41:10 +0000 Subject: [PATCH] New router code nearing completion. --- src/ipxwrapper.c | 3 +-- src/ipxwrapper.h | 1 + src/router.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/ipxwrapper.c b/src/ipxwrapper.c index bb293a5..2d36132 100644 --- a/src/ipxwrapper.c +++ b/src/ipxwrapper.c @@ -53,7 +53,6 @@ static DWORD router_tid = 0; static int init_router(void); static DWORD WINAPI router_main(LPVOID argp); -static void add_host(const unsigned char *net, const unsigned char *node, uint32_t ipaddr); BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) { if(why == DLL_PROCESS_ATTACH) { @@ -323,7 +322,7 @@ static DWORD WINAPI router_main(LPVOID notused) { } /* Add a host to the hosts list or update an existing one */ -static void add_host(const unsigned char *net, const unsigned char *node, uint32_t ipaddr) { +void add_host(const unsigned char *net, const unsigned char *node, uint32_t ipaddr) { ipx_host *hptr = hosts; while(hptr) { diff --git a/src/ipxwrapper.h b/src/ipxwrapper.h index eea2f1b..9abb471 100644 --- a/src/ipxwrapper.h +++ b/src/ipxwrapper.h @@ -117,6 +117,7 @@ ipx_socket *get_socket(SOCKET fd); void lock_mutex(void); void unlock_mutex(void); ipx_host *find_host(const unsigned char *net, const unsigned char *node); +void add_host(const unsigned char *net, const unsigned char *node, uint32_t ipaddr); void log_open(); void log_close(); diff --git a/src/router.c b/src/router.c index b7bf7f8..38f50ee 100644 --- a/src/router.c +++ b/src/router.c @@ -21,6 +21,7 @@ #include "router.h" #include "common.h" #include "ipxwrapper.h" +#include "interface.h" /* Allocate router_vars structure and initialise all members * Returns NULL on failure @@ -165,7 +166,59 @@ DWORD router_main(void *arg) { return 1; } - /* TODO: Deliver packet */ + ipx_packet *packet = (ipx_packet*)router->recvbuf; + + /* Check that the packet arrived from the subnet of an enabled network + * interface and drop it if not. + */ + + if(global_conf.filter) { + struct ipx_interface *iface = router->interfaces; + + while(iface) { + if((iface->ipaddr & iface->netmask) == (addr.sin_addr.s_addr & iface->netmask)) { + break; + } + + iface = iface->next; + } + + if(!iface) { + continue; + } + } + + packet->size = ntohs(packet->size); + + if(packet->size > MAX_PACKET_SIZE || packet->size + sizeof(ipx_packet) - 1 != len) { + log_printf("Recieved packet with incorrect size field, discarding"); + continue; + } + + add_host(packet->src_net, packet->src_node, addr.sin_addr.s_addr); + + struct router_addr *ra = router->addrs; + + while(ra) { + unsigned char f6[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; + + /* TODO: Packet type filter and shutdown checks */ + + if( + (memcmp(packet->dest_net, ra->addr.sa_netnum, 4) == 0 || memcmp(packet->dest_net, f6, 4) == 0) && + (memcmp(packet->dest_node, ra->addr.sa_nodenum, 6) == 0 || memcmp(packet->dest_node, f6, 6) == 0) && + packet->dest_socket == ra->addr.sa_socket + ) { + addr.sin_addr.s_addr = inet_addr("127.0.0.1"); + addr.sin_port = htons(ra->local_port); + + if(sendto(router->udp_sock, (char*)packet, len, 0, (struct sockaddr*)&addr, sizeof(addr)) == -1) { + log_printf("Error relaying packet: %s", w32_error(WSAGetLastError())); + } + } + + ra = ra->next; + } LeaveCriticalSection(&(router->crit_sec)); }