Usermode/libnet - Added support for change to routing API
[tpg/acess2.git] / Usermode / Libraries / libnet.so_src / main.c
1 /*
2  * Acess2 Networking Toolkit
3  * By John Hodge (thePowersGang)
4  * 
5  * main.c
6  * - Library main (and misc functions)
7  */
8 #include <net.h>
9 #include <acess/sys.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13
14 // === CODE ===
15 int SoMain(void)
16 {
17         return 0;
18 }
19
20 int Net_GetAddressSize(int AddressType)
21 {
22         switch(AddressType)
23         {
24         case 4: return 4;       // IPv4
25         case 6: return 16;      // IPv6
26         default:
27                 return 0;
28         }
29 }
30
31 int Net_OpenSocket(int AddrType, void *Addr, const char *Filename)
32 {
33          int    addrLen = Net_GetAddressSize(AddrType);
34          int    i;
35         uint8_t *addrBuffer = Addr;
36         char    hexAddr[addrLen*2+1];
37         
38         for( i = 0; i < addrLen; i ++ )
39                 sprintf(hexAddr+i*2, "%02x", addrBuffer[i]);
40         
41         if(Filename)
42         {
43                  int    len = snprintf(NULL, 100, "/Devices/ip/routes/%i:%s/%s", AddrType, hexAddr, Filename);
44                 char    path[len+1];
45                 snprintf(path, 100, "/Devices/ip/routes/%i:%s/%s", AddrType, hexAddr, Filename);
46                 return open(path, OPENFLAG_READ|OPENFLAG_WRITE);
47         }
48         else
49         {
50                  int    len = snprintf(NULL, 100, "/Devices/ip/routes/%i:%s", AddrType, hexAddr);
51                 char    path[len+1];
52                 snprintf(path, 100, "/Devices/ip/routes/%i:%s", AddrType, hexAddr);
53                 return open(path, OPENFLAG_READ);
54         }
55 }
56
57 //TODO: Move out to another file
58 char *Net_GetInterface(int AddressType, void *Address)
59 {
60          int    size, routeNum;
61          int    fd;
62         size = Net_GetAddressSize(AddressType);
63         
64         if( size == 0 ) {
65                 fprintf(stderr, "BUG: AddressType = %i unknown\n", AddressType);
66                 return NULL;
67         }
68         
69         // Query the route manager for the route number
70         {
71                 char    buf[sizeof(int)+size];
72                 
73                 // Open
74                 fd = open("/Devices/ip/routes", 0);
75                 if( !fd ) {
76                         fprintf(stderr, "ERROR: It seems that '/Devices/ip/routes' does not exist, are you running Acess2?\n");
77                         return NULL;
78                 }
79                 
80                 // Make structure and ask
81                 *(int*)buf = AddressType;
82                 memcpy(&buf[sizeof(int)], Address, size);
83                 routeNum = ioctl(fd, ioctl(fd, 3, "locate_route"), buf);
84                 
85                 // Close
86                 close(fd);
87         }
88         
89         // Check answer validity
90         if( routeNum > 0 ) {
91                  int    len = sprintf(NULL, "/Devices/ip/routes/%i", routeNum);
92                 char    buf[len+1];
93                 char    *ret;
94                 
95                 sprintf(buf, "/Devices/ip/routes/%i", routeNum);
96                 
97                 // Open route
98                 fd = open(buf, 0);
99                 if( !fd )       return NULL;    // Shouldn't happen :/
100                 
101                 // Allocate space for name
102                 ret = malloc( ioctl(fd, ioctl(fd, 3, "get_interface"), NULL) + 1 );
103                 if( !ret ) {
104                         fprintf(stderr, "malloc() failure - Allocating space for interface name\n");
105                         return NULL;
106                 }
107                 
108                 // Get name
109                 ioctl(fd, ioctl(fd, 3, "get_interface"), ret);
110                 
111                 // Close and return
112                 close(fd);
113                 
114                 return ret;
115         }
116         
117         return NULL;    // Error
118 }

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