AcessNative/nativefs - Fixed memory leak due to Close() not being bound
[tpg/acess2.git] / Usermode / Libraries / libnet.so_src / socket.c
1 /*
2  * Acess2 Networking Toolkit
3  * By John Hodge (thePowersGang)
4  * 
5  * socket.c
6  * - 
7  */
8 #include <net.h>
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <acess/sys.h>
12
13 int Net_OpenSocket(int AddrType, void *Addr, const char *Filename)
14 {
15          int    addrLen = Net_GetAddressSize(AddrType);
16          int    i;
17         uint8_t *addrBuffer = Addr;
18         char    hexAddr[addrLen*2+1];
19         
20         for( i = 0; i < addrLen; i ++ )
21                 sprintf(hexAddr+i*2, "%02x", addrBuffer[i]);
22         
23         if(Filename)
24         {
25                  int    len = snprintf(NULL, 100, "/Devices/ip/routes/@%i:%s/%s", AddrType, hexAddr, Filename);
26                 char    path[len+1];
27                 snprintf(path, 100, "/Devices/ip/routes/@%i:%s/%s", AddrType, hexAddr, Filename);
28                 _SysDebug("%s", path);
29                 return open(path, OPENFLAG_READ|OPENFLAG_WRITE);
30         }
31         else
32         {
33                  int    len = snprintf(NULL, 100, "/Devices/ip/routes/@%i:%s", AddrType, hexAddr);
34                 char    path[len+1];
35                 snprintf(path, 100, "/Devices/ip/routes/@%i:%s", AddrType, hexAddr);
36                 return open(path, OPENFLAG_READ);
37         }
38 }
39
40 int Net_OpenSocket_TCPC(int AddrType, void *Addr, int Port)
41 {
42         int fd = Net_OpenSocket(AddrType, Addr, "tcpc");
43         if( fd == -1 )  return -1;
44         
45         ioctl(fd, 5, &Port);    // Remote Port
46         ioctl(fd, 6, Addr);     // Remote address
47         ioctl(fd, 7, NULL);     // connect
48         return fd;
49 }
50

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