int cli_exec_cmd_with_to(void *request, uint32_t *request_length, void *reply, uint32_t *reply_length, int timeout) { int ret; struct timeval tv; tv.tv_sec = timeout; tv.tv_usec = 0; ret = cli_send(request, request_length); if(ret <= 0){ log_err("cannot send a request to server."); return -1; } ret = cli_recv(reply, reply_length, tv); if(ret <= 0){ log_err("cannot get a reply from server."); return -1; } if(((cmdif_request_hdr*)request)->xid != ((cmdif_reply_hdr*)reply)->xid){ log_err("xid mismatch (%u != %u).", ((cmdif_request_hdr*)request)->xid, ((cmdif_reply_hdr*)reply)->xid); return -1; } return 0; }
int cli_exec_cmd(void *request, uint32_t *request_length, void *reply, uint32_t *reply_length) { int ret; struct timeval tv; tv.tv_sec = CLI_CMD_REPLY_TIMEOUT/1000; tv.tv_usec = (CLI_CMD_REPLY_TIMEOUT - tv.tv_sec * 1000) * 1000; ret = cli_send(request, request_length); if(ret <= 0){ log_err("cannot send a request to server."); return -1; } ret = cli_recv(reply, reply_length, tv); if(ret <= 0){ log_err("cannot get a reply from server."); return -1; } if(((cmdif_request_hdr*)request)->xid != ((cmdif_reply_hdr*)reply)->xid){ log_err("xid mismatch (%u != %u).", ((cmdif_request_hdr*)request)->xid, ((cmdif_reply_hdr*)reply)->xid); return -1; } return 0; }
END_TEST //------------------------------------------------------------------------------ START_TEST(stuntcp_timeo) { usleep(200000); srv_loop(); fail_unless(LIST_FIRST(&sternd.stuntcp.clients) == NULL, "Client not reaped"); fail_unless(cli_recv(cli) == NULL, "No response expected"); }
END_TEST //------------------------------------------------------------------------------ START_TEST(stuntcp_close) { shutdown(cli, SHUT_WR); srv_loop(); fail_unless(LIST_FIRST(&sternd.stuntcp.clients) == NULL, "Client not reaped"); fail_unless(cli_recv(cli) == NULL, "No response expected"); }
END_TEST //------------------------------------------------------------------------------ START_TEST(stuntcp_multiple) { struct stun_message *stun; int i; int clis[6]; struct sockaddr addr; socklen_t len = sizeof(addr); clis[0] = cli; for (i = 1; i < 6; i++) clis[i] = stuntcp_client(); srv_loop(); for (i = 0; i < 4; i++) { stun = stun_new(STUN_BINDING_REQUEST); cli_send(stun, clis[i]); } close(clis[3]); shutdown(clis[4], SHUT_WR); usleep(200000); srv_loop(); for (i = 0; i < 3; i++) { stun = cli_recv(clis[i]); fail_if(stun == NULL, "No message"); fail_if(stun->mapped_address == NULL, "No mapping response"); getsockname(clis[i], &addr, &len); check_address(stun->mapped_address, &addr); stun_free(stun); } fail_unless(cli_recv(clis[4]) == NULL, "EOF expected"); fail_unless(cli_recv(clis[5]) == NULL, "EOF expected"); for (i = 1; i < 6; i++) close(clis[i]); }
END_TEST //------------------------------------------------------------------------------ START_TEST(stuntcp_binding_request) { struct stun_message *stun = stun_new(STUN_BINDING_REQUEST); struct sockaddr addr; socklen_t len = sizeof(addr); cli_send(stun, cli); srv_loop(); stun = cli_recv(cli); fail_if(stun == NULL, "No message"); fail_if(stun->mapped_address == NULL, "No mapping response"); getsockname(cli, &addr, &len); check_address(stun->mapped_address, &addr); stun_free(stun); }