Usermode - heap debugging and memmove speedup
[tpg/acess2.git] / Usermode / Applications / ping_src / main.c
1 /*
2  * Acess2 ping 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                 char    *_iface = malloc( sizeof("/Devices/ip/") + strlen(iface) + 1 );
86                 strcpy(_iface, "/Devices/ip/");
87                 strcat(_iface, iface);
88                 free(iface);    // TODO: Handle when this is not heap
89                 iface = _iface;
90                 printf("iface = '%s'\n", iface);
91                 fd = open(iface, OPENFLAG_EXEC);
92                 if(fd == -1) {
93                         fprintf(stderr, "ERROR: Unable to open interface '%s'\n", iface);
94                         return 1;
95                 }
96                 
97         }
98         else
99         {
100                 fd = Net_OpenSocket(type, addr, NULL);
101                 if( fd == -1 ) {
102                         fprintf(stderr, "Unable to find a route to '%s'\n",
103                                 Net_PrintAddress(type, addr)
104                                 );
105                         return -1;
106                 }
107         }
108         
109         call = ioctl(fd, 3, "ping");
110         if(call == 0) {
111                 fprintf(stderr, "ERROR: '%s' does not have a 'ping' call\n", iface);
112                 return 1;
113         }
114         
115         for( i = 0; i < giNumberOfPings; i ++ )
116         {
117                 ping = ioctl(fd, call, addr);
118                 printf("ping = %i\n", ping);
119         }
120                 
121         close(fd);
122         
123         return 0;
124 }
125
126 void PrintUsage(char *ProgName)
127 {
128         fprintf(stderr, "Usage: %s <address> [<interface>]\n", ProgName);
129 }
130
131 void PrintHelp(char *ProgName)
132 {
133         PrintUsage(ProgName);
134         fprintf(stderr, " -h\tPrint this message\n");
135 }
136

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