fgms 0.11.8
The
FlightGear MultiPlayer Server
project
clitest.cxx
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <arpa/inet.h>
5 #include <signal.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include "libcli.hxx"
10 
11 #define CLITEST_PORT 8000
12 #define MODE_CONFIG_INT 10
13 
14 #ifdef __GNUC__
15 # define UNUSED(d) d __attribute__ ((unused))
16 #else
17 # define UNUSED(d) d
18 #endif
19 
20 using namespace LIBCLI;
21 using namespace std;
22 
23 class my_cli : public CLI
24 {
25 public:
26  void setup();
27  int check_auth (const string& username, const string& password);
28  int check_enable(const string& password);
29 
30 private:
31  int test (char *command, char *argv[], int argc);
32 };
33 
34 int my_cli::check_auth (const string& username, const string& password)
35 {
36  if (username != "fred")
37  return LIBCLI::ERROR_ANY;
38  if (password != "fred")
39  return LIBCLI::ERROR_ANY;
40  return LIBCLI::OK;
41 }
42 
43 int my_cli::check_enable(const string& password)
44 {
45  return (password == "fred");
46 }
47 
49 {
50  typedef Command<CLI>::cpp_callback_func callback_ptr;
51  typedef CLI::cpp_auth_func auth_callback;
52  typedef CLI::cpp_enable_func enable_callback;
53  Command<CLI>* c;
54  Command<CLI>* c2;
55 
56  set_auth_callback( static_cast<auth_callback> (& my_cli::check_auth) );
57  set_enable_callback( static_cast<enable_callback> (& my_cli::check_enable) );
58  c = new Command<CLI> (
59  this,
60  "show",
63  "Show running system information"
64  );
65  register_command (c);
66 
67  register_command ( new Command<CLI> (
68  this,
69  "version",
70  static_cast<callback_ptr> (&my_cli::test),
71  LIBCLI::UNPRIVILEGED,
72  LIBCLI::MODE_ANY,
73  "Show version information"
74  ), c);
75 
76  c2 = new Command<CLI> (
77  this,
78  "blacklist",
79  static_cast<callback_ptr> (&my_cli::test),
80  LIBCLI::UNPRIVILEGED,
81  LIBCLI::MODE_ANY,
82  "Show list"
83  );
84  register_command (c2, c);
85 
86  register_command ( new Command<CLI> (
87  this,
88  "brief",
89  static_cast<callback_ptr> (&my_cli::test),
90  LIBCLI::UNPRIVILEGED,
91  LIBCLI::MODE_ANY,
92  "Show list brief"
93  ), c2);
94 }
95 
96 int my_cli::test (char *command, char *argv[], int argc)
97 {
98  print ("called %s with \"%s\"", __FUNCTION__, command);
99  print ("%d arguments:", argc);
100  for (int i = 0; i < argc; i++)
101  print(" %s", argv[i]);
102  return 0;
103 }
104 
105 int main( int argc, char** argv )
106 {
107  DEBUG d (__FUNCTION__,__FILE__,__LINE__);
108  my_cli* cli;
109  int s, x;
110  struct sockaddr_in addr;
111  int on = 1;
112 #ifndef _MSC_VER
113  struct termios OldModes;
114 #endif
115 
116  signal(SIGCHLD, SIG_IGN);
117 
118  cli = new my_cli();
119  cli->setup();
120  cli->set_banner("libcli test environment");
121  cli->set_hostname("router");
122 
123  if (argc > 1)
124  { // read from stdin
125  printf("Listening on stdin\n");
126  printf("Login with fred/fred, enable with 'fred'\n");
127  while (1)
128  {
129 #ifndef _MSC_VER
130  ( void ) tcgetattr ( fileno (stdin), &OldModes );
131 #endif
132  cli->loop (0); // stdin
133 #ifndef _MSC_VER
134  ( void ) tcsetattr ( fileno ( stdin ), TCSANOW, &OldModes );
135 #endif
136  exit (0);
137  }
138  }
139  // listen to a socket
140  if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
141  {
142  perror("socket");
143  return 1;
144  }
145  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
146  memset(&addr, 0, sizeof(addr));
147  addr.sin_family = AF_INET;
148  addr.sin_addr.s_addr = htonl(INADDR_ANY);
149  addr.sin_port = htons(CLITEST_PORT);
150  if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
151  {
152  perror("bind");
153  return 1;
154  }
155  if (listen(s, 50) < 0)
156  {
157  perror("listen");
158  return 1;
159  }
160  printf("Listening on stdin\n");
161  printf("Login with fred/fred, enable with 'fred'\n");
162  while ((x = accept(s, NULL, 0)))
163  {
164  int pid = fork();
165  if (pid < 0)
166  {
167  perror("fork");
168  return 1;
169  }
170 
171  /* parent */
172  if (pid > 0)
173  {
174  socklen_t len = sizeof(addr);
175  if (getpeername(x, (struct sockaddr *) &addr, &len) >= 0)
176  printf(" * accepted connection from %s\n", inet_ntoa(addr.sin_addr));
177  close(x);
178  if (argc == 1)
179  {
180  return 0;
181  }
182  continue;
183  }
184 
185  /* child */
186  close(s);
187  cli->loop(x);
188  exit(0);
189  }
190  return 0;
191 }
int test(char *command, char *argv[], int argc)
Definition: clitest.cxx:96
int(CLI::* cpp_auth_func)(const string &, const string &)
Definition: libcli.hxx:42
void setup()
Definition: clitest.cxx:48
int check_enable(const string &password)
Definition: clitest.cxx:43
int loop()
Definition: libcli.cxx:1896
void set_banner(const string &banner)
Definition: libcli.cxx:155
int main(int argc, char **argv)
Definition: clitest.cxx:105
static pid_t pid
Definition: server.c:1142
Definition: debug.hxx:31
int(CLI::* cpp_enable_func)(const string &)
Definition: libcli.hxx:44
int check_auth(const string &username, const string &password)
Definition: clitest.cxx:34
#define CLITEST_PORT
Definition: clitest.cxx:11
void set_hostname(const string &hostname)
Definition: libcli.cxx:165