From 58cb682ba1001ea6146bdc6b2bfa9961d470bf52 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 27 May 2012 18:26:32 +0800 Subject: [PATCH] Usermode/libnet - Sorting things out (and made a TCP client helper function) --- Usermode/Libraries/libnet.so_src/Makefile | 2 +- .../Libraries/libnet.so_src/include_exp/net.h | 2 + Usermode/Libraries/libnet.so_src/main.c | 30 +---------- Usermode/Libraries/libnet.so_src/socket.c | 50 +++++++++++++++++++ 4 files changed, 55 insertions(+), 29 deletions(-) create mode 100644 Usermode/Libraries/libnet.so_src/socket.c diff --git a/Usermode/Libraries/libnet.so_src/Makefile b/Usermode/Libraries/libnet.so_src/Makefile index aa2f2f9a..29abaaa0 100644 --- a/Usermode/Libraries/libnet.so_src/Makefile +++ b/Usermode/Libraries/libnet.so_src/Makefile @@ -6,7 +6,7 @@ CPPFLAGS += CFLAGS += -Wall LDFLAGS += -lc -soname libnet.so -OBJ = main.o address.o +OBJ = main.o address.o socket.o BIN = libnet.so include ../Makefile.tpl diff --git a/Usermode/Libraries/libnet.so_src/include_exp/net.h b/Usermode/Libraries/libnet.so_src/include_exp/net.h index 7200efe0..43445d19 100644 --- a/Usermode/Libraries/libnet.so_src/include_exp/net.h +++ b/Usermode/Libraries/libnet.so_src/include_exp/net.h @@ -55,4 +55,6 @@ extern char *Net_GetInterface(int AddrType, void *Addr); */ extern int Net_OpenSocket(int AddrType, void *Addr, const char *SocketName); +extern int Net_OpenSocket_TCPC(int AddrType, void *Addr, int Port); + #endif diff --git a/Usermode/Libraries/libnet.so_src/main.c b/Usermode/Libraries/libnet.so_src/main.c index 403a3394..2adaf1b8 100644 --- a/Usermode/Libraries/libnet.so_src/main.c +++ b/Usermode/Libraries/libnet.so_src/main.c @@ -29,32 +29,6 @@ int Net_GetAddressSize(int AddressType) } } -int Net_OpenSocket(int AddrType, void *Addr, const char *Filename) -{ - int addrLen = Net_GetAddressSize(AddrType); - int i; - uint8_t *addrBuffer = Addr; - char hexAddr[addrLen*2+1]; - - for( i = 0; i < addrLen; i ++ ) - sprintf(hexAddr+i*2, "%02x", addrBuffer[i]); - - if(Filename) - { - int len = snprintf(NULL, 100, "/Devices/ip/routes/@%i:%s/%s", AddrType, hexAddr, Filename); - char path[len+1]; - snprintf(path, 100, "/Devices/ip/routes/@%i:%s/%s", AddrType, hexAddr, Filename); - return open(path, OPENFLAG_READ|OPENFLAG_WRITE); - } - else - { - int len = snprintf(NULL, 100, "/Devices/ip/routes/@%i:%s", AddrType, hexAddr); - char path[len+1]; - snprintf(path, 100, "/Devices/ip/routes/@%i:%s", AddrType, hexAddr); - return open(path, OPENFLAG_READ); - } -} - //TODO: Move out to another file char *Net_GetInterface(int AddressType, void *Address) { @@ -75,7 +49,7 @@ char *Net_GetInterface(int AddressType, void *Address) // 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"); + fprintf(stderr, "ERROR: Unable to open '/Devices/ip/routes'\n"); return NULL; } @@ -99,7 +73,7 @@ char *Net_GetInterface(int AddressType, void *Address) // Open route fd = open(buf, 0); if( fd == -1 ) { - fprintf(stderr, "Net_GetInterface - ERROR: Unabel to open %s\n", buf); + fprintf(stderr, "Net_GetInterface - ERROR: Unable to open %s\n", buf); return NULL; // Shouldn't happen :/ } diff --git a/Usermode/Libraries/libnet.so_src/socket.c b/Usermode/Libraries/libnet.so_src/socket.c new file mode 100644 index 00000000..0b540793 --- /dev/null +++ b/Usermode/Libraries/libnet.so_src/socket.c @@ -0,0 +1,50 @@ +/* + * Acess2 Networking Toolkit + * By John Hodge (thePowersGang) + * + * socket.c + * - + */ +#include +#include +#include +#include + +int Net_OpenSocket(int AddrType, void *Addr, const char *Filename) +{ + int addrLen = Net_GetAddressSize(AddrType); + int i; + uint8_t *addrBuffer = Addr; + char hexAddr[addrLen*2+1]; + + for( i = 0; i < addrLen; i ++ ) + sprintf(hexAddr+i*2, "%02x", addrBuffer[i]); + + if(Filename) + { + int len = snprintf(NULL, 100, "/Devices/ip/routes/@%i:%s/%s", AddrType, hexAddr, Filename); + char path[len+1]; + snprintf(path, 100, "/Devices/ip/routes/@%i:%s/%s", AddrType, hexAddr, Filename); + _SysDebug("%s", path); + return open(path, OPENFLAG_READ|OPENFLAG_WRITE); + } + else + { + int len = snprintf(NULL, 100, "/Devices/ip/routes/@%i:%s", AddrType, hexAddr); + char path[len+1]; + snprintf(path, 100, "/Devices/ip/routes/@%i:%s", AddrType, hexAddr); + return open(path, OPENFLAG_READ); + } +} + +int Net_OpenSocket_TCPC(int AddrType, void *Addr, int Port) +{ + int fd = Net_OpenSocket(AddrType, Addr, "tcpc"); + if( fd == -1 ) return -1; + + ioctl(fd, 5, &Port); // Remote Port + ioctl(fd, 6, Addr); // Remote address + ioctl(fd, 7, NULL); // connect + return fd; +} + -- 2.20.1