+ // Routes
+ if( strcmp(argv[1], "route") == 0 )
+ {
+ // Add new route
+ if( argc > 2 && strcmp(argv[2], "add") == 0 )
+ {
+ uint8_t dest[16] = {0};
+ uint8_t nextHop[16] = {0};
+ int addrType, subnetBits = -1;
+ int nextHopType, nextHopBits=-1;
+ char *ifaceName = NULL;
+ int metric = DEFAULT_METRIC;
+ // Usage:
+ // ifconfig route add <host>[/<prefix>] <interface> [<metric>]
+ // ifconfig route add <host>[/<prefix>] <next hop> [<metric>]
+ if( argc - 3 < 2 ) {
+ fprintf(stderr, "ERROR: '%s route add' takes at least two arguments, %i passed\n",
+ argv[0], argc-3);
+ PrintUsage(argv[0]);
+ return -1;
+ }
+
+ if( argc - 3 > 3 ) {
+ fprintf(stderr, "ERROR: '%s route add' takes at most three arguments, %i passed\n",
+ argv[0], argc-3);
+ PrintUsage(argv[0]);
+ return -1;
+ }
+
+ // Destination IP
+ addrType = ParseIPAddress(argv[3], dest, &subnetBits);
+ if( subnetBits == -1 ) {
+ subnetBits = Net_GetAddressSize(addrType)*8;
+ }
+ // Interface Name / Next Hop
+ if( (nextHopType = ParseIPAddress(argv[4], nextHop, &nextHopBits)) == 0 )
+ {
+ // Interface name
+ ifaceName = argv[4];
+ }
+ else
+ {
+ // Next Hop
+ // - Check if it's the same type as the network/destination
+ if( nextHopType != addrType ) {
+ fprintf(stderr, "ERROR: Address type mismatch\n");
+ return -1;
+ }
+ // - Make sure there's no mask
+ if( nextHopBits != -1 ) {
+ fprintf(stderr, "Error: Next hop cannot be masked\n");
+ return -1;
+ }
+ }
+
+ // Metric
+ if( argc - 3 >= 3 )
+ {
+ metric = atoi(argv[5]);
+ if( metric == 0 && argv[5][0] != '0' ) {
+ fprintf(stderr, "ERROR: Metric should be a number\n");
+ return -1;
+ }
+ }
+
+ // Make the route!
+ AddRoute(ifaceName, addrType, dest, subnetBits, metric, nextHop);
+
+ return 0;
+ }
+ // Delete a route
+ else if( argc > 2 && strcmp(argv[2], "del") == 0 )
+ {
+ // Usage:
+ // ifconfig route del <routenum>
+ // ifconfig route del <host>[/<prefix>]
+ }
+ else
+ {
+ // List routes
+ DumpRoutes();
+ }
+ return 0;
+ }
+ // Add a new interface
+ else if( strcmp(argv[1], "add") == 0 )
+ {
+ if( argc < 4 ) {
+ fprintf(stderr, "ERROR: '%s add' requires two arguments, %i passed\n", argv[0], argc-2);
+ PrintUsage(argv[0]);
+ return -1;
+ }
+ // TODO: Also set the IP address as the usage says it does
+ ret = AddInterface( argv[2] );
+ if(ret < 0) return ret;
+ ret = SetAddress( ret, argv[3] );
+ return ret;
+ }
+ // Delete an interface
+ else if( strcmp(argv[1], "del") == 0 )
+ {