2015년 7월 6일 월요일

UdpNtpClient

UDP NTP Client

NTP

*Network Time Protocol - wikipedia
NTP

*참조:https://blog.cloudflare.com/understanding-and-mitigating-ntp-based-ddos-attacks/

Do UdpNtpClient

  1. Import Program NTPClinet_HelloWorld: “http://developer.mbed.org/users/embeddist/code/UdpNtpClient/
    2015-07-06_15-22-24

  2. if same program is existed, Import Name of NTPClinet_HelloWorld will be changed(2).
    2015-07-06_15-23-39

  3. Remove mbed-rtos and EthernetInterface

    • Remove mbe-rtos
      2015-07-06_15-25-15
    • Remove EethernetInterface
      2015-07-06_15-25-25
    • Directory after removing mbed-rtos & EthernetInterface
      2015-07-06_15-26-46
  4. Import WIZnetInterface”https://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/"(1)
    2015-07-06_15-28-12

  5. Dirctory for UdpNtpClient
    2015-07-06_15-29-11

  6. Make Codes: porting to WIZnetInterface from EthernetInterface

    • Add arrange for MAC address, call init(mac_addr) for initialing ethernet class.
      2015-07-06_15-47-20
  7. UDP APIs in WIZnetInterface

    • NTPClient.cpp\Bind() in UDP
      2015-07-06_15-55-19

    • NTPClient.cpp\sendTo(): send in UDP
      2015-07-06_15-52-14

    • NTPClient.cpp\receiveFrom(): recv in UDP
      2015-07-06_15-52-41

    • NTPClient.cpp\Close() in UDP
      2015-07-06_15-54-43

  8. Confirm packets by WireShark

    • NTP version 4, clinet
      2015-07-06_17-15-36
    • NTP version 4, server
      2015-07-06_17-15-50

Code Repository

I will update…

Ref. Code

#include "mbed.h"
#include "EthernetInterface.h"
#include "NTPClient.h"

EthernetInterface eth;
NTPClient ntp;

int main() 
{
    uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x01, 0x02, 0x03};

    //eth.init(); //Use DHCP
    eth.init(mac_addr); //Use DHCP

    eth.connect();

    printf("Trying to update time...\r\n");
    //if (ntp.setTime("0.pool.ntp.org") == 0)
    if (ntp.setTime("jp.pool.ntp.org") == 0)
    {
      printf("Set time successfully\r\n");
      time_t ctTime;
      ctTime = time(NULL);
      printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
    }
    else
    {
      printf("Error\r\n");
    } 

    eth.disconnect();  

    while(1) {
    }
}