2fe0f24f485584cf7ee2c9cbccdf1f476653c436
[tpg/acess2.git] / Usermode / Applications / ifconfig_src / main.c
1 /*
2  * Acess2 IFCONFIG command
3  */
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <acess/sys.h>
8
9 // === CONSTANTS ===
10 #define FILENAME_MAX    255
11 #define IPSTACK_ROOT    "/Devices/ip"
12
13 // === PROTOTYPES ===
14 void    PrintUsage(char *ProgName);
15 void    DumpInterfaces( int DumpAll );
16  int    AddInterface( char *Address );
17  int    DoAutoConfig( char *Device );
18
19 // === CODE ===
20 /**
21  * \fn int main(int argc, char *argv[])
22  * \brief Entrypoint
23  */
24 int main(int argc, char *argv[])
25 {
26         if(argc == 1) {
27                 DumpInterfaces(0);
28                 return 0;
29         }
30         
31         if( strcmp(argv[1], "add") == 0 ) {
32                 if( argc < 3 ) {
33                         fprintf(stderr, "ERROR: `add` requires an argument\n");
34                         PrintUsage(argv[0]);
35                         return 0;
36                 }
37                 return AddInterface( argv[2] );
38         }
39         
40         if( strcmp(argv[1], "autoconf") == 0 ) {
41                 DoAutoConfig(argv[2]);
42                 return 0;
43         }
44         
45         PrintUsage(argv[0]);
46         
47         return 0;
48 }
49
50 void PrintUsage(char *ProgName)
51 {
52         fprintf(stderr, "Usage: %s [add <ipaddr>]\n", ProgName);
53 }
54
55 void DumpInterfaces(int DumpAll)
56 {
57          int    dp, fd;
58          int    type;
59         char    path[sizeof(IPSTACK_ROOT)+1+FILENAME_MAX] = IPSTACK_ROOT"/";
60         char    *filename = &path[sizeof(IPSTACK_ROOT)];
61         
62         dp = open(IPSTACK_ROOT, OPENFLAG_READ);
63         
64         while( readdir(dp, filename) )
65         {
66                 if(filename[0] == '.')  continue;
67                 
68                 fd = open(path, OPENFLAG_READ);
69                 if(fd == -1) {
70                         printf("%s:\tUnable to open ('%s'\n\n", filename, path);
71                 }
72                 
73                 type = ioctl(fd, 4, NULL);
74                 
75                 printf("%s:\t", filename);
76                 {
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);
81                         free(buf);
82                 }
83                 switch(type)
84                 {
85                 case 0:
86                         printf("DISABLED\n");
87                         break;
88                 case 4:
89                         {
90                         uint8_t ip[4];
91                          int    subnet;
92                         printf("IPv4\n");
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]);
98                         }
99                         break;
100                 case 6:
101                         {
102                         uint16_t        ip[8];
103                          int    subnet;
104                         printf("IPv6\n");
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],
110                                 subnet);
111                         }
112                         break;
113                 default:
114                         printf("UNKNOWN\n");
115                         break;
116                 }
117                 printf("\n");
118                         
119                 close(fd);
120         }
121         
122         close(dp);
123 }
124
125 int AddInterface( char *Device )
126 {
127          int    dp, ret;
128         
129         dp = open(IPSTACK_ROOT, OPENFLAG_READ);
130         ret = ioctl(dp, 4, Device);
131         printf("AddInterface: ret = 0x%x = %i\n", ret, ret);
132         close(dp);
133         
134         if( ret < 0 ) {
135                 fprintf(stderr, "Unable to add '%s' as a network interface\n", Device);
136                 return -1;
137         }
138         
139         printf("-- Added '"IPSTACK_ROOT"/%i' using device %s\n", ret, Device);
140         
141         return ret;
142 }
143
144 int DoAutoConfig( char *Device )
145 {
146          int    tmp, fd;
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};
150          int    subnet = 8;
151         
152         tmp = AddInterface(Device);
153         if( tmp < 0 )   return tmp;
154         
155         sprintf(path, IPSTACK_ROOT"/%i", tmp);
156         
157         fd = open(path, OPENFLAG_READ);
158         if( fd == -1 ) {
159                 fprintf(stderr, "Unable to open '%s'\n", path);
160                 return -1;
161         }
162         
163         tmp = 4;        // IPv4
164         tmp = ioctl(fd, ioctl(fd, 3, "getset_type"), &tmp);
165         if( tmp != 4 ) {
166                 fprintf(stderr, "Error in setting address type (got %i, expected 4)\n", tmp);
167                 return -1;
168         }
169         // Set Address
170         ioctl(fd, ioctl(fd, 3, "set_address"), addr);
171         // Set Subnet
172         ioctl(fd, ioctl(fd, 3, "getset_subnet"), &subnet);
173         // Set Gateway
174         ioctl(fd, ioctl(fd, 3, "set_gateway"), gw);
175         
176         close(fd);
177         
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],
180                 subnet,
181                 gw[0], gw[1], gw[2], gw[3]);
182         
183         return 0;
184 }

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