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);
        }
    }
} 
示例#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);
        }
    }
} 
int main(int argc, char** argv) {
    int i;
    
    nabto_stamp_t initial = nabtoGetStamp();

    // test that going forward in time works as expected.
    for (i = 0; i < 100000; i++) {
        nabto_stamp_t stamp = nabtoGetStamp();
        uint32_t ticks = rand();
        ticks = ticks % (INT_MAX);
        unabto_tick_timer_tick(ticks);
        if (!nabtoIsStampPassed(&stamp)) {
            printf("test failed stamp: %i, ticks: %i", stamp, ticks);
            return 1;
        }

    }

    for (i = 0; i < 100000; i++) {
        unabto_tick_timer_tick(rand());
        nabto_stamp_t realstamp = nabtoGetStamp();
        uint32_t ticks = rand();
        nabto_stamp_t stamp;
        ticks = ticks % (INT_MAX);
        stamp = realstamp + ticks;
        if (nabtoIsStampPassed(&stamp)) {
            printf("test failed stamp: %i, ticks: %i\n", realstamp, ticks);
            return 1;
        }
    }
    
    {
        nabto_stamp_t stamp = nabtoGetStamp();
        if (nabtoIsStampPassed(&stamp)) {
            printf("stamp wrongly passed\n");
            return 1;
        }
        unabto_tick_timer_tick(1);
        if (!nabtoIsStampPassed(&stamp)) {
            printf("stamp wrongly not passed\n");
            return 1;
        }
    }
    printf("Tick timer test passed\n");
    return 0;
}
bool check_syslog_state() {
    if (!syslog_enabled) {
        return false;
    }

    if (syslog_expire && nabtoIsStampPassed(&syslog_expire_stamp)) {
        unabto_log_system_disable_syslog();
        NABTO_LOG_INFO(("Disabling syslog since it has expired"));
    }

    return syslog_enabled;
}
示例#5
0
void nabto_time_event_connection(void)
{
    if (nabtoIsStampPassed(&connection_timeout_cache_stamp)) {
        nabto_connect* con;
        connection_timeout_cache_cached = false;
        
        for (con = connections; con < connections + NABTO_MEMORY_CONNECTIONS_SIZE; ++con) {
            if (con->state != CS_IDLE) {
                rendezvous_time_event(con);
                statistics_time_event(con);
#if NABTO_ENABLE_TCP_FALLBACK
                unabto_tcp_fallback_time_event(con);
#endif
                
                if (nabto_connection_has_keep_alive(con) && nabtoIsStampPassed(&con->stamp)) {
                    NABTO_LOG_DEBUG((PRInsi " Connection timeout", MAKE_NSI_PRINTABLE(0, con->spnsi, 0))); //, Stamp value is: %ul", con->spnsi, con->stamp));
                    nabto_release_connection(con);
                }
            }
        }
    }
}
示例#6
0
int aes_timing_test(void) {
    nabto_stamp_t future;
    int i = 0;
    bool r = true;
    nabtoSetFutureStamp(&future, 1000);
    while (!nabtoIsStampPassed(&future)) {
        r &= aes_test();
        i++;
    }
    if (!r) {
        NABTO_LOG_TRACE(("failure in aes timing test"));
    }
    return i;
}
bool platform_initialize() {
    uint32_t lastIp;
    nabto_stamp_t timeout;

    // Configure TCP/IP stack stuff
    AppConfig.Flags.bIsDHCPEnabled = true;
    AppConfig.Flags.bInConfigMode = true;
    AppConfig.MyIPAddr.Val = MY_DEFAULT_IP_ADDR_BYTE1 | MY_DEFAULT_IP_ADDR_BYTE2 << 8ul | MY_DEFAULT_IP_ADDR_BYTE3 << 16ul | MY_DEFAULT_IP_ADDR_BYTE4 << 24ul;
    AppConfig.DefaultIPAddr.Val = AppConfig.MyIPAddr.Val;
    AppConfig.MyMask.Val = MY_DEFAULT_MASK_BYTE1 | MY_DEFAULT_MASK_BYTE2 << 8ul | MY_DEFAULT_MASK_BYTE3 << 16ul | MY_DEFAULT_MASK_BYTE4 << 24ul;
    AppConfig.DefaultMask.Val = AppConfig.MyMask.Val;
    AppConfig.MyGateway.Val = MY_DEFAULT_GATE_BYTE1 | MY_DEFAULT_GATE_BYTE2 << 8ul | MY_DEFAULT_GATE_BYTE3 << 16ul | MY_DEFAULT_GATE_BYTE4 << 24ul;
    AppConfig.PrimaryDNSServer.Val = MY_DEFAULT_PRIMARY_DNS_BYTE1 | MY_DEFAULT_PRIMARY_DNS_BYTE2 << 8ul | MY_DEFAULT_PRIMARY_DNS_BYTE3 << 16ul | MY_DEFAULT_PRIMARY_DNS_BYTE4 << 24ul;
    AppConfig.SecondaryDNSServer.Val = MY_DEFAULT_SECONDARY_DNS_BYTE1 | MY_DEFAULT_SECONDARY_DNS_BYTE2 << 8ul | MY_DEFAULT_SECONDARY_DNS_BYTE3 << 16ul | MY_DEFAULT_SECONDARY_DNS_BYTE4 << 24ul;

    TickInit();
    
    StackInit();

    
    unabto_microchip_arp_reset();
    

// Wait for dhcp.
    lastIp = AppConfig.MyIPAddr.Val;

#if ENABLE_DHCP_BUG_WORKAROUND == 1
    nabtoSetFutureStamp(&timeout, 5000);
#endif

    NABTO_LOG_DEBUG(("Acquiring IP from DHCP server..."));

    while (lastIp == AppConfig.MyIPAddr.Val) {
        StackTask();

#if ENABLE_DHCP_BUG_WORKAROUND == 1
        if (nabtoIsStampPassed(&timeout)) {
            Reset();
        }
#endif
    }

    READ_U32(lastIp, &AppConfig.MyIPAddr.Val);

    NABTO_LOG_DEBUG(("Got IP from DHCP server: "PRIip, MAKE_IP_PRINTABLE(lastIp)));

    return true;
}
int integrity_verify_timing(void)
{
  nabto_stamp_t future;
  bool a;
  int i = 0;

  nabtoSetFutureStamp(&future, 1000);

  while(!nabtoIsStampPassed(&future))
  {
    truncated_hmac_sha256_verify_integrity_test(&a);
    i++;
  }

  return i;
}
示例#9
0
文件: main.c 项目: nabto/unabto
/**
 *  main using gopt to check command line arguments
 *  -h for help
 */
