#include #include #include #include #include #include #include "libcli.h" #include #define CLITEST_PORT 23 #define MODE_CONFIG_INT 10 #ifdef __GNUC__ # define UNUSED(d) d __attribute__ ((unused)) #else # define UNUSED(d) d #endif int cmd_test(struct cli_def *cli, char *command, char *argv[], int argc) { int i; cli_print(cli, "called %s with \"%s\"", __FUNCTION__, command); cli_print(cli, "%d arguments:", argc); for (i = 0; i < argc; i++) cli_print(cli, " %s", argv[i]); return CLI_OK; } int cmd_set(struct cli_def *cli, UNUSED(char *command), char *argv[], int argc) { if (argc < 2) { cli_print(cli, "Specify a variable to set"); return CLI_OK; } cli_print(cli, "Setting \"%s\" to \"%s\"", argv[0], argv[1]); return CLI_OK; } int cmd_config_int(struct cli_def *cli, UNUSED(char *command), char *argv[], int argc) { if (argc < 1) { cli_print(cli, "Specify an interface to configure"); return CLI_OK; } if (strcmp(argv[0], "?") == 0) cli_print(cli, " test0/0"); else if (strcasecmp(argv[0], "test0/0") == 0) cli_set_configmode(cli, MODE_CONFIG_INT, "test"); else cli_print(cli, "Unknown interface %s", argv[0]); return CLI_OK; } int cmd_config_int_exit(struct cli_def *cli, UNUSED(char *command), UNUSED(char *argv[]), UNUSED(int argc)) { cli_set_configmode(cli, MODE_CONFIG, NULL); return CLI_OK; } int check_auth(char *username, char *password) { if (strcasecmp(username, "admin") != 0) return CLI_ERROR; if (strcasecmp(password, "admin") != 0) return CLI_ERROR; return CLI_OK; } int check_enable(char *password) { return !strcasecmp(password, "topsecret"); } void pc(UNUSED(struct cli_def *cli), char *string) { printf("%s\n", string); } struct cli_def* CLI_Init(void) { struct cli_command *show=NULL, *set=NULL, *clear = NULL; struct cli_def *cli; cli = cli_init(); cli_set_banner(cli, RELEASE); cli_set_hostname(cli, "Telsinex"); show = cli_register_command(cli, NULL, "show", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); set = cli_register_command(cli, NULL, "set", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); clear = cli_register_command(cli, NULL, "clear", NULL, PRIVILEGE_UNPRIVILEGED, MODE_EXEC, NULL); cli_register_command(cli, NULL, "exit", cmd_config_int_exit, PRIVILEGE_PRIVILEGED, MODE_CONFIG_INT, "Exit from interface configuration"); cli_set_auth_callback(cli, check_auth); cli_set_enable_callback(cli, check_enable); diag_printf("\n \n"); return (cli); } void TELNET_start(void) { int s, x; struct sockaddr_in servaddr; struct cli_def *cli; struct linger linger = {1,0}; //linger.l_onoff = 1; //linger.l_linger = 0; int on = 1; cli = CLI_Init(); if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); return 1; } setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); if (setsockopt(s, SOL_SOCKET, SO_LINGER, &linger,sizeof(linger)) < 0) { perror("linger fail"); return 1; } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(CLITEST_PORT); if (bind(s, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { perror("bind"); return 1; } if (listen(s, 50) < 0) { perror("listen"); return 1; } printf("Listening on port %d\n", CLITEST_PORT); while ((x = accept(s, NULL, 0))) { cli_loop(cli, x); close(x); } cli_done(cli); return 0; }