//------------------------------------------------------------------------------ static void cli_send(struct stun_message *stun, int cli) { char buf[8192]; int len; len = stun_to_bytes(buf, sizeof(buf), stun); fail_if(len == -1, "Bad message"); fail_if(send(cli, buf, len, 0) != len, "Cannot send"); stun_free(stun); }
int lh_api_store_status(LH_Device* device){ StunMessage *msg_store = stun_get_status_store_message(device); int sent_success = lh_send_to_server(msg_store); stun_free(msg_store); if (sent_success){ log_info("Store status sent"); return 1; } else { log_warn("Store status message could not be sent") return 0; } }
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); }
int lh_api_send_event(LH_Device* device, char* event_name, char* payload){ StunMessage *msg_event = stun_get_event_message(device, event_name, payload); int sent_success = lh_send_to_server(msg_event); stun_free(msg_event); if (sent_success){ log_info("Keepalive sent"); return 1; } else { log_warn("Event message could not be sent") return 0; } // char* url = malloc((LHINGS_V1_API_PREFIX_LEN + strlen("devices/") + UUID_STRING_LEN + strlen("/events/") + strlen(event_name) + 1) * sizeof url); // url[0] = 0; // strcat(url, LHINGS_V1_API_PREFIX); // strcat(url, "devices/"); // strcat(url, device->uuid); // strcat(url, "/events/"); // strcat(url, event_name); // // LH_Dict* headers = lh_dict_new(); // lh_dict_put(headers, "X-Api-Key", device->api_key); // lh_dict_put(headers, "Content-Type", "application/json"); // LH_HttpResponse* response = lh_http_execute_post(url, headers, payload); // if (*response->http_code != HTTP_OK){ // char* log_msg = lh_get_message_str("Could not send event. Reason: %s", response->response_body); // log_warn(log_msg); // free(log_msg); // lh_http_free(response); // lh_dict_free(headers); // free(url); // return 0; // } // char *message = lh_get_message_str("Sent event %s", event_name); // log_info(message); // free(message); // lh_http_free(response); // lh_dict_free(headers); // free(url); // return 1; }
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]); }