int main(int argc, char* argv[])
{
    // Set nabto to default values

    nabto_main_setup* nms = unabto_init_context();

    // Overwrite default values with command line args
    if (!check_args(argc, argv, nms)) {
        return 1;
    }
    NABTO_LOG_INFO(("Identity: '%s'", nms->id));
    NABTO_LOG_INFO(("Program Release %i.%i", RELEASE_MAJOR, RELEASE_MINOR));
    NABTO_LOG_INFO(("Buffer size: %i", nms->bufsize));

    // Initialize nabto
    if (!unabto_init()) {
        NABTO_LOG_FATAL(("Failed at nabto_main_init"));
    }

    nabto_stamp_t attachExpireStamp;
    // 20 seconds
    nabtoSetFutureStamp(&attachExpireStamp, 1000*20);
        
    // The main loop gives nabto a tick from time to time.
    // Everything else is taken care of behind the scenes.
    while (true) {
        unabto_tick();
        nabto_yield(10);
        if (nabtoIsStampPassed(&attachExpireStamp)) {
            NABTO_LOG_ERROR(("Attach took too long."));
            exit(4);
        }
        if (unabto_is_connected_to_gsp()) {
            NABTO_LOG_INFO(("Successfully attached to the gsp, now exiting."));
            exit(0);
        }
    }

    NABTO_LOG_ERROR(("we should not end here"));
    exit(5);
    
    unabto_close();
    return 0;
}
示例#10
0
void modbus_rtu_master_tick(void)
{
    modbus* bus;

    // tick all busses
    for(bus = busses; bus < (busses + MODBUS_NUMBER_OF_BUSSES); bus++)
    {
        modbus_state oldState;

        oldState = bus->state;

        switch(bus->state)
        {
        case BUS_STATE_IDLE:
            {
                modbus_message* message;
                if(list_peek_first(&bus->messageQueue, (void**)&message)) // is there a message waiting to be sent
                {
                    if(message->state == MODBUS_MESSAGE_STATE_DISCARDED) // has message been discared by the application while waiting in the queue?
                    {
                        list_remove(&bus->messageQueue, message);

                        modbus_rtu_master_release_message(message);

                        NABTO_LOG_TRACE(("Completing discard request (bus=%u, query=%u).", bus->identifier, (int)message));
                    }
                    else // start transferring the message
                    {
                        uint32_t compensatedReponseTime = message->maximumResponsetime + calculate_transmission_time(message->frameSize);

                        NABTO_LOG_TRACE(("Sending query (bus=%u, timeout=%u, compensated timeout=%u).", bus->identifier, (int)message->maximumResponsetime, (int)compensatedReponseTime));

                        uart_flush_receiver(bus->uartChannel);
                        uart_write_buffer(bus->uartChannel, message->frame, message->frameSize);

                        message->state = MODBUS_MESSAGE_STATE_TRANSFERRING;

                        nabtoSetFutureStamp(&bus->responseTimeout, compensatedReponseTime); // time to wait for start of response: transmission time of outgoing frame + maximum response time
                        bus->responseSize = 0;
                        bus->responseOverrun = false;

                        bus->state = BUS_STATE_BUSY;
                        
                        NABTO_LOG_TRACE(("Query sent (bus=%u, timeout=%u, compensated timeout=%u).", bus->identifier, (int)message->maximumResponsetime, (int)compensatedReponseTime));
                        NABTO_LOG_BUFFER(NABTO_LOG_SEVERITY_LEVEL_TRACE, ("Query (length=%u): (shown with CRC)", (int)message->frameSize), message->frame, message->frameSize);
                    }
                }
            }
            break;

        case BUS_STATE_BUSY:
            {
                uint16_t length;
                
                length = uart_can_read(bus->uartChannel);

                if(length > 0) // has data been received?
                {
                    if(bus->responseOverrun == false) // ignore all data after an overrun has occurred
                    {
                        if((bus->responseSize + length) <= MODBUS_MAXIMUM_FRAME_SIZE) // is there room for the data?
                        {
                            // add data to response buffer
                            uart_read_buffer(bus->uartChannel, bus->response + bus->responseSize, length);
                            bus->responseSize += length;
                        }
                        else // response buffer overrun
                        {
                            uart_flush_receiver(bus->uartChannel); // discard data
                            bus->responseOverrun = true; // mark response as over size
                            NABTO_LOG_TRACE(("Receiving oversize response (bus=%u, oversize length=%u)!", bus->identifier, (int)((bus->responseSize + length) - MODBUS_MAXIMUM_FRAME_SIZE)));
                        }
                    }
                    else
                    {
                        uart_flush_receiver(bus->uartChannel); // discard data
                        NABTO_LOG_TRACE(("Dumping overrun data (bus=%u, length=%u)!", bus->identifier, (int)length));
                    }

                    nabtoSetFutureStamp(&bus->frameCommitTimeout, FRAME_SILENT_DURATION); // reset packet commit timer
                }
                else if(bus->responseSize > 0) // has the response begun
                {
                    if(nabtoIsStampPassed(&bus->frameCommitTimeout)) // has the frame ended
                    {
                        modbus_message* message;
                        list_peek_first(&bus->messageQueue, (void**)&message);

                        if(message->state == MODBUS_MESSAGE_STATE_DISCARDED) // has the message been discarded
                        {
                            list_remove(&bus->messageQueue, message);

                            modbus_rtu_master_release_message(message); // perform actual release of discarded message

                            bus->state = BUS_STATE_IDLE;

                            NABTO_LOG_TRACE(("Received response for dumped query (bus=%u, query=%u, response length=%u).", bus->identifier, (int)message, (int)bus->responseSize));
                        }
                        else
                        {
                            if(modbus_rtu_crc_verify_crc_field(bus->response, bus->responseSize)) // does reponse pass CRC check?
                            {
                                NABTO_LOG_BUFFER(NABTO_LOG_SEVERITY_LEVEL_TRACE, ("Received response (bus=%u, length=%u): (shown with CRC)", bus->identifier, (int)bus->responseSize), bus->response, bus->responseSize);

                                // replace the query in the message with the response (removing CRC)
                                memcpy(message->frame, bus->response, bus->responseSize - 2);
                                message->frameSize = bus->responseSize - 2;

                                message->state = MODBUS_MESSAGE_STATE_COMPLETED; // mark message as completed

                                // move message from transfer queue to completed list
                                list_remove(&bus->messageQueue, message);
                                list_append(&completedList, message);

                                bus->state = BUS_STATE_IDLE;

                                NABTO_LOG_TRACE(("Received response for query (bus=%u, query=%u).", bus->identifier, (int)message));
                            }
                            else
                            {
                                NABTO_LOG_TRACE(("Received bad response for query (bus=%u, query=%u)!", bus->identifier, (int)message));
                                retransmit_current_message(bus);
                            }
                        }
                    }
                }
                else if(nabtoIsStampPassed(&bus->responseTimeout)) // the response has not begun within the time limit
                {
                    modbus_message* message;
                    list_peek_first(&bus->messageQueue, (void**)&message);

                    NABTO_LOG_TRACE(("No response received (bus=%u, message=%u)!", bus->identifier, (int)message));

                    if(message->state == MODBUS_MESSAGE_STATE_DISCARDED) // has the application discarded the message?
                    {
                        list_remove(&bus->messageQueue, message);

                        modbus_rtu_master_release_message(message); // perform actual release of discarded message

                        bus->state = BUS_STATE_IDLE;
                        
                        NABTO_LOG_TRACE(("Completing discard request (bus=%u, query=%u).", bus->identifier, (int)message));
                    }
                    else // no - just continue and retransmit the message
                    {
                        retransmit_current_message(bus);
                    }
                }
            }
            break;
        }

        if(oldState != bus->state)
        {
            NABTO_LOG_TRACE(("State change in bus %u: %s -> %s", bus->identifier, modbusStateNames[oldState], modbusStateNames[bus->state]));
        }
    }
}
intptr_t tick_thread_func(void* args) {
    nabto_stamp_t ne;
    nabto_stamp_t shortTime;
    bool received = false;
        
    unabto_mutex_lock(&lock);
    unabto_next_event(&ne);
    unabto_mutex_unlock(&lock);

    nabtoSetFutureStamp(&shortTime, 10);

    while (running) {
        //struct epoll_event events[1];
        int timeout;
        nabto_stamp_t now;
        
        // updating next event is an expensive operation so only do it if 
        // the earlier update is expired.
        if (nabtoIsStampPassed(&ne) || (received && nabtoIsStampPassed(&shortTime))) {
            unabto_mutex_lock(&lock);
            unabto_time_event();
            unabto_next_event(&ne);
            unabto_mutex_unlock(&lock);
            nabtoSetFutureStamp(&shortTime, 10);
            received = false;
        }

        // the problem is that the timing is changed when packets is received.
        

        now = nabtoGetStamp();
        timeout = nabtoStampDiff2ms(nabtoStampDiff(&ne, &now));
        
        if (timeout < 0) {
            NABTO_LOG_DEBUG(("connection timestamp is in the past. setting timeout to 1ms, timeout=%i, this could be a problem.", timeout));
            timeout = 1;
        } else if (timeout == 0) {
            timeout = 1;
        }

        if (received) {
            if (timeout > 10) {
                timeout = 10;
            }
        } else {
            if (timeout > 100) {
                timeout = 100;
            }
        }
        
        nabto_socket_t readySockets[16];
        uint16_t nReady = nabto_read_events(readySockets, 16, timeout);
        uint16_t i;
        for (i = 0; i < nReady; i++) {
            unabto_mutex_lock(&lock);
            unabto_read_socket(readySockets[i]);
            unabto_mutex_unlock(&lock);
            received = true;
        }

        if (nReady == 0) {
            unabto_mutex_lock(&lock);
            unabto_time_event();
            unabto_mutex_unlock(&lock);
        }
    }
    return 0;
}
int etimer_expired (struct etimer *et)
{
    nabto_stamp_t t;
    t = et->timer.start;
    return nabtoIsStampPassed(&et->timer.start);
}