1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.9 2004-07-08 16:54:35 bodea Exp $";
18 #include <arpa/inet.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
35 extern tunnelt
*tunnel
;
36 extern sessiont
*session
;
37 extern radiust
*radius
;
38 extern ippoolt
*ip_address_pool
;
39 extern struct Tstats
*_statistics
;
40 struct cli_def
*cli
= NULL
;
42 extern int clifd
, udpfd
, tunfd
, snoopfd
, ifrfd
, cluster_sockfd
;
44 extern struct configt
*config
;
45 extern struct config_descriptt config_values
[];
47 extern struct Tringbuffer
*ringbuffer
;
49 extern struct cli_session_actions
*cli_session_actions
;
50 extern struct cli_tunnel_actions
*cli_tunnel_actions
;
52 char *debug_levels
[] = {
76 int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
77 int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
78 int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
79 int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
80 int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
81 int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
82 int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
83 int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
84 int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
85 int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
86 int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
87 int cmd_show_cluster(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
88 int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
89 int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
90 int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
91 int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
92 int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
93 int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
94 int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
95 int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
96 int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
97 int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
98 int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
99 int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
100 int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
101 int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
102 int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
);
103 int regular_stuff(struct cli_def
*cli
);
105 void init_cli(char *hostname
)
109 struct cli_command
*c
;
110 struct cli_command
*c2
;
112 struct sockaddr_in addr
;
115 if (hostname
&& *hostname
)
116 cli_set_hostname(cli
, hostname
);
118 cli_set_hostname(cli
, "l2tpns");
120 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
121 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
123 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
125 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
126 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
127 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
128 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
129 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
130 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
131 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
132 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
133 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
134 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
135 cli_register_command(cli
, c
, "users", cmd_show_users
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of all connected users or details of selected user");
136 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
138 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
139 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
140 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
143 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
145 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
146 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
149 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
151 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
152 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
153 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
155 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily enable interception for a user");
156 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily enable throttling for a user");
157 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
159 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
160 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP peer");
162 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
163 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily disable interception for a user");
164 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily disable throttling for a user");
165 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
166 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
167 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP peer");
169 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
170 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
171 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
172 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
174 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
175 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
177 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
178 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
180 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
181 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
183 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
185 // Enable regular processing
186 cli_regular(cli
, regular_stuff
);
188 if (!(f
= fopen(CLIUSERS
, "r")))
190 log(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
194 while (fgets(buf
, 4096, f
))
197 if (*buf
== '#') continue;
198 if ((p
= strchr(buf
, '\r'))) *p
= 0;
199 if ((p
= strchr(buf
, '\n'))) *p
= 0;
201 if (!(p
= strchr((char *)buf
, ':'))) continue;
203 if (!strcmp(buf
, "enable"))
205 cli_allow_enable(cli
, p
);
206 log(3, 0, 0, 0, "Setting enable password\n");
210 cli_allow_user(cli
, buf
, p
);
211 log(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
217 memset(&addr
, 0, sizeof(addr
));
218 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
219 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
222 // Set cli fd as non-blocking
223 flags
= fcntl(clifd
, F_GETFL
, 0);
224 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
226 addr
.sin_family
= AF_INET
;
227 addr
.sin_port
= htons(23);
228 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
230 log(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
236 void cli_do(int sockfd
)
241 if (config
->scheduler_fifo
)
244 struct sched_param params
= {0};
245 params
.sched_priority
= 0;
246 if ((ret
= sched_setscheduler(0, SCHED_OTHER
, ¶ms
)) == 0)
248 log(3, 0, 0, 0, "Dropped FIFO scheduler\n");
252 log(0, 0, 0, 0, "Error setting scheduler to OTHER: %s\n", strerror(errno
));
253 log(0, 0, 0, 0, "This is probably really really bad.\n");
257 signal(SIGPIPE
, SIG_DFL
);
258 signal(SIGCHLD
, SIG_DFL
);
259 signal(SIGHUP
, SIG_DFL
);
260 signal(SIGUSR1
, SIG_DFL
);
261 signal(SIGQUIT
, SIG_DFL
);
262 signal(SIGKILL
, SIG_DFL
);
263 signal(SIGALRM
, SIG_DFL
);
264 signal(SIGTERM
, SIG_DFL
);
267 if (udpfd
) close(udpfd
); udpfd
= 0;
268 if (tunfd
) close(tunfd
); tunfd
= 0;
269 if (snoopfd
) close(snoopfd
); snoopfd
= 0;
270 for (i
= 0; i
< config
->num_radfds
; i
++)
271 if (radfds
[i
]) close(radfds
[i
]);
272 if (ifrfd
) close(ifrfd
); ifrfd
= 0;
273 if (cluster_sockfd
) close(cluster_sockfd
); cluster_sockfd
= 0;
274 if (clifd
) close(clifd
); clifd
= 0;
276 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
277 if (bgp_peers
[i
].sock
!= -1)
278 close(bgp_peers
[i
].sock
);
282 int require_auth
= 1;
283 struct sockaddr_in addr
;
284 int l
= sizeof(addr
);
285 if (getpeername(sockfd
, (struct sockaddr
*)&addr
, &l
) == 0)
287 log(3, 0, 0, 0, "Accepted connection to CLI from %s\n", inet_toa(addr
.sin_addr
.s_addr
));
288 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
291 log(0, 0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
295 log(3, 0, 0, 0, "CLI is remote, requiring authentication\n");
296 if (!cli
->users
) /* paranoia */
298 log(0, 0, 0, 0, "No users for remote authentication! Exiting CLI\n");
304 /* no username/pass required */
312 debug_rb_tail
= ringbuffer
->tail
;
314 memset(&debug_flags
, 0, sizeof(debug_flags
));
315 debug_flags
.critical
= 1;
319 snprintf(prompt
, 1005, "l2tpns> ");
320 cli_loop(cli
, sockfd
, prompt
);
324 log(3, 0, 0, 0, "Closed CLI connection\n");
328 void cli_print_log(struct cli_def
*cli
, char *string
)
330 log(3, 0, 0, 0, "%s\n", string
);
333 void cli_do_file(FILE *fh
)
335 log(3, 0, 0, 0, "Reading configuration file\n");
336 cli_print_callback(cli
, cli_print_log
);
337 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
338 cli_print_callback(cli
, NULL
);
341 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
352 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
354 int v
= va_arg(ap
, int);
355 snprintf(buf
, sizeof(buf
), entry
, v
);
361 desc
= va_arg(ap
, char *);
363 cli_print(cli
, " %-20s %s", p
, desc
);
365 cli_print(cli
, " %s", p
);
367 entry
= desc
? va_arg(ap
, char *) : 0;
372 cli_print(cli
, " <cr>");
377 int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
381 if (CLI_HELP_REQUESTED
)
382 return cli_arg_help(cli
, 1,
383 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
389 // Show individual session
390 for (i
= 0; i
< argc
; i
++)
394 if (s
<= 0 || s
>= MAXSESSION
)
396 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
399 cli_print(cli
, "\r\nSession %d:", s
);
400 cli_print(cli
, " User: %s", session
[s
].user
[0] ? session
[s
].user
: "none");
401 cli_print(cli
, " Calling Num: %s", session
[s
].calling
);
402 cli_print(cli
, " Called Num: %s", session
[s
].called
);
403 cli_print(cli
, " Tunnel ID: %d", session
[s
].tunnel
);
404 cli_print(cli
, " IP address: %s", inet_toa(htonl(session
[s
].ip
)));
405 cli_print(cli
, " HSD sid: %lu", session
[s
].sid
);
406 cli_print(cli
, " Idle time: %u seconds", abs(time_now
- session
[s
].last_packet
));
407 cli_print(cli
, " Next Recv: %u", session
[s
].nr
);
408 cli_print(cli
, " Next Send: %u", session
[s
].ns
);
409 cli_print(cli
, " Bytes In/Out: %lu/%lu", (unsigned long)session
[s
].total_cout
, (unsigned long)session
[s
].total_cin
);
410 cli_print(cli
, " Pkts In/Out: %lu/%lu", (unsigned long)session
[s
].pout
, (unsigned long)session
[s
].pin
);
411 cli_print(cli
, " MRU: %d", session
[s
].mru
);
412 cli_print(cli
, " Radius Session: %u", session
[s
].radius
);
413 cli_print(cli
, " Rx Speed: %lu", session
[s
].rx_connect_speed
);
414 cli_print(cli
, " Tx Speed: %lu", session
[s
].tx_connect_speed
);
415 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
416 cli_print(cli
, " Intercepted: %s:%d", inet_toa(session
[s
].snoop_ip
), session
[s
] .snoop_port
);
418 cli_print(cli
, " Intercepted: no");
419 cli_print(cli
, " Throttled: %s", session
[s
].throttle
? "YES" : "no");
420 cli_print(cli
, " Walled Garden: %s", session
[s
].walled_garden
? "YES" : "no");
421 cli_print(cli
, " Filter BucketI: %d", session
[s
].tbf_in
);
422 cli_print(cli
, " Filter BucketO: %d", session
[s
].tbf_out
);
428 cli_print(cli
, " %s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
442 for (i
= 1; i
< MAXSESSION
; i
++)
444 char *userip
, *tunnelip
;
445 if (!session
[i
].opened
) continue;
446 userip
= strdup(inet_toa(htonl(session
[i
].ip
)));
447 tunnelip
= strdup(inet_toa(htonl(tunnel
[ session
[i
].tunnel
].ip
)));
448 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
451 session
[i
].user
[0] ? session
[i
].user
: "*",
453 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
454 (session
[i
].throttle
) ? "Y" : "N",
455 (session
[i
].walled_garden
) ? "Y" : "N",
456 abs(time_now
- (unsigned long)session
[i
].opened
),
457 (unsigned long)session
[i
].total_cout
,
458 (unsigned long)session
[i
].total_cin
,
459 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
461 session
[i
].calling
[0] ? session
[i
].calling
: "*");
462 if (userip
) free(userip
);
463 if (tunnelip
) free(tunnelip
);
468 int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
470 int i
, x
, show_all
= 0;
478 if (CLI_HELP_REQUESTED
)
481 return cli_arg_help(cli
, 1,
482 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
485 return cli_arg_help(cli
, 1,
486 "all", "Show all tunnels, including unused",
487 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
494 if (strcmp(argv
[0], "all") == 0)
500 // Show individual tunnel
501 for (i
= 0; i
< argc
; i
++)
506 if (t
<= 0 || t
>= MAXTUNNEL
)
508 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
511 cli_print(cli
, "\r\nTunnel %d:", t
);
512 cli_print(cli
, " State: %s", states
[tunnel
[t
].state
]);
513 cli_print(cli
, " Hostname: %s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
514 cli_print(cli
, " Remote IP: %s", inet_toa(htonl(tunnel
[t
].ip
)));
515 cli_print(cli
, " Remote Port: %d", tunnel
[t
].port
);
516 cli_print(cli
, " Rx Window: %u", tunnel
[t
].window
);
517 cli_print(cli
, " Next Recv: %u", tunnel
[t
].nr
);
518 cli_print(cli
, " Next Send: %u", tunnel
[t
].ns
);
519 cli_print(cli
, " Queue Len: %u", tunnel
[t
].controlc
);
520 cli_print(cli
, " Last Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
522 for (x
= 0; x
< MAXSESSION
; x
++)
523 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
524 sprintf(s
, "%s%u ", s
, x
);
525 cli_print(cli
, " Sessions: %s", s
);
531 // Show tunnel summary
532 cli_print(cli
, "%4s %20s %20s %6s %s",
538 for (i
= 1; i
< MAXTUNNEL
; i
++)
541 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
|| !tunnel
[i
].hostname
[0])) continue;
543 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
544 cli_print(cli
, "%4d %20s %20s %6s %6d",
546 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
547 inet_toa(htonl(tunnel
[i
].ip
)),
548 states
[tunnel
[i
].state
],
554 int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
561 if (CLI_HELP_REQUESTED
)
562 return cli_arg_help(cli
, 1,
563 "USER", "Show details for specific username",
566 for (i
= 0; i
< MAXSESSION
; i
++)
568 if (!session
[i
].opened
) continue;
569 if (!session
[i
].user
[0]) continue;
573 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
575 if (strcmp(argv
[j
], session
[i
].user
) == 0)
577 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
578 sargv
[sargc
] = sid
[sargc
];
586 cli_print(cli
, "%s", session
[i
].user
);
590 return cmd_show_session(cli
, "users", sargv
, sargc
);
595 int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
597 if (CLI_HELP_REQUESTED
)
598 return CLI_HELP_NO_ARGS
;
600 cli_print(cli
, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
601 cli_print(cli
, "%-10s %8lu %8lu %8lu", "RX",
602 GET_STAT(tun_rx_bytes
),
603 GET_STAT(tun_rx_packets
),
604 GET_STAT(tun_rx_errors
));
605 cli_print(cli
, "%-10s %8lu %8lu %8lu", "TX",
606 GET_STAT(tun_tx_bytes
),
607 GET_STAT(tun_tx_packets
),
608 GET_STAT(tun_tx_errors
));
611 cli_print(cli
, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
612 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "RX",
613 GET_STAT(tunnel_rx_bytes
),
614 GET_STAT(tunnel_rx_packets
),
615 GET_STAT(tunnel_rx_errors
),
617 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "TX",
618 GET_STAT(tunnel_tx_bytes
),
619 GET_STAT(tunnel_tx_packets
),
620 GET_STAT(tunnel_tx_errors
),
621 GET_STAT(tunnel_retries
));
624 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
625 cli_print(cli
, "-----------------------------------------");
626 cli_print(cli
, "%-30s%lu", "radius_retries", GET_STAT(radius_retries
));
627 cli_print(cli
, "%-30s%lu", "arp_sent", GET_STAT(arp_sent
));
628 cli_print(cli
, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped
));
629 cli_print(cli
, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created
));
630 cli_print(cli
, "%-30s%lu", "session_created", GET_STAT(session_created
));
631 cli_print(cli
, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout
));
632 cli_print(cli
, "%-30s%lu", "session_timeout", GET_STAT(session_timeout
));
633 cli_print(cli
, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout
));
634 cli_print(cli
, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow
));
635 cli_print(cli
, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow
));
636 cli_print(cli
, "%-30s%lu", "session_overflow", GET_STAT(session_overflow
));
637 cli_print(cli
, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated
));
638 cli_print(cli
, "%-30s%lu", "ip_freed", GET_STAT(ip_freed
));
639 cli_print(cli
, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded
));
640 cli_print(cli
, "%-30s%lu", "recv_forward", GET_STAT(recv_forward
));
644 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
645 cli_print(cli
, "-----------------------------------------");
646 cli_print(cli
, "%-30s%lu", "call_processtun", GET_STAT(call_processtun
));
647 cli_print(cli
, "%-30s%lu", "call_processipout", GET_STAT(call_processipout
));
648 cli_print(cli
, "%-30s%lu", "call_processudp", GET_STAT(call_processudp
));
649 cli_print(cli
, "%-30s%lu", "call_processpap", GET_STAT(call_processpap
));
650 cli_print(cli
, "%-30s%lu", "call_processchap", GET_STAT(call_processchap
));
651 cli_print(cli
, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp
));
652 cli_print(cli
, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp
));
653 cli_print(cli
, "%-30s%lu", "call_processipin", GET_STAT(call_processipin
));
654 cli_print(cli
, "%-30s%lu", "call_processccp", GET_STAT(call_processccp
));
655 cli_print(cli
, "%-30s%lu", "call_processrad", GET_STAT(call_processrad
));
656 cli_print(cli
, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp
));
657 cli_print(cli
, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp
));
658 cli_print(cli
, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap
));
659 cli_print(cli
, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip
));
660 cli_print(cli
, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
661 cli_print(cli
, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend
));
662 cli_print(cli
, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill
));
663 cli_print(cli
, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
664 cli_print(cli
, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill
));
665 cli_print(cli
, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
666 cli_print(cli
, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup
));
667 cli_print(cli
, "%-30s%lu", "call_assign_ip_address",GET_STAT(call_assign_ip_address
));
668 cli_print(cli
, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address
));
669 cli_print(cli
, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
670 cli_print(cli
, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend
));
671 cli_print(cli
, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry
));
676 int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
682 if (CLI_HELP_REQUESTED
)
683 return cli_arg_help(cli
, 1,
684 "tag", "Include CVS release tag",
685 "file", "Include file versions",
688 for (i
= 0; i
< argc
; i
++)
689 if (!strcmp(argv
[i
], "tag"))
691 else if (!strcmp(argv
[i
], "file"))
694 cli_print(cli
, "L2TPNS %s", VERSION
);
697 char const *p
= strchr(cvs_name
, ':');
709 e
= strpbrk(p
, " \t$");
710 cli_print(cli
, "Tag: %.*s", e
? e
- p
+ 1 : strlen(p
), p
);
715 extern linked_list
*loaded_plugins
;
718 cli_print(cli
, "Files:");
719 cli_print(cli
, " %s", cvs_id_arp
);
721 cli_print(cli
, " %s", cvs_id_bgp
);
723 cli_print(cli
, " %s", cvs_id_cli
);
724 cli_print(cli
, " %s", cvs_id_cluster
);
725 cli_print(cli
, " %s", cvs_id_constants
);
726 cli_print(cli
, " %s", cvs_id_control
);
727 cli_print(cli
, " %s", cvs_id_icmp
);
728 cli_print(cli
, " %s", cvs_id_l2tpns
);
729 cli_print(cli
, " %s", cvs_id_ll
);
730 cli_print(cli
, " %s", cvs_id_md5
);
731 cli_print(cli
, " %s", cvs_id_ppp
);
732 cli_print(cli
, " %s", cvs_id_radius
);
733 cli_print(cli
, " %s", cvs_id_tbf
);
734 cli_print(cli
, " %s", cvs_id_util
);
736 ll_reset(loaded_plugins
);
737 while ((p
= ll_next(loaded_plugins
)))
739 char const **id
= dlsym(p
, "cvs_id");
741 cli_print(cli
, " %s", *id
);
748 int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
751 int used
= 0, free
= 0, show_all
= 0;
753 if (!config
->cluster_iam_master
)
755 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
759 if (CLI_HELP_REQUESTED
)
762 return cli_arg_help(cli
, 1, NULL
);
764 return cli_arg_help(cli
, 1,
765 "all", "Show all pool addresses, including unused",
769 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
773 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
774 for (i
= 0; i
< MAXIPPOOL
; i
++)
776 if (!ip_address_pool
[i
].address
) continue;
777 if (ip_address_pool
[i
].assigned
)
779 cli_print(cli
, "%-15s Y %8d %s",
780 inet_toa(htonl(ip_address_pool
[i
].address
)), ip_address_pool
[i
].session
, session
[ip_address_pool
[i
].session
].user
);
786 if (ip_address_pool
[i
].last
)
787 cli_print(cli
, "%-15s N %8s [%s] %ds",
788 inet_toa(htonl(ip_address_pool
[i
].address
)), "",
789 ip_address_pool
[i
].user
, time_now
- ip_address_pool
[i
].last
);
791 cli_print(cli
, "%-15s N", inet_toa(htonl(ip_address_pool
[i
].address
)));
798 cli_print(cli
, "(Not displaying unused addresses)");
800 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
804 void print_save_config(struct cli_def
*cli
, char *string
)
807 fprintf(save_config_fh
, "%s\n", string
);
810 int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
812 if (CLI_HELP_REQUESTED
)
813 return CLI_HELP_NO_ARGS
;
815 if ((save_config_fh
= fopen(config
->config_file
, "w")))
817 cli_print(cli
, "Writing configuration");
818 cli_print_callback(cli
, print_save_config
);
819 cmd_show_run(cli
, command
, argv
, argc
);
820 cli_print_callback(cli
, NULL
);
821 fclose(save_config_fh
);
825 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
830 int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
834 if (CLI_HELP_REQUESTED
)
835 return CLI_HELP_NO_ARGS
;
837 cli_print(cli
, "# Current configuration:");
839 for (i
= 0; config_values
[i
].key
; i
++)
841 void *value
= ((void *)config
) + config_values
[i
].offset
;
842 if (config_values
[i
].type
== STRING
)
843 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *)value
);
844 else if (config_values
[i
].type
== IP
)
845 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_toa(*(unsigned *)value
));
846 else if (config_values
[i
].type
== SHORT
)
847 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *)value
);
848 else if (config_values
[i
].type
== BOOL
)
849 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *)value
) ? "yes" : "no");
850 else if (config_values
[i
].type
== INT
)
851 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *)value
);
852 else if (config_values
[i
].type
== UNSIGNED_LONG
)
853 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *)value
);
856 cli_print(cli
, "# Plugins");
857 for (i
= 0; i
< MAXPLUGINS
; i
++)
859 if (*config
->plugins
[i
])
861 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
865 cli_print(cli
, "# end");
869 int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
871 int i
, free
= 0, used
= 0, show_all
= 0;
882 if (CLI_HELP_REQUESTED
)
885 return cli_arg_help(cli
, 1, NULL
);
887 return cli_arg_help(cli
, 1,
888 "all", "Show all RADIUS sessions, including unused",
892 cli_print(cli
, "%6s%5s%6s%9s%9s%4s", "Radius", "Sock", "State", "Session", "Retry", "Try");
896 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
899 for (i
= 1; i
< MAXRADIUS
; i
++)
901 if (radius
[i
].state
== RADIUSNULL
)
906 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
908 cli_print(cli
, "%6d%5d%6s%9d%9u%4d",
911 states
[radius
[i
].state
],
917 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
922 int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
926 if (CLI_HELP_REQUESTED
)
927 return CLI_HELP_NO_ARGS
;
929 cli_print(cli
, "Plugins currently loaded:");
930 for (i
= 0; i
< MAXPLUGINS
; i
++)
932 if (*config
->plugins
[i
])
934 cli_print(cli
, " %s", config
->plugins
[i
]);
940 int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
944 if (CLI_HELP_REQUESTED
)
945 return CLI_HELP_NO_ARGS
;
947 cli_print(cli
, "Token bucket filters:");
948 cli_print(cli
, "%-6s %8s %-4s", "ID", "Handle", "Used");
949 for (i
= 0; i
< MAXSESSION
; i
++)
951 if (!session
[i
].throttle
)
954 cli_print(cli
, "%-6d %8d %8d",
962 int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
964 if (CLI_HELP_REQUESTED
)
965 return CLI_HELP_NO_ARGS
;
967 cli_print(cli
, " _\n"
973 " ( \\ `. `-. _,.-:\\\n"
974 " \\ \\ `. `-._ __..--' ,-';/\n"
975 " \\ `. `-. `-..___..---' _.--' ,'/\n"
976 " `. `. `-._ __..--' ,' /\n"
977 " `. `-_ ``--..'' _.-' ,'\n"
978 " `-_ `-.___ __,--' ,'\n"
979 " `-.__ `----\"\"\" __.-'\n"
980 "hh `--..____..--'");
985 int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
987 if (CLI_HELP_REQUESTED
)
988 return CLI_HELP_NO_ARGS
;
990 cli_print(cli
, "Counters cleared");
991 SET_STAT(last_reset
, time(NULL
));
995 int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1000 if (CLI_HELP_REQUESTED
)
1001 return cli_arg_help(cli
, argc
> 1,
1002 "USER", "Username of session to drop", NULL
);
1004 if (!config
->cluster_iam_master
)
1006 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1012 cli_print(cli
, "Specify a user to drop");
1016 for (i
= 0; i
< argc
; i
++)
1018 if (!(s
= sessionbyuser(argv
[i
])))
1020 cli_print(cli
, "User %s is not connected", argv
[i
]);
1024 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1026 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1027 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1034 int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1039 if (CLI_HELP_REQUESTED
)
1040 return cli_arg_help(cli
, argc
> 1,
1041 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1043 if (!config
->cluster_iam_master
)
1045 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1051 cli_print(cli
, "Specify a tunnel to drop");
1055 for (i
= 0; i
< argc
; i
++)
1057 if ((t
= atol(argv
[i
])) <= 0 || (t
>= MAXTUNNEL
))
1059 cli_print(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1065 cli_print(cli
, "Tunnel %d is not connected", t
);
1071 cli_print(cli
, "Tunnel %d is already being shut down", t
);
1075 cli_print(cli
, "Tunnel %d shut down (%s)", t
, tunnel
[t
].hostname
);
1076 cli_tunnel_actions
[t
].action
|= CLI_TUN_KILL
;
1082 int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1087 if (CLI_HELP_REQUESTED
)
1088 return cli_arg_help(cli
, argc
> 1,
1089 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1091 if (!config
->cluster_iam_master
)
1093 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1099 cli_print(cli
, "Specify a session id to drop");
1103 for (i
= 0; i
< argc
; i
++)
1105 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1107 cli_print(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1111 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1113 cli_print(cli
, "Dropping session %d", s
);
1114 cli_session_actions
[s
].action
|= CLI_SESS_KILL
;
1118 cli_print(cli
, "Session %d is not active.", s
);
1125 int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1131 if (CLI_HELP_REQUESTED
)
1136 return cli_arg_help(cli
, 0,
1137 "USER", "Username of session to snoop", NULL
);
1140 return cli_arg_help(cli
, 0,
1141 "A.B.C.D", "IP address of snoop destination", NULL
);
1144 return cli_arg_help(cli
, 0,
1145 "N", "Port of snoop destination", NULL
);
1149 return cli_arg_help(cli
, 1, NULL
);
1156 if (!config
->cluster_iam_master
)
1158 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1164 cli_print(cli
, "Specify username ip port");
1168 if (!(s
= sessionbyuser(argv
[0])))
1170 cli_print(cli
, "User %s is not connected", argv
[0]);
1174 ip
= inet_addr(argv
[1]);
1175 if (!ip
|| ip
== INADDR_NONE
)
1177 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1181 port
= atoi(argv
[2]);
1184 cli_print(cli
, "Invalid port %s", argv
[2]);
1188 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], inet_toa(session
[s
].snoop_ip
), session
[s
].snoop_port
);
1189 cli_session_actions
[s
].snoop_ip
= ip
;
1190 cli_session_actions
[s
].snoop_port
= port
;
1191 cli_session_actions
[s
].action
|= CLI_SESS_SNOOP
;
1196 int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1201 if (CLI_HELP_REQUESTED
)
1202 return cli_arg_help(cli
, argc
> 1,
1203 "USER", "Username of session to un-snoop", NULL
);
1205 if (!config
->cluster_iam_master
)
1207 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1213 cli_print(cli
, "Specify a user");
1217 for (i
= 0; i
< argc
; i
++)
1219 if (!(s
= sessionbyuser(argv
[i
])))
1221 cli_print(cli
, "User %s is not connected", argv
[i
]);
1225 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1226 cli_session_actions
[s
].action
|= CLI_SESS_NOSNOOP
;
1231 int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1236 if (CLI_HELP_REQUESTED
)
1237 return cli_arg_help(cli
, argc
> 1,
1238 "USER", "Username of session to throttle", NULL
);
1240 if (!config
->cluster_iam_master
)
1242 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1248 cli_print(cli
, "Specify a user");
1252 for (i
= 0; i
< argc
; i
++)
1254 if (!(s
= sessionbyuser(argv
[i
])))
1256 cli_print(cli
, "User %s is not connected", argv
[i
]);
1260 if (session
[s
].throttle
)
1262 cli_print(cli
, "User %s already throttled", argv
[i
]);
1266 cli_print(cli
, "Throttling user %s", argv
[i
]);
1267 cli_session_actions
[s
].throttle
= config
->rl_rate
; // could be configurable at some stage
1268 cli_session_actions
[s
].action
|= CLI_SESS_THROTTLE
;
1274 int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1279 if (CLI_HELP_REQUESTED
)
1280 return cli_arg_help(cli
, argc
> 1,
1281 "USER", "Username of session to un-throttle", NULL
);
1283 if (!config
->cluster_iam_master
)
1285 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1291 cli_print(cli
, "Specify a user");
1295 for (i
= 0; i
< argc
; i
++)
1297 if (!(s
= sessionbyuser(argv
[i
])))
1299 cli_print(cli
, "User %s is not connected", argv
[i
]);
1303 if (!session
[s
].throttle
)
1305 cli_print(cli
, "User %s not throttled", argv
[i
]);
1309 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1310 cli_session_actions
[s
].action
|= CLI_SESS_NOTHROTTLE
;
1316 int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1320 if (CLI_HELP_REQUESTED
)
1321 return cli_arg_help(cli
, 1,
1322 "all", "Enable debugging for all except \"data\"",
1323 "critical", "", // FIXME: add descriptions
1333 char *p
= (char *) &debug_flags
;
1334 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1338 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1339 (debug_flags
.critical
) ? " critical" : "",
1340 (debug_flags
.error
) ? " error" : "",
1341 (debug_flags
.warning
) ? " warning" : "",
1342 (debug_flags
.info
) ? " info" : "",
1343 (debug_flags
.calls
) ? " calls" : "",
1344 (debug_flags
.data
) ? " data" : "");
1350 cli_print(cli
, "Debugging off");
1354 for (i
= 0; i
< argc
; i
++)
1356 int len
= strlen(argv
[i
]);
1358 if (argv
[i
][0] == 'c' && len
< 2)
1359 len
= 2; /* distinguish [cr]itical from [ca]lls */
1361 if (!strncasecmp(argv
[i
], "critical", len
)) { debug_flags
.critical
= 1; continue; }
1362 if (!strncasecmp(argv
[i
], "error", len
)) { debug_flags
.error
= 1; continue; }
1363 if (!strncasecmp(argv
[i
], "warning", len
)) { debug_flags
.warning
= 1; continue; }
1364 if (!strncasecmp(argv
[i
], "info", len
)) { debug_flags
.info
= 1; continue; }
1365 if (!strncasecmp(argv
[i
], "calls", len
)) { debug_flags
.calls
= 1; continue; }
1366 if (!strncasecmp(argv
[i
], "data", len
)) { debug_flags
.data
= 1; continue; }
1367 if (!strncasecmp(argv
[i
], "all", len
))
1369 memset(&debug_flags
, 1, sizeof(debug_flags
));
1370 debug_flags
.data
= 0;
1374 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1380 int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1384 if (CLI_HELP_REQUESTED
)
1385 return cli_arg_help(cli
, 1,
1386 "all", "Disable all debugging",
1387 "critical", "", // FIXME: add descriptions
1397 memset(&debug_flags
, 0, sizeof(debug_flags
));
1401 for (i
= 0; i
< argc
; i
++)
1403 int len
= strlen(argv
[i
]);
1405 if (argv
[i
][0] == 'c' && len
< 2)
1406 len
= 2; /* distinguish [cr]itical from [ca]lls */
1408 if (!strncasecmp(argv
[i
], "critical", len
)) { debug_flags
.critical
= 0; continue; }
1409 if (!strncasecmp(argv
[i
], "error", len
)) { debug_flags
.error
= 0; continue; }
1410 if (!strncasecmp(argv
[i
], "warning", len
)) { debug_flags
.warning
= 0; continue; }
1411 if (!strncasecmp(argv
[i
], "info", len
)) { debug_flags
.info
= 0; continue; }
1412 if (!strncasecmp(argv
[i
], "calls", len
)) { debug_flags
.calls
= 0; continue; }
1413 if (!strncasecmp(argv
[i
], "data", len
)) { debug_flags
.data
= 0; continue; }
1414 if (!strncasecmp(argv
[i
], "all", len
))
1416 memset(&debug_flags
, 0, sizeof(debug_flags
));
1420 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1426 int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1428 int i
, firstfree
= 0;
1430 if (CLI_HELP_REQUESTED
)
1431 return cli_arg_help(cli
, argc
> 1,
1432 "PLUGIN", "Name of plugin to load", NULL
);
1436 cli_print(cli
, "Specify a plugin to load");
1440 for (i
= 0; i
< MAXPLUGINS
; i
++)
1442 if (!*config
->plugins
[i
] && !firstfree
)
1444 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1446 cli_print(cli
, "Plugin is already loaded");
1453 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1454 config
->reload_config
= 1;
1455 cli_print(cli
, "Loading plugin %s", argv
[0]);
1461 int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1465 if (CLI_HELP_REQUESTED
)
1466 return cli_arg_help(cli
, argc
> 1,
1467 "PLUGIN", "Name of plugin to unload", NULL
);
1471 cli_print(cli
, "Specify a plugin to remove");
1475 for (i
= 0; i
< MAXPLUGINS
; i
++)
1477 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1479 config
->reload_config
= 1;
1480 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1485 cli_print(cli
, "Plugin is not loaded");
1489 char *duration(time_t secs
)
1491 static char *buf
= NULL
;
1494 if (!buf
) buf
= calloc(64, 1);
1498 int days
= secs
/ 86400;
1499 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1505 int mins
= secs
/ 60;
1506 int hrs
= mins
/ 60;
1509 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1511 else if (secs
>= 60)
1513 int mins
= secs
/ 60;
1514 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1517 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1522 int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1525 char buf
[100], *p
= buf
, *loads
[3];
1526 int i
, num_sessions
= 0;
1528 if (CLI_HELP_REQUESTED
)
1529 return CLI_HELP_NO_ARGS
;
1531 fh
= fopen("/proc/loadavg", "r");
1532 fgets(buf
, 100, fh
);
1535 for (i
= 0; i
< 3; i
++)
1536 loads
[i
] = strdup(strsep(&p
, " "));
1539 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1541 for (i
= 1; i
< MAXSESSION
; i
++)
1542 if (session
[i
].opened
) num_sessions
++;
1544 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1546 duration(time_now
- config
->start_time
),
1548 loads
[0], loads
[1], loads
[2]
1550 for (i
= 0; i
< 3; i
++)
1551 if (loads
[i
]) free(loads
[i
]);
1553 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1558 int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1562 if (CLI_HELP_REQUESTED
)
1568 int len
= strlen(argv
[0])-1;
1569 for (i
= 0; config_values
[i
].key
; i
++)
1570 if (!len
|| !strncmp(argv
[0], config_values
[i
].key
, len
))
1571 cli_print(cli
, " %s", config_values
[i
].key
);
1577 return cli_arg_help(cli
, 0,
1578 "VALUE", "Value for variable", NULL
);
1582 return cli_arg_help(cli
, 1, NULL
);
1591 cli_print(cli
, "Specify variable and value");
1595 for (i
= 0; config_values
[i
].key
; i
++)
1597 void *value
= ((void *)config
) + config_values
[i
].offset
;
1598 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1600 // Found a value to set
1601 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1602 switch (config_values
[i
].type
)
1605 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1608 *(int *)value
= atoi(argv
[1]);
1611 *(unsigned long *)value
= atol(argv
[1]);
1614 *(short *)value
= atoi(argv
[1]);
1617 *(unsigned *)value
= inet_addr(argv
[1]);
1620 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1626 cli_print(cli
, "Unknown variable type");
1629 config
->reload_config
= 1;
1634 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1638 int regular_stuff(struct cli_def
*cli
)
1640 int i
= debug_rb_tail
;
1644 while (i
!= ringbuffer
->tail
)
1646 int show_message
= 0;
1648 if (*ringbuffer
->buffer
[i
].message
)
1650 // Always show messages if we are doing general debug
1651 if (ringbuffer
->buffer
[i
].level
== 0 && debug_flags
.critical
) show_message
= 1;
1652 if (ringbuffer
->buffer
[i
].level
== 1 && debug_flags
.error
) show_message
= 1;
1653 if (ringbuffer
->buffer
[i
].level
== 2 && debug_flags
.warning
) show_message
= 1;
1654 if (ringbuffer
->buffer
[i
].level
== 3 && debug_flags
.info
) show_message
= 1;
1655 if (ringbuffer
->buffer
[i
].level
== 4 && debug_flags
.calls
) show_message
= 1;
1656 if (ringbuffer
->buffer
[i
].level
== 5 && debug_flags
.data
) show_message
= 1;
1661 ipt address
= htonl(ringbuffer
->buffer
[i
].address
);
1663 struct in_addr addr
;
1665 memcpy(&addr
, &address
, sizeof(ringbuffer
->buffer
[i
].address
));
1666 ipaddr
= inet_ntoa(addr
);
1668 cli_print(cli
, "\r%s-%s-%u-%u %s",
1669 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1671 ringbuffer
->buffer
[i
].tunnel
,
1672 ringbuffer
->buffer
[i
].session
,
1673 ringbuffer
->buffer
[i
].message
);
1678 if (++i
== ringbuffer
->tail
) break;
1679 if (i
== RINGBUFFER_SIZE
) i
= 0;
1682 debug_rb_tail
= ringbuffer
->tail
;