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

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