Skip to content

Commit

Permalink
app: fchQuery over HTTPS
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed May 30, 2021
1 parent 9d4698b commit d2b8584
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Packet encoding and decoding
* [NDNLPv2](https://redmine.named-data.net/projects/nfd/wiki/NDNLPv2)
* fragmentation and reassembly: yes, requires in-order delivery
* Nack: partial
* PIT token: yes
* PIT token: 8-octet only
* congestion mark: no
* link layer reliability: no
* Signed Interest: [v0.3 format](https://named-data.net/doc/NDN-packet-spec/0.3/signed-interest.html)
Expand Down Expand Up @@ -57,7 +57,7 @@ Application layer services
* [NDNCERT](https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3) server and client
* ESP32 only
* supported challenges: "nop" and "possession"
* [NDN-FCH](https://github.com/named-data/ndn-fch) client for connecting to the global NDN testbed
* [NDN-FCH](https://github.com/11th-ndn-hackathon/ndn-fch) client for connecting to the global NDN testbed and other connected networks
* ESP8266 and ESP32 only
* [UnixTime](https://github.com/yoursunny/ndn6-tools/blob/main/unix-time-service.md) client for time synchronization

Expand Down
15 changes: 12 additions & 3 deletions examples/PingClient/PingClient.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#include <WiFiClientSecureBearSSL.h>
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#include <WiFiClientSecure.h>
#endif
#include <esp8266ndn.h>

Expand Down Expand Up @@ -37,11 +39,18 @@ setup()
}
delay(1000);

IPAddress router = esp8266ndn::queryFchService();
if (router == IPAddress(INADDR_NONE)) {
#if defined(ARDUINO_ARCH_ESP8266)
BearSSL::WiFiClientSecure fchSocketClient;
fchSocketClient.setInsecure();
#elif defined(ARDUINO_ARCH_ESP32)
WiFiClientSecure fchSocketClient;
fchSocketClient.setInsecure();
#endif
auto fchResponse = esp8266ndn::fchQuery(fchSocketClient);
if (!fchResponse.ok) {
ESP.restart();
}
transport.beginTunnel(router);
transport.beginTunnel(fchResponse.ip);
}

void
Expand Down
33 changes: 18 additions & 15 deletions src/app/autoconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,30 @@

namespace esp8266ndn {

IPAddress
queryFchService(String serviceUri)
FchResponse
fchQuery(::WiFiClient& client, String serviceUri)
{
WiFiClient tcp;
FchResponse res;
HTTPClient http;
http.begin(tcp, serviceUri);
int httpCode = http.GET();
if (httpCode != HTTP_CODE_OK) {
LOG(serviceUri << F(" error: ") << httpCode);
return IPAddress(0, 0, 0, 0);
http.begin(client, serviceUri);

int status = http.GET();
if (status != HTTP_CODE_OK) {
LOG(serviceUri << F(" error: ") << status);
return res;
}
String response = http.getString();
LOG(serviceUri << F(" response: ") << response);

IPAddress ip;
if (!WiFi.hostByName(response.c_str(), ip)) {
String body = http.getString();
body.trim();
LOG(serviceUri << F(" body: ") << body);

if (!WiFi.hostByName(body.c_str(), res.ip)) {
LOG(F("DNS error"));
return IPAddress(0, 0, 0, 0);
return res;
}
LOG(F("DNS resolved to: ") << ip);
return ip;
LOG(F("DNS resolved to: ") << res.ip);
res.ok = true;
return res;
}

} // namespace esp8266ndn
Expand Down
15 changes: 12 additions & 3 deletions src/app/autoconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,23 @@
#include <IPAddress.h>
#include <WString.h>

class WiFiClient;

namespace esp8266ndn {

struct FchResponse
{
bool ok = false;
IPAddress ip;
};

/**
* @brief Query NDN-FCH service to find a nearby NDN router.
* @return Router IP address, or "0.0.0.0" on failure.
* @param client @c WiFiClient or @c WiFiClientSecure instance.
* @param serviceUri NDN-FCH service base URI.
*/
IPAddress
queryFchService(String serviceUri = "http://ndn-fch.named-data.net/");
FchResponse
fchQuery(::WiFiClient& client, String serviceUri = "https://fch.ndn.today/");

} // namespace esp8266ndn

Expand Down

0 comments on commit d2b8584

Please sign in to comment.