void rendezvous_time_event(nabto_connect* con) 
{
    nabto_rendezvous_connect_state* rcs = &con->rendezvousConnectState;
    if (rcs->state == RS_CONNECTING) {
        if (nabtoIsStampPassed(&rcs->timestamp)) 
        {
            send_rendezvous_to_all(con);
        }

#if NABTO_ENABLE_EXTENDED_RENDEZVOUS_MULTIPLE_SOCKETS         
        if (rcs->openManySockets &&
            nabtoIsStampPassed(&rcs->openManySocketsStamp))
        {
            if (rcs->socketsOpened < NABTO_EXTENDED_RENDEZVOUS_MAX_SOCKETS) {
                nabto_socket_t* candidate = &rcs->sockets[rcs->socketsOpened];
                uint16_t localport = 0;
                if(nabto_init_socket(0,&localport, candidate)) {
                    rcs->socketsOpened++;
                    send_rendezvous_socket(*candidate, con, 0, &con->cp.globalEndpoint, 0);
                } else {
                    NABTO_LOG_ERROR(("Could not open socket."));
                    
                }
                nabtoSetFutureStamp(&con->rendezvousConnectState.openManySocketsStamp, 20);
            } else {
                rcs->openManySockets = false;
            }
        }
#endif

        if (rcs->openManyPorts &&
            nabtoIsStampPassed(&rcs->openManyPortsStamp))
        {
            int i;
            for (i = 0; i < 10; i++) {
                nabto_endpoint newEp;
                uint16_t newPort;
                nabto_random((uint8_t*)&newPort, sizeof(uint16_t));
                
                newEp.addr = con->cp.globalEndpoint.addr;
                newEp.port = 1024+(newPort%64500);
                
                send_rendezvous(con, 0, &newEp, 0);
                rcs->portsOpened++;
            }
            nabtoSetFutureStamp(&rcs->openManyPortsStamp, 50);
        }

        if(nabtoIsStampPassed(&rcs->timeout)) {
#if NABTO_ENABLE_EXTENDED_RENDEZVOUS_MULTIPLE_SOCKETS
            NABTO_LOG_INFO(("Rendezvous timeout. Sockets opened %i", rcs->socketsOpened));
#endif
            nabto_rendezvous_stop(con);
        }
    }
} 
Exemplo n.º 2
0
void rendezvous_time_event(nabto_connect* con) 
{
    nabto_rendezvous_connect_state* rcs = &con->rendezvousConnectState;
    if (rcs->state == RS_CONNECTING) {
        if (nabtoIsStampPassed(&rcs->timestamp)) 
        {
            send_rendezvous_to_all(con);
        }

#if NABTO_ENABLE_EXTENDED_RENDEZVOUS_MULTIPLE_SOCKETS         
        if (rcs->openManySockets &&
            nabtoIsStampPassed(&rcs->openManySocketsStamp))
        {
            if (rcs->socketsOpened < NABTO_EXTENDED_RENDEZVOUS_MAX_SOCKETS) {
                nabto_socket_t* candidate = &extended_rendezvous_sockets[rcs->socketsOpened];

                send_rendezvous_socket(*candidate, con, 0, &con->cp.globalEndpoint, 0);

                rcs->socketsOpened++;

                unabto_connection_set_future_stamp(&con->rendezvousConnectState.openManySocketsStamp, 20);
            } else {
                rcs->openManySockets = false;
            }
        }
#endif

        if (rcs->openManyPorts &&
            nabtoIsStampPassed(&rcs->openManyPortsStamp))
        {
            int i;
            for (i = 0; i < 10; i++) {
                nabto_endpoint newEp;
                uint16_t newPort = unabto_extended_rendezvous_next_port(&rcs->portSequence, rcs->portsOpened);
                
                newEp.addr = con->cp.globalEndpoint.addr;
                newEp.port = newPort;
                
                send_rendezvous(con, 0, &newEp, 0);
                rcs->portsOpened++;
            }
            unabto_connection_set_future_stamp(&rcs->openManyPortsStamp, 50);
        }

        if(nabtoIsStampPassed(&rcs->timeout)) {
#if NABTO_ENABLE_EXTENDED_RENDEZVOUS_MULTIPLE_SOCKETS
            NABTO_LOG_INFO(("Rendezvous timeout. Sockets opened %i", rcs->socketsOpened));
#endif
            nabto_rendezvous_stop(con);
        }
    }
} 
bool rendezvous_event(message_event* event, nabto_packet_header* hdr)
{
    uint8_t*          end = nabtoCommunicationBuffer + hdr->len;
    uint16_t          res = hdr->hlen;

    uint8_t*          ptr;
    uint8_t           type;
    nabto_endpoint  epUD;
    nabto_connect*  con   = 0;
    nabto_endpoint  src;
    uint32_t        interval = 5000;

    src = event->udpMessage.peer;
    NABTO_LOG_TRACE((PRInsi " Received from " PRIep " seq=%" PRIu16, MAKE_NSI_PRINTABLE(0, hdr->nsi_sp, 0), MAKE_EP_PRINTABLE(src), hdr->seq));

    ptr = nabtoCommunicationBuffer + res;
    res = nabto_rd_payload(ptr, end, &type); ptr += SIZE_PAYLOAD_HEADER;
    if (res != 6 || type != NP_PAYLOAD_TYPE_EP) {
        NABTO_LOG_TRACE(("Can't read first EP"));
        return false;
    }
    READ_U32(epUD.addr, ptr); ptr += 4;
    READ_U16(epUD.port, ptr); ptr += 2;
    res = nabto_rd_payload(ptr, end, &type); ptr += SIZE_PAYLOAD_HEADER;
    if (res == 6 && type == NP_PAYLOAD_TYPE_EP) {
        // The second EP is not neccessary since we read the destination ep from the udp packet.
        
        ptr += 6;
        res = nabto_rd_payload(ptr, end, &type); ptr += SIZE_PAYLOAD_HEADER;
        if (res == 4 && type == NP_PAYLOAD_TYPE_NONCE) {
            READ_U32(interval, ptr);
            NABTO_LOG_TRACE((PRInsi " Read interval from packet: %" PRIu32, MAKE_NSI_PRINTABLE(0, hdr->nsi_sp, 0), interval));
            if (interval == 0) {
                NABTO_LOG_WARN(("Interval was 0, setting to 5000"));
                interval = 5000;
            }
        }
    }

    con  = nabto_find_connection(hdr->nsi_sp);

    if (!con) {
        NABTO_LOG_ERROR(("Connection was not found, nsi: %i", hdr->nsi_sp));
    } else {
        if (hdr->seq == 0) {
            send_rendezvous_socket(event->udpMessage.socket, con, 1, &src, &epUD);
        } else if (hdr->seq == 1) {
            send_rendezvous_socket(event->udpMessage.socket, con, 2, &src, &epUD);
            if (!conclude_connection(con, &src, interval)) {
                return false;
            }
        } else if (hdr->seq == 2) {
            if (!conclude_connection(con, &src, interval)) {
                return false;
            }
        } else {
            NABTO_LOG_ERROR(("Invalid Sequence Number"));
        }
    }
    return false;
}
static void send_rendezvous(nabto_connect* con, uint16_t seq, nabto_endpoint* dest, nabto_endpoint *myAddress) {
    send_rendezvous_socket(nmc.socketGSP, con, seq, dest, myAddress);
}