Networking - Working on DHCP client (and related changes)
[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                 uint32_t        *type = (void*)buf;
73                 
74                 // Open
75                 fd = open("/Devices/ip/routes", 0);
76                 if( !fd ) {
77                         fprintf(stderr, "ERROR: It seems that '/Devices/ip/routes' does not exist, are you running Acess2?\n");
78                         return NULL;
79                 }
80                 
81                 // Make structure and ask
82                 *type = AddressType;
83                 memcpy(type+1, Address, size);
84                 routeNum = ioctl(fd, ioctl(fd, 3, "locate_route"), buf);
85                 
86                 // Close
87                 close(fd);
88         }
89         
90         // Check answer validity
91         if( routeNum > 0 ) {
92                  int    len = sprintf(NULL, "/Devices/ip/routes/%i", routeNum);
93                 char    buf[len+1];
94                 char    *ret;
95                 
96                 sprintf(buf, "/Devices/ip/routes/%i", routeNum);
97                 
98                 // Open route
99                 fd = open(buf, 0);
100                 if( !fd )       return NULL;    // Shouldn't happen :/
101                 
102                 // Allocate space for name
103                 ret = malloc( ioctl(fd, ioctl(fd, 3, "get_interface"), NULL) + 1 );
104                 if( !ret ) {
105                         fprintf(stderr, "malloc() failure - Allocating space for interface name\n");
106                         return NULL;
107                 }
108                 
109                 // Get name
110                 ioctl(fd, ioctl(fd, 3, "get_interface"), ret);
111                 
112                 // Close and return
113                 close(fd);
114                 
115                 return ret;
116         }
117         
118         return NULL;    // Error
119 }

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