1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| #include "cli.h" #include "cli_debug.h"
#define CLI_PORT 9999 #define MODE_CONFIG_INT 10
#ifdef __GNUC__ # define UNUSED(d) d __attribute__ ((unused)) #else # define UNUSED(d) d #endif
unsigned int regular_count = 0; unsigned int debug_regular = 0;
int check_auth(char *username, char *password) { if (strcasecmp(username, "root") != 0) return CLI_ERROR; if (strcasecmp(password, "root") != 0) return CLI_ERROR; return CLI_OK; }
int regular_callback(struct cli_def *cli) { regular_count++; if (debug_regular) { cli_print(cli, "Regular callback - %u times so far", regular_count); cli_reprompt(cli); } return CLI_OK; }
int check_enable(char *password) { return !strcasecmp(password, "root"); }
void pc(struct cli_def *cli, char *string) { printf("%s\n", string); }
int cli_handle() { pthread_t tid; printf ("Enter Cli handle functions.\n"); printf ("Cli ip:port %s %d\n","Any",CLI_PORT); printf ("User: %s passwd:%s\n","root","root");
int ret = 0; ret = pthread_create(&tid, NULL, cli_main, (void *)NULL); if (0 != ret) { printf ("error:pthread_create\n"); return 1; } pthread_join(tid, 0);
return 0; }
int cli_main() { struct cli_command *c; struct cli_def *cli; int s, x; struct sockaddr_in addr; int on = 1; signal(SIGCHLD, SIG_IGN); cli = cli_init(); cli_set_banner(cli, "dp_mod debug CLI console"); cli_set_hostname(cli, "Smart"); cli_regular(cli, regular_callback); cli_regular_interval(cli, 5); cli_set_idle_timeout(cli, 600); cli_register_funcs(cli); cli_set_auth_callback(cli, check_auth); if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); return 1; } setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(CLI_PORT); if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { perror("bind"); return 1; }
if (listen(s, 50) < 0) { perror("listen"); return 1; }
printf("Listening on port %d\n", CLI_PORT); while ((x = accept(s, NULL, 0))) { int pid = fork(); if (pid < 0) { perror("fork"); return 1; }
if (pid > 0) { socklen_t len = sizeof(addr); if (getpeername(x, (struct sockaddr *) &addr, &len) >= 0) printf(" * accepted connection from %s\n", inet_ntoa(addr.sin_addr));
close(x); continue; }
close(s); cli_loop(cli, x); exit(0); }
cli_done(cli); return 0; }
|