// Attempts to login the user int login(int socket, char * read_buffer, char * write_buffer){ memset(read_buffer, 0, READ_BUFFER_SIZE); memset(write_buffer, 0, WRITE_BUFFER_SIZE); /* The loop will be sending login information until a succesful login or until the server closes the connection */ while(1){ send_login(socket); int charRead = sreadLine(socket, read_buffer, READ_BUFFER_SIZE - 1); // Interpret response from the server if(charRead == 0){ fprintf(stdout, "Connection closed by the server.\n"); return 0; // Connection closed by server }else{ if(strstr(read_buffer, "OK") != NULL){ fprintf(stdout, "Login succesful.\n"); return 1; // Succesful login }else{ fprintf(stdout, "Wrong username or password. Try again.\n"); continue; } } } }
//********************************************************************************** void perform_respond(){ char params[20][256]; int count, rolle, parent, fltausw, fltwert; time_t sessionid; long len; int maxlen = 1199; char *envptr; char input[1200]; char *lenstr; if(strcmp(getenv("REQUEST_METHOD"), "GET") == 0){ envptr = getenv("QUERY_STRING"); if(envptr == NULL || strlen(envptr) == 0){ count = 0; } else if(strlen(envptr) > maxlen){ send_error(mERR_MAXLEN); return; } else{ count = parse_data_from_input(envptr, params); } } else if(strcmp(getenv("REQUEST_METHOD"), "POST") == 0){ lenstr = getenv("CONTENT_LENGTH"); if(lenstr == NULL || sscanf(lenstr, "%ld", &len) != 1 || len > maxlen){ send_error(mERR_MAXLEN); return; } else{ fgets(input, len + 1, stdin); count = parse_data_from_input(input, params); } } else{ send_error(mERR_REQUEST); return; } if(count == 0){ send_login(mMSG_BLANK); } else if(strcmp(params[0], "action=verify_login") == 0){ verify_login(params, count); } else if(strcmp(params[0], "action=home") == 0){ if(count == 5){ sscanf(params[1], "sessionid=%ld", &sessionid); sscanf(params[2], "rolle=%d", &rolle); fltausw = 0; sscanf(params[3], "fltausw=%d", &fltausw); fltwert = 0; sscanf(params[4], "fltwert=%d", &fltwert); send_tasks(sessionid, rolle, fltausw, fltwert, mMSG_BLANK); } } else if(strcmp(params[0], "action=send_subtasks") == 0){ if(count == 4){ sscanf(params[1], "parent=%d", &parent); sscanf(params[2], "sessionid=%ld", &sessionid); sscanf(params[3], "rolle=%d", &rolle); send_subtasks(parent, sessionid, rolle, mMSG_BLANK); } } else if(strcmp(params[0], "action=save_task") == 0){ save_task(params, count); } else if(strcmp(params[0], "action=save_subtask") == 0){ save_subtask(params, count); } else if(strcmp(params[0], "action=send_query") == 0){ if(count == 3){ sscanf(params[1], "sessionid=%ld", &sessionid); sscanf(params[2], "rolle=%d", &rolle); send_query(sessionid, rolle); } } else if(strcmp(params[0], "action=query") == 0){ query(params, count); } else if(strcmp(params[0], "action=send_usradmin") == 0){ if(count == 3){ sscanf(params[1], "sessionid=%ld", &sessionid); sscanf(params[2], "rolle=%d", &rolle); send_usradmin(sessionid, rolle, mMSG_BLANK); } } else if(strcmp(params[0], "action=verify_pwchange") == 0){ verify_pwchange(params, count); } else if(strcmp(params[0], "action=admin") == 0){ send_admin(params, count); } else{ send_error(mERR_ACTION); return; } }
/**=========================================== * @brief main thread, starting up the client and the other threads * @param argc:int argv**char * ==========================================*/ int main(int argc, char **argv) { char *name = "Guest"; char *server = "localhost"; char *port = "54321"; int thread; int ret, c, conn=0; while(optind < argc) { int option_index = 0; static struct option long_options[] = { {"name", required_argument, 0, 'n'}, {"port", optional_argument, 0, 'p'}, {"help", no_argument, 0, 'h'}, {0,0,0,0} }; c = getopt_long(argc, argv, "hp:n:", long_options, &option_index); if(c == -1) break; switch(c) { case '?': /* unknown parameter */ case ':': /* missing argument */ print_help(argv[0]); break; case 'n': name = strdup(optarg); if(!name) return -1; break; case 'p': port = strdup(optarg); break; case 'h': print_help(argv[0]); break; default: break; } } while(optind < argc) { server = argv[optind++]; } GCI.name = name; printf("Benutzername: %s\n", GCI.name); printf("server: %s", server); struct addrinfo *addr_info, *p; ret = getaddrinfo(server, port, NULL, &addr_info); if(ret) { printf("getaddrinfo: %s\n", gai_strerror(ret)); exit(-1); } p = addr_info; while (p) { if(p->ai_socktype != SOCK_STREAM)/* we only care about TCP */ { p = p->ai_next; continue; } int sock = socket(p->ai_family, p->ai_socktype, 0); if(sock == -1) { perror("socket"); exit(-1); } if(connect(sock, p->ai_addr, p->ai_addrlen) == 0) { signal(SIGINT, sigint_handler); // move to better positioon sem_V(keymng_local(KEY_GCI_SEM)); printf("Socket OK"); GCI.sock = sock; send_login(GCI.name); int state = wait_loginOK(); if(state !=0) { printf("Keine antwort erhalten \n"); return 0; } printf("juhu ich bin eingeloggt \n"); GCI.status = preparation; conn =1; guiInit(&argc, &argv); printf("GUI init \n"); setClientMode(); preparation_showWindow(); guiShowMessageDialog("Willkommen bei You Dont Know Rainer",0); // start the threads thread = pthread_create(&listener_thread_id, NULL, &listener_thread, NULL); if(thread) { printf("Failed to start Listener Thread\n"); exit(0); } thread = pthread_create(&fragen_thread_id, NULL, &fragen_thread, NULL); if(thread) { printf("Failed to start Fragewechsel Thread\n"); exit(0); } //katalog request sendCR(); guiMain(); guiDestroy(); } close(sock); p = p->ai_next; } if(conn==0) printf("Could not connect to server :/\n"); freeaddrinfo(addr_info); exit(0); }
int main(int argc,char **argv) { if(argc >=2 && strcmp(argv[1],"-d") == 0) { //daemon(0,0); //maybe cause SIGTOP tty Interrupt NOTE("Starting as daemon, forking to background"); init_daemon(); } /* * Make sure only one copy of the daemon is running. */ if (already_running()) { syslog(LOG_ERR, "net4g daemon already running\n"); exit(1); } // close timer on gpio record2file(WTD_TRIG,"none",4); record2file(WTD_BRIG,"0",1); openlog("net4g",LOG_NOWAIT,LOG_DAEMON); glb_cfg = config_init(); nvram_renew("/tmp/board_info"); nvram_renew("/tmp/pub_info"); // nvram_buflist(); init_signals(); #if 0 pthread_t pid = 0; if(pthread_create(&pid,NULL,(void*)handle_gps,NULL) < 0 ) { ERROR("create gps thread error!\n"); exit(1); } pthread_detach(pid); #else pthread_t tpid = 0; if(pthread_create(&tpid,NULL,(void*)handle_tty,NULL) < 0 ) { ERROR("create tty thread error!\n"); exit(1); } pthread_detach(tpid); #endif #if 1 pthread_t apid = 0; if(pthread_create(&apid,NULL,(void*)handle_agps,NULL) < 0 ) { ERROR("create Agps thread error!\n"); exit(1); } pthread_detach(apid); #endif pthread_t ptid = 0; if(pthread_create(&ptid,NULL,(void*)handle_timing_gpio,NULL) < 0 ) { ERROR("create GPIO thread error!\n"); exit(1); } pthread_detach(ptid); int disconnected = 1; int faild_login = 1; int ret = 0; int socket = 0; struct timeval last_tv; struct timeval now_tv; unsigned int login_count = 1; unsigned int info_count = 1; unsigned int failcount = 0; char recv_buf[1024] = {0}; while(!exit_flag && failcount < 360) { record2file(WTD_BRIG,"1",1); if(access("/dev/ttyUSB2",F_OK) != 0) { ERROR("waiting for detecting 4G modult;ttyUSB2\n"); goto DISCONN; } if(access("/tmp/dialok",F_OK) != 0) { ERROR("waiting for ppp dial OK\n"); goto DISCONN; } if(disconnected) { record2file("/tmp/onoffline","Offline",7); //system("/bin/echo Offline > /tmp/onoffline"); faild_login = 1; nvram_renew("/tmp/board_info"); socket = init_connect(nvram_get("remote_ip"),atoi(nvram_get("remote_port")),1); NOTE("remote socket = %d\n",socket); if(socket >= 0) { gettimeofday(&last_tv,NULL); disconnected = 0; set_socket_keepalive(socket); glb_remote_socket = socket; failcount = 0; } else { failcount++; glb_remote_socket = -1; record2file(WTD_BRIG,"0",1); sleep_seconds_intr(30); continue; } } // login if(faild_login) { record2file("/tmp/onoffline","Offline",7); //system("/bin/echo Offline > /tmp/onoffline"); nvram_renew("/tmp/board_info"); ret = send_login(socket,login_count++); if(ret > 0) { gettimeofday(&last_tv,NULL); faild_login = 0; login_count = 0; //system("/bin/echo Online > /tmp/onoffline"); record2file("/tmp/onoffline","Online",6); } else if(ret == -2){ //socket error ERROR("Error send login!\n"); goto DISCONN; } else { //recv msg format error NOTE("To relogin!\n"); failcount++; record2file(WTD_BRIG,"0",1); sleep_seconds_intr(30); continue; } } gettimeofday(&now_tv,NULL); //printf("time;%ld:%ld\n",now_tv.tv_sec,last_tv.tv_sec); if(now_tv.tv_sec - last_tv.tv_sec >= 230) { gettimeofday(&last_tv,NULL); NOTE("net4g -- keepalive...%u, but server no response, to reconnect\n",info_count); //server no response ,but send ok goto DISCONN; } fd_set fds; struct timeval tv; FD_ZERO(&fds); FD_SET(socket, &fds); /* init socket timeout, set to 60 seconds */ tv.tv_sec = 60; tv.tv_usec = 0; //server handle socket event if((ret = select(socket + 1, &fds, NULL, NULL, &tv)) < 0) { if(errno == EINTR) { //gettimeofday(&last_tv,NULL); NOTE("server socket select EINTR\n"); record2file(WTD_BRIG,"0",1); continue; } else { ERROR("select error:%d\n",errno); goto DISCONN; } } else if(ret == 0) { ret = send_board_info(socket,info_count++); if(ret <= 0) { ERROR("Error send gpsinfo!\n"); goto DISCONN; } else { record2file(WTD_BRIG,"0",1); continue; } } if(FD_ISSET(socket, &fds) <= 0) { ERROR("something wrong while waiting for socket,error:%d\n",errno); goto DISCONN; } memset(recv_buf,0,sizeof(recv_buf)); ret = recv(socket,recv_buf,1023,0); if(ret <= 0) { ERROR("Error while recv socket:%d:%s\n",errno,strerror(errno)); goto DISCONN; } NOTE("RECV:%s\n",recv_buf); ret = handle_msg(socket,recv_buf); if(ret < 0) { goto DISCONN; } else if(ret == 1) { exit_flag = 1; goto EXIT; } else { //ok } failcount = 0; record2file(WTD_BRIG,"0",1); gettimeofday(&last_tv,NULL); continue; DISCONN: failcount++; glb_remote_socket = -1; disconnected = 1; faild_login = 1; if(socket > 0) close(socket); socket = -1; sleep_seconds_intr(25); record2file(WTD_BRIG,"0",1); sleep_seconds_intr(1); } //end while(1) EXIT: exit_flag = 1; if(socket > 0) close(socket); NOTE("net4g process exit!!\n"); config_close(glb_cfg); closelog(); return 0; }
bool KClient::Poll() { switch(state){ case TEST_INIT: if( GetState() == vce::VCE_STATE_ESTABLISHED ){ std::cerr << "send ping" << std::endl; send_ping( vce::GetTime()); state = TEST_PING_SENT; break; } else { std::cerr << GetState() << ">" ; if( GetState() == vce::VCE_STATE_UNKNOWN ){ return false; } } break; case TEST_PING_SENT: std::cerr << ">"; break; case TEST_PING_RECEIVED: { // 新しいIDを生成して登録(sign up)する std::ostringstream idss; idss << "ringo" << time(NULL) << "." << g_id_generator; localAccountName = idss.str(); localPassword = std::string("testpass"); g_id_generator ++; send_signup( localAccountName.c_str(), localPassword.c_str()); std::cerr << "signup" << std::endl; state = TEST_SIGNUP_SENT; } break; case TEST_SIGNUP_SENT: std::cerr << ">"; break; case TEST_SIGNUP_RECEIVED: send_authentication( localAccountName.c_str(), localPassword.c_str()); state = TEST_AUTHENTICATION_SENT; std::cerr << "auth" << std::endl; break; case TEST_AUTHENTICATION_SENT: std::cerr << ">"; break; case TEST_AUTHENTICATION_RECEIVED: send_createCharacter( "ringo" ); state = TEST_CREATECHARACTER_SENT; std::cerr << "createchar" << std::endl; break; case TEST_CREATECHARACTER_SENT: std::cerr << ">"; break; case TEST_CREATECHARACTER_RECEIVED: std::cerr << "createchar ok." << std::endl; send_listCharacter(); state = TEST_LISTCHARACTER_SENT; std::cerr << "listchar." << std::endl; break; case TEST_LISTCHARACTER_SENT: std::cerr << ">"; break; case TEST_LISTCHARACTER_RECEIVED: std::cerr << "listchar ok." << std::endl; send_login( "ringo" ); state = TEST_LOGIN_SENT; std::cerr << "login." << std::endl; break; case TEST_LOGIN_SENT: std::cerr << ">"; break; case TEST_LOGIN_RECEIVED: state = TEST_INGAME; break; case TEST_INGAME: { vce::VUint64 nowtimer = vce::GetTime(); if( nowtimer > (vtimer+500) ){ vtimer = nowtimer; std::cerr << ">"; ingameSender(); printStat(); } } break; case TEST_LOGOUT_SENT: std::cerr << ">"; break; case TEST_SESSION_CLOSED: break; case TEST_FINISHED: // do nothing std::cerr << "."; break; default: std::cerr << "invalid test state" << std::endl; assert(0); } return true; }