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

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