+void AddRoute(const char *Interface, int AddressType, void *Dest, int MaskBits, int Metric, void *NextHop)
+{
+ int fd;
+ int num;
+ char *ifaceToFree = NULL;
+
+ // Get interface name
+ if( !Interface )
+ {
+ if( !NextHop ) {
+ fprintf(stderr,
+ "BUG: AddRoute(Interface=NULL,...,NextHop=NULL)\n"
+ "Only one should be NULL\n"
+ );
+ return ;
+ }
+
+ // Query for the interface name
+ Interface = ifaceToFree = Net_GetInterface(AddressType, NextHop);
+ }
+ // Check address type (if the interface was passed)
+ // - If we got the interface name, then it should be correct
+ else
+ {
+ char ifacePath[sizeof(IPSTACK_ROOT"/")+strlen(Interface)+1];
+
+ // Open interface
+ strcpy(ifacePath, IPSTACK_ROOT"/");
+ strcat(ifacePath, Interface);
+ fd = open(ifacePath, 0);
+ if( fd == -1 ) {
+ fprintf(stderr, "Error: Interface '%s' does not exist\n", Interface);
+ return ;
+ }
+ // Get and check type
+ num = ioctl(fd, ioctl(fd, 3, "getset_type"), NULL);
+ if( num != AddressType ) {
+ fprintf(stderr, "Error: Passed type does not match interface type (%i != %i)\n",
+ AddressType, num);
+ return ;
+ }
+
+ close(fd);
+ }
+
+ // Create route
+ int addrsize = Net_GetAddressSize(AddressType);
+ int len = snprintf(NULL, 0, "/Devices/ip/routes/%i::%i:%i", AddressType, MaskBits, Metric) + addrsize*2;
+ char path[len+1];
+ {
+ int i, ofs;
+ ofs = sprintf(path, "/Devices/ip/routes/%i:", AddressType);
+ for( i = 0; i < addrsize; i ++ )
+ sprintf(path+ofs+i*2, "%02x", ((uint8_t*)Dest)[i]);
+ ofs += addrsize*2;
+ sprintf(path+ofs, ":%i:%i", MaskBits, Metric);
+ }
+
+ fd = open(path, 0);
+ if( fd != -1 ) {
+ close(fd);
+ fprintf(stderr, "Unable to create route '%s', already exists\n", path);
+ return ;
+ }
+ fd = open(path, OPENFLAG_CREATE, 0);
+ if( fd == -1 ) {
+ fprintf(stderr, "Unable to create '%s'\n", path);
+ return ;
+ }
+
+ if( NextHop )
+ ioctl(fd, ioctl(fd, 3, "set_nexthop"), NextHop);
+ ioctl(fd, ioctl(fd, 3, "set_interface"), (void*)Interface);
+
+ close(fd);
+
+ // Check if the interface name was allocated by us
+ if( ifaceToFree )
+ free(ifaceToFree);
+}
+
+/**
+ * \note Debugging HACK!
+ * \brief Autoconfigure the specified device to 10.0.2.55/24 using
+ * 10.0.2.2 as the gateway.
+ */
+int DoAutoConfig(const char *Device)