Misc warning killing
[tpg/acess2.git] / Usermode / Applications / ifconfig_src / main.c
index 9aa9d72..334ea13 100644 (file)
@@ -46,13 +46,13 @@ int main(int argc, char *argv[])
        if( strcmp(argv[1], "route") == 0 )
        {
                // Add new route
-               if( strcmp(argv[2], "add") == 0 )
+               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;
+                       char    *ifaceName = NULL;
                         int    metric = DEFAULT_METRIC;
                        // Usage:
                        // ifconfig route add <host>[/<prefix>] <interface> [<metric>]
@@ -113,7 +113,7 @@ int main(int argc, char *argv[])
                        return 0;
                }
                // Delete a route
-               else if( strcmp(argv[2], "del") == 0 )
+               else if( argc > 2 && strcmp(argv[2], "del") == 0 )
                {
                        // Usage:
                        // ifconfig route del <routenum>
@@ -183,12 +183,12 @@ void PrintUsage(const char *ProgName)
        fprintf(stderr, "    %s [<interface>]\n", ProgName);
        fprintf(stderr, "        Print the current interfaces (or only <interface> if passed)\n");
        fprintf(stderr, "\n");
-       fprintf(stderr, "    %s routes\n", ProgName);
+       fprintf(stderr, "    %s route\n", ProgName);
        fprintf(stderr, "        Print the routing tables\n");
-       fprintf(stderr, "    %s routes add <host>[/<prefix>] [<nexthop> OR <iface>] [<metric>]\n", ProgName);
+       fprintf(stderr, "    %s route add <host>[/<prefix>] [<nexthop> OR <iface>] [<metric>]\n", ProgName);
        fprintf(stderr, "        Add a new route\n");
-       fprintf(stderr, "    %s routes del <host>[/<prefix>]\n", ProgName);
-       fprintf(stderr, "    %s routes del <routenum>\n", ProgName);
+       fprintf(stderr, "    %s route del <host>[/<prefix>]\n", ProgName);
+       fprintf(stderr, "    %s route del <routenum>\n", ProgName);
        fprintf(stderr, "        Add a new route\n");
        fprintf(stderr, "\n");
        fprintf(stderr, "A note on Acess's IP Stack:\n");
@@ -227,6 +227,8 @@ void DumpRoutes(void)
        
        dp = open(IPSTACK_ROOT"/routes", OPENFLAG_READ);
        
+       printf("Type\tNetwork \tGateway \tMetric\tIFace\n");
+       
        while( readdir(dp, filename) )
        {
                if(filename[0] == '.')  continue;
@@ -325,22 +327,24 @@ void DumpRoute(const char *Name)
                return ;
        }
        
-       type = ioctl(fd, 4, NULL);
-       
-       // Ignore -1 values
-       if( type == -1 ) {
-               return ;
-       }
-       
-       printf("%s:\t", Name);
-       {
-                int    call_num = ioctl(fd, 3, "get_interface");
-                int    len = ioctl(fd, call_num, NULL);
-               char    *buf = malloc(len+1);
-               ioctl(fd, call_num, buf);
-               printf("'%s'\t", buf);
-               free(buf);
+        int    ofs = 2;
+       type = atoi(Name);
+       
+        int    i;
+        int    len = Net_GetAddressSize(type);
+       uint8_t net[len], gw[len];
+        int    subnet, metric;
+       for( i = 0; i < len; i ++ ) {
+               char tmp[5] = "0x00";
+               tmp[2] = Name[ofs++];
+               tmp[3] = Name[ofs++];
+               net[i] = atoi(tmp);
        }
+       ofs ++;
+       subnet = atoi(Name+ofs);
+       ofs ++;
+       metric = atoi(Name+ofs);
+       ioctl(fd, ioctl(fd, 3, "get_nexthop"), gw);     // Get Gateway/NextHop
        
        // Get the address type
        switch(type)
@@ -349,37 +353,29 @@ void DumpRoute(const char *Name)
                printf("DISABLED\n");
                break;
        case 4: // IPv4
-               {
-               uint8_t net[4], addr[4];
-                int    subnet, metric;
-               printf("IPv4\n");
-               ioctl(fd, ioctl(fd, 3, "get_network"), net);    // Get Network
-               ioctl(fd, ioctl(fd, 3, "get_nexthop"), addr);   // Get Gateway/NextHop
-               subnet = ioctl(fd, ioctl(fd, 3, "getset_subnetbits"), NULL);    // Get Subnet Bits
-               metric = ioctl(fd, ioctl(fd, 3, "getset_metric"), NULL);        // Get Subnet Bits
-               printf("\tNetwork: %s/%i\n", Net_PrintAddress(4, net), subnet);
-               printf("\tGateway: %s\n", Net_PrintAddress(4, addr));
-               printf("\tMetric:  %i\n", metric);
-               }
+               printf("IPv4\t");
                break;
        case 6: // IPv6
-               {
-               uint16_t        net[8], addr[8];
-                int    subnet, metric;
-               printf("IPv6\n");
-               ioctl(fd, ioctl(fd, 3, "get_network"), net);    // Get Network
-               ioctl(fd, ioctl(fd, 3, "get_nexthop"), addr);   // Get Gateway/NextHop
-               subnet = ioctl(fd, ioctl(fd, 3, "getset_subnetbits"), NULL);    // Get Subnet Bits
-               metric = ioctl(fd, ioctl(fd, 3, "getset_metric"), NULL);        // Get Subnet Bits
-               printf("\tNetwork: %s/%i\n", Net_PrintAddress(6, net), subnet);
-               printf("\tGateway: %s\n", Net_PrintAddress(6, addr));
-               printf("\tMetric:  %i\n", metric);
-               }
+               printf("IPv6\t");
                break;
        default:        // Unknow
                printf("UNKNOWN (%i)\n", type);
                break;
        }
+       printf("%s/%i\t", Net_PrintAddress(type, net), subnet);
+       printf("%s \t", Net_PrintAddress(type, gw));
+       printf("%i\t", metric);
+       
+       // Interface
+       {
+                int    call_num = ioctl(fd, 3, "get_interface");
+                int    len = ioctl(fd, call_num, NULL);
+               char    *buf = malloc(len+1);
+               ioctl(fd, call_num, buf);
+               printf("'%s'\t", buf);
+               free(buf);
+       }
+       
        printf("\n");
                        
        close(fd);
@@ -411,7 +407,6 @@ void AddRoute(const char *Interface, int AddressType, void *Dest, int MaskBits,
 {
         int    fd;
         int    num;
-       char    tmp[sizeof(IPSTACK_ROOT"/routes/") + 5];        // enough for 4 digits
        char    *ifaceToFree = NULL;
        
        // Get interface name
@@ -454,19 +449,33 @@ void AddRoute(const char *Interface, int AddressType, void *Dest, int MaskBits,
        }
        
        // Create route
-       fd = open(IPSTACK_ROOT"/routes", 0);
-       num = ioctl(fd, ioctl(fd, 3, "add_route"), (char*)Interface);
-       close(fd);
-       
-       // Open route
-       sprintf(tmp, IPSTACK_ROOT"/routes/%i", num);
-       fd = open(tmp, 0);
+        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 ;
+       }
        
-       ioctl(fd, ioctl(fd, 3, "set_network"), Dest);
        if( NextHop )
                ioctl(fd, ioctl(fd, 3, "set_nexthop"), NextHop);
-       ioctl(fd, ioctl(fd, 3, "getset_subnetbits"), &MaskBits);
-       ioctl(fd, ioctl(fd, 3, "getset_metric"), &Metric);
+       ioctl(fd, ioctl(fd, 3, "set_interface"), (void*)Interface);
        
        close(fd);
        
@@ -477,8 +486,8 @@ void AddRoute(const char *Interface, int AddressType, void *Dest, int MaskBits,
 
 /**
  * \note Debugging HACK!
- * \brief Autoconfigure the specified device to 10.0.2.55/8 using
- *        10.0.2.1 as the gateway.
+ * \brief Autoconfigure the specified device to 10.0.2.55/24 using
+ *        10.0.2.2 as the gateway.
  */
 int DoAutoConfig(const char *Device)
 {

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