void timeout_handle(int signun) { if(fd) close(fd); clear_fd(&fd); printf("%s",TIMEOUT_STR); exit(EXIT_SUCCESS); }
int midnet_writer(struct _tcp_session* client_request) { int ret; char* buffer = client_request->buffer; char* token; if(client_request==NULL) { log_message_error("MidNet: Writing data to TCP",PROGRAM,"NULL TCP client structure"); return 0; } token = strstr(client_request->buffer, "\n\n"); if(token==NULL) { log_message_error("MidNet: Writing data to TCP",PROGRAM,"Invalid TCP buffer"); return 0; } if(token>=(buffer+BUFFER_LENGTH-1)) { log_message_error("MidNet: Writing data to TCP",PROGRAM,"Buffer Overflow"); return 0; } *(token+2) = '\0'; ret = clear_fd(0); if(ret==0) { log_message_error("MidNet: Writing data to TCP",PROGRAM,"Error while cleaning file descriptor"); return 0; } ret = is_valid_request(buffer); if(ret==0) { log_message_error("MidNet: Writing data to TCP",PROGRAM,"Wrong command"); return 0; } ret = midnet_processing_request(client_request); if(ret==0) { log_message_error("MidNet: Writing data to TCP",PROGRAM,"Error while processing the request"); return 0; } client_request->limit = client_request->position; client_request->position = 0; client_request->close_request = 1; return 1; }
int main(int argc,char* argv[]) { int ret=true; char ch; char *p,*p1; char buf[100]; char temp[50]; char str[1000]; signal(SIGALRM,timeout_handle); alarm(TIMEOUT_TIME); if((argc==1)) { printf("usage: 3Ginfo [-d] [-c] [-s]\n"); return -1; } //openlog(LOG_IDENT,LOG_PID,LOG_USER); //chdir(SDK_FORDER); memset(para_in[0],0,sizeof(para_in[0])); memset(para_in[1],0,sizeof(para_in[1])); memset(para_in[2],0,sizeof(para_in[2])); memset(para_in[3],0,sizeof(para_in[3])); while((ch=getopt(argc,argv,"d:c:s:m:"))!=(char)-1) { switch(ch) { case 'd': if(optarg!=NULL) { strcpy(para_in[0],optarg); } break; case 'c': if(optarg!=NULL) { strcpy(para_in[1],optarg); } break; case 's': if(optarg!=NULL) { strcpy(para_in[2],optarg); } break; case 'm': if(optarg!=NULL) { strcpy(para_in[3],optarg); } break; default: printf("error command"); ret=false; exit(0); break; } } p=default_command; if(strlen(para_in[0])==0) strcpy(para_in[0],default_device); if(strlen(para_in[1])==0) strcpy(para_in[1],default_command); if(strlen(para_in[2])==0) strcpy(para_in[2],default_sign); if(strlen(para_in[3])==0) strcpy(para_in[3],default_max_signal); //printf("para_in[0]=%s,strlen=%d\n",para_in[0],strlen(para_in[0])); //printf("para_in[1]=%s,strlen=%d\n",para_in[1],strlen(para_in[1])); //printf("para_in[2]=%s,strlen=%d\n",para_in[2],strlen(para_in[2])); strcat(para_in[1],"\r\n"); serial_init(para_in[0]); //system("echo \"open 3g device for get signal \" "); if(open_device(&fd,para_in[0])==false) { printf("open device error!"); ret=false; exit(0); } //system("echo \"!!!!!!!send ate0 \" "); send_read(fd,"ATE0\r\n",buf,WAIT_OK); // sprintf(str,"echo \"send %s\n\" ",para_in[0]); //system(str); if(send_read(fd,para_in[1],buf,WAIT_VALUE)==false) { // system("echo \"ret=false\" "); ret=false; printf("read error!"); close_device(fd); clear_fd(&fd); exit(0); } //sprintf(str,"echo \"buf %s\n\" ",buf); //system(str); //printf("last_buf:%s\n",buf); if(buf==NULL) { printf("read error!"); ret=false; close_device(fd); clear_fd(&fd); exit(0); } //ret close_device(fd); clear_fd(&fd); p=strchr(buf,':'); if(p!=NULL) { p++; p1=strchr(buf,','); if(p1!=NULL) { strncpy(temp,p,p1-p); printf("%s , ",temp); printf("("); printf("%s",para_in[3]); printf(")"); } else{ printf(""); } } else{ printf(""); } exit(0); }
int main(int argc, char **argv) { int status; char ip_addr[INET_ADDRSTRLEN]; char *service; int server_sock = -1; int exit_flag = 0; int ftp_sock = 9999; fd_set temp; /*Init. Logger*/ #ifndef MY_PC cse4589_init_log(argv[2]); #endif int sock; if(argc<3) { printf("No args\n"); return 1; } if(!validate_port(argv[2])) return 0; FD_ZERO(&wait_fd); FD_ZERO(&temp); FD_SET(0, &wait_fd); tv.tv_sec = 15; tv.tv_usec = 0; if(!strcmp(argv[1], "s")) { is_server = true; } else if(strcmp(argv[1], "c")) { printf("Usage ./sock [s/c]\n"); return 0; } set_listening_port(argv[2]); server_sock = server_start(argv[2]); if(server_sock <=0)return 0; sock = server_sock; add_fd(sock); int i =2; while(!exit_flag) { temp = wait_fd; int ret = select(active_sockets, &temp, NULL, NULL, NULL); if(ret) { if(FD_ISSET(STDIN, &temp)) { exit_flag = parse_shell(); } else if(FD_ISSET(server_sock, &temp)) { // server socket is active, check for new connections if(is_server) { int new_socket = server_accept(server_sock); add_fd(new_socket); } else { int new_socket = server_accept(server_sock); //add ftp socket add_fd(new_socket); ftp_sock = new_socket; } } else if(FD_ISSET(ftp_sock, &temp)) { client_receive_file(&ftp_sock); } else { for(int fd = 3; fd<=active_sockets; fd++) { if(FD_ISSET(fd, &temp)) { if(is_server) server_receive(fd); else client_receive(fd); } } } //End of else } //end of select handling } //end loop clear_fd(sock); close(sock); #ifndef MY_PC fclose(fopen(LOGFILE, "w")); #endif return 0; }