2 * Acess2 Networking Toolkit
3 * By John Hodge (thePowersGang)
6 * - Library main (and misc functions)
20 int Net_GetAddressSize(int AddressType)
24 case 4: return 4; // IPv4
25 case 6: return 16; // IPv6
31 int Net_OpenSocket(int AddrType, void *Addr, const char *Filename)
33 int addrLen = Net_GetAddressSize(AddrType);
35 uint8_t *addrBuffer = Addr;
36 char hexAddr[addrLen*2+1];
38 for( i = 0; i < addrLen; i ++ )
39 sprintf(hexAddr+i*2, "%02x", addrBuffer[i]);
43 int len = snprintf(NULL, 100, "/Devices/ip/routes/%i:%s/%s", AddrType, hexAddr, Filename);
45 snprintf(path, 100, "/Devices/ip/routes/%i:%s/%s", AddrType, hexAddr, Filename);
46 return open(path, OPENFLAG_READ|OPENFLAG_WRITE);
50 int len = snprintf(NULL, 100, "/Devices/ip/routes/%i:%s", AddrType, hexAddr);
52 snprintf(path, 100, "/Devices/ip/routes/%i:%s", AddrType, hexAddr);
53 return open(path, OPENFLAG_READ);
57 //TODO: Move out to another file
58 char *Net_GetInterface(int AddressType, void *Address)
62 size = Net_GetAddressSize(AddressType);
65 fprintf(stderr, "BUG: AddressType = %i unknown\n", AddressType);
69 // Query the route manager for the route number
71 char buf[sizeof(int)+size];
74 fd = open("/Devices/ip/routes", 0);
76 fprintf(stderr, "ERROR: It seems that '/Devices/ip/routes' does not exist, are you running Acess2?\n");
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);
89 // Check answer validity
91 int len = sprintf(NULL, "/Devices/ip/routes/%i", routeNum);
95 sprintf(buf, "/Devices/ip/routes/%i", routeNum);
99 if( !fd ) return NULL; // Shouldn't happen :/
101 // Allocate space for name
102 ret = malloc( ioctl(fd, ioctl(fd, 3, "get_interface"), NULL) + 1 );
104 fprintf(stderr, "malloc() failure - Allocating space for interface name\n");
109 ioctl(fd, ioctl(fd, 3, "get_interface"), ret);
117 return NULL; // Error