/* void response_client_open_ex(response_client_t *uclient, char *conn_str, char *(*id_f)(), char *(*pph_f)()) { uclient->id_f = id_f; uclient->passphrase_f = pph_f; response_client_open_int(uclient, conn_str); } */ int response_client_send(response_client_t *uclient, response_request_t *request) { int ret; long nbytes; char buff[UCLIENT_BUFSIZE]; #if defined(USERVER_ENCRYPTED) char cipher[UCLIENT_BUFSIZE]; char message[UCLIENT_BUFSIZE]; uint32_t timestamp; char sts[32]; char *passphrase = uclient->passphrase_f(); char otp[100]; char *id = uclient->id_f(); char challenge[32]; int len = 32; int len1 = 32; lvc_t lvc; timestamp = get_ts(); len1 = ts2str(timestamp, sts); generate_otp(otp, passphrase, sts); do_encrypt(challenge, &len, sts, len1, otp); lvc_init(&lvc, message, UCLIENT_BUFSIZE); fprintf(stdout, "lvc_pack id:%s\n", id); lvc_pack( &lvc, strlen(id), id ); fprintf(stdout, "lvc_pack ts:%u\n", timestamp); lvc_pack( &lvc, sizeof(uint32_t), (char *)×tamp ); fprintf(stdout, "lvc_pack challenge:%d\n", len); lvc_pack( &lvc, len, challenge ); #endif response_build_request(buff, sizeof(buff), request); nbytes = strlen(buff); #if defined(USERVER_ENCRYPTED) fprintf(stdout, "Message to send:%.*s\n", nbytes, buff); len = sizeof(cipher); do_encrypt(cipher, &len, buff, nbytes, otp); lvc_pack( &lvc, len, cipher ); lvc_pack_finish(&lvc); nbytes = lvc.len; ret = pj_sock_sendto(uclient->fd, lvc.data, &nbytes, 0, (const pj_sockaddr_t *)uclient->connect_data, sizeof(pj_sockaddr_in)); #else ret = pj_sock_sendto(uclient->fd, buff, &nbytes, 0, (const pj_sockaddr_t *)uclient->connect_data, sizeof(pj_sockaddr_in)); #endif if(ret != 0) { PERROR_IF_TRUE(1, "Error in sending data\n"); return -1; } return nbytes; }
int $UPROTO$_secure_server_proc(void *param) { int ret; unsigned int len; int port; char *first, *second, *third; char cnt_str[30]; pj_sockaddr_in caddr; char *caddr_str; pj_time_val timeout; pj_fd_set_t read_fds; char buffer[USERVER_BUFSIZE]; $UPROTO$_request_t request; // For lvc parsing lvc_t lvc; int len1; char *val; uint32_t *ts; char sts[32]; char plain[USERVER_BUFSIZE]; char *pph; char otp[32]; pj_str_t pjstr; // End for lvc parsing $UPROTO$_server_t *userver = ($UPROTO$_server_t *)param; ansi_copy_str(cnt_str, userver->connect_str); first = cnt_str; second = strchr(first, ':'); EXIT_IF_TRUE(second == NULL, "Wrong connection string format\n"); *second = '\0'; second++; if(0 == strcmp(first, "udp")) { third = strchr(second, ':'); EXIT_IF_TRUE(third == NULL, "Wrong connection string format\n"); *third = '\0'; third++; port = atoi(third); open_udp_socket(userver, second, port); if (userver->on_open_socket_f != NULL) userver->on_open_socket_f(userver); userver->recv_f = &udp_recvfrom; userver->send_f = &udp_sendto; } else if( 0 == strcmp(first, "tty") ) { open_tty(userver, second); userver->recv_f = &tty_recvfrom; userver->send_f = &tty_sendto; } else { EXIT_IF_TRUE(1, "Unsuported protocol\n"); } // thread loop timeout.sec = 0; timeout.msec = 100; userver->is_end = 0; while( !userver->is_end ) { while (!userver->is_online) { SHOW_LOG(3, "Server is currently offline...\n"); pj_thread_sleep(1000); } PJ_FD_ZERO(&read_fds); PJ_FD_SET(userver->fd, &read_fds); pj_mutex_lock(userver->mutex); ret = pj_sock_select(userver->fd + 1, &read_fds, NULL, NULL, &timeout); pj_mutex_unlock(userver->mutex); EXIT_IF_TRUE(ret < 0, "Error on server socket\n"); if( PJ_FD_ISSET(userver->fd, &read_fds) ) { len = sizeof(caddr); pj_bzero(&caddr, len); pj_mutex_lock(userver->mutex); ret = userver->recv_f(userver->fd, buffer, USERVER_BUFSIZE, (void *)&caddr, &len); pj_mutex_unlock(userver->mutex); caddr_str = pj_inet_ntoa(caddr.sin_addr); if( ret > 0 ) { buffer[ret] = '\0'; lvc_init(&lvc, buffer, ret); lvc_unpack(&lvc, &len, &val); pj_strset(&pjstr, val, len); pph = userver->get_pph_f(&pjstr); if( pph != NULL ) { lvc_unpack(&lvc, &len, &val); ts = (uint32_t *)val; ts2str(*ts, sts); lvc_unpack(&lvc, &len, &val); generate_otp(otp, pph, sts); do_decrypt(val, len, plain, &len1, otp); if( pj_ansi_strncmp(sts, plain, len1) == 0 ) { lvc_unpack(&lvc, &len, &val); do_decrypt(val, len, plain, &len1, otp); plain[len1] = '\0'; $UPROTO$_parse_request(plain, len1, &request); userver->on_request_f(userver, &request, caddr_str); } } //else { //} } } pj_thread_sleep(100); } return 0; }