2 * Acess2 Networking Toolkit
3 * By John Hodge (thePowersGang)
6 * - Library main (and misc functions)
21 int Net_GetAddressSize(int AddressType)
25 case 4: return 4; // IPv4
26 case 6: return 16; // IPv6
32 int Net_OpenSocket(int AddrType, void *Addr, const char *Filename)
34 int addrLen = Net_GetAddressSize(AddrType);
36 uint8_t *addrBuffer = Addr;
37 char hexAddr[addrLen*2+1];
39 for( i = 0; i < addrLen; i ++ )
40 sprintf(hexAddr+i*2, "%02x", addrBuffer[i]);
44 int len = snprintf(NULL, 100, "/Devices/ip/routes/@%i:%s/%s", AddrType, hexAddr, Filename);
46 snprintf(path, 100, "/Devices/ip/routes/@%i:%s/%s", AddrType, hexAddr, Filename);
47 return open(path, OPENFLAG_READ|OPENFLAG_WRITE);
51 int len = snprintf(NULL, 100, "/Devices/ip/routes/@%i:%s", AddrType, hexAddr);
53 snprintf(path, 100, "/Devices/ip/routes/@%i:%s", AddrType, hexAddr);
54 return open(path, OPENFLAG_READ);
58 //TODO: Move out to another file
59 char *Net_GetInterface(int AddressType, void *Address)
63 size = Net_GetAddressSize(AddressType);
66 fprintf(stderr, "BUG: AddressType = %i unknown\n", AddressType);
70 // Query the route manager for the route number
72 char buf[sizeof(int)+size];
73 uint32_t *type = (void*)buf;
76 fd = open("/Devices/ip/routes", 0);
78 fprintf(stderr, "ERROR: It seems that '/Devices/ip/routes' does not exist, are you running Acess2?\n");
82 // Make structure and ask
84 memcpy(type+1, Address, size);
85 routeNum = ioctl(fd, ioctl(fd, 3, "locate_route"), buf);
91 // Check answer validity
93 int len = sprintf(NULL, "/Devices/ip/routes/#%i", routeNum);
97 sprintf(buf, "/Devices/ip/routes/#%i", routeNum);
102 fprintf(stderr, "Net_GetInterface - ERROR: Unabel to open %s\n", buf);
103 return NULL; // Shouldn't happen :/
106 // Allocate space for name
107 ret = malloc( ioctl(fd, ioctl(fd, 3, "get_interface"), NULL) + 1 );
109 fprintf(stderr, "malloc() failure - Allocating space for interface name\n");
114 ioctl(fd, ioctl(fd, 3, "get_interface"), ret);
122 return NULL; // Error