void udp_server_task(void const *argument) { DigitalOut indicator(LED1); UDPSocket server; server.bind(ECHO_SERVER_PORT); // printf("[udp_server_task] Start\r\n"); Endpoint client; char buffer[BUFFER_SIZE] = { 0 }; while (true) { //printf("[udp_server_task] Wait for packet...\r\n"); int n = server.receiveFrom(client, buffer, sizeof(buffer)); if (n > 0) { //printf("[udp_server_task] Received packet from: %s\r\n", client.get_address()); const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE - 1 : n; buffer[buffer_string_end_index] = '\0'; //printf("[udp_server_task] Server received: %s\r\n", buffer); if (host_port == 0) { strcpy(host_address, client.get_address()); host_port = ECHO_SERVER_PORT + 1; //printf("[udp_server_task] Set host address and port: %s:%d\r\n", host_address, host_port); } // Dispatch data to client for sending to test HOST cli_serv_mutex.lock(); // LOCK // Push to datagram queue datagram_queue.push_front(std::string(buffer)); max_queue_len = datagram_queue.size() > max_queue_len ? datagram_queue.size() : max_queue_len; received_packets++; cli_serv_mutex.unlock(); // LOCK indicator = !indicator; } } }
int main (void) { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("IP Address is %s\n", eth.getIPAddress()); UDPSocket server; server.bind(ECHO_SERVER_PORT); Endpoint client; char buffer[256]; while (true) { printf("\nWait for packet...\n"); int n = server.receiveFrom(client, buffer, sizeof(buffer)); printf("Received packet from: %s\n", client.get_address()); server.sendTo(client, buffer, n); } }
int main() { bool result = false; EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("UDP client IP Address is %s\n", eth.getIPAddress()); UDPSocket sock; sock.init(); Endpoint nist; nist.set_address(HTTP_SERVER_NAME, HTTP_SERVER_PORT); char out_buffer[] = "plop"; // Does not matter sock.sendTo(nist, out_buffer, sizeof(out_buffer)); union { char in_buffer_tab[4]; unsigned int in_buffer_uint; }; const int n = sock.receiveFrom(nist, in_buffer_tab, sizeof(in_buffer_tab)); if (n > 0) { result = true; const unsigned int timeRes = ntohl(in_buffer_uint); const float years = timeRes / 60.0 / 60.0 / 24.0 / 365; printf("UDP: Received %d bytes from server %s on port %d\r\n", n, nist.get_address(), nist.get_port()); printf("UDP: %u seconds since 01/01/1900 00:00 GMT ... %s\r\n", timeRes, timeRes > 0 ? "[OK]" : "[FAIL]"); printf("UDP: %.2f years since 01/01/1900 00:00 GMT ... %s\r\n", years, timeRes > YEARS_TO_PASS ? "[OK]" : "[FAIL]"); if (years < YEARS_TO_PASS) { result = false; } } sock.close(); eth.disconnect(); notify_completion(result); return 0; }
int main (void) { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("Server IP Address is %s:%d\n", eth.getIPAddress(), ECHO_SERVER_PORT); UDPSocket server; server.bind(ECHO_SERVER_PORT); Endpoint client; char buffer[BUFFER_SIZE] = {0}; while (true) { printf("Wait for packet...\n"); int n = server.receiveFrom(client, buffer, sizeof(buffer)); if (n > 0) { printf("Received packet from: %s\n", client.get_address()); const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE-1 : n; buffer[buffer_string_end_index] = '\0'; printf("Server received: %s\n", buffer); server.sendTo(client, buffer, n); } } }
bool UDPSocket::confEndpoint(Endpoint & ep) { char * host; char cmd[30]; if (!endpoint_configured) { host = ep.get_address(); if (host[0] != '\0') { // set host sprintf(cmd, "set i h %s\r", host); if (!wifi->sendCommand(cmd, "AOK")) return false; // set remote port sprintf(cmd, "set i r %d\r", ep.get_port()); if (!wifi->sendCommand(cmd, "AOK")) return false; wifi->exit(); endpoint_configured = true; return true; } } return true; }
NTPResult NTPClient::setTime(const char* host, uint16_t port, uint32_t timeout) { #ifdef __DEBUG__ time_t ctTime; ctTime = time(NULL); DBG("Time is set to (UTC): %s", ctime(&ctTime)); #endif //Create & bind socket DBG("Binding socket"); m_sock.bind(0); //Bind to a random port m_sock.set_blocking(false, timeout); //Set not blocking struct NTPPacket pkt; //Now ping the server and wait for response DBG("Ping"); //Prepare NTP Packet: pkt.li = 0; //Leap Indicator : No warning pkt.vn = 4; //Version Number : 4 pkt.mode = 3; //Client mode pkt.stratum = 0; //Not relevant here pkt.poll = 0; //Not significant as well pkt.precision = 0; //Neither this one is pkt.rootDelay = 0; //Or this one pkt.rootDispersion = 0; //Or that one pkt.refId = 0; //... pkt.refTm_s = 0; pkt.origTm_s = 0; pkt.rxTm_s = 0; pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE pkt.refTm_f = pkt.origTm_f = pkt.rxTm_f = pkt.txTm_f = 0; Endpoint outEndpoint; if( outEndpoint.set_address(host, port) < 0) { m_sock.close(); return NTP_DNS; } //Set timeout, non-blocking and wait using select int ret = m_sock.sendTo( outEndpoint, (char*)&pkt, sizeof(NTPPacket) ); if (ret < 0 ) { ERR("Could not send packet"); m_sock.close(); return NTP_CONN; } //Read response Endpoint inEndpoint; DBG("Pong"); do { ret = m_sock.receiveFrom( inEndpoint, (char*)&pkt, sizeof(NTPPacket) ); //FIXME need a DNS Resolver to actually compare the incoming address with the DNS name if(ret < 0) { ERR("Could not receive packet"); m_sock.close(); return NTP_CONN; } } while( strcmp(outEndpoint.get_address(), inEndpoint.get_address()) != 0 ); if(ret < sizeof(NTPPacket)) //TODO: Accept chunks { ERR("Receive packet size does not match"); m_sock.close(); return NTP_PRTCL; } if( pkt.stratum == 0) //Kiss of death message : Not good ! { ERR("Kissed to death!"); m_sock.close(); return NTP_PRTCL; } //Correct Endianness pkt.refTm_s = ntohl( pkt.refTm_s ); pkt.refTm_f = ntohl( pkt.refTm_f ); pkt.origTm_s = ntohl( pkt.origTm_s ); pkt.origTm_f = ntohl( pkt.origTm_f ); pkt.rxTm_s = ntohl( pkt.rxTm_s ); pkt.rxTm_f = ntohl( pkt.rxTm_f ); pkt.txTm_s = ntohl( pkt.txTm_s ); pkt.txTm_f = ntohl( pkt.txTm_f ); //Compute offset, see RFC 4330 p.13 uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL)); int64_t offset = ( (int64_t)( pkt.rxTm_s - pkt.origTm_s ) + (int64_t) ( pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow DBG("Sent @%ul", pkt.txTm_s); DBG("Offset: %lld", offset); //Set time accordingly set_time( time(NULL) + offset ); #ifdef __DEBUG__ ctTime = time(NULL); DBG("Time is now (UTC): %s", ctime(&ctTime)); #endif m_sock.close(); return NTP_OK; }
int main() { int result; lwm2m_object_t * objArray[4]; printf("Start\n"); ethSetup(); printf("Initializing tinydtls\n"); // fake loading of PSK.. psk_id_length = strlen(PSK_DEFAULT_IDENTITY); psk_key_length = strlen(PSK_DEFAULT_KEY); memcpy(psk_id, PSK_DEFAULT_IDENTITY, psk_id_length); memcpy(psk_key, PSK_DEFAULT_KEY, psk_key_length); printf("Init\n"); dtls_init(); printf("New context\n"); dtls_context = dtls_new_context(&lwm2mH); if (dtls_context == NULL) { printf("error creating the dtls context\n"); } printf("Setting handlers\n"); dtls_set_handler(dtls_context, &cb); if (!dtls_context) { printf("can't create dtls_context\n"); exit(-1); } printf("Initialazing Wakaama\n"); // create objects objArray[0] = get_security_object(123, "coaps://5.39.83.206:5684", false); securityObjP = objArray[0]; objArray[1] = get_server_object(123, "U", 20, false); serverObject = objArray[1]; objArray[2] = get_object_device(); objArray[3] = get_object_firmware(); /* * The liblwm2m library is now initialized with the functions that will be in * charge of communication */ lwm2mH = lwm2m_init(prv_connect_server, prv_buffer_send, NULL); if (NULL == lwm2mH) { fprintf(stderr, "lwm2m_init() failed\r\n"); return -1; } // configure the liblwm2m lib result = lwm2m_configure(lwm2mH, "julien", NULL, 4, objArray); if (result != 0) { printf("lwm2m_configure() failed: 0x%X\n", result); return -1; } // start result = lwm2m_start(lwm2mH); if (result != 0) { printf("lwm2m_start() failed: 0x%X\n", result); return -1; } // main loop while (true) { char buffer[1024]; Endpoint server; printf("loop...\n"); struct timeval timeout; timeout.tv_sec = 10; timeout.tv_usec = 0; result = lwm2m_step(lwm2mH, &timeout); if (result != 0) { printf("lwm2m_step error %d\n", result); } int n = udp.receiveFrom(server, buffer, sizeof(buffer)); printf("Received packet from: %s of size %d\n", server.get_address(), n); if (n>0) { // TODO: find connection connection_t * connP = connList; while(connP != NULL) { if (strcmp(connP->host, server.get_address()) == 0) { printf("found connection\n"); // is it a secure connection? if (connP->dtlsSession != NULL) { printf("dtls session\n"); result = dtls_handle_message(dtls_context, connP->dtlsSession, buffer, n); printf("dtls handle message %d\n",result); } else { printf("nosec session\n"); lwm2m_handle_packet(lwm2mH, (uint8_t*)buffer, n, (void*)connP); } break; } } if (connP == NULL) printf("no connection\n"); } } }