From 0c1bf884877e4b89eb224e91627508d42ca70974 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 7 Nov 2010 13:18:05 +0800 Subject: [PATCH] Compile fixes, implemented Net_GetInterface --- Usermode/Applications/irc_src/Makefile | 2 +- Usermode/Applications/irc_src/main.c | 11 ++-- Usermode/Libraries/libnet.so_src/main.c | 78 +++++++++++++++++++++++++ Usermode/Libraries/libnet.so_src/net.h | 2 +- 4 files changed, 87 insertions(+), 6 deletions(-) diff --git a/Usermode/Applications/irc_src/Makefile b/Usermode/Applications/irc_src/Makefile index 98fa2bb0..b51ed6af 100644 --- a/Usermode/Applications/irc_src/Makefile +++ b/Usermode/Applications/irc_src/Makefile @@ -5,6 +5,6 @@ LDFLAGS += -lnet OBJ = main.o -BIN = ../ls +BIN = ../irc -include ../Makefile.tpl diff --git a/Usermode/Applications/irc_src/main.c b/Usermode/Applications/irc_src/main.c index c202c6bd..119e025d 100644 --- a/Usermode/Applications/irc_src/main.c +++ b/Usermode/Applications/irc_src/main.c @@ -193,7 +193,8 @@ int writef(int FD, const char *Format, ...) */ int OpenTCP(const char *AddressString, short PortNumber) { - int fd, addrType, iface; + int fd, addrType; + char *iface; char addrBuffer[8]; // Parse IP Address @@ -205,7 +206,7 @@ int OpenTCP(const char *AddressString, short PortNumber) // Finds the interface for the destination address iface = Net_GetInterface(addrType, addrBuffer); - if( iface == -1 ) { + if( iface == NULL ) { fprintf(stderr, "Unable to find a route to '%s'\n", AddressString); return -1; } @@ -213,12 +214,14 @@ int OpenTCP(const char *AddressString, short PortNumber) // Open client socket // TODO: Move this out to libnet? { - int len = snprintf(NULL, 100, "/Devices/ip/%i/tcpc", iface); + int len = snprintf(NULL, 100, "/Devices/ip/%s/tcpc", iface); char path[len+1]; - snprintf(path, 100, "/Devices/ip/%i/tcpc", iface); + snprintf(path, 100, "/Devices/ip/%s/tcpc", iface); fd = open(path, OPENFLAG_READ|OPENFLAG_WRITE); } + free(iface); + if( fd == -1 ) { return -1; } diff --git a/Usermode/Libraries/libnet.so_src/main.c b/Usermode/Libraries/libnet.so_src/main.c index 526e296d..938d1142 100644 --- a/Usermode/Libraries/libnet.so_src/main.c +++ b/Usermode/Libraries/libnet.so_src/main.c @@ -6,9 +6,87 @@ * - Library main (and misc functions) */ #include +#include +#include +#include +#include // === CODE === int SoMain(void) { return 0; } + +int Net_GetAddressSize(int AddressType) +{ + switch(AddressType) + { + case 4: return 4; // IPv4 + case 6: return 16; // IPv6 + default: + return 0; + } +} + +//TODO: Move out to another file +char *Net_GetInterface(int AddressType, void *Address) +{ + int size, routeNum; + int fd; + size = Net_GetAddressSize(AddressType); + + if( size == 0 ) { + fprintf(stderr, "BUG: AddressType = %i unknown\n", AddressType); + return NULL; + } + + // Query the route manager for the route number + { + char buf[sizeof(int)+size]; + + // Open + fd = open("/Devices/ip/routes", 0); + if( !fd ) { + fprintf(stderr, "ERROR: It seems that '/Devices/ip/routes' does not exist, are you running Acess2?\n"); + return NULL; + } + + // Make structure and ask + *(int*)buf = AddressType; + memcpy(&buf[sizeof(int)], Address, size); + routeNum = ioctl(fd, ioctl(fd, 3, "locate_route"), buf); + + // Close + close(fd); + } + + // Check answer validity + if( routeNum > 0 ) { + int len = sprintf(NULL, "/Devices/ip/routes/%i", routeNum); + char buf[len+1]; + char *ret; + + sprintf(buf, "/Devices/ip/routes/%i", routeNum); + + // Open route + fd = open(buf, 0); + if( !fd ) return NULL; // Shouldn't happen :/ + + // Allocate space for name + ret = malloc( ioctl(fd, ioctl(fd, 3, "get_interface"), NULL) + 1 ); + if( !ret ) { + fprintf(stderr, "malloc() failure - Allocating space for interface name\n"); + return NULL; + } + + // Get name + ioctl(fd, ioctl(fd, 3, "get_interface"), ret); + + // Close and return + close(fd); + + return ret; + } + + return NULL; // Error +} diff --git a/Usermode/Libraries/libnet.so_src/net.h b/Usermode/Libraries/libnet.so_src/net.h index 05deb9f9..7c7a9b5a 100644 --- a/Usermode/Libraries/libnet.so_src/net.h +++ b/Usermode/Libraries/libnet.so_src/net.h @@ -20,6 +20,6 @@ extern int Net_ParseAddress(const char *String, void *Addr); * \param Addr Address in binary format * \return Interface number */ -extern int Net_GetInterface(int AddrType, void *Addr); +extern char *Net_GetInterface(int AddrType, void *Addr); #endif -- 2.20.1