int main(void) { char *src = "ANS=993444\n=c8c8\nc8fsksdafjaksekjfjlk\nREQQQ=2348234\nDO=B\nETYP=3\n"; char buf[128] = { 0 , }; char *p; p = search_req_str_in_the_packet(src, "RE"); if ( p ) { printf("%s\n", p); } if ( get_request_type_str(src, buf) == 0 ) { printf("buf=%s\n", buf); } return 0; }
bool send_request(struct server_data* server, const char* request, int request_length) { int socket_fd = get_client_socket_fd(); char ip_address_str[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &server->address.sin_addr, ip_address_str, INET_ADDRSTRLEN); int port = ntohs(server->address.sin_port); char request_type_str[MTN_REQ_TYPE_LEN + 1]; get_request_type_str(request_type_str, get_request_type(request)); if(sendto(socket_fd, request, request_length, 0, (struct sockaddr *) &(server->address), sizeof(server->address)) == -1) { error_errno("cannot send %s request to %s:%d", request_type_str, ip_address_str, port); return false; } time_t send_time; if((send_time = time(NULL)) == -1) { fatal_error(1, "cannot measure time"); } info("Sending %s request to %s:%d", request_type_str, ip_address_str, port); struct timespec timeout; timeout.tv_sec = send_time + SERVER_RESPONSE_TIMEOUT; timeout.tv_nsec = 0; pthread_mutex_lock(&server->mutex); while(server->state != RESP) { if(pthread_cond_timedwait(&response_cond, &server->mutex, &timeout) == ETIMEDOUT) { info("Server %s:%d is not responding to %s request", ip_address_str, port, request_type_str); server->last_connect_attempt_time = send_time; server->not_connect_count++; if(server->not_connect_count % MAX_NOT_CONNECT_COUNT == 0) { // client runs out of attempts to connect to the server // client now waits for next attempt server->state = WAIT; } pthread_mutex_unlock(&server->mutex); return false; } } char response_type_str[MTN_RES_TYPE_LEN + 1]; get_response_type_str(response_type_str, get_response_type(server->response)); time_t connect_time; if((connect_time = time(NULL)) == -1) { fatal_error(1, "cannot measure time"); } info("Receiving %s response from %s:%d", response_type_str, ip_address_str, port); info("%s", server->response); server->last_connect_attempt_time = connect_time; server->not_connect_count = 0; pthread_mutex_unlock(&server->mutex); return true; }