DDNS use No-IP

Dependents:   TCPEchoClient-WIZwiki-W7500

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DDNSClient.cpp Source File

DDNSClient.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "DDNSClient.h"
00004 #define DEBUG
00005 DDNSClient::DDNSClient() 
00006 {
00007     noip_ddns = "dynupdate.no-ip.com";
00008     noip_ddns_port = 80;    
00009     
00010     check_ip = "checkip.dyndns.com";
00011     check_ip_port = 80; 
00012 }
00013 DDNSClient::~DDNSClient()
00014 {
00015   
00016 }
00017 void DDNSClient::updateNoIP(void)
00018 {
00019     char buf[1024]={0,};
00020 
00021 #ifdef DEBUG
00022     printf("Updating NoIP...\r\n");       
00023 #endif
00024     while (DDNSupdate.connect(noip_ddns, noip_ddns_port) < 0) {
00025     #ifdef DEBUG
00026         printf("Unable to connect to (%s) on port (%d)\r\n", noip_ddns, noip_ddns_port);
00027     #endif
00028         wait(0.5);
00029     } 
00030 #ifdef DEBUG
00031     printf("Sended message to server :\r\n");
00032 #endif  
00033     snprintf(buf, sizeof(buf), "GET /nic/update?hostname=%s&myip=%s HTTP/1.0\r\n", _hostname, _ip);                                                               
00034     DDNSupdate.send(buf, strlen(buf));
00035 #ifdef DEBUG
00036     printf("%s", buf);
00037 #endif
00038     
00039     snprintf(buf, sizeof(buf), "Host: dynupdate.no-ip.com\r\n"); //Write request                                                                 
00040     DDNSupdate.send(buf, strlen(buf));
00041 #ifdef DEBUG
00042     printf("%s", buf);
00043 #endif
00044     
00045     snprintf(buf, sizeof(buf), "Authorization: Basic %s\r\n", _idpass); //Write request                                                                 
00046     DDNSupdate.send(buf, strlen(buf));
00047 #ifdef DEBUG
00048     printf("%s", buf);
00049 #endif
00050     
00051     snprintf(buf, sizeof(buf), "User-Agent: mbed client/1.0 ricky@wiznet.co.kr\r\n\r\n"); //Write request                                                                 
00052     DDNSupdate.send(buf, strlen(buf));
00053 #ifdef DEBUG
00054     printf("%s", buf);
00055 #endif
00056 
00057 #ifdef DEBUG
00058     printf("Received message from server :\r\n");
00059 #endif
00060 
00061     wait(5.0);
00062     memset(buf, 0, sizeof(buf));
00063     DDNSupdate.receive_all(buf, sizeof(buf));
00064 #ifdef DEBUG
00065     printf("%s", buf);
00066 #endif
00067 
00068     wait(5.0);
00069     memset(buf, 0, sizeof(buf));
00070     DDNSupdate.receive_all(buf, sizeof(buf));
00071 #ifdef DEBUG
00072     printf("%s", buf);
00073 #endif
00074 
00075     DDNSupdate.close();
00076     
00077 }
00078 void DDNSClient::userSetNoIP(char* idpass, char* hostname)
00079 {
00080     _hostname = hostname;
00081     _idpass = idpass;
00082 }
00083 void DDNSClient::checkIp(void)
00084 {
00085     char buf[1024]={0,};
00086     char* tok;
00087     
00088 #ifdef DEBUG
00089     printf("Checiking IP...\r\n");       
00090 #endif
00091     while (CheckIp.connect(check_ip, check_ip_port) < 0) {
00092     #ifdef DEBUG
00093         printf("Unable to connect to (%s) on port (%d)\r\n", check_ip, check_ip_port);
00094     #endif
00095         wait(0.5);
00096     } 
00097 #ifdef DEBUG
00098     printf("Sended message to server :\r\n");
00099 #endif  
00100     snprintf(buf, sizeof(buf), "GET / HTTP/1.0\r\n", _hostname, _ip);                                                               
00101     CheckIp.send(buf, strlen(buf));
00102 #ifdef DEBUG
00103     printf("%s", buf);
00104 #endif
00105     snprintf(buf, sizeof(buf), "Host: checkip.dyndns.com\r\n\r\n");                                                               
00106     CheckIp.send(buf, strlen(buf));
00107 #ifdef DEBUG
00108     printf("%s", buf);
00109 #endif
00110 
00111 #ifdef DEBUG
00112     printf("Received message from server :\r\n");
00113 #endif
00114 
00115     wait(3.0);
00116     memset(buf, 0, sizeof(buf));
00117     CheckIp.receive_all(buf, sizeof(buf));
00118 #ifdef DEBUG
00119     printf("%s", buf);
00120 #endif
00121 
00122     tok = strstr(buf, "Address: ");
00123     tok = strtok(tok+9, "<");
00124     _ip = tok;
00125     
00126 #ifdef DEBUG
00127     printf("IP:%s", tok);
00128 #endif
00129     CheckIp.close();
00130 }