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

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