bool build_and_send_packet(struct nabto_stream_s* stream, uint8_t type, uint32_t seq, const uint8_t* winInfoData, size_t winInfoSize, uint8_t* data, uint16_t size, struct nabto_stream_sack_data* sackData) { enum { l_win = NP_PAYLOAD_WINDOW_SYN_BYTELENGTH - NP_PAYLOAD_WINDOW_BYTELENGTH }; uint8_t* ptr; nabto_connect* con = stream->connection; uint8_t* buf = nabtoCommunicationBuffer; struct nabto_stream_tcb* tcb = &stream->u.tcb; uint32_t ackToSend = unabto_stream_ack_number_to_send(tcb); uint16_t recvWinSize = unabto_stream_advertised_window_size(tcb); if (con == NULL) { return false; } nabtoSetFutureStamp(&tcb->ackStamp, 2*tcb->cfg.timeoutMsec); ptr = insert_data_header(buf, con->spnsi, con->nsico, stream->streamTag); ptr = insert_payload(ptr, NP_PAYLOAD_TYPE_WINDOW, 0, l_win + winInfoSize + (type == NP_PAYLOAD_WINDOW_FLAG_ACK ? 2 : 0)); WRITE_FORWARD_U8 (ptr, type); WRITE_FORWARD_U8 (ptr, NP_STREAM_VERSION); WRITE_FORWARD_U16(ptr, stream->idCP); WRITE_FORWARD_U16(ptr, stream->idSP); WRITE_FORWARD_U32(ptr, seq); WRITE_FORWARD_U32(ptr, ackToSend); if (type == NP_PAYLOAD_WINDOW_FLAG_ACK) { WRITE_FORWARD_U16(ptr, recvWinSize); } if (winInfoSize) { memcpy(ptr, (const void*) winInfoData, winInfoSize); ptr += winInfoSize; } if (sackData && sackData->nPairs > 0) { uint8_t i; ptr = insert_payload(ptr, NP_PAYLOAD_TYPE_SACK, 0, 8 * sackData->nPairs); for (i = 0; i < sackData->nPairs; i++) { WRITE_FORWARD_U32(ptr, sackData->pairs[i].start); WRITE_FORWARD_U32(ptr, sackData->pairs[i].end); } } ptr = insert_payload(ptr, NP_PAYLOAD_TYPE_CRYPTO, 0, 0); if (send_and_encrypt_packet_con(con, data, size, ptr)) { tcb->ackSent = ackToSend; tcb->lastSentAdvertisedWindow = recvWinSize; stream->stats.sentPackets++; return true; } else { return false; } }
uint8_t* insert_rendezvous_stats_payload(uint8_t* ptr, uint8_t* end, nabto_connect* con) { UNABTO_ASSERT(ptr <= end); if (end-ptr < NP_PAYLOAD_RENDEZVOUS_STATS_BYTELENGTH) { return NULL; } ptr = insert_payload(ptr, NP_PAYLOAD_TYPE_RENDEZVOUS_STATS, 0, 7); WRITE_FORWARD_U8(ptr, NP_PAYLOAD_RENDEZVOUS_STATS_VERSION); WRITE_FORWARD_U8(ptr, con->clientNatType); WRITE_FORWARD_U8(ptr, nmc.context.natType); WRITE_FORWARD_U16(ptr, con->rendezvousConnectState.portsOpened); WRITE_FORWARD_U16(ptr, con->rendezvousConnectState.socketsOpened); return ptr; }
bool unabto_dns_fallback_data_send_packet_start_request(unabto_dns_fallback_session* session, uint16_t chunks, nabto_endpoint* ep, uint16_t totalLength) { uint8_t requestBuffer[19]; uint8_t* ptr = requestBuffer; uint8_t* buffer; uint8_t* end; uint16_t id; size_t length; WRITE_FORWARD_U8(ptr, UDF_TYPE_SEND_START); WRITE_FORWARD_U16(ptr, 19); WRITE_FORWARD_U16(ptr, session->sessionId); WRITE_FORWARD_U16(ptr, session->random++); WRITE_FORWARD_U16(ptr, session->packetId); WRITE_FORWARD_U16(ptr, chunks); WRITE_FORWARD_U32(ptr, ep->addr); WRITE_FORWARD_U16(ptr, ep->port); WRITE_FORWARD_U16(ptr, totalLength); buffer = requestEncoded; end = buffer + ENCODED_BUFFER_SIZE; buffer = unabto_dns_fallback_encode_request_as_question(buffer, end, requestBuffer, 19, session->domain, &id); if (buffer == NULL) { return false; } length = buffer - requestEncoded; NABTO_LOG_INFO(("Sending DNS start request id: %" PRIu16, id)); nabto_write(session->dnsClient.sockets[(session->dnsClient.lastUsedSocket++) % DNS_CLIENT_SOCKETS], requestEncoded, length, session->dnsClient.dnsServer.addr, session->dnsClient.dnsServer.port); return true; }
bool unabto_dns_fallback_data_send_packet_chunk_request(unabto_dns_fallback_session* session, uint8_t* start, uint16_t dataStart, uint16_t length, uint16_t chunkId) { uint8_t* ptr = requestBuffer; size_t requestLength; uint8_t* buffer; uint8_t* end; uint16_t id; size_t packetLength; WRITE_FORWARD_U8(ptr, UDF_TYPE_SEND_CHUNK); WRITE_FORWARD_U16(ptr, length+13); WRITE_FORWARD_U16(ptr, session->sessionId); WRITE_FORWARD_U16(ptr, session->random++); WRITE_FORWARD_U16(ptr, session->packetId); WRITE_FORWARD_U16(ptr, chunkId); WRITE_FORWARD_U16(ptr, dataStart); memcpy(ptr, start, length); ptr += length; requestLength = ptr - requestBuffer; buffer = requestEncoded; end = buffer + ENCODED_BUFFER_SIZE; buffer = unabto_dns_fallback_encode_request_as_question(buffer, end, requestBuffer, requestLength, session->domain, &id); if (buffer == NULL) { return false; } packetLength = buffer - requestEncoded; NABTO_LOG_INFO(("Sending DNS chunk request id: %" PRIu16, id)); nabto_write(session->dnsClient.sockets[(session->dnsClient.lastUsedSocket++) % DNS_CLIENT_SOCKETS], requestEncoded, packetLength, session->dnsClient.dnsServer.addr, session->dnsClient.dnsServer.port); return true; }
bool unabto_dns_fallback_data_send_get_request(unabto_dns_fallback_session* session) { uint8_t* ptr = requestBuffer; size_t requestLength; uint8_t* buffer = nabtoCommunicationBuffer; uint8_t* end = buffer + nabtoCommunicationBufferSize; size_t packetLength; WRITE_FORWARD_U8(ptr, UDF_TYPE_GET); WRITE_FORWARD_U16(ptr, 9); WRITE_FORWARD_U16(ptr, session->sessionId); WRITE_FORWARD_U16(ptr, session->random++); WRITE_FORWARD_U16(ptr, session->recvFrequency); requestLength = ptr - requestBuffer; buffer = unabto_dns_fallback_encode_request_as_question(buffer, end, requestBuffer, requestLength, session->domain, &session->current_get_identification); if (buffer == NULL) { return false; } packetLength = buffer - nabtoCommunicationBuffer; NABTO_LOG_INFO(("Sending DNS get request id: %" PRIu16 " timeout: %" PRIu16, session->current_get_identification, session->recvFrequency)); nabto_write(session->dnsClient.sockets[(session->dnsClient.lastUsedSocket++) % DNS_CLIENT_SOCKETS], nabtoCommunicationBuffer, packetLength, session->dnsClient.dnsServer.addr, session->dnsClient.dnsServer.port); return true; }