Nice job, making umoddi3 call umoddi3 in the usermode stuff :(
[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+1] = 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", filename, path);
71                         continue ;
72                 }
73                 
74                 type = ioctl(fd, 4, NULL);
75                 
76                 printf("%s:\t", filename);
77                 {
78                         int     call = ioctl(fd, 3, "get_device");
79                         int len = ioctl(fd, call, NULL);
80                         char *buf = malloc(len+1);
81                         ioctl(fd, call, buf);
82                         printf("'%s'\t", buf);
83                         free(buf);
84                 }
85                 switch(type)
86                 {
87                 case 0:
88                         printf("DISABLED\n");
89                         break;
90                 case 4:
91                         {
92                         uint8_t ip[4];
93                          int    subnet;
94                         printf("IPv4\n");
95                         ioctl(fd, 5, ip);       // Get IP Address
96                         subnet = ioctl(fd, 7, NULL);    // Get Subnet Bits
97                         printf("\t%i.%i.%i.%i/%i\n", ip[0], ip[1], ip[2], ip[3], subnet);
98                         ioctl(fd, 8, ip);       // Get Gateway
99                         printf("\tGateway: %i.%i.%i.%i\n", ip[0], ip[1], ip[2], ip[3]);
100                         }
101                         break;
102                 case 6:
103                         {
104                         uint16_t        ip[8];
105                          int    subnet;
106                         printf("IPv6\n");
107                         ioctl(fd, 5, ip);       // Get IP Address
108                         subnet = ioctl(fd, 7, NULL);    // Get Subnet Bits
109                         printf("\t%x:%x:%x:%x:%x:%x:%x:%x/%i\n",
110                                 ip[0], ip[1], ip[2], ip[3],
111                                 ip[4], ip[5], ip[6], ip[7],
112                                 subnet);
113                         }
114                         break;
115                 default:
116                         printf("UNKNOWN\n");
117                         break;
118                 }
119                 printf("\n");
120                         
121                 close(fd);
122         }
123         
124         close(dp);
125 }
126
127 int AddInterface( char *Device )
128 {
129          int    dp, ret;
130         
131         dp = open(IPSTACK_ROOT, OPENFLAG_READ);
132         ret = ioctl(dp, 4, Device);
133         printf("AddInterface: ret = 0x%x = %i\n", ret, ret);
134         close(dp);
135         
136         if( ret < 0 ) {
137                 fprintf(stderr, "Unable to add '%s' as a network interface\n", Device);
138                 return -1;
139         }
140         
141         printf("-- Added '"IPSTACK_ROOT"/%i' using device %s\n", ret, Device);
142         
143         return ret;
144 }
145
146 int DoAutoConfig( char *Device )
147 {
148          int    tmp, fd;
149         char    path[sizeof(IPSTACK_ROOT)+5+1]; // ip000
150         uint8_t addr[4] = {10,0,2,55};
151         uint8_t gw[4] = {10,0,2,1};
152          int    subnet = 8;
153         
154         tmp = AddInterface(Device);
155         if( tmp < 0 )   return tmp;
156         
157         sprintf(path, IPSTACK_ROOT"/%i", tmp);
158         
159         fd = open(path, OPENFLAG_READ);
160         if( fd == -1 ) {
161                 fprintf(stderr, "Unable to open '%s'\n", path);
162                 return -1;
163         }
164         
165         tmp = 4;        // IPv4
166         tmp = ioctl(fd, ioctl(fd, 3, "getset_type"), &tmp);
167         if( tmp != 4 ) {
168                 fprintf(stderr, "Error in setting address type (got %i, expected 4)\n", tmp);
169                 return -1;
170         }
171         // Set Address
172         ioctl(fd, ioctl(fd, 3, "set_address"), addr);
173         // Set Subnet
174         ioctl(fd, ioctl(fd, 3, "getset_subnet"), &subnet);
175         // Set Gateway
176         ioctl(fd, ioctl(fd, 3, "set_gateway"), gw);
177         
178         close(fd);
179         
180         printf("Set address to %i.%i.%i.%i/%i (GW: %i.%i.%i.%i)\n",
181                 addr[0], addr[1], addr[2], addr[3],
182                 subnet,
183                 gw[0], gw[1], gw[2], gw[3]);
184         
185         return 0;
186 }

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