3 * - By John Hodge (thePowersGang)
6 * - `ip addr` sub command
11 int Addr_main(int argc, char *argv[]);
12 int AddInterface(const char *Device, int Type);
13 int SetAddress(int IFNum, const void *Address, int SubnetSize);
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 int type, subnet_size;
32 // Check argument counts
34 fprintf(stderr, "ERROR: '%s add' requires two arguments, %i passed\n",
41 type = ParseIPAddress(argv[3], addr, &subnet_size);
43 fprintf(stderr, "'%s' cannot be parsed as an IP address\n", argv[3]);
48 ret = AddInterface( argv[2], type );
49 if(ret < 0) return ret;
50 ret = SetAddress( ret, addr, subnet_size );
53 // Delete an interface
54 else if( strcmp(argv[1], "del") == 0 )
57 fprintf(stderr, "ERROR: '%s del' requires an argument\n", argv[0]);
65 DumpInterface(argv[1]);
71 * \brief Create a new interface using the passed device
72 * \param Device Network device to bind to
74 int AddInterface(const char *Device, int Type)
78 dp = _SysOpen(IPSTACK_ROOT, OPENFLAG_READ);
87 ret = _SysIOCtl(dp, 4, &ifinfo);
91 fprintf(stderr, "Unable to add '%s' as a network interface\n", Device);
95 printf("-- Added '"IPSTACK_ROOT"/%i' using device %s\n", ret, Device);
101 * \brief Set the address on an interface from a textual IP address
103 int SetAddress(int IFNum, const void *Address, int SubnetSize)
105 char path[sizeof(IPSTACK_ROOT)+1+5+1]; // ip000
109 sprintf(path, IPSTACK_ROOT"/%i", IFNum);
110 fd = _SysOpen(path, OPENFLAG_READ);
112 fprintf(stderr, "Unable to open '%s'\n", path);
117 _SysIOCtl(fd, _SysIOCtl(fd, 3, "set_address"), (void*)Address);
120 _SysIOCtl(fd, _SysIOCtl(fd, 3, "getset_subnet"), &SubnetSize);
125 //DumpInterface( path+sizeof(IPSTACK_ROOT)+1 );
131 * \brief Dump all interfaces
133 void DumpInterfaces(void)
136 char filename[FILENAME_MAX+1];
138 dp = _SysOpen(IPSTACK_ROOT, OPENFLAG_READ);
140 while( _SysReadDir(dp, filename) )
142 if(filename[0] == '.') continue;
143 DumpInterface(filename);
150 * \brief Dump an interface
152 void DumpInterface(const char *Name)
156 char path[sizeof(IPSTACK_ROOT)+1+FILENAME_MAX+1] = IPSTACK_ROOT"/";
160 fd = _SysOpen(path, OPENFLAG_READ);
162 fprintf(stderr, "Bad interface name '%s' (%s does not exist)\t", Name, path);
166 type = _SysIOCtl(fd, 4, NULL);
173 printf("%s:\t", Name);
175 int call_num = _SysIOCtl(fd, 3, "get_device");
176 int len = _SysIOCtl(fd, call_num, NULL);
177 char *buf = malloc(len+1);
178 _SysIOCtl(fd, call_num, buf);
179 printf("'%s'\n", buf);
183 // Get the address type
186 case 0: // Disabled/Unset
187 printf("DISABLED\n");
194 _SysIOCtl(fd, 5, ip); // Get IP Address
195 subnet = _SysIOCtl(fd, 7, NULL); // Get Subnet Bits
196 printf("%i.%i.%i.%i/%i\n", ip[0], ip[1], ip[2], ip[3], subnet);
204 _SysIOCtl(fd, 5, ip); // Get IP Address
205 subnet = _SysIOCtl(fd, 7, NULL); // Get Subnet Bits
206 printf("%x:%x:%x:%x:%x:%x:%x:%x/%i\n",
207 ntohs(ip[0]), ntohs(ip[1]), ntohs(ip[2]), ntohs(ip[3]),
208 ntohs(ip[4]), ntohs(ip[5]), ntohs(ip[6]), ntohs(ip[7]),
213 printf("UNKNOWN (%i)\n", type);