Usermode/AxWin4 - Mouse input in progress
[tpg/acess2.git] / Usermode / Applications / ip_src / addr.c
index cf5d5dc..2b048c5 100644 (file)
@@ -9,8 +9,8 @@
 
 // === PROTOTYPES ===
  int   Addr_main(int argc, char *argv[]);
- int   AddInterface(const char *Device);
- int   SetAddress(int IFNum, const char *Address);
+ int   AddInterface(const char *Device, int Type);
+ int   SetAddress(int IFNum, const void *Address, int SubnetSize);
 void   DumpInterfaces(void);
 void   DumpInterface(const char *Name);
 void   DumpRoute(const char *Name);
@@ -26,14 +26,28 @@ int Addr_main(int argc, char *argv[])
        }
        else if( strcmp(argv[1], "add") == 0 )
        {
+               uint8_t addr[16];
+                int    type, subnet_size;
+
+               // Check argument counts
                if( argc - 2 < 2 ) {
-                       fprintf(stderr, "ERROR: '%s add' requires two arguments, %i passed\n", argv[0], argc-2);
+                       fprintf(stderr, "ERROR: '%s add' requires two arguments, %i passed\n",
+                               argv[0], argc-2);
                        PrintUsage(argv[0]);
                        return -1;
                }
-               ret = AddInterface( argv[2] );
+
+               // Parse IP Address
+               type = ParseIPAddress(argv[3], addr, &subnet_size);
+               if(type == 0) {
+                       fprintf(stderr, "'%s' cannot be parsed as an IP address\n", argv[3]);
+                       return -1;
+               }
+       
+               // Fun
+               ret = AddInterface( argv[2], type );
                if(ret < 0)     return ret;
-               ret = SetAddress( ret, argv[3] );
+               ret = SetAddress( ret, addr, subnet_size );
                return ret;
        }
        // Delete an interface
@@ -57,13 +71,21 @@ int Addr_main(int argc, char *argv[])
  * \brief Create a new interface using the passed device
  * \param Device       Network device to bind to
  */
