mirror of https://github.com/arendst/Tasmota.git
Add telegram DNS checks
This commit is contained in:
parent
b5dd23ae9f
commit
541be4a8ab
|
@ -51,23 +51,20 @@
|
|||
#define htons(x) ( ((x)<< 8 & 0xFF00) | ((x)>> 8 & 0x00FF) )
|
||||
#endif
|
||||
|
||||
|
||||
void DNSClient::begin(const IPAddress& aDNSServer) {
|
||||
iDNSServer = aDNSServer;
|
||||
iRequestId = 0;
|
||||
}
|
||||
|
||||
void DNSClient::setTimeout(uint16_t aTimeout) {
|
||||
void DNSClient::setTimeout(uint32_t aTimeout) {
|
||||
iTimeout = aTimeout;
|
||||
}
|
||||
|
||||
int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult) {
|
||||
int ret =0;
|
||||
|
||||
// See if it's a numeric IP address
|
||||
if (aResult.fromString(aHostname)) {
|
||||
// It is, our work here is done
|
||||
return 1;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
// Check we've got a valid DNS server to use
|
||||
|
@ -75,6 +72,7 @@ int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult) {
|
|||
return INVALID_SERVER;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
// Find a socket to use
|
||||
if (iUdp.begin(1024+(millis() & 0xF)) == 1) {
|
||||
// Try up to three times
|
||||
|
@ -107,10 +105,9 @@ int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
uint16_t DNSClient::BuildRequest(const char* aName) {
|
||||
int DNSClient::BuildRequest(const char* aName) {
|
||||
// Build header
|
||||
// 1 1 1 1 1 1
|
||||
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
|
||||
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
||||
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
||||
// | ID |
|
||||
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
||||
|
@ -178,10 +175,10 @@ uint16_t DNSClient::BuildRequest(const char* aName) {
|
|||
twoByteBuffer = htons(CLASS_IN); // Internet class of question
|
||||
iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer));
|
||||
// Success! Everything buffered okay
|
||||
return 1;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress) {
|
||||
int DNSClient::ProcessResponse(uint32_t aTimeout, IPAddress& aAddress) {
|
||||
uint32_t startTime = millis();
|
||||
|
||||
// Wait for a response packet
|
||||
|
@ -236,7 +233,7 @@ uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress) {
|
|||
|
||||
// Skip over any questions
|
||||
memcpy(&staging, &header[4], sizeof(uint16_t));
|
||||
for (uint16_t i =0; i < htons(staging); i++) {
|
||||
for (uint32_t i = 0; i < htons(staging); i++) {
|
||||
// Skip over the name
|
||||
uint8_t len;
|
||||
do {
|
||||
|
@ -251,7 +248,7 @@ uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress) {
|
|||
} while (len != 0);
|
||||
|
||||
// Now jump over the type and class
|
||||
for (int i =0; i < 4; i++) {
|
||||
for (uint32_t i = 0; i < 4; i++) {
|
||||
iUdp.read(); // we don't care about the returned byte
|
||||
}
|
||||
}
|
||||
|
@ -261,7 +258,7 @@ uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress) {
|
|||
// type A answer) and some authority and additional resource records but
|
||||
// we're going to ignore all of them.
|
||||
|
||||
for (uint16_t i =0; i < answerCount; i++) {
|
||||
for (uint32_t i = 0; i < answerCount; i++) {
|
||||
// Skip the name
|
||||
uint8_t len;
|
||||
do {
|
||||
|
@ -297,7 +294,7 @@ uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress) {
|
|||
iUdp.read((uint8_t*)&answerClass, sizeof(answerClass));
|
||||
|
||||
// Ignore the Time-To-Live as we don't do any caching
|
||||
for (int i =0; i < TTL_SIZE; i++) {
|
||||
for (uint32_t i = 0; i < TTL_SIZE; i++) {
|
||||
iUdp.read(); // We don't care about the returned byte
|
||||
}
|
||||
|
||||
|
@ -323,7 +320,7 @@ uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress) {
|
|||
}
|
||||
} else {
|
||||
// This isn't an answer type we're after, move onto the next one
|
||||
for (uint16_t i =0; i < htons(header_flags); i++) {
|
||||
for (uint32_t i = 0; i < htons(header_flags); i++) {
|
||||
iUdp.read(); // we don't care about the returned byte
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
class DNSClient {
|
||||
public:
|
||||
void begin(const IPAddress& aDNSServer);
|
||||
void setTimeout(uint16_t aTimeout = 1000);
|
||||
void setTimeout(uint32_t aTimeout = 1000);
|
||||
|
||||
/* Resolve the given hostname to an IP address.
|
||||
@param aHostname Name to be resolved
|
||||
|
@ -30,8 +30,8 @@ public:
|
|||
int getHostByName(const char* aHostname, IPAddress& aResult);
|
||||
|
||||
protected:
|
||||
uint16_t BuildRequest(const char* aName);
|
||||
uint16_t ProcessResponse(uint16_t aTimeout, IPAddress& aAddress);
|
||||
int BuildRequest(const char* aName);
|
||||
int ProcessResponse(uint32_t aTimeout, IPAddress& aAddress);
|
||||
|
||||
IPAddress iDNSServer;
|
||||
uint16_t iRequestId;
|
||||
|
|
|
@ -2455,7 +2455,7 @@ void SyslogAsync(bool refresh) {
|
|||
if (!WifiHostByName(SettingsText(SET_SYSLOG_HOST), temp_syslog_host_addr)) { // If sleep enabled this might result in exception so try to do it once using hash
|
||||
TasmotaGlobal.syslog_level = 0;
|
||||
TasmotaGlobal.syslog_timer = SYSLOG_TIMER;
|
||||
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_APPLICATION "Loghost DNS resolve failed (%s). " D_RETRY_IN " %d " D_UNIT_SECOND), SettingsText(SET_SYSLOG_HOST), SYSLOG_TIMER);
|
||||
AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_APPLICATION D_RETRY_IN " %d " D_UNIT_SECOND), SYSLOG_TIMER);
|
||||
return;
|
||||
}
|
||||
syslog_host_hash = current_hash;
|
||||
|
|
|
@ -733,7 +733,11 @@ bool WifiHostByName(const char* aHostname, IPAddress& aResult) {
|
|||
// Use this instead of WiFi.hostByName or connect(host_name,.. to block less if DNS server is not found
|
||||
uint32_t dns_address = (!TasmotaGlobal.global_state.eth_down) ? Settings->eth_ipv4_address[3] : Settings->ipv4_address[3];
|
||||
DnsClient.begin((IPAddress)dns_address);
|
||||
return (1 == DnsClient.getHostByName(aHostname, aResult));
|
||||
if (DnsClient.getHostByName(aHostname, aResult) != 1) {
|
||||
AddLog(LOG_LEVEL_DEBUG, PSTR("DNS: Unable to resolve '%s'"), aHostname);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void WifiPollNtp() {
|
||||
|
@ -793,7 +797,7 @@ uint32_t WifiGetNtp(void) {
|
|||
}
|
||||
if (!WifiHostByName(ntp_server, time_server_ip)) {
|
||||
ntp_server_id++;
|
||||
AddLog(LOG_LEVEL_DEBUG, PSTR("NTP: Unable to resolve '%s'"), ntp_server);
|
||||
// AddLog(LOG_LEVEL_DEBUG, PSTR("NTP: Unable to resolve '%s'"), ntp_server);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -107,9 +107,13 @@ String TelegramConnectToTelegram(const String &command) {
|
|||
|
||||
String host = F("api.telegram.org");
|
||||
String response = "";
|
||||
IPAddress telegram_host_ip;
|
||||
if (!WifiHostByName(host.c_str(), telegram_host_ip)) {
|
||||
return response;
|
||||
}
|
||||
uint32_t tls_connect_time = millis();
|
||||
if (telegramClient->connect(host.c_str(), 443)) {
|
||||
|
||||
// if (telegramClient->connect(host.c_str(), 443)) {
|
||||
if (telegramClient->connect(telegram_host_ip, 443)) {
|
||||
// AddLog(LOG_LEVEL_DEBUG, PSTR("TGM: Connected in %d ms, max ThunkStack used %d"), millis() - tls_connect_time, telegramClient->getMaxThunkStackUse());
|
||||
|
||||
// telegramClient->println("GET /"+command); // Fails after 20210621
|
||||
|
|
Loading…
Reference in New Issue