2 * Acess2 IFCONFIG command
10 #define FILENAME_MAX 255
11 #define IPSTACK_ROOT "/Devices/ip"
14 void PrintUsage(char *ProgName);
15 void DumpInterfaces( int DumpAll );
16 int AddInterface( char *Address );
17 int DoAutoConfig( char *Device );
21 * \fn int main(int argc, char *argv[])
24 int main(int argc, char *argv[])
31 if( strcmp(argv[1], "add") == 0 ) {
33 fprintf(stderr, "ERROR: `add` requires an argument\n");
37 return AddInterface( argv[2] );
40 if( strcmp(argv[1], "autoconf") == 0 ) {
41 DoAutoConfig(argv[2]);
50 void PrintUsage(char *ProgName)
52 fprintf(stderr, "Usage: %s [add <ipaddr>]\n", ProgName);
55 void DumpInterfaces(int DumpAll)
59 char path[sizeof(IPSTACK_ROOT)+1+FILENAME_MAX] = IPSTACK_ROOT"/";
60 char *filename = &path[sizeof(IPSTACK_ROOT)];
62 dp = open(IPSTACK_ROOT, OPENFLAG_READ);
64 while( readdir(dp, filename) )
66 if(filename[0] == '.') continue;
68 fd = open(path, OPENFLAG_READ);
70 printf("%s:\tUnable to open ('%s'\n\n", filename, path);
73 type = ioctl(fd, 4, NULL);
75 printf("%s:\t", filename);
77 int len = ioctl(fd, ioctl(fd, 3, "get_device"), NULL);
78 char *buf = malloc(len+1);
79 ioctl(fd, ioctl(fd, 3, "get_device"), buf);
80 printf("'%s'\t", buf);
93 ioctl(fd, 5, ip); // Get IP Address
94 subnet = ioctl(fd, 7, NULL); // Get Subnet Bits
95 printf("\t%i.%i.%i.%i/%i\n", ip[0], ip[1], ip[2], ip[3], subnet);
96 ioctl(fd, 8, ip); // Get Gateway
97 printf("\tGateway: %i.%i.%i.%i\n", ip[0], ip[1], ip[2], ip[3]);
105 ioctl(fd, 5, ip); // Get IP Address
106 subnet = ioctl(fd, 7, NULL); // Get Subnet Bits
107 printf("\t%x:%x:%x:%x:%x:%x:%x:%x/%i\n",
108 ip[0], ip[1], ip[2], ip[3],
109 ip[4], ip[5], ip[6], ip[7],
125 int AddInterface( char *Device )
129 dp = open(IPSTACK_ROOT, OPENFLAG_READ);
130 ret = ioctl(dp, 4, Device);
131 printf("AddInterface: ret = 0x%x = %i\n", ret, ret);
135 fprintf(stderr, "Unable to add '%s' as a network interface\n", Device);
139 printf("-- Added '"IPSTACK_ROOT"/%i' using device %s\n", ret, Device);
144 int DoAutoConfig( char *Device )
147 char path[sizeof(IPSTACK_ROOT)+5+1]; // ip000
148 uint8_t addr[4] = {10,0,2,55};
149 uint8_t gw[4] = {10,0,2,1};
152 tmp = AddInterface(Device);
153 if( tmp < 0 ) return tmp;
155 sprintf(path, IPSTACK_ROOT"/%i", tmp);
157 fd = open(path, OPENFLAG_READ);
159 fprintf(stderr, "Unable to open '%s'\n", path);
164 tmp = ioctl(fd, ioctl(fd, 3, "getset_type"), &tmp);
166 fprintf(stderr, "Error in setting address type (got %i, expected 4)\n", tmp);
170 ioctl(fd, ioctl(fd, 3, "set_address"), addr);
172 ioctl(fd, ioctl(fd, 3, "getset_subnet"), &subnet);
174 ioctl(fd, ioctl(fd, 3, "set_gateway"), gw);
178 printf("Set address to %i.%i.%i.%i/%i (GW: %i.%i.%i.%i)\n",
179 addr[0], addr[1], addr[2], addr[3],
181 gw[0], gw[1], gw[2], gw[3]);