Usermode/libnet - Sorting things out (and made a TCP client helper function)
authorJohn Hodge <[email protected]>
Sun, 27 May 2012 10:26:32 +0000 (18:26 +0800)
committerJohn Hodge <[email protected]>
Sun, 27 May 2012 10:26:32 +0000 (18:26 +0800)
Usermode/Libraries/libnet.so_src/Makefile
Usermode/Libraries/libnet.so_src/include_exp/net.h
Usermode/Libraries/libnet.so_src/main.c
Usermode/Libraries/libnet.so_src/socket.c [new file with mode: 0644]

index aa2f2f9..29abaaa 100644 (file)
@@ -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
index 7200efe..43445d1 100644 (file)
@@ -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
index 403a339..2adaf1b 100644 (file)
@@ -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 (file)
index 0000000..0b54079
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Acess2 Networking Toolkit
+ * By John Hodge (thePowersGang)
+ * 
+ * socket.c
+ * - 
+ */
+#include <net.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <acess/sys.h>
+
+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;
+}
+

UCC git Repository :: git.ucc.asn.au