Fixed crt0.o not creating its output directory
[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 //TODO: Move out to another file
32 char *Net_GetInterface(int AddressType, void *Address)
33 {
34          int    size, routeNum;
35          int    fd;
36         size = Net_GetAddressSize(AddressType);
37         
38         if( size == 0 ) {
39                 fprintf(stderr, "BUG: AddressType = %i unknown\n", AddressType);
40                 return NULL;
41         }
42         
43         // Query the route manager for the route number
44         {
45                 char    buf[sizeof(int)+size];
46                 
47                 // Open
48                 fd = open("/Devices/ip/routes", 0);
49                 if( !fd ) {
50                         fprintf(stderr, "ERROR: It seems that '/Devices/ip/routes' does not exist, are you running Acess2?\n");
51                         return NULL;
52                 }
53                 
54                 // Make structure and ask
55                 *(int*)buf = AddressType;
56                 memcpy(&buf[sizeof(int)], Address, size);
57                 routeNum = ioctl(fd, ioctl(fd, 3, "locate_route"), buf);
58                 
59                 // Close
60                 close(fd);
61         }
62         
63         // Check answer validity
64         if( routeNum > 0 ) {
65                  int    len = sprintf(NULL, "/Devices/ip/routes/%i", routeNum);
66                 char    buf[len+1];
67                 char    *ret;
68                 
69                 sprintf(buf, "/Devices/ip/routes/%i", routeNum);
70                 
71                 // Open route
72                 fd = open(buf, 0);
73                 if( !fd )       return NULL;    // Shouldn't happen :/
74                 
75                 // Allocate space for name
76                 ret = malloc( ioctl(fd, ioctl(fd, 3, "get_interface"), NULL) + 1 );
77                 if( !ret ) {
78                         fprintf(stderr, "malloc() failure - Allocating space for interface name\n");
79                         return NULL;
80                 }
81                 
82                 // Get name
83                 ioctl(fd, ioctl(fd, 3, "get_interface"), ret);
84                 
85                 // Close and return
86                 close(fd);
87                 
88                 return ret;
89         }
90         
91         return NULL;    // Error
92 }

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