3 * - By John Hodge (thePowersGang)
6 * - `ip addr` sub command
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);
19 int Addr_main(int argc, char *argv[])
27 else if( strcmp(argv[1], "add") == 0 )
30 fprintf(stderr, "ERROR: '%s add' requires two arguments, %i passed\n", argv[0], argc-2);
34 ret = AddInterface( argv[2] );
35 if(ret < 0) return ret;
36 ret = SetAddress( ret, argv[3] );
39 // Delete an interface
40 else if( strcmp(argv[1], "del") == 0 )
43 fprintf(stderr, "ERROR: '%s del' requires an argument\n", argv[0]);
51 DumpInterface(argv[1]);
57 * \brief Create a new interface using the passed device
58 * \param Device Network device to bind to
60 int AddInterface(const char *Device)
64 dp = open(IPSTACK_ROOT, OPENFLAG_READ);
65 ret = ioctl(dp, 4, (void*)Device);
69 fprintf(stderr, "Unable to add '%s' as a network interface\n", Device);
73 printf("-- Added '"IPSTACK_ROOT"/%i' using device %s\n", ret, Device);
79 * \brief Set the address on an interface from a textual IP address
81 int SetAddress(int IFNum, const char *Address)
85 char path[sizeof(IPSTACK_ROOT)+1+5+1]; // ip000
89 type = ParseIPAddress(Address, addr, &subnet);
91 fprintf(stderr, "'%s' cannot be parsed as an IP address\n", Address);
96 sprintf(path, IPSTACK_ROOT"/%i", IFNum);
97 fd = open(path, OPENFLAG_READ);
99 fprintf(stderr, "Unable to open '%s'\n", path);
104 tmp = ioctl(fd, ioctl(fd, 3, "getset_type"), &tmp);
106 fprintf(stderr, "Error in setting address type (got %i, expected %i)\n", tmp, type);
111 ioctl(fd, ioctl(fd, 3, "set_address"), addr);
114 ioctl(fd, ioctl(fd, 3, "getset_subnet"), &subnet);
119 //DumpInterface( path+sizeof(IPSTACK_ROOT)+1 );
125 * \brief Dump all interfaces
127 void DumpInterfaces(void)
130 char filename[FILENAME_MAX+1];
132 dp = open(IPSTACK_ROOT, OPENFLAG_READ);
134 while( SysReadDir(dp, filename) )
136 if(filename[0] == '.') continue;
137 DumpInterface(filename);
144 * \brief Dump an interface
146 void DumpInterface(const char *Name)
150 char path[sizeof(IPSTACK_ROOT)+1+FILENAME_MAX+1] = IPSTACK_ROOT"/";
154 fd = open(path, OPENFLAG_READ);
156 fprintf(stderr, "Bad interface name '%s' (%s does not exist)\t", Name, path);
160 type = ioctl(fd, 4, NULL);
167 printf("%s:\t", Name);
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);
177 // Get the address type
180 case 0: // Disabled/Unset
181 printf("DISABLED\n");
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);
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]),
207 printf("UNKNOWN (%i)\n", type);