-int AddInterface(const char *Device)
+int AddInterface(const char *Device, int Type)
 {
         int    dp, ret;
        
-       dp = open(IPSTACK_ROOT, OPENFLAG_READ);
-       ret = ioctl(dp, 4, (void*)Device);
-       close(dp);
+       dp = _SysOpen(IPSTACK_ROOT, OPENFLAG_READ);
+       struct {
+               const char *Dev;
+               const char *Name;
+                int    Type;
+       } ifinfo;
+       ifinfo.Dev = Device;
+       ifinfo.Name = "";
+       ifinfo.Type = Type;
+       ret = _SysIOCtl(dp, 4, &ifinfo);
+       _SysClose(dp);
        
        if( ret < 0 ) {
                fprintf(stderr, "Unable to add '%s' as a network interface\n", Device);
@@ -78,42 +100,26 @@ int AddInterface(const char *Device)
 /**
  * \brief Set the address on an interface from a textual IP address
  */
-int SetAddress(int IFNum, const char *Address)
+int SetAddress(int IFNum, const void *Address, int SubnetSize)
 {
-       uint8_t addr[16];
-        int    type;
        char    path[sizeof(IPSTACK_ROOT)+1+5+1];       // ip000
-        int    tmp, fd, subnet;
-       
-       // Parse IP Address
-       type = ParseIPAddress(Address, addr, &subnet);
-       if(type == 0) {
-               fprintf(stderr, "'%s' cannot be parsed as an IP address\n", Address);
-               return -1;
-       }
+        int    fd;
        
        // Open file
        sprintf(path, IPSTACK_ROOT"/%i", IFNum);
-       fd = open(path, OPENFLAG_READ);
+       fd = _SysOpen(path, OPENFLAG_READ);
        if( fd == -1 ) {
                fprintf(stderr, "Unable to open '%s'\n", path);
                return -1;
        }
        
-       tmp = type;
-       tmp = ioctl(fd, ioctl(fd, 3, "getset_type"), &tmp);
-       if( tmp != type ) {
-               fprintf(stderr, "Error in setting address type (got %i, expected %i)\n", tmp, type);
-               close(fd);
-               return -1;
-       }
        // Set Address
-       ioctl(fd, ioctl(fd, 3, "set_address"), addr);
+       _SysIOCtl(fd, _SysIOCtl(fd, 3, "set_address"), (void*)Address);
        
        // Set Subnet
-       ioctl(fd, ioctl(fd, 3, "getset_subnet"), &subnet);
+       _SysIOCtl(fd, _SysIOCtl(fd, 3, "getset_subnet"), &SubnetSize);
        
-       close(fd);
+       _SysClose(fd);
        
        // Dump!
        //DumpInterface( path+sizeof(IPSTACK_ROOT)+1 );
@@ -129,15 +135,15 @@ void DumpInterfaces(void)
         int    dp;
        char    filename[FILENAME_MAX+1];
        
-       dp = open(IPSTACK_ROOT, OPENFLAG_READ);
+       dp = _SysOpen(IPSTACK_ROOT, OPENFLAG_READ);
        
-       while( SysReadDir(dp, filename) )
+       while( _SysReadDir(dp, filename) )
        {
                if(filename[0] == '.')  continue;
                DumpInterface(filename);
        }
        
-       close(dp);
+       _SysClose(dp);
 }
 
 /**
@@ -151,13 +157,13 @@ void DumpInterface(const char *Name)
        
        strcat(path, Name);
        
-       fd = open(path, OPENFLAG_READ);
+       fd = _SysOpen(path, OPENFLAG_READ);
        if(fd == -1) {
                fprintf(stderr, "Bad interface name '%s' (%s does not exist)\t", Name, path);
                return ;
        }
        
-       type = ioctl(fd, 4, NULL);
+       type = _SysIOCtl(fd, 4, NULL);
        
        // Ignore -1 values
        if( type == -1 ) {
@@ -166,10 +172,10 @@ void DumpInterface(const char *Name)
        
        printf("%s:\t", Name);
        {
-                int    call_num = ioctl(fd, 3, "get_device");
-                int    len = ioctl(fd, call_num, NULL);
+                int    call_num = _SysIOCtl(fd, 3, "get_device");
+                int    len = _SysIOCtl(fd, call_num, NULL);
                char    *buf = malloc(len+1);
-               ioctl(fd, call_num, buf);
+               _SysIOCtl(fd, call_num, buf);
                printf("'%s'\n", buf);
                free(buf);
        }
@@ -185,8 +191,8 @@ void DumpInterface(const char *Name)
                uint8_t ip[4];
                 int    subnet;
                printf("IPv4\t");
-               ioctl(fd, 5, ip);       // Get IP Address
-               subnet = ioctl(fd, 7, NULL);    // Get Subnet Bits
+               _SysIOCtl(fd, 5, ip);   // Get IP Address
+               subnet = _SysIOCtl(fd, 7, NULL);        // Get Subnet Bits
                printf("%i.%i.%i.%i/%i\n", ip[0], ip[1], ip[2], ip[3], subnet);
                }
                break;
@@ -195,8 +201,8 @@ void DumpInterface(const char *Name)
                uint16_t        ip[8];
                 int    subnet;
                printf("IPv6\t");
-               ioctl(fd, 5, ip);       // Get IP Address
-               subnet = ioctl(fd, 7, NULL);    // Get Subnet Bits
+               _SysIOCtl(fd, 5, ip);   // Get IP Address
+               subnet = _SysIOCtl(fd, 7, NULL);        // Get Subnet Bits
                printf("%x:%x:%x:%x:%x:%x:%x:%x/%i\n",
                        ntohs(ip[0]), ntohs(ip[1]), ntohs(ip[2]), ntohs(ip[3]),
                        ntohs(ip[4]), ntohs(ip[5]), ntohs(ip[6]), ntohs(ip[7]),
@@ -208,6 +214,6 @@ void DumpInterface(const char *Name)
                break;
        }
                        
-       close(fd);
+       _SysClose(fd);
 }
 

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