Usermode/libnet - Implementation of DNS lookup, untested
[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 //TODO: Move out to another file
33 char *Net_GetInterface(int AddressType, void *Address)
34 {
35          int    size, routeNum;
36          int    fd;
37         size = Net_GetAddressSize(AddressType);
38         
39         if( size == 0 ) {
40                 fprintf(stderr, "BUG: AddressType = %i unknown\n", AddressType);
41                 return NULL;
42         }
43         
44         // Query the route manager for the route number
45         {
46                 char    buf[sizeof(int)+size];
47                 uint32_t        *type = (void*)buf;
48                 
49                 // Open
50                 fd = _SysOpen("/Devices/ip/routes", 0);
51                 if( !fd ) {
52                         fprintf(stderr, "ERROR: Unable to open '/Devices/ip/routes'\n");
53                         return NULL;
54                 }
55                 
56                 // Make structure and ask
57                 *type = AddressType;
58                 memcpy(type+1, Address, size);
59                 routeNum = _SysIOCtl(fd, _SysIOCtl(fd, 3, "locate_route"), buf);
60                 
61                 // Close
62                 _SysClose(fd);
63         }
64         
65         // Check answer validity
66         if( routeNum > 0 ) {
67                  int    len = sprintf(NULL, "/Devices/ip/routes/#%i", routeNum);
68                 char    buf[len+1];
69                 char    *ret;
70                 
71                 sprintf(buf, "/Devices/ip/routes/#%i", routeNum);
72                 
73                 // Open route
74                 fd = _SysOpen(buf, 0);
75                 if( fd == -1 ) {
76                         fprintf(stderr, "Net_GetInterface - ERROR: Unable to open %s\n", buf);
77                         return NULL;    // Shouldn't happen :/
78                 }
79                 
80                 // Allocate space for name
81                 ret = malloc( _SysIOCtl(fd, _SysIOCtl(fd, 3, "get_interface"), NULL) + 1 );
82                 if( !ret ) {
83                         fprintf(stderr, "malloc() failure - Allocating space for interface name\n");
84                         return NULL;
85                 }
86                 
87                 // Get name
88                 _SysIOCtl(fd, _SysIOCtl(fd, 3, "get_interface"), ret);
89                 
90                 // Close and return
91                 _SysClose(fd);
92                 
93                 return ret;
94         }
95         
96         return NULL;    // Error
97 }

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