1 // L2TPNS Command Line Interface
4 char const *cvs_name
= "$Name: $";
5 char const *cvs_id_cli
= "$Id: cli.c,v 1.7 2004-07-02 07:30:43 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
, tapfd
, snoopfd
, ifrfd
, cluster_sockfd
;
44 extern sessionidt
*cli_session_kill
;
45 extern tunnelidt
*cli_tunnel_kill
;
46 extern struct configt
*config
;
47 extern struct config_descriptt config_values
[];
49 extern struct Tringbuffer
*ringbuffer
;
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
);
109 struct cli_command
*c
;
110 struct cli_command
*c2
;
112 struct sockaddr_in addr
;
115 cli_set_hostname(cli
, "l2tpns");
117 c
= cli_register_command(cli
, NULL
, "show", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
118 cli_register_command(cli
, c
, "banana", cmd_show_banana
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a banana");
120 cli_register_command(cli
, c
, "bgp", cmd_show_bgp
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show BGP status");
122 cli_register_command(cli
, c
, "cluster", cmd_show_cluster
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show cluster information");
123 cli_register_command(cli
, c
, "ipcache", cmd_show_ipcache
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show contents of the IP cache");
124 cli_register_command(cli
, c
, "plugins", cmd_show_plugins
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all installed plugins");
125 cli_register_command(cli
, c
, "pool", cmd_show_pool
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the IP address allocation pool");
126 cli_register_command(cli
, c
, "radius", cmd_show_radius
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show active radius queries");
127 cli_register_command(cli
, c
, "running-config", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the currently running configuration");
128 cli_register_command(cli
, c
, "session", cmd_show_session
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of sessions or details for a single session");
129 cli_register_command(cli
, c
, "tbf", cmd_show_tbf
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all token bucket filters in use");
130 cli_register_command(cli
, c
, "throttle", cmd_show_throttle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "List all throttled sessions and associated TBFs");
131 cli_register_command(cli
, c
, "tunnels", cmd_show_tunnels
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show a list of tunnels or details for a single tunnel");
132 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");
133 cli_register_command(cli
, c
, "version", cmd_show_version
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show currently running software version");
135 c2
= cli_register_command(cli
, c
, "histogram", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
136 cli_register_command(cli
, c2
, "idle", cmd_show_hist_idle
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session idle times");
137 cli_register_command(cli
, c2
, "open", cmd_show_hist_open
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show histogram of session durations");
140 cli_register_command(cli
, c
, "counters", cmd_show_counters
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Display all the internal counters and running totals");
142 c
= cli_register_command(cli
, NULL
, "clear", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
143 cli_register_command(cli
, c
, "counters", cmd_clear_counters
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Clear internal counters");
146 cli_register_command(cli
, NULL
, "uptime", cmd_uptime
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show uptime and bandwidth utilisation");
148 c
= cli_register_command(cli
, NULL
, "write", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
149 cli_register_command(cli
, c
, "memory", cmd_write_memory
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Save the running config to flash");
150 cli_register_command(cli
, c
, "terminal", cmd_show_run
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Show the running config");
152 cli_register_command(cli
, NULL
, "snoop", cmd_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily enable interception for a user");
153 cli_register_command(cli
, NULL
, "throttle", cmd_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily enable throttling for a user");
154 cli_register_command(cli
, NULL
, "debug", cmd_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Set the level of logging that is shown on the console");
156 c
= cli_register_command(cli
, NULL
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
157 cli_register_command(cli
, c
, "bgp", cmd_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Withdraw routes from BGP peer");
159 c
= cli_register_command(cli
, NULL
, "no", NULL
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, NULL
);
160 cli_register_command(cli
, c
, "snoop", cmd_no_snoop
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily disable interception for a user");
161 cli_register_command(cli
, c
, "throttle", cmd_no_throttle
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Temporarily disable throttling for a user");
162 cli_register_command(cli
, c
, "debug", cmd_no_debug
, PRIVILEGE_UNPRIVILEGED
, MODE_EXEC
, "Turn off logging of a certain level of debugging");
163 c2
= cli_register_command(cli
, c
, "suspend", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
164 cli_register_command(cli
, c2
, "bgp", cmd_no_suspend_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Advertise routes to BGP peer");
166 c
= cli_register_command(cli
, NULL
, "drop", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
167 cli_register_command(cli
, c
, "user", cmd_drop_user
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a user");
168 cli_register_command(cli
, c
, "tunnel", cmd_drop_tunnel
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a tunnel and all sessions on that tunnel");
169 cli_register_command(cli
, c
, "session", cmd_drop_session
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Disconnect a session");
171 c
= cli_register_command(cli
, NULL
, "restart", NULL
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, NULL
);
172 cli_register_command(cli
, c
, "bgp", cmd_restart_bgp
, PRIVILEGE_PRIVILEGED
, MODE_EXEC
, "Restart BGP");
174 c
= cli_register_command(cli
, NULL
, "load", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
175 cli_register_command(cli
, c
, "plugin", cmd_load_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Load a plugin");
177 c
= cli_register_command(cli
, NULL
, "remove", NULL
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, NULL
);
178 cli_register_command(cli
, c
, "plugin", cmd_remove_plugin
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Remove a plugin");
180 cli_register_command(cli
, NULL
, "set", cmd_set
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
, "Set a configuration variable");
182 // Enable regular processing
183 cli_regular(cli
, regular_stuff
);
185 if (!(f
= fopen(CLIUSERS
, "r")))
187 log(0, 0, 0, 0, "WARNING! No users specified. Command-line access is open to all\n");
191 while (fgets(buf
, 4096, f
))
194 if (*buf
== '#') continue;
195 if ((p
= strchr(buf
, '\r'))) *p
= 0;
196 if ((p
= strchr(buf
, '\n'))) *p
= 0;
198 if (!(p
= strchr((char *)buf
, ':'))) continue;
200 if (!strcmp(buf
, "enable"))
202 cli_allow_enable(cli
, p
);
203 log(3, 0, 0, 0, "Setting enable password\n");
207 cli_allow_user(cli
, buf
, p
);
208 log(3, 0, 0, 0, "Allowing user %s to connect to the CLI\n", buf
);
214 memset(&addr
, 0, sizeof(addr
));
215 clifd
= socket(PF_INET
, SOCK_STREAM
, 6);
216 setsockopt(clifd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
219 // Set cli fd as non-blocking
220 flags
= fcntl(clifd
, F_GETFL
, 0);
221 fcntl(clifd
, F_SETFL
, flags
| O_NONBLOCK
);
223 addr
.sin_family
= AF_INET
;
224 addr
.sin_port
= htons(23);
225 if (bind(clifd
, (void *) &addr
, sizeof(addr
)) < 0)
227 log(0, 0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno
));
233 void cli_do(int sockfd
)
238 if (config
->scheduler_fifo
)
241 struct sched_param params
= {0};
242 params
.sched_priority
= 0;
243 if ((ret
= sched_setscheduler(0, SCHED_OTHER
, ¶ms
)) == 0)
245 log(3, 0, 0, 0, "Dropped FIFO scheduler\n");
249 log(0, 0, 0, 0, "Error setting scheduler to OTHER: %s\n", strerror(errno
));
250 log(0, 0, 0, 0, "This is probably really really bad.\n");
254 signal(SIGPIPE
, SIG_DFL
);
255 signal(SIGCHLD
, SIG_DFL
);
256 signal(SIGHUP
, SIG_DFL
);
257 signal(SIGUSR1
, SIG_DFL
);
258 signal(SIGQUIT
, SIG_DFL
);
259 signal(SIGKILL
, SIG_DFL
);
260 signal(SIGALRM
, SIG_DFL
);
261 signal(SIGTERM
, SIG_DFL
);
264 if (udpfd
) close(udpfd
); udpfd
= 0;
265 if (tapfd
) close(tapfd
); tapfd
= 0;
266 if (snoopfd
) close(snoopfd
); snoopfd
= 0;
267 for (i
= 0; i
< config
->num_radfds
; i
++)
268 if (radfds
[i
]) close(radfds
[i
]);
269 if (ifrfd
) close(ifrfd
); ifrfd
= 0;
270 if (cluster_sockfd
) close(cluster_sockfd
); cluster_sockfd
= 0;
271 if (clifd
) close(clifd
); clifd
= 0;
273 for (i
= 0; i
< BGP_NUM_PEERS
; i
++)
274 if (bgp_peers
[i
].sock
!= -1)
275 close(bgp_peers
[i
].sock
);
279 int require_auth
= 1;
280 struct sockaddr_in addr
;
281 int l
= sizeof(addr
);
282 if (getpeername(sockfd
, (struct sockaddr
*)&addr
, &l
) == 0)
284 log(3, 0, 0, 0, "Accepted connection to CLI from %s\n", inet_toa(addr
.sin_addr
.s_addr
));
285 require_auth
= addr
.sin_addr
.s_addr
!= inet_addr("127.0.0.1");
288 log(0, 0, 0, 0, "getpeername() failed on cli socket. Requiring authentication: %s\n", strerror(errno
));
292 log(3, 0, 0, 0, "CLI is remote, requiring authentication\n");
293 if (!cli
->users
) /* paranoia */
295 log(0, 0, 0, 0, "No users for remote authentication! Exiting CLI\n");
301 /* no username/pass required */
309 debug_rb_tail
= ringbuffer
->tail
;
311 memset(&debug_flags
, 0, sizeof(debug_flags
));
312 debug_flags
.critical
= 1;
316 snprintf(prompt
, 1005, "l2tpns> ");
317 cli_loop(cli
, sockfd
, prompt
);
321 log(3, 0, 0, 0, "Closed CLI connection\n");
325 void cli_print_log(struct cli_def
*cli
, char *string
)
327 log(3, 0, 0, 0, "%s\n", string
);
330 void cli_do_file(FILE *fh
)
332 log(3, 0, 0, 0, "Reading configuration file\n");
333 cli_print_callback(cli
, cli_print_log
);
334 cli_file(cli
, fh
, PRIVILEGE_PRIVILEGED
, MODE_CONFIG
);
335 cli_print_callback(cli
, NULL
);
338 int cli_arg_help(struct cli_def
*cli
, int cr_ok
, char *entry
, ...)
349 if ((p
= strchr(entry
, '%')) && !strchr(p
+1, '%') && p
[1] == 'd')
351 int v
= va_arg(ap
, int);
352 snprintf(buf
, sizeof(buf
), entry
, v
);
358 desc
= va_arg(ap
, char *);
360 cli_print(cli
, " %-20s %s", p
, desc
);
362 cli_print(cli
, " %s", p
);
364 entry
= desc
? va_arg(ap
, char *) : 0;
369 cli_print(cli
, " <cr>");
374 int cmd_show_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
379 if (CLI_HELP_REQUESTED
)
380 return cli_arg_help(cli
, 1,
381 "<1-%d>", MAXSESSION
-1, "Show specific session by id",
387 // Show individual session
388 for (i
= 0; i
< argc
; i
++)
392 if (s
<= 0 || s
>= MAXSESSION
)
394 cli_print(cli
, "Invalid session id \"%s\"", argv
[i
]);
397 cli_print(cli
, "\r\nSession %d:", s
);
398 cli_print(cli
, " User: %s", session
[s
].user
[0] ? session
[s
].user
: "none");
399 cli_print(cli
, " Calling Num: %s", session
[s
].calling
);
400 cli_print(cli
, " Called Num: %s", session
[s
].called
);
401 cli_print(cli
, " Tunnel ID: %d", session
[s
].tunnel
);
402 cli_print(cli
, " IP address: %s", inet_toa(htonl(session
[s
].ip
)));
403 cli_print(cli
, " HSD sid: %lu", session
[s
].sid
);
404 cli_print(cli
, " Idle time: %u seconds", abs(time_now
- session
[s
].last_packet
));
405 cli_print(cli
, " Next Recv: %u", session
[s
].nr
);
406 cli_print(cli
, " Next Send: %u", session
[s
].ns
);
407 cli_print(cli
, " Bytes In/Out: %lu/%lu", (unsigned long)session
[s
].total_cout
, (unsigned long)session
[s
].total_cin
);
408 cli_print(cli
, " Pkts In/Out: %lu/%lu", (unsigned long)session
[s
].pout
, (unsigned long)session
[s
].pin
);
409 cli_print(cli
, " MRU: %d", session
[s
].mru
);
410 cli_print(cli
, " Radius Session: %u", session
[s
].radius
);
411 cli_print(cli
, " Rx Speed: %lu", session
[s
].rx_connect_speed
);
412 cli_print(cli
, " Tx Speed: %lu", session
[s
].tx_connect_speed
);
413 if (session
[s
].snoop_ip
&& session
[s
].snoop_port
)
414 cli_print(cli
, " Intercepted: %s:%d", inet_toa(session
[s
].snoop_ip
), session
[s
] .snoop_port
);
416 cli_print(cli
, " Intercepted: no");
417 cli_print(cli
, " Throttled: %s", session
[s
].throttle
? "YES" : "no");
418 cli_print(cli
, " Walled Garden: %s", session
[s
].walled_garden
? "YES" : "no");
419 cli_print(cli
, " Filter BucketI: %d", session
[s
].tbf_in
);
420 cli_print(cli
, " Filter BucketO: %d", session
[s
].tbf_out
);
426 cli_print(cli
, " %s %4s %-32s %-15s %s %s %s %10s %10s %10s %4s %-15s %s",
440 for (i
= 1; i
< MAXSESSION
; i
++)
442 char *userip
, *tunnelip
;
443 if (!session
[i
].opened
) continue;
444 userip
= strdup(inet_toa(htonl(session
[i
].ip
)));
445 tunnelip
= strdup(inet_toa(htonl(tunnel
[ session
[i
].tunnel
].ip
)));
446 cli_print(cli
, "%5d %4d %-32s %-15s %s %s %s %10u %10lu %10lu %4u %-15s %s",
449 session
[i
].user
[0] ? session
[i
].user
: "*",
451 (session
[i
].snoop_ip
&& session
[i
].snoop_port
) ? "Y" : "N",
452 (session
[i
].throttle
) ? "Y" : "N",
453 (session
[i
].walled_garden
) ? "Y" : "N",
454 abs(time_now
- (unsigned long)session
[i
].opened
),
455 (unsigned long)session
[i
].total_cout
,
456 (unsigned long)session
[i
].total_cin
,
457 abs(time_now
- (session
[i
].last_packet
? session
[i
].last_packet
: time_now
)),
459 session
[i
].calling
[0] ? session
[i
].calling
: "*");
460 if (userip
) free(userip
);
461 if (tunnelip
) free(tunnelip
);
466 int cmd_show_tunnels(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
468 int i
, x
, show_all
= 0;
477 if (CLI_HELP_REQUESTED
)
480 return cli_arg_help(cli
, 1,
481 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
484 return cli_arg_help(cli
, 1,
485 "all", "Show all tunnels, including unused",
486 "<1-%d>", MAXTUNNEL
-1, "Show specific tunnel by id",
493 if (strcmp(argv
[0], "all") == 0)
499 // Show individual tunnel
500 for (i
= 0; i
< argc
; i
++)
505 if (t
<= 0 || t
>= MAXTUNNEL
)
507 cli_print(cli
, "Invalid tunnel id \"%s\"", argv
[i
]);
510 cli_print(cli
, "\r\nTunnel %d:", t
);
511 cli_print(cli
, " State: %s", states
[tunnel
[t
].state
]);
512 cli_print(cli
, " Hostname: %s", tunnel
[t
].hostname
[0] ? tunnel
[t
].hostname
: "(none)");
513 cli_print(cli
, " Remote IP: %s", inet_toa(htonl(tunnel
[t
].ip
)));
514 cli_print(cli
, " Remote Port: %d", tunnel
[t
].port
);
515 cli_print(cli
, " Rx Window: %u", tunnel
[t
].window
);
516 cli_print(cli
, " Next Recv: %u", tunnel
[t
].nr
);
517 cli_print(cli
, " Next Send: %u", tunnel
[t
].ns
);
518 cli_print(cli
, " Queue Len: %u", tunnel
[t
].controlc
);
519 cli_print(cli
, " Last Packet Age:%u", (unsigned)(time_now
- tunnel
[t
].last
));
521 for (x
= 0; x
< MAXSESSION
; x
++)
522 if (session
[x
].tunnel
== t
&& session
[x
].opened
&& !session
[x
].die
)
523 sprintf(s
, "%s%u ", s
, x
);
524 cli_print(cli
, " Sessions: %s", s
);
530 // Show tunnel summary
531 cli_print(cli
, "%s %s %s %s %s",
537 for (i
= 1; i
< MAXTUNNEL
; i
++)
540 if (!show_all
&& (!tunnel
[i
].ip
|| tunnel
[i
].die
|| !tunnel
[i
].hostname
[0])) continue;
542 for (x
= 0; x
< MAXSESSION
; x
++) if (session
[x
].tunnel
== i
&& session
[x
].opened
&& !session
[x
].die
) sessions
++;
543 cli_print(cli
, "%d %s %s %s %d",
545 *tunnel
[i
].hostname
? tunnel
[i
].hostname
: "(null)",
546 inet_toa(htonl(tunnel
[i
].ip
)),
547 states
[tunnel
[i
].state
],
553 int cmd_show_users(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
560 if (CLI_HELP_REQUESTED
)
561 return cli_arg_help(cli
, 1,
562 "USER", "Show details for specific username",
565 for (i
= 0; i
< MAXSESSION
; i
++)
567 if (!session
[i
].opened
) continue;
568 if (!session
[i
].user
[0]) continue;
572 for (j
= 0; j
< argc
&& sargc
< 32; j
++)
574 if (strcmp(argv
[j
], session
[i
].user
) == 0)
576 snprintf(sid
[sargc
], sizeof(sid
[0]), "%d", i
);
577 sargv
[sargc
] = sid
[sargc
];
585 cli_print(cli
, "%s", session
[i
].user
);
589 return cmd_show_session(cli
, "users", sargv
, sargc
);
594 int cmd_show_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
596 if (CLI_HELP_REQUESTED
)
597 return CLI_HELP_NO_ARGS
;
599 cli_print(cli
, "%-10s %-8s %-10s %-8s", "Ethernet", "Bytes", "Packets", "Errors");
600 cli_print(cli
, "%-10s %8lu %8lu %8lu", "RX",
601 GET_STAT(tap_rx_bytes
),
602 GET_STAT(tap_rx_packets
),
603 GET_STAT(tap_rx_errors
));
604 cli_print(cli
, "%-10s %8lu %8lu %8lu", "TX",
605 GET_STAT(tap_tx_bytes
),
606 GET_STAT(tap_tx_packets
),
607 GET_STAT(tap_tx_errors
));
610 cli_print(cli
, "%-10s %-8s %-10s %-8s %-8s", "Tunnel", "Bytes", "Packets", "Errors", "Retries");
611 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "RX",
612 GET_STAT(tunnel_rx_bytes
),
613 GET_STAT(tunnel_rx_packets
),
614 GET_STAT(tunnel_rx_errors
),
616 cli_print(cli
, "%-10s %8lu %8lu %8lu %8lu", "TX",
617 GET_STAT(tunnel_tx_bytes
),
618 GET_STAT(tunnel_tx_packets
),
619 GET_STAT(tunnel_tx_errors
),
620 GET_STAT(tunnel_retries
));
623 cli_print(cli
, "%-30s%-10s", "Counter", "Value");
624 cli_print(cli
, "-----------------------------------------");
625 cli_print(cli
, "%-30s%lu", "radius_retries", GET_STAT(radius_retries
));
626 cli_print(cli
, "%-30s%lu", "arp_errors", GET_STAT(arp_errors
));
627 cli_print(cli
, "%-30s%lu", "arp_replies", GET_STAT(arp_replies
));
628 cli_print(cli
, "%-30s%lu", "arp_discarded", GET_STAT(arp_discarded
));
629 cli_print(cli
, "%-30s%lu", "arp_sent", GET_STAT(arp_sent
));
630 cli_print(cli
, "%-30s%lu", "arp_recv", GET_STAT(arp_recv
));
631 cli_print(cli
, "%-30s%lu", "packets_snooped", GET_STAT(packets_snooped
));
632 cli_print(cli
, "%-30s%lu", "tunnel_created", GET_STAT(tunnel_created
));
633 cli_print(cli
, "%-30s%lu", "session_created", GET_STAT(session_created
));
634 cli_print(cli
, "%-30s%lu", "tunnel_timeout", GET_STAT(tunnel_timeout
));
635 cli_print(cli
, "%-30s%lu", "session_timeout", GET_STAT(session_timeout
));
636 cli_print(cli
, "%-30s%lu", "radius_timeout", GET_STAT(radius_timeout
));
637 cli_print(cli
, "%-30s%lu", "radius_overflow", GET_STAT(radius_overflow
));
638 cli_print(cli
, "%-30s%lu", "tunnel_overflow", GET_STAT(tunnel_overflow
));
639 cli_print(cli
, "%-30s%lu", "session_overflow", GET_STAT(session_overflow
));
640 cli_print(cli
, "%-30s%lu", "ip_allocated", GET_STAT(ip_allocated
));
641 cli_print(cli
, "%-30s%lu", "ip_freed", GET_STAT(ip_freed
));
642 cli_print(cli
, "%-30s%lu", "cluster_forwarded", GET_STAT(c_forwarded
));
643 cli_print(cli
, "%-30s%lu", "recv_forward", GET_STAT(recv_forward
));
647 cli_print(cli
, "\n%-30s%-10s", "Counter", "Value");
648 cli_print(cli
, "-----------------------------------------");
649 cli_print(cli
, "%-30s%lu", "call_processtap", GET_STAT(call_processtap
));
650 cli_print(cli
, "%-30s%lu", "call_processarp", GET_STAT(call_processarp
));
651 cli_print(cli
, "%-30s%lu", "call_processipout", GET_STAT(call_processipout
));
652 cli_print(cli
, "%-30s%lu", "call_processudp", GET_STAT(call_processudp
));
653 cli_print(cli
, "%-30s%lu", "call_processpap", GET_STAT(call_processpap
));
654 cli_print(cli
, "%-30s%lu", "call_processchap", GET_STAT(call_processchap
));
655 cli_print(cli
, "%-30s%lu", "call_processlcp", GET_STAT(call_processlcp
));
656 cli_print(cli
, "%-30s%lu", "call_processipcp", GET_STAT(call_processipcp
));
657 cli_print(cli
, "%-30s%lu", "call_processipin", GET_STAT(call_processipin
));
658 cli_print(cli
, "%-30s%lu", "call_processccp", GET_STAT(call_processccp
));
659 cli_print(cli
, "%-30s%lu", "call_processrad", GET_STAT(call_processrad
));
660 cli_print(cli
, "%-30s%lu", "call_sendarp", GET_STAT(call_sendarp
));
661 cli_print(cli
, "%-30s%lu", "call_sendipcp", GET_STAT(call_sendipcp
));
662 cli_print(cli
, "%-30s%lu", "call_sendchap", GET_STAT(call_sendchap
));
663 cli_print(cli
, "%-30s%lu", "call_sessionbyip", GET_STAT(call_sessionbyip
));
664 cli_print(cli
, "%-30s%lu", "call_sessionbyuser", GET_STAT(call_sessionbyuser
));
665 cli_print(cli
, "%-30s%lu", "call_tunnelsend", GET_STAT(call_tunnelsend
));
666 cli_print(cli
, "%-30s%lu", "call_tunnelkill", GET_STAT(call_tunnelkill
));
667 cli_print(cli
, "%-30s%lu", "call_tunnelshutdown", GET_STAT(call_tunnelshutdown
));
668 cli_print(cli
, "%-30s%lu", "call_sessionkill", GET_STAT(call_sessionkill
));
669 cli_print(cli
, "%-30s%lu", "call_sessionshutdown", GET_STAT(call_sessionshutdown
));
670 cli_print(cli
, "%-30s%lu", "call_sessionsetup", GET_STAT(call_sessionsetup
));
671 cli_print(cli
, "%-30s%lu", "call_assign_ip_address",GET_STAT(call_assign_ip_address
));
672 cli_print(cli
, "%-30s%lu", "call_free_ip_address", GET_STAT(call_free_ip_address
));
673 cli_print(cli
, "%-30s%lu", "call_dump_acct_info", GET_STAT(call_dump_acct_info
));
674 cli_print(cli
, "%-30s%lu", "call_radiussend", GET_STAT(call_radiussend
));
675 cli_print(cli
, "%-30s%lu", "call_radiusretry", GET_STAT(call_radiusretry
));
680 int cmd_show_version(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
686 if (CLI_HELP_REQUESTED
)
687 return cli_arg_help(cli
, 1,
688 "tag", "Include CVS release tag",
689 "file", "Include file versions",
692 for (i
= 0; i
< argc
; i
++)
693 if (!strcmp(argv
[i
], "tag"))
695 else if (!strcmp(argv
[i
], "file"))
698 cli_print(cli
, "L2TPNS %s", VERSION
);
701 char const *p
= strchr(cvs_name
, ':');
713 e
= strpbrk(p
, " \t$");
714 cli_print(cli
, "Tag: %.*s", e
? e
- p
+ 1 : strlen(p
), p
);
719 extern linked_list
*loaded_plugins
;
722 cli_print(cli
, "Files:");
723 cli_print(cli
, " %s", cvs_id_arp
);
725 cli_print(cli
, " %s", cvs_id_bgp
);
727 cli_print(cli
, " %s", cvs_id_cli
);
728 cli_print(cli
, " %s", cvs_id_cluster
);
729 cli_print(cli
, " %s", cvs_id_constants
);
730 cli_print(cli
, " %s", cvs_id_control
);
731 cli_print(cli
, " %s", cvs_id_icmp
);
732 cli_print(cli
, " %s", cvs_id_l2tpns
);
733 cli_print(cli
, " %s", cvs_id_ll
);
734 cli_print(cli
, " %s", cvs_id_md5
);
735 cli_print(cli
, " %s", cvs_id_ppp
);
736 cli_print(cli
, " %s", cvs_id_radius
);
737 cli_print(cli
, " %s", cvs_id_tbf
);
738 cli_print(cli
, " %s", cvs_id_util
);
740 ll_reset(loaded_plugins
);
741 while ((p
= ll_next(loaded_plugins
)))
743 char const **id
= dlsym(p
, "cvs_id");
745 cli_print(cli
, " %s", *id
);
752 int cmd_show_pool(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
755 int used
= 0, free
= 0, show_all
= 0;
758 if (!config
->cluster_iam_master
)
760 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
764 if (CLI_HELP_REQUESTED
)
767 return cli_arg_help(cli
, 1, NULL
);
769 return cli_arg_help(cli
, 1,
770 "all", "Show all pool addresses, including unused",
774 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
778 cli_print(cli
, "%-15s %4s %8s %s", "IP Address", "Used", "Session", "User");
779 for (i
= 0; i
< MAXIPPOOL
; i
++)
781 if (!ip_address_pool
[i
].address
) continue;
782 if (ip_address_pool
[i
].assigned
)
784 cli_print(cli
, "%-15s Y %8d %s",
785 inet_toa(htonl(ip_address_pool
[i
].address
)), ip_address_pool
[i
].session
, session
[ip_address_pool
[i
].session
].user
);
791 if (ip_address_pool
[i
].last
)
792 cli_print(cli
, "%-15s N %8s [%s] %ds",
793 inet_toa(htonl(ip_address_pool
[i
].address
)), "",
794 ip_address_pool
[i
].user
, time_now
- ip_address_pool
[i
].last
);
796 cli_print(cli
, "%-15s N", inet_toa(htonl(ip_address_pool
[i
].address
)));
803 cli_print(cli
, "(Not displaying unused addresses)");
805 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
809 void print_save_config(struct cli_def
*cli
, char *string
)
812 fprintf(save_config_fh
, "%s\n", string
);
815 int cmd_write_memory(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
817 if (CLI_HELP_REQUESTED
)
818 return CLI_HELP_NO_ARGS
;
820 if ((save_config_fh
= fopen(config
->config_file
, "w")))
822 cli_print(cli
, "Writing configuration");
823 cli_print_callback(cli
, print_save_config
);
824 cmd_show_run(cli
, command
, argv
, argc
);
825 cli_print_callback(cli
, NULL
);
826 fclose(save_config_fh
);
830 cli_print(cli
, "Error writing configuration: %s", strerror(errno
));
835 int cmd_show_run(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
839 if (CLI_HELP_REQUESTED
)
840 return CLI_HELP_NO_ARGS
;
842 cli_print(cli
, "# Current configuration:");
844 for (i
= 0; config_values
[i
].key
; i
++)
846 void *value
= ((void *)config
) + config_values
[i
].offset
;
847 if (config_values
[i
].type
== STRING
)
848 cli_print(cli
, "set %s \"%.*s\"", config_values
[i
].key
, config_values
[i
].size
, (char *)value
);
849 else if (config_values
[i
].type
== IP
)
850 cli_print(cli
, "set %s %s", config_values
[i
].key
, inet_toa(*(unsigned *)value
));
851 else if (config_values
[i
].type
== SHORT
)
852 cli_print(cli
, "set %s %hu", config_values
[i
].key
, *(short *)value
);
853 else if (config_values
[i
].type
== BOOL
)
854 cli_print(cli
, "set %s %s", config_values
[i
].key
, (*(int *)value
) ? "yes" : "no");
855 else if (config_values
[i
].type
== INT
)
856 cli_print(cli
, "set %s %d", config_values
[i
].key
, *(int *)value
);
857 else if (config_values
[i
].type
== UNSIGNED_LONG
)
858 cli_print(cli
, "set %s %lu", config_values
[i
].key
, *(unsigned long *)value
);
861 cli_print(cli
, "# Plugins");
862 for (i
= 0; i
< MAXPLUGINS
; i
++)
864 if (*config
->plugins
[i
])
866 cli_print(cli
, "load plugin \"%s\"", config
->plugins
[i
]);
870 cli_print(cli
, "# end");
874 int cmd_show_radius(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
885 int i
, free
= 0, used
= 0, show_all
= 0;
888 if (CLI_HELP_REQUESTED
)
891 return cli_arg_help(cli
, 1, NULL
);
893 return cli_arg_help(cli
, 1,
894 "all", "Show all RADIUS sessions, including unused",
898 cli_print(cli
, "%6s%5s%6s%9s%9s%4s", "Radius", "Sock", "State", "Session", "Retry", "Try");
902 if (argc
> 0 && strcmp(argv
[0], "all") == 0)
905 for (i
= 1; i
< MAXRADIUS
; i
++)
907 if (radius
[i
].state
== RADIUSNULL
)
912 if (!show_all
&& radius
[i
].state
== RADIUSNULL
) continue;
914 cli_print(cli
, "%6d%5d%6s%9d%9u%4d",
917 states
[radius
[i
].state
],
923 cli_print(cli
, "\r\nFree: %d\r\nUsed: %d", free
, used
);
928 int cmd_show_plugins(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
932 if (CLI_HELP_REQUESTED
)
933 return CLI_HELP_NO_ARGS
;
935 cli_print(cli
, "Plugins currently loaded:");
936 for (i
= 0; i
< MAXPLUGINS
; i
++)
938 if (*config
->plugins
[i
])
940 cli_print(cli
, " %s", config
->plugins
[i
]);
946 int cmd_show_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
950 if (CLI_HELP_REQUESTED
)
951 return CLI_HELP_NO_ARGS
;
953 cli_print(cli
, "Token bucket filters:");
954 cli_print(cli
, "%-6s %8s %-4s", "ID", "Handle", "Used");
955 for (i
= 0; i
< MAXSESSION
; i
++)
957 if (!session
[i
].throttle
)
960 cli_print(cli
, "%-6d %8d %8d",
968 int cmd_show_banana(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
970 if (CLI_HELP_REQUESTED
)
971 return CLI_HELP_NO_ARGS
;
973 cli_print(cli
, " _\n"
979 " ( \\ `. `-. _,.-:\\\n"
980 " \\ \\ `. `-._ __..--' ,-';/\n"
981 " \\ `. `-. `-..___..---' _.--' ,'/\n"
982 " `. `. `-._ __..--' ,' /\n"
983 " `. `-_ ``--..'' _.-' ,'\n"
984 " `-_ `-.___ __,--' ,'\n"
985 " `-.__ `----\"\"\" __.-'\n"
986 "hh `--..____..--'");
991 int cmd_clear_counters(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
993 if (CLI_HELP_REQUESTED
)
994 return CLI_HELP_NO_ARGS
;
996 cli_print(cli
, "Counters cleared");
997 SET_STAT(last_reset
, time(NULL
));
1001 int cmd_drop_user(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1006 if (CLI_HELP_REQUESTED
)
1007 return cli_arg_help(cli
, argc
> 1,
1008 "USER", "Username of session to drop", NULL
);
1010 if (!config
->cluster_iam_master
)
1012 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1018 cli_print(cli
, "Specify a user to drop");
1022 for (i
= 0; i
< argc
; i
++)
1024 if (!(s
= sessionbyuser(argv
[i
])))
1026 cli_print(cli
, "User %s is not connected", argv
[i
]);
1030 if (session
[s
].ip
&& session
[s
].opened
&& !session
[s
].die
)
1034 cli_print(cli
, "Dropping user %s", session
[s
].user
);
1035 for (x
= 0; x
< MAXSESSION
; x
++)
1037 if (!cli_session_kill
[x
])
1039 cli_session_kill
[x
] = s
;
1049 int cmd_drop_tunnel(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1054 if (CLI_HELP_REQUESTED
)
1055 return cli_arg_help(cli
, argc
> 1,
1056 "<1-%d>", MAXTUNNEL
-1, "Tunnel id to drop", NULL
);
1058 if (!config
->cluster_iam_master
)
1060 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1066 cli_print(cli
, "Specify a tunnel to drop");
1070 for (i
= 0; i
< argc
; i
++)
1074 if ((tid
= atol(argv
[i
])) <= 0 || (tid
>= MAXTUNNEL
))
1076 cli_print(cli
, "Invalid tunnel ID (1-%d)", MAXTUNNEL
-1);
1080 if (!tunnel
[tid
].ip
)
1082 cli_print(cli
, "Tunnel %d is not connected", tid
);
1086 if (tunnel
[tid
].die
)
1088 cli_print(cli
, "Tunnel %d is already being shut down", tid
);
1092 for (x
= 0; x
< MAXTUNNEL
; x
++)
1094 if (!cli_tunnel_kill
[x
])
1096 cli_tunnel_kill
[x
] = tid
;
1097 cli_print(cli
, "Tunnel %d shut down (%s)", tid
, tunnel
[tid
].hostname
);
1106 int cmd_drop_session(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1111 if (CLI_HELP_REQUESTED
)
1112 return cli_arg_help(cli
, argc
> 1,
1113 "<1-%d>", MAXSESSION
-1, "Session id to drop", NULL
);
1115 if (!config
->cluster_iam_master
)
1117 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1123 cli_print(cli
, "Specify a session id to drop");
1127 for (i
= 0; i
< argc
; i
++)
1129 if ((s
= atol(argv
[i
])) <= 0 || (s
> MAXSESSION
))
1131 cli_print(cli
, "Invalid session ID (1-%d)", MAXSESSION
-1);
1135 if (session
[s
].opened
&& !session
[s
].die
)
1138 for (x
= 0; x
< MAXSESSION
; x
++)
1140 if (!cli_session_kill
[x
])
1142 cli_session_kill
[x
] = s
;
1146 cli_print(cli
, "Dropping session %d", s
);
1150 cli_print(cli
, "Session %d is not active.", s
);
1157 int cmd_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1163 if (CLI_HELP_REQUESTED
)
1168 return cli_arg_help(cli
, 0,
1169 "USER", "Username of session to snoop", NULL
);
1172 return cli_arg_help(cli
, 0,
1173 "A.B.C.D", "IP address of snoop destination", NULL
);
1176 return cli_arg_help(cli
, 0,
1177 "N", "Port of snoop destination", NULL
);
1181 return cli_arg_help(cli
, 1, NULL
);
1188 if (!config
->cluster_iam_master
)
1190 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1196 cli_print(cli
, "Specify username ip port");
1200 if (!(s
= sessionbyuser(argv
[0])))
1202 cli_print(cli
, "User %s is not connected", argv
[0]);
1206 ip
= inet_addr(argv
[1]);
1207 if (!ip
|| ip
== INADDR_NONE
)
1209 cli_print(cli
, "Cannot parse IP \"%s\"", argv
[1]);
1213 port
= atoi(argv
[2]);
1216 cli_print(cli
, "Invalid port %s", argv
[2]);
1220 session
[s
].snoop_ip
= ip
;
1221 session
[s
].snoop_port
= port
;
1223 cli_print(cli
, "Snooping user %s to %s:%d", argv
[0], inet_toa(session
[s
].snoop_ip
), session
[s
].snoop_port
);
1227 int cmd_no_snoop(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1232 if (CLI_HELP_REQUESTED
)
1233 return cli_arg_help(cli
, argc
> 1,
1234 "USER", "Username of session to un-snoop", NULL
);
1236 if (!config
->cluster_iam_master
)
1238 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1244 cli_print(cli
, "Specify a user");
1248 for (i
= 0; i
< argc
; i
++)
1250 if (!(s
= sessionbyuser(argv
[i
])))
1252 cli_print(cli
, "User %s is not connected", argv
[i
]);
1255 session
[s
].snoop_ip
= 0;
1256 session
[s
].snoop_port
= 0;
1258 cli_print(cli
, "Not snooping user %s", argv
[i
]);
1263 int cmd_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1268 if (CLI_HELP_REQUESTED
)
1269 return cli_arg_help(cli
, argc
> 1,
1270 "USER", "Username of session to throttle", NULL
);
1272 if (!config
->cluster_iam_master
)
1274 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1280 cli_print(cli
, "Specify a user");
1284 for (i
= 0; i
< argc
; i
++)
1286 if (!(s
= sessionbyuser(argv
[i
])))
1288 cli_print(cli
, "User %s is not connected", argv
[i
]);
1291 if (!throttle_session(s
, config
->rl_rate
))
1292 cli_print(cli
, "Error throttling %s", argv
[i
]);
1294 cli_print(cli
, "Throttling user %s", argv
[i
]);
1300 int cmd_no_throttle(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1305 if (CLI_HELP_REQUESTED
)
1306 return cli_arg_help(cli
, argc
> 1,
1307 "USER", "Username of session to un-throttle", NULL
);
1309 if (!config
->cluster_iam_master
)
1311 cli_print(cli
, "Can't do this on a slave. Do it on %s", inet_toa(config
->cluster_master_address
));
1317 cli_print(cli
, "Specify a user");
1321 for (i
= 0; i
< argc
; i
++)
1323 if (!(s
= sessionbyuser(argv
[i
])))
1325 cli_print(cli
, "User %s is not connected", argv
[i
]);
1328 throttle_session(s
, 0);
1330 cli_print(cli
, "Unthrottling user %s", argv
[i
]);
1336 int cmd_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1340 if (CLI_HELP_REQUESTED
)
1341 return cli_arg_help(cli
, 1,
1342 "all", "Enable debugging for all except \"data\"",
1343 "critical", "", // FIXME: add descriptions
1353 char *p
= (char *) &debug_flags
;
1354 for (i
= 0; i
< sizeof(debug_flags
); i
++)
1358 cli_print(cli
, "Currently debugging:%s%s%s%s%s%s",
1359 (debug_flags
.critical
) ? " critical" : "",
1360 (debug_flags
.error
) ? " error" : "",
1361 (debug_flags
.warning
) ? " warning" : "",
1362 (debug_flags
.info
) ? " info" : "",
1363 (debug_flags
.calls
) ? " calls" : "",
1364 (debug_flags
.data
) ? " data" : "");
1370 cli_print(cli
, "Debugging off");
1374 for (i
= 0; i
< argc
; i
++)
1376 int len
= strlen(argv
[i
]);
1378 if (argv
[i
][0] == 'c' && len
< 2)
1379 len
= 2; /* distinguish [cr]itical from [ca]lls */
1381 if (!strncasecmp(argv
[i
], "critical", len
)) { debug_flags
.critical
= 1; continue; }
1382 if (!strncasecmp(argv
[i
], "error", len
)) { debug_flags
.error
= 1; continue; }
1383 if (!strncasecmp(argv
[i
], "warning", len
)) { debug_flags
.warning
= 1; continue; }
1384 if (!strncasecmp(argv
[i
], "info", len
)) { debug_flags
.info
= 1; continue; }
1385 if (!strncasecmp(argv
[i
], "calls", len
)) { debug_flags
.calls
= 1; continue; }
1386 if (!strncasecmp(argv
[i
], "data", len
)) { debug_flags
.data
= 1; continue; }
1387 if (!strncasecmp(argv
[i
], "all", len
))
1389 memset(&debug_flags
, 1, sizeof(debug_flags
));
1390 debug_flags
.data
= 0;
1394 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1400 int cmd_no_debug(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1404 if (CLI_HELP_REQUESTED
)
1405 return cli_arg_help(cli
, 1,
1406 "all", "Disable all debugging",
1407 "critical", "", // FIXME: add descriptions
1417 memset(&debug_flags
, 0, sizeof(debug_flags
));
1421 for (i
= 0; i
< argc
; i
++)
1423 int len
= strlen(argv
[i
]);
1425 if (argv
[i
][0] == 'c' && len
< 2)
1426 len
= 2; /* distinguish [cr]itical from [ca]lls */
1428 if (!strncasecmp(argv
[i
], "critical", len
)) { debug_flags
.critical
= 0; continue; }
1429 if (!strncasecmp(argv
[i
], "error", len
)) { debug_flags
.error
= 0; continue; }
1430 if (!strncasecmp(argv
[i
], "warning", len
)) { debug_flags
.warning
= 0; continue; }
1431 if (!strncasecmp(argv
[i
], "info", len
)) { debug_flags
.info
= 0; continue; }
1432 if (!strncasecmp(argv
[i
], "calls", len
)) { debug_flags
.calls
= 0; continue; }
1433 if (!strncasecmp(argv
[i
], "data", len
)) { debug_flags
.data
= 0; continue; }
1434 if (!strncasecmp(argv
[i
], "all", len
))
1436 memset(&debug_flags
, 0, sizeof(debug_flags
));
1440 cli_print(cli
, "Invalid debugging flag \"%s\"", argv
[i
]);
1446 int cmd_load_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1448 int i
, firstfree
= 0;
1450 if (CLI_HELP_REQUESTED
)
1451 return cli_arg_help(cli
, argc
> 1,
1452 "PLUGIN", "Name of plugin to load", NULL
);
1456 cli_print(cli
, "Specify a plugin to load");
1460 for (i
= 0; i
< MAXPLUGINS
; i
++)
1462 if (!*config
->plugins
[i
] && !firstfree
)
1464 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1466 cli_print(cli
, "Plugin is already loaded");
1473 strncpy(config
->plugins
[firstfree
], argv
[0], sizeof(config
->plugins
[firstfree
]) - 1);
1474 config
->reload_config
= 1;
1475 cli_print(cli
, "Loading plugin %s", argv
[0]);
1481 int cmd_remove_plugin(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1485 if (CLI_HELP_REQUESTED
)
1486 return cli_arg_help(cli
, argc
> 1,
1487 "PLUGIN", "Name of plugin to unload", NULL
);
1491 cli_print(cli
, "Specify a plugin to remove");
1495 for (i
= 0; i
< MAXPLUGINS
; i
++)
1497 if (strcmp(config
->plugins
[i
], argv
[0]) == 0)
1499 config
->reload_config
= 1;
1500 memset(config
->plugins
[i
], 0, sizeof(config
->plugins
[i
]));
1505 cli_print(cli
, "Plugin is not loaded");
1509 char *duration(time_t secs
)
1511 static char *buf
= NULL
;
1514 if (!buf
) buf
= calloc(64, 1);
1518 int days
= secs
/ 86400;
1519 p
= sprintf(buf
, "%d day%s, ", days
, days
> 1 ? "s" : "");
1525 int mins
= secs
/ 60;
1526 int hrs
= mins
/ 60;
1529 sprintf(buf
+ p
, "%d:%02d", hrs
, mins
);
1531 else if (secs
>= 60)
1533 int mins
= secs
/ 60;
1534 sprintf(buf
+ p
, "%d min%s", mins
, mins
> 1 ? "s" : "");
1537 sprintf(buf
, "%ld sec%s", secs
, secs
> 1 ? "s" : "");
1542 int cmd_uptime(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1545 char buf
[100], *p
= buf
, *loads
[3];
1546 int i
, num_sessions
= 0;
1549 if (CLI_HELP_REQUESTED
)
1550 return CLI_HELP_NO_ARGS
;
1552 fh
= fopen("/proc/loadavg", "r");
1553 fgets(buf
, 100, fh
);
1556 for (i
= 0; i
< 3; i
++)
1557 loads
[i
] = strdup(strsep(&p
, " "));
1560 strftime(buf
, 99, "%H:%M:%S", localtime(&time_now
));
1562 for (i
= 1; i
< MAXSESSION
; i
++)
1563 if (session
[i
].opened
) num_sessions
++;
1565 cli_print(cli
, "%s up %s, %d users, load average: %s, %s, %s",
1567 duration(time_now
- config
->start_time
),
1569 loads
[0], loads
[1], loads
[2]
1571 for (i
= 0; i
< 3; i
++)
1572 if (loads
[i
]) free(loads
[i
]);
1574 cli_print(cli
, "Bandwidth: %s", config
->bandwidth
);
1579 int cmd_set(struct cli_def
*cli
, char *command
, char **argv
, int argc
)
1583 if (CLI_HELP_REQUESTED
)
1589 int len
= strlen(argv
[0])-1;
1590 for (i
= 0; config_values
[i
].key
; i
++)
1591 if (!len
|| !strncmp(argv
[0], config_values
[i
].key
, len
))
1592 cli_print(cli
, " %s", config_values
[i
].key
);
1598 return cli_arg_help(cli
, 0,
1599 "VALUE", "Value for variable", NULL
);
1603 return cli_arg_help(cli
, 1, NULL
);
1612 cli_print(cli
, "Specify variable and value");
1616 for (i
= 0; config_values
[i
].key
; i
++)
1618 void *value
= ((void *)config
) + config_values
[i
].offset
;
1619 if (strcmp(config_values
[i
].key
, argv
[0]) == 0)
1621 // Found a value to set
1622 cli_print(cli
, "Setting \"%s\" to \"%s\"", argv
[0], argv
[1]);
1623 switch (config_values
[i
].type
)
1626 strncpy((char *)value
, argv
[1], config_values
[i
].size
- 1);
1629 *(int *)value
= atoi(argv
[1]);
1632 *(unsigned long *)value
= atol(argv
[1]);
1635 *(short *)value
= atoi(argv
[1]);
1638 *(unsigned *)value
= inet_addr(argv
[1]);
1641 if (strcasecmp(argv
[1], "yes") == 0 || strcasecmp(argv
[1], "true") == 0 || strcasecmp(argv
[1], "1") == 0)
1647 cli_print(cli
, "Unknown variable type");
1650 config
->reload_config
= 1;
1655 cli_print(cli
, "Unknown variable \"%s\"", argv
[0]);
1659 int regular_stuff(struct cli_def
*cli
)
1661 int i
= debug_rb_tail
;
1665 while (i
!= ringbuffer
->tail
)
1667 int show_message
= 0;
1669 if (*ringbuffer
->buffer
[i
].message
)
1671 // Always show messages if we are doing general debug
1672 if (ringbuffer
->buffer
[i
].level
== 0 && debug_flags
.critical
) show_message
= 1;
1673 if (ringbuffer
->buffer
[i
].level
== 1 && debug_flags
.error
) show_message
= 1;
1674 if (ringbuffer
->buffer
[i
].level
== 2 && debug_flags
.warning
) show_message
= 1;
1675 if (ringbuffer
->buffer
[i
].level
== 3 && debug_flags
.info
) show_message
= 1;
1676 if (ringbuffer
->buffer
[i
].level
== 4 && debug_flags
.calls
) show_message
= 1;
1677 if (ringbuffer
->buffer
[i
].level
== 5 && debug_flags
.data
) show_message
= 1;
1682 ipt address
= htonl(ringbuffer
->buffer
[i
].address
);
1684 struct in_addr addr
;
1686 memcpy(&addr
, &address
, sizeof(ringbuffer
->buffer
[i
].address
));
1687 ipaddr
= inet_ntoa(addr
);
1689 cli_print(cli
, "\r%s-%s-%u-%u %s",
1690 debug_levels
[(int)ringbuffer
->buffer
[i
].level
],
1692 ringbuffer
->buffer
[i
].tunnel
,
1693 ringbuffer
->buffer
[i
].session
,
1694 ringbuffer
->buffer
[i
].message
);
1699 if (++i
== ringbuffer
->tail
) break;
1700 if (i
== RINGBUFFER_SIZE
) i
= 0;
1703 debug_rb_tail
= ringbuffer
->tail
;