@@ -142,26 +142,39 @@ MDNSResponder::~MDNSResponder() {
142142 _answers = 0 ;
143143}
144144
145- bool MDNSResponder::begin (const char * hostname){
146- size_t n = strlen (hostname);
145+ bool MDNSResponder::begin (const char * hostName){
146+ return _begin (hostName, 0 , 120 );
147+ }
148+
149+ bool MDNSResponder::begin (const char * hostName, IPAddress ip, uint32_t ttl){
150+ return _begin (hostName, ip, ttl);
151+ }
152+
153+ bool MDNSResponder::_begin (const char *hostName, uint32_t ip, uint32_t ttl){
154+ size_t n = strlen (hostName);
147155 if (n > 63 ) { // max size for a single label.
148156 return false ;
149157 }
150158
159+ _ip = ip;
160+
151161 // Copy in hostname characters as lowercase
152- _hostName = hostname ;
162+ _hostName = hostName ;
153163 _hostName.toLowerCase ();
154164
155165 // If instance name is not already set copy hostname to instance name
156- if (_instanceName.equals (" " ) ) _instanceName=hostname ;
166+ if (_instanceName.equals (" " ) ) _instanceName=hostName ;
157167
158- _gotIPHandler = WiFi.onStationModeGotIP ([this ](const WiFiEventStationModeGotIP& event){
159- _restart ();
160- });
168+ // only if the IP hasn't been set manually, use the events
169+ if (ip == 0 ) {
170+ _gotIPHandler = WiFi.onStationModeGotIP ([this ](const WiFiEventStationModeGotIP& event){
171+ _restart ();
172+ });
161173
162- _disconnectedHandler = WiFi.onStationModeDisconnected ([this ](const WiFiEventStationModeDisconnected& event) {
163- _restart ();
164- });
174+ _disconnectedHandler = WiFi.onStationModeDisconnected ([this ](const WiFiEventStationModeDisconnected& event) {
175+ _restart ();
176+ });
177+ }
165178
166179 return _listen ();
167180}
@@ -442,7 +455,11 @@ uint16_t MDNSResponder::_getServicePort(char *name, char *proto){
442455
443456uint32_t MDNSResponder::_getOurIp (){
444457 int mode = wifi_get_opmode ();
445- if (mode & STATION_MODE){
458+
459+ // if has a manually set IP use this
460+ if (_ip){
461+ return _ip;
462+ } else if (mode & STATION_MODE){
446463 struct ip_info staIpInfo;
447464 wifi_get_ip_info (STATION_IF, &staIpInfo);
448465 return staIpInfo.ip .addr ;
0 commit comments