Usermode/libc - Fix strchr and strrchr behavior
[tpg/acess2.git] / Tools / NetTest / main.c
1 /*
2  * Acess2 Networking Test Suite (NetTest)
3  * - By John Hodge (thePowersGang)
4  *
5  * main.c
6  * - Program Core
7  */
8 #include <stdio.h>
9 #include <acess_logging.h>
10 #include <nettest.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <time.h>
14
15 extern int      VFS_Init(void);
16 extern int      IPStack_Install(char **Args);
17
18 // === CODE ===
19 void PrintUsage(const char *ProgramName)
20 {
21         fprintf(stderr, "Usage: %s <commands...>\n", ProgramName);
22         fprintf(stderr, "\n");
23         fprintf(stderr,
24                 "Options:\n"
25                 "-dev <mac>:<type>:<arg>\n"
26                 "-ip <dev>,<addr>/<mask>\n"
27                 "-route <net>/<mask>,<nexthop>\n"
28                 "\n"
29                 "Commands:\n"
30                 "netcat <addr> <port>\n"
31                 "\n"
32                 "Device Types:\n"
33                 "tun - Linux TUN/TAP device (takes an optional name)\n"
34                 "unix - Unix named pipe (datagram)\n"
35                 );
36 }
37
38 int main(int argc, char *argv[])
39 {
40          int    rv;
41         
42         if( argc <= 1 ) {
43                 PrintUsage(argv[0]);
44                 return 1;
45         }
46
47         srand(time(NULL));      
48
49         // Startup
50         VFS_Init();
51         {
52                 char    *ipstack_args[] = {NULL};
53                 IPStack_Install( ipstack_args );
54         }
55         
56         for( int i = 1; i < argc; i ++ )
57         {
58                 char *arg = argv[i];;
59                 if( arg[0] != '-' )
60                 {
61                         if( strcmp(arg, "netcat") == 0 )
62                         {
63                                 if( argc-i != 3 ) {
64                                         Log_Error("NetTest", "'netcat' <addr> <port>");
65                                         PrintUsage(argv[0]);
66                                         return -1;
67                                 }
68
69                                 NetTest_Suite_Netcat(argv[i+1], strtol(argv[i+2], NULL, 0));
70                                 i += 2;
71                         }
72                         else if( strcmp(arg, "cmdline") == 0 )
73                         {
74                                 NetTest_Suite_Cmdline();
75                         }
76                         else
77                         {
78                                 Log_Error("NetTest", "Unknown suite name '%s'", arg);
79                                 PrintUsage(argv[0]);
80                         }
81                 }
82                 else
83                 {
84                         if( strcmp(arg, "--help") == 0 )
85                         {
86                                 PrintUsage(0);
87                                 return 0;
88                         }
89                         else if( strcmp(arg, "-dev") == 0 )
90                         {
91                                 if( i+1 == argc ) { PrintUsage(argv[0]); return 1; }
92                                 rv = NativeNic_AddDev(argv[i+1]);
93                                 if( rv ) {
94                                         Log_Error("NetTest", "Failed to add device %s", argv[i+1]);
95                                         return -1;
96                                 }
97                                 i ++;
98                         }
99                         else if( strcmp(arg, "-ip") == 0 )
100                         {
101                                 if( i+1 == argc ) { PrintUsage(argv[0]); return 1; }
102                                 // Parse argument and poke ipstack
103                                 if( NetTest_AddAddress(argv[i+1]) ) {
104                                         return -1;
105                                 }
106                                 i ++;
107                         }
108                         else
109                         {
110                                 Log_Error("NetTest", "Unknown argument '%s'", arg);
111                                 PrintUsage(argv[0]);
112                                 return -1;
113                         }
114                 }
115         }
116
117         // Teardown
118         Log_Log("NetTest", "Shutting down");
119         fflush(stdout);
120         return 0;
121 }
122
123 size_t NetTest_WriteStdout(const void *Data, size_t Size)
124 {
125         return fwrite(Data, 1, Size, stdout);
126 }

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