Networking - Working on DHCP client (and related changes)
[tpg/acess2.git] / Usermode / Applications / ping_src / main.c
1 /*
2  * Acess2 IFCONFIG command
3  */
4 #include <stdlib.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <acess/sys.h>
9 #include <net.h>
10
11 // === CONSTANTS ===
12 #define IPSTACK_ROOT    "/Devices/ip"
13
14 // === PROTOTYPES ===
15 void    PrintUsage(char *ProgName);
16 void    PrintHelp(char *ProgName);
17
18 // === GLOBALS ===
19  int    giNumberOfPings = 1;
20
21 // === CODE ===
22 /**
23  * \fn int main(int argc, char *argv[])
24  * \brief Entrypoint
25  */
26 int main(int argc, char *argv[])
27 {
28         char    *ipStr = NULL;
29         char    *iface = NULL;
30          int    i, j;
31         uint8_t addr[16];
32          int    type;
33          
34          int    fd, call, ping;
35         
36         for(i = 1; i < argc; i++)
37         {
38                 if(argv[i][0] != '-')
39                 {
40                         if(!ipStr)
41                                 ipStr = argv[i];
42                         else if(!iface)
43                                 iface = argv[i];
44                         else {
45                                 PrintUsage(argv[0]);
46                                 return 1;
47                         }
48                 }
49                 
50                 if(argv[i][1] == '-')
51                 {
52                         char    *arg = &argv[i][2];
53                         if(strcmp(arg, "help") == 0) {
54                                 PrintHelp(argv[1]);
55                                 return 0;
56                         }
57                 }
58                 
59                 for( j = 1; argv[i][j]; j++ )
60                 {
61                         switch(argv[i][j])
62                         {
63                         case '?':
64                         case 'h':
65                                 PrintHelp(argv[1]);
66                                 return 0;
67                         }
68                 }
69         }
70         
71         if(!ipStr) {
72                 PrintUsage(argv[0]);
73                 return 1;
74         }
75         
76         // Read Address
77         type = Net_ParseAddress(ipStr, addr);
78         if( type == 0 ) {
79                 fprintf(stderr, "Invalid IP Address\n");
80                 return 1;
81         }
82         
83         if( !iface )
84         {
85                 iface = Net_GetInterface(type, addr);
86                 if( !iface ) {
87                         fprintf(stderr, "Unable to find a route to '%s'\n",
88                                 Net_PrintAddress(type, addr)
89                                 );
90                         return -1;
91                 }
92                 
93                 printf("iface = '%s'\n", iface);
94         }
95         
96         {
97                 char    *_iface = malloc( sizeof("/Devices/ip/") + strlen(iface) + 1 );
98                 strcpy(_iface, "/Devices/ip/");
99                 strcat(_iface, iface);
100                 free(iface);    // TODO: Handle when this is not heap
101                 iface = _iface;
102                 printf("iface = '%s'\n", iface);
103         }
104         
105         fd = open(iface, OPENFLAG_EXEC);
106         if(fd == -1) {
107                 fprintf(stderr, "ERROR: Unable to open interface '%s'\n", iface);
108                 return 1;
109         }
110                 
111         call = ioctl(fd, 3, "ping");
112         if(call == 0) {
113                 fprintf(stderr, "ERROR: '%s' does not have a 'ping' call\n", iface);
114                 return 1;
115         }
116         
117         for( i = 0; i < giNumberOfPings; i ++ )
118         {
119                 ping = ioctl(fd, call, addr);
120                 printf("ping = %i\n", ping);
121         }
122                 
123         close(fd);
124         
125         return 0;
126 }
127
128 void PrintUsage(char *ProgName)
129 {
130         fprintf(stderr, "Usage: %s <address> [<interface>]\n", ProgName);
131 }
132
133 void PrintHelp(char *ProgName)
134 {
135         PrintUsage(ProgName);
136         fprintf(stderr, " -h\tPrint this message\n");
137 }
138

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