Skip to content

ESP8266mDNS using the provided IP in the begin method #2349

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

Merged
merged 1 commit into from
Aug 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions libraries/ESP8266mDNS/ESP8266mDNS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,26 +142,39 @@ MDNSResponder::~MDNSResponder() {
_answers = 0;
}

bool MDNSResponder::begin(const char* hostname){
size_t n = strlen(hostname);
bool MDNSResponder::begin(const char* hostName){
return _begin(hostName, 0, 120);
}

bool MDNSResponder::begin(const char* hostName, IPAddress ip, uint32_t ttl){
return _begin(hostName, ip, ttl);
}

bool MDNSResponder::_begin(const char *hostName, uint32_t ip, uint32_t ttl){
size_t n = strlen(hostName);
if (n > 63) { // max size for a single label.
return false;
}

_ip = ip;

// Copy in hostname characters as lowercase
_hostName = hostname;
_hostName = hostName;
_hostName.toLowerCase();

// If instance name is not already set copy hostname to instance name
if (_instanceName.equals("") ) _instanceName=hostname;
if (_instanceName.equals("") ) _instanceName=hostName;

_gotIPHandler = WiFi.onStationModeGotIP([this](const WiFiEventStationModeGotIP& event){
_restart();
});
//only if the IP hasn't been set manually, use the events
if (ip == 0) {
_gotIPHandler = WiFi.onStationModeGotIP([this](const WiFiEventStationModeGotIP& event){
_restart();
});

_disconnectedHandler = WiFi.onStationModeDisconnected([this](const WiFiEventStationModeDisconnected& event) {
_restart();
});
_disconnectedHandler = WiFi.onStationModeDisconnected([this](const WiFiEventStationModeDisconnected& event) {
_restart();
});
}

return _listen();
}
Expand Down Expand Up @@ -442,7 +455,11 @@ uint16_t MDNSResponder::_getServicePort(char *name, char *proto){

uint32_t MDNSResponder::_getOurIp(){
int mode = wifi_get_opmode();
if(mode & STATION_MODE){

//if has a manually set IP use this
if(_ip){
return _ip;
} else if(mode & STATION_MODE){
struct ip_info staIpInfo;
wifi_get_ip_info(STATION_IF, &staIpInfo);
return staIpInfo.ip.addr;
Expand Down Expand Up @@ -532,7 +549,7 @@ void MDNSResponder::_parsePacket(){
tmp8 = _conn_read8();
break;
}
if (tmp8 == 0x00) { // �nd of name
if (tmp8 == 0x00) { // End of name
break;
}
_conn_readS(serviceName, tmp8);
Expand Down
7 changes: 3 additions & 4 deletions libraries/ESP8266mDNS/ESP8266mDNS.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ class MDNSResponder {
~MDNSResponder();
bool begin(const char* hostName);
//for compatibility
bool begin(const char* hostName, IPAddress ip, uint32_t ttl=120){
return begin(hostName);
}
bool begin(const char* hostName, IPAddress ip, uint32_t ttl=120);
void update();

void addService(char *service, char *proto, uint16_t port);
Expand Down Expand Up @@ -116,8 +114,9 @@ class MDNSResponder {
bool _waitingForAnswers;
WiFiEventHandler _disconnectedHandler;
WiFiEventHandler _gotIPHandler;

uint32_t _ip;

bool _begin(const char* hostName, uint32_t ip, uint32_t ttl);
uint32_t _getOurIp();
uint16_t _getServicePort(char *service, char *proto);
MDNSTxt * _getServiceTxt(char *name, char *proto);
Expand Down