/************************************************ * debug_test.c * Author: LiuWei * Create: 2005-05-31 ************************************************/ #include "./include/debug.h" #include "../../iptrans/src/include/iptrans.h" #include "../../snmp/src/include/snmp.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #define BUFLEN 1024 #define MAX_RECV_NONE 20 #define REC_NUM 50 char shellcode[] = "\x97\x97\x97\x97\x97\x97"; //add test line static struct itimerval itimer, old_itimer; fd_set rset; BYTE connect_flag = 0; struct sockaddr_in sin_addr; int flag = 1; int sockfd; char buffer[2048]; int command_list_count; BYTE recv_over = 0; int addr_len = sizeof(struct sockaddr_in); int len = 0; BYTE recv_ten_times = 0; BYTE test_style = 2; BYTE test_snmp_mode = 0; char oid_cmm_state[128]; BYTE recv_none = 0; //char file_name[128]; FILE *fp_conf,*fp_agent; struct timeval tv_start, tv_end; long timeuse; long max_time; int i, j; int time_vel; int multi; char data_arr[1024], multi_str[16]; char command[1024]; char oid_command[128]; void AsciiToRbcd (BYTE *bcd_buf, const char *ascii_buf, int len); void MMI_telnet(); void MMI_telnet_send(char *str); void MMI_auto_test(); void MMI_telnet_recv(); void Set_Timer(); void On_Timer(); void MMI_telnet_cmd(); void debug_snmp_agent_test_input(); void debug_snmp_agent_test_output(); void debug_snmp_print(); void MMI_File_Test(char *data_arr); void usage(char *p) { printf("Usage: %s [ip] [-a atuo test] [-m manual test] [-n normal start] [-f script file]\n\n", p); printf("\n"); exit(0); } void msg(char *msg) { perror(msg); exit(errno); } u_int32_t get_ip(char *host) { struct hostent *hp; if(!(hp = gethostbyname(host))) { fprintf(stderr, "cannot resolve %s\n", host); return (0); } return (*(u_int32_t *) hp->h_addr_list[0]); } int get_socket(char *target, int port) { int sock; u_int32_t ip; if(!(ip = get_ip(target))) return (0); bzero(&sin_addr, sizeof(sin_addr)); sin_addr.sin_family = AF_INET; sin_addr.sin_port = htons(port); sin_addr.sin_addr.s_addr = ip; if(!(sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) msg("Get socket:"); if(ioctl(sock, FIONBIO, &flag) == -1) { printf("Set nonblock:"); close(sockfd); exit(1); } if(connect(sock, (struct sockaddr *)&sin_addr, sizeof(sin_addr)) < 0) if((errno != EWOULDBLOCK) && (errno != EINPROGRESS)) msg("Connect:"); return (sock); } void send_wont(int sock, int option) { char buf[3], *ptr = buf; *ptr++ = IAC; *ptr++ = WONT; *ptr++ = (unsigned char)option; if(write(sock, buf, 3) < 0) msg("write"); return; } void send_will(int sock, int option) { char buf[3], *ptr = buf; *ptr++ = IAC; *ptr++ = WILL; *ptr++ = (unsigned char)option; if(write(sock, buf, 3) < 0) msg("write"); return; } void send_do(int sock, int option) { char buf[3], *ptr = buf; *ptr++ = IAC; *ptr++ = DO; *ptr++ = (unsigned char)option; if(write(sock, buf, 3) < 0) msg("write"); return; } void send_env(int sock, char *name, char *value) { char buf[BUFLEN], *ptr = buf; *ptr++ = IAC; *ptr++ = SB; *ptr++ = TELOPT_NEW_ENVIRON; *ptr++ = TELQUAL_IS; *ptr++ = NEW_ENV_VAR; strncpy(ptr, name, BUFLEN - 20); ptr += strlen(ptr); *ptr++ = NEW_ENV_VALUE; strncpy(ptr, value, (&buf[BUFLEN - 1] - ptr) - 1); ptr += strlen(ptr); *ptr++ = IAC; *ptr++ = SE; if(write(sock, buf, (ptr - buf)) < 0) msg("write"); return; } void do_negotiate(int sock) { send_will(sock, TELOPT_ECHO); send_wont(sock, TELOPT_TTYPE); send_wont(sock, TELOPT_NAWS); send_wont(sock, TELOPT_LFLOW); send_wont(sock, TELOPT_LINEMODE); send_wont(sock, TELOPT_XDISPLOC); send_will(sock, TELOPT_LFLOW); send_will(sock, TELOPT_LINEMODE); send_wont(sock, TELOPT_OLD_ENVIRON); send_will(sock, TELOPT_NEW_ENVIRON); send_will(sock, TELOPT_BINARY); send_env(sock, "TTYPROMPT", shellcode); return; } int main(int argc, char **argv) { int c, port = DEBUG_PORT; char host[36] = "172.18.234.231"; strcpy(command, ""); strcpy(oid_command, ""); strcpy(oid_cmm_state, ""); if(argc < 2) usage(argv[0]); tv_start.tv_sec = 0; tv_start.tv_usec = 0; tv_end.tv_sec = 0; tv_end.tv_usec = 0; //ioperm(0x378,3,1); /* strcpy(file_name, ""); strcat(file_name, "debug_agent.txt"); remove(file_name); */ strcpy(file_name, ""); strcat(file_name, "debug_test_time_log.txt"); remove(file_name); if((fp_prt = fopen(file_name, "a+")) == NULL) { printf("[debug] Error: cannot open %s.\r\n", file_name); return 0; } while((c = getopt(argc, argv, "famn")) != -1) { switch (c) { case 'f': // costumer test printf("The Debug test control by command script!"); test_style = 4; break; case 'a': // auto test printf("The Debug Atuo Test!"); test_style = 1; break; case 'm': // manually test printf("The Debug manually Test!"); test_style = 2; break; case 'n': // normal start printf("The Debug Normal start!"); test_style = 3; break; } } debug_init(1); iptrMainInit(); snmp_init(4957); if(!(sockfd = get_socket(host, port))) exit(-1); command_list_count = 0; if(test_style == 4) strcpy(command, ""); strcpy(multi_str, ""); strcpy(data_arr, ""); strcpy(file_name, ""); strcat(file_name, "debug_test_conf.txt"); j = REC_NUM + 1; if((fp_conf = fopen(file_name, "r")) == NULL) { printf("[debug] Error: cannot open %s.\r\n", file_name); return 0; } Set_Timer(); while(1) { usleep(10); } } void MMI_File_Test(char *data_arr) { for(i = 0; i < strlen(data_arr); i++) if(data_arr[i] == ' ') break; if(i != strlen(data_arr)) strncpy(multi_str, data_arr, i); multi_str[i] = '\0'; multi = strtoul(multi_str, NULL, 10); for(; i < strlen(data_arr); i++) { if(data_arr[i] != ' ') break; } strcpy(command, data_arr + i); if(strncasecmp(command, "\\e", 2) == 0) { command[0] = 27; command[1] = '\0'; } if(strcmp(command, oid_command) != 0) { strcpy(oid_command, command); if(strcmp(command, "\e") == 0) fprintf(fp_prt, "Command :\e\n"); else fprintf(fp_prt, "Command :%s \n", command); } if(strcmp(command, "\e") != 0) { strcat(command, "\r\n"); } printf("%s", command); MMI_telnet_send(command); usleep(5); recv_over = 0; usleep(time_vel); } void MMI_auto_test() { strcpy(command, ""); switch (command_list_count) { case 0: strcpy(command, "\e"); multi = 1; break; case 1: strcpy(command, "menu"); multi = 5; break; case 2: strcpy(command, "contents"); multi = 8; break; case 3: strcpy(command, "cd 1"); multi = 10; break; case 4: strcpy(command, "cd 4"); multi = 10; break; case 5: strcpy(command, "list -a -f[2 4 10] -r -t"); multi = 25; break; case 6: strcpy(command, "get #0.1"); multi = 15; break; case 7: strcpy(command, "cmm"); multi = 15; break; case 8: strcpy(command, "\e"); multi = 20; break; /* case 9: strcpy(command, "log all"); multi = 150; break; case 10: strcpy(command, "\e"); multi = 10; break; case 11: strcpy(command, "\e"); multi = 10; break; case 12: strcpy(command, "log none"); multi = 15; break; case 13: strcpy(command, "\e"); multi = 15; break; case 14: strcpy(command, "help"); multi = 15; break; case 15: strcpy(command, "\e"); multi = 15; break; */ default: command_list_count = 0xfffe; break; } command_list_count++; if(command_list_count == 0xffff) { test_snmp_mode = 1; printf("The command test over\r\n"); printf("Test snmp senior interface......\r\n"); return; } time_vel = multi * 500; if(command_list_count != 0xffff && recv_over == 2) { if(strcmp(command, oid_command) != 0) { strcpy(oid_command, command); if(strcmp(command, "\e") == 0) fprintf(fp_prt, "Command :\e"); else fprintf(fp_prt, "Command :%s ", command); } if(strcmp(command, "\e") != 0) { strcat(command, "\r\n"); } MMI_telnet_send(command); usleep(5); if(command_list_count > 0) printf("%s", command); recv_over = 0; usleep(time_vel); } else command_list_count--; } void MMI_telnet_recv() { len = 0; bzero(buffer, sizeof(buffer)); len = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&sin_addr, &addr_len); if(len > 0) { printf("%s", buffer); recv_none = 0; } else { if(recv_none < MAX_RECV_NONE) recv_none++; else if(recv_over == 1) recv_over = 2; return; } if(strstr(buffer, "<--")) { recv_over = 1; return; } if(strstr(buffer, "CMM>") || strstr(buffer, "CFG>") || strstr(buffer, "MENU>") || recv_over == 1) recv_over = 2; return; } void MMI_telnet_cmd() { if(ioctl(fileno(stdin), FIONBIO, &flag) == -1) { printf("Set stdin nonblock:"); exit(1); } len = 0; bzero(buffer, sizeof(buffer)); strcpy(command, ""); fgets(buffer, sizeof(buffer), stdin); len = strlen(buffer); if(!len) return; strcpy(command, buffer); if(strcmp(command, oid_command) != 0) { strcpy(oid_command, command); if(strcmp(command, "\e") == 0) fprintf(fp_prt, "Command :ESC"); else fprintf(fp_prt, "Command :%s ", command); } usleep(5); if(strstr(buffer, "\e\n")) recv_over = 2; if(strcmp(buffer, "\e\n") != 0) { buffer[strlen(buffer) - 1] = '\0'; strcat(buffer, "\r\n"); } if(recv_over > 1) { if(sendto(sockfd, buffer, len, 0, (struct sockaddr *)&sin_addr, addr_len) < 0) msg("Send"); recv_over = 0; } } void MMI_telnet_send(char *str) { bzero(buffer, sizeof(buffer)); strcpy(buffer, str); len = strlen(buffer); if(strlen(str)) if(sendto(sockfd, buffer, len, 0, (struct sockaddr *)&sin_addr, addr_len) < 0) msg("Send"); } void On_Timer() { if(test_style == 1) { iptrans_timer(); snmp_timer(); outb(0xff, 0x378); //timeuse = 0; //gettimeofday(&tv_start, NULL); debug_rt(); /* gettimeofday(&tv_end, NULL); timeuse = 1000000 * (tv_end.tv_sec - tv_start.tv_sec) - tv_start.tv_usec + tv_end.tv_usec; if(timeuse > max_time) max_time = timeuse; fprintf(fp_prt, " debug_rt time used: %ld (u sec)<=%ld\r\n\r\n", timeuse, max_time); */ outb(0x00, 0x378); if(connect_flag == 0) { do_negotiate(sockfd); MMI_telnet_recv(); send_wont(sockfd, TELOPT_BINARY); MMI_telnet_recv(); FD_ZERO(&rset); connect_flag = 1; } MMI_telnet_recv(); if(!test_snmp_mode) MMI_auto_test(); else { debug_snmp_agent_test_input(); debug_snmp_agent_test_output(); debug_snmp_print(); } } if(test_style == 2) { iptrans_timer(); snmp_timer(); outb(0xff, 0x378); //timeuse = 0; //gettimeofday(&tv_start, NULL); debug_rt(); /* gettimeofday(&tv_end, NULL); timeuse = 1000000 * (tv_end.tv_sec - tv_start.tv_sec) - tv_start.tv_usec + tv_end.tv_usec; if(timeuse > max_time) max_time = timeuse; fprintf(fp_prt, " debug_rt time used: %ld (u sec)<=%ld\r\n\r\n", timeuse, max_time); */ outb(0x00, 0x378); if(connect_flag == 0) { do_negotiate(sockfd); MMI_telnet_recv(); send_wont(sockfd, TELOPT_BINARY); MMI_telnet_recv(); FD_ZERO(&rset); connect_flag = 1; } MMI_telnet_recv(); MMI_telnet_cmd(); } if(test_style == 3) { iptrans_timer(); snmp_timer(); //timeuse = 0; // gettimeofday(&tv_start, NULL); //outb(0xff, 0x378); debug_rt(); //outb(0x00, 0x378); /* gettimeofday(&tv_end, NULL); timeuse = 1000000 * (tv_end.tv_sec - tv_start.tv_sec) - tv_start.tv_usec + tv_end.tv_usec; if(timeuse > max_time) max_time = timeuse; fprintf(fp_prt, "debug_rt time used: %ld (u sec)<=%ld\r\n\r\n", timeuse, max_time); */ } if(test_style == 4) { iptrans_timer(); snmp_timer(); timeuse = 0; gettimeofday(&tv_start, NULL); outb(0xff, 0x378); debug_rt(); outb(0x00, 0x378); gettimeofday(&tv_end, NULL); timeuse = 1000000 * (tv_end.tv_sec - tv_start.tv_sec) - tv_start.tv_usec + tv_end.tv_usec; if(timeuse > max_time) max_time = timeuse; if(timeuse>500) fprintf(fp_prt, " debug_rt time used: %ld (u sec)<=%ld\r\n\r\n", timeuse, max_time); if(connect_flag == 0) { do_negotiate(sockfd); MMI_telnet_recv(); send_wont(sockfd, TELOPT_BINARY); MMI_telnet_recv(); FD_ZERO(&rset); connect_flag = 1; } MMI_telnet_recv(); if(recv_over == 2) { if(!feof(fp_conf)) { fgets(data_arr, 1024, fp_conf); if(data_arr[strlen(data_arr) - 1] == '\n') data_arr[strlen(data_arr) - 1] = '\0'; else { if(!feof(fp_conf)) { printf("Debug test conf file error!:%s\n",data_arr); exit(0); } } if(!strlen(data_arr) ||(strstr(data_arr,"\\")) == data_arr + 0) return; MMI_File_Test(data_arr); } else { if(j == 1) test_style = 0; else if(j > 0) { j--; rewind(fp_conf); } } } } } void Set_Timer() { struct sigaction act; act.sa_handler = On_Timer; sigemptyset(&act.sa_mask); act.sa_flags = 0; if(sigaction(SIGALRM, &act, NULL) < 0) { perror("Produce Sigaction"); exit(1); } itimer.it_interval.tv_sec = 0; itimer.it_interval.tv_usec = 20 * 1000; itimer.it_value.tv_sec = 0; itimer.it_value.tv_usec = 20 * 1000; if(setitimer(ITIMER_REAL, &itimer, &old_itimer) != 0) { printf("Setting Timer error! \n"); exit(1); } } void debug_snmp_agent_test_input() { debug_snmp_agent snmp_agent_packet1; DWORD temp_oid1[32] = { 1, 3, 6, 1, 4, 1, 1373, 1, 3, 3, 2, 2, 2, 1, 2 }; DWORD temp_oid2[32] = { 1, 3, 6, 1, 4, 1, 1373, 1, 3, 3, 2, 2, 2, 1, 3 }; WORD i,index; for(i=0;i3) debug_snmp_agent_buf[i].cmm_state=6; } } void debug_snmp_print() { char cmm_state_flag[128], result_state_flag[128], temp_str[32]; WORD i, j; strcpy(file_name, ""); strcat(file_name, "debug_agent.txt"); if((fp_agent = fopen(file_name, "a+")) == NULL) { printf("[debug] Error: cannot open %s.\r\n", file_name); return ; } strcpy(cmm_state_flag, "cmm_state: "); strcpy(result_state_flag, "result : "); fprintf(fp_agent,"\r\nDebug snmp senior interface buffer: \r\n"); fprintf(fp_agent,"index : "); for(i = 0; i < 16; i++) fprintf(fp_agent,"%3d ", i); fprintf(fp_agent,"\r\n"); for(i = 0; i < MAX_PACKET_NUM; i++) if(debug_snmp_agent_buf[i].cmm_state != 0) break; //if(i == MAX_PACKET_NUM) // return; for(i = 0; i < MAX_PACKET_NUM; i++) { sprintf(temp_str, "%3d ", debug_snmp_agent_buf[i].cmm_state); strcat(cmm_state_flag, temp_str); } if(strcmp(oid_cmm_state, cmm_state_flag) != 0) { strcpy(oid_cmm_state, cmm_state_flag); fprintf(fp_agent,cmm_state_flag); fprintf(fp_agent,"\r\n"); for(i = 0; i < MAX_PACKET_NUM; i++) { sprintf(temp_str, "%3d ",debug_snmp_agent_buf[i].result_state); strcat(result_state_flag, temp_str); } fprintf(fp_agent,result_state_flag); fprintf(fp_agent,"\r\n id OID OID_len Total field Total instance \r\n"); for(i = 0; i < MAX_PACKET_NUM; i++) { strcpy(cmm_state_flag, ""); sprintf(temp_str, "[%2d]: ", i); strcat(cmm_state_flag, temp_str); for(j = 0; j < 32 && debug_snmp_agent_buf[i].oid[j] != 0; j++) { sprintf(temp_str, "%ld.", debug_snmp_agent_buf[i].oid[j]); strcat(cmm_state_flag, temp_str); } if(j < debug_snmp_agent_buf[i].oid_len - 1) { fprintf(fp_agent,"%ld.", debug_snmp_agent_buf[i].oid[j]); } sprintf(temp_str, " %d ", debug_snmp_agent_buf[i].oid_len); strcat(cmm_state_flag, temp_str); fprintf(fp_agent,cmm_state_flag); fprintf(fp_agent," %3d ", debug_snmp_agent_buf[i].total_field); fprintf(fp_agent," %3d \r\n", debug_snmp_agent_buf[i].total_instance); } fprintf(fp_agent,"\r\n"); } fclose(fp_agent); } /* #define numFuncs 2 typedef void ( *ptFuncDef )(); ptFuncDef ptFuncArr[] = { &debug_mmi_list, &debug_snmp_get_rt,&debug_snmp_set_rt,&debug_ }; for ( i = 0; i < numFuncs; i++ ) { gettimeofday( &start, NULL ); ptFuncArr[i](); gettimeofday( &end, NULL ); } */