2 * Acess2 IFCONFIG command
10 #define FILENAME_MAX 255
11 #define IPSTACK_ROOT "/Devices/ip"
14 void PrintUsage(const char *ProgName);
15 void DumpInterfaces(void);
16 void DumpInterface(const char *Name);
17 int AddInterface(const char *Device);
18 int DoAutoConfig(const char *Device);
22 * \brief Program entrypoint
24 int main(int argc, char *argv[])
26 // No args, dump interfaces
32 // Add a new interface
33 if( strcmp(argv[1], "add") == 0 ) {
35 fprintf(stderr, "ERROR: %s add require two arguments, %i passed\n", argv[0], argc-2);
39 // TODO: Also set the IP address as the usage says it does
40 return AddInterface( argv[2] );
43 // Autoconfigure an interface
44 // NOTE: Debugging hack (see the function for more details)
45 if( strcmp(argv[1], "autoconf") == 0 ) {
46 DoAutoConfig(argv[2]);
50 // Print usage instructions
57 * \brief Print usage instructions
59 void PrintUsage(const char *ProgName)
61 fprintf(stderr, "Usage:\n");
62 fprintf(stderr, " %s add <device> <ip>/<prefix>\n", ProgName);
63 fprintf(stderr, " Add a new interface listening on <device> with the specified\n");
64 fprintf(stderr, " address.\n");
65 fprintf(stderr, " %s del <interface>\n", ProgName);
66 fprintf(stderr, " %s set <interface> <option> <value>\n", ProgName);
67 fprintf(stderr, " Set an option on an interface, a list of valid options follows\n");
68 fprintf(stderr, " gw IPv4 default gateway\n");
69 fprintf(stderr, " %s [<interface>]\n", ProgName);
70 fprintf(stderr, " Print the current interfaces (or only <interface> if passed)\n");
71 fprintf(stderr, "\n");
72 fprintf(stderr, "A note on Acess's IP Stack:\n");
73 fprintf(stderr, " Each interface corresponds to only one IP address (either IPv4\n");
74 fprintf(stderr, " or IPv6). A network device can have multiple interfaces bound\n");
75 fprintf(stderr, " to it, allowing multiple addresses for one network connection\n");
76 fprintf(stderr, "\n");
80 * \brief Dump all interfaces
82 void DumpInterfaces(void)
85 char filename[FILENAME_MAX+1];
87 dp = open(IPSTACK_ROOT, OPENFLAG_READ);
89 while( readdir(dp, filename) )
91 if(filename[0] == '.') continue;
92 DumpInterface(filename);
99 * \brief Dump an interface
101 void DumpInterface(const char *Name)
105 char path[sizeof(IPSTACK_ROOT)+1+FILENAME_MAX+1] = IPSTACK_ROOT"/";
109 fd = open(path, OPENFLAG_READ);
111 printf("%s:\tUnable to open ('%s')\n", Name, path);
115 type = ioctl(fd, 4, NULL);
117 printf("%s:\t", Name);
119 int call_num = ioctl(fd, 3, "get_device");
120 int len = ioctl(fd, call_num, NULL);
121 char *buf = malloc(len+1);
122 ioctl(fd, call_num, buf);
123 printf("'%s'\t", buf);
126 // Get the address type
129 case 0: // Disabled/Unset
130 printf("DISABLED\n");
137 ioctl(fd, 5, ip); // Get IP Address
138 subnet = ioctl(fd, 7, NULL); // Get Subnet Bits
139 printf("\tAddress: %i.%i.%i.%i/%i\n", ip[0], ip[1], ip[2], ip[3], subnet);
140 ioctl(fd, 8, ip); // Get Gateway
141 printf("\tGateway: %i.%i.%i.%i\n", ip[0], ip[1], ip[2], ip[3]);
149 ioctl(fd, 5, ip); // Get IP Address
150 subnet = ioctl(fd, 7, NULL); // Get Subnet Bits
151 printf("\t%x:%x:%x:%x:%x:%x:%x:%x/%i\n",
152 ip[0], ip[1], ip[2], ip[3],
153 ip[4], ip[5], ip[6], ip[7],
158 printf("UNKNOWN (%i)\n", type);
167 * \brief Create a new interface using the passed device
168 * \param Device Network device to bind to
170 int AddInterface(const char *Device)
174 dp = open(IPSTACK_ROOT, OPENFLAG_READ);
175 ret = ioctl(dp, 4, (void*)Device);
176 printf("AddInterface: ret = 0x%x = %i\n", ret, ret);
180 fprintf(stderr, "Unable to add '%s' as a network interface\n", Device);
184 printf("-- Added '"IPSTACK_ROOT"/%i' using device %s\n", ret, Device);
190 * \note Debugging HACK!
191 * \brief Autoconfigure the specified device to 10.0.2.55/8 using
192 * 10.0.2.1 as the gateway.
194 int DoAutoConfig(const char *Device)
197 char path[sizeof(IPSTACK_ROOT)+5+1]; // ip000
198 uint8_t addr[4] = {10,0,2,55};
199 uint8_t gw[4] = {10,0,2,1};
202 tmp = AddInterface(Device);
203 if( tmp < 0 ) return tmp;
205 sprintf(path, IPSTACK_ROOT"/%i", tmp);
207 fd = open(path, OPENFLAG_READ);
209 fprintf(stderr, "Unable to open '%s'\n", path);
214 tmp = ioctl(fd, ioctl(fd, 3, "getset_type"), &tmp);
216 fprintf(stderr, "Error in setting address type (got %i, expected 4)\n", tmp);
220 ioctl(fd, ioctl(fd, 3, "set_address"), addr);
222 ioctl(fd, ioctl(fd, 3, "getset_subnet"), &subnet);
224 ioctl(fd, ioctl(fd, 3, "set_gateway"), gw);
228 printf("Set address to %i.%i.%i.%i/%i (GW: %i.%i.%i.%i)\n",
229 addr[0], addr[1], addr[2], addr[3],
231 gw[0], gw[1], gw[2], gw[3]);