Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESPAsyncTCP connect to dns hostname from async callback fails #17

Closed
marvinroger opened this issue Aug 17, 2016 · 6 comments
Closed

ESPAsyncTCP connect to dns hostname from async callback fails #17

marvinroger opened this issue Aug 17, 2016 · 6 comments

Comments

@marvinroger
Copy link

Moving the issue from marvinroger/async-mqtt-client#18, as requested :)

@me-no-dev
Copy link
Owner

done :) cc4d1ac
give it a go ;)
BTW i'm looking into running axTLS async (no success yet)

@marvinroger
Copy link
Author

Fixed, thanks!
Oh you'll succeed, I am not worried. axTLS 2.0 brings TLS 1.2 moreover, so perfect timing. :)

@me-no-dev
Copy link
Owner

@marvinroger check this out: 110a997

client->connect(host, port, true) for SSL

@me-no-dev
Copy link
Owner

requires latest git of ESP8266 arduino

@me-no-dev
Copy link
Owner

me-no-dev commented Aug 22, 2016

#include <time.h>

extern "C" void system_set_os_print(uint8 onoff);
//call after wifi is up to set the time for ssl
bool initTime(){
  time_t rawtime;
  configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  uint8_t i = 0;
  system_set_os_print(0);
  while(time(&rawtime) == 0 && i++ < 100) delay(10);
  system_set_os_print(1);
  if(i==100){
    Serial.println("Time Init Failed");
    return false;
  }
  return true;
}

#include "include/ssl.h"
static AsyncClient * aClient = NULL;

const char *sslHost = "192.168.254.16";
const uint16_t sslPort = 443;

static void securePrintInfo(SSL *ssl){
  if(!ssl){
    return;
  }
  const char *common_name = ssl_get_cert_dn(ssl, SSL_X509_CERT_COMMON_NAME);
  if (common_name) {
    Serial.printf("COMMON NAME: %s\r\n", common_name);
  }

  Serial.printf("CIPHER: ");
  switch (ssl_get_cipher_id(ssl)){
    case SSL_AES128_SHA:
      Serial.printf("AES128-SHA");
      break;

    case SSL_AES256_SHA:
      Serial.printf("AES256-SHA");
      break;

    case SSL_RC4_128_SHA:
      Serial.printf("RC4-SHA");
      break;

    case SSL_RC4_128_MD5:
      Serial.printf("RC4-MD5");
      break;

    default:
      Serial.printf("Unknown - %d", ssl_get_cipher_id(ssl));
      break;
  }

  Serial.printf("\r\n");

  int i;
  const uint8_t *session_id = ssl_get_session_id(ssl);
  int sess_id_size = ssl_get_session_id_size(ssl);
  if (sess_id_size > 0){
    Serial.printf("SESSION: ");
    for (i = 0; i < sess_id_size; i++){
      Serial.printf("%02x", session_id[i]);
    }
    Serial.printf("\r\n");
  }
}

void runAsyncClient(){
  if(aClient)//client already exists
    return;

  aClient = new AsyncClient();
  if(!aClient)//could not allocate client
    return;

  aClient->onError([](void * arg, AsyncClient * client, int error){
    Serial.println("Connect Error");
    aClient = NULL;
    delete client;
  }, NULL);

  aClient->onConnect([](void * arg, AsyncClient * client){
    Serial.println("Connected");
    securePrintInfo(client->getSSL());
    aClient->onError(NULL, NULL);

    client->onDisconnect([](void * arg, AsyncClient * c){
      Serial.println("Disconnected");
      aClient = NULL;
      delete c;
    }, NULL);

    client->onData([](void * arg, AsyncClient * c, void * data, size_t len){
      Serial.print("\r\nData: ");
      Serial.println(len);
      uint8_t * d = (uint8_t*)data;
      for(size_t i=0; i<len;i++)
        Serial.write(d[i]);
    }, NULL);

    //send the request
    char m[256];
    sprintf(m, "GET /test.htm HTTP/1.0\r\nHost: %s\r\n\r\n", sslHost);
    int wrote = client->write(m, strlen(m));
    Serial.printf("Sent: %u => %d\r\n", strlen(m), wrote);
  }, NULL);

  if(!aClient->connect(sslHost, sslPort, true)){
    Serial.println("Connect Fail");
    AsyncClient * client = aClient;
    aClient = NULL;
    delete client;
  }
}

@marvinroger
Copy link
Author

Awesome! I will play with this as soon as I am back from vacation. Well done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants