Usermode - Renamed readdir() to SysReadDir()
[tpg/acess2.git] / Usermode / Applications / ip_src / addr.c
1 /*
2  * Acess2 ip command
3  * - By John Hodge (thePowersGang)
4  * 
5  * addr.c
6  * - `ip addr` sub command
7  */
8 #include "common.h"
9
10 // === PROTOTYPES ===
11  int    Addr_main(int argc, char *argv[]);
12  int    AddInterface(const char *Device);
13  int    SetAddress(int IFNum, const char *Address);
14 void    DumpInterfaces(void);
15 void    DumpInterface(const char *Name);
16 void    DumpRoute(const char *Name);
17
18 // === CODE ===
19 int Addr_main(int argc, char *argv[])
20 {
21          int    ret;
22         if( argc <= 1 )
23         {
24                 // No action
25                 DumpInterfaces();
26         }
27         else if( strcmp(argv[1], "add") == 0 )
28         {
29                 if( argc - 2 < 2 ) {
30                         fprintf(stderr, "ERROR: '%s add' requires two arguments, %i passed\n", argv[0], argc-2);
31                         PrintUsage(argv[0]);
32                         return -1;
33                 }
34                 ret = AddInterface( argv[2] );
35                 if(ret < 0)     return ret;
36                 ret = SetAddress( ret, argv[3] );
37                 return ret;
38         }
39         // Delete an interface
40         else if( strcmp(argv[1], "del") == 0 )
41         {
42                 if( argc - 2 < 1 ) {
43                         fprintf(stderr, "ERROR: '%s del' requires an argument\n", argv[0]);
44                         PrintUsage(argv[0]);
45                         return -1;
46                 }
47         }
48         else
49         {
50                 // Show named iface?
51                 DumpInterface(argv[1]);
52         }
53         return 0;
54 }
55
56 /**
57  * \brief Create a new interface using the passed device
58  * \param Device        Network device to bind to
59  */
60 int AddInterface(const char *Device)
61 {
62          int    dp, ret;
63         
64         dp = open(IPSTACK_ROOT, OPENFLAG_READ);
65         ret = ioctl(dp, 4, (void*)Device);
66         close(dp);
67         
68         if( ret < 0 ) {
69                 fprintf(stderr, "Unable to add '%s' as a network interface\n", Device);
70                 return -1;
71         }
72         
73         printf("-- Added '"IPSTACK_ROOT"/%i' using device %s\n", ret, Device);
74         
75         return ret;
76 }
77
78 /**
79  * \brief Set the address on an interface from a textual IP address
80  */
81 int SetAddress(int IFNum, const char *Address)
82 {
83         uint8_t addr[16];
84          int    type;
85         char    path[sizeof(IPSTACK_ROOT)+1+5+1];       // ip000
86          int    tmp, fd, subnet;
87         
88         // Parse IP Address
89         type = ParseIPAddress(Address, addr, &subnet);
90         if(type == 0) {
91                 fprintf(stderr, "'%s' cannot be parsed as an IP address\n", Address);
92                 return -1;
93         }
94         
95         // Open file
96         sprintf(path, IPSTACK_ROOT"/%i", IFNum);
97         fd = open(path, OPENFLAG_READ);
98         if( fd == -1 ) {
99                 fprintf(stderr, "Unable to open '%s'\n", path);
100                 return -1;
101         }
102         
103         tmp = type;
104         tmp = ioctl(fd, ioctl(fd, 3, "getset_type"), &tmp);
105         if( tmp != type ) {
106                 fprintf(stderr, "Error in setting address type (got %i, expected %i)\n", tmp, type);
107                 close(fd);
108                 return -1;
109         }
110         // Set Address
111         ioctl(fd, ioctl(fd, 3, "set_address"), addr);
112         
113         // Set Subnet
114         ioctl(fd, ioctl(fd, 3, "getset_subnet"), &subnet);
115         
116         close(fd);
117         
118         // Dump!
119         //DumpInterface( path+sizeof(IPSTACK_ROOT)+1 );
120         
121         return 0;
122 }
123
124 /**
125  * \brief Dump all interfaces
126  */
127 void DumpInterfaces(void)
128 {
129          int    dp;
130         char    filename[FILENAME_MAX+1];
131         
132         dp = open(IPSTACK_ROOT, OPENFLAG_READ);
133         
134         while( SysReadDir(dp, filename) )
135         {
136                 if(filename[0] == '.')  continue;
137                 DumpInterface(filename);
138         }
139         
140         close(dp);
141 }
142
143 /**
144  * \brief Dump an interface
145  */
146 void DumpInterface(const char *Name)
147 {
148          int    fd;
149          int    type;
150         char    path[sizeof(IPSTACK_ROOT)+1+FILENAME_MAX+1] = IPSTACK_ROOT"/";
151         
152         strcat(path, Name);
153         
154         fd = open(path, OPENFLAG_READ);
155         if(fd == -1) {
156                 fprintf(stderr, "Bad interface name '%s' (%s does not exist)\t", Name, path);
157                 return ;
158         }
159         
160         type = ioctl(fd, 4, NULL);
161         
162         // Ignore -1 values
163         if( type == -1 ) {
164                 return ;
165         }
166         
167         printf("%s:\t", Name);
168         {
169                  int    call_num = ioctl(fd, 3, "get_device");
170                  int    len = ioctl(fd, call_num, NULL);
171                 char    *buf = malloc(len+1);
172                 ioctl(fd, call_num, buf);
173                 printf("'%s'\n", buf);
174                 free(buf);
175         }
176         printf("\t");
177         // Get the address type
178         switch(type)
179         {
180         case 0: // Disabled/Unset
181                 printf("DISABLED\n");
182                 break;
183         case 4: // IPv4
184                 {
185                 uint8_t ip[4];
186                  int    subnet;
187                 printf("IPv4\t");
188                 ioctl(fd, 5, ip);       // Get IP Address
189                 subnet = ioctl(fd, 7, NULL);    // Get Subnet Bits
190                 printf("%i.%i.%i.%i/%i\n", ip[0], ip[1], ip[2], ip[3], subnet);
191                 }
192                 break;
193         case 6: // IPv6
194                 {
195                 uint16_t        ip[8];
196                  int    subnet;
197                 printf("IPv6\t");
198                 ioctl(fd, 5, ip);       // Get IP Address
199                 subnet = ioctl(fd, 7, NULL);    // Get Subnet Bits
200                 printf("%x:%x:%x:%x:%x:%x:%x:%x/%i\n",
201                         ntohs(ip[0]), ntohs(ip[1]), ntohs(ip[2]), ntohs(ip[3]),
202                         ntohs(ip[4]), ntohs(ip[5]), ntohs(ip[6]), ntohs(ip[7]),
203                         subnet);
204                 }
205                 break;
206         default:        // Unknow
207                 printf("UNKNOWN (%i)\n", type);
208                 break;
209         }
210                         
211         close(fd);
212 }
213

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