//============================================================================= uint8* ICACHE_FLASH_ATTR web_strnstr(const uint8* buffer, const uint8* token, int len) { const uint8* p; int tokenlen = ets_strlen(token); if (tokenlen == 0) { return (uint8 *)buffer; }; for (p = buffer; *p && (p + tokenlen <= buffer + len); p++) { if ((*p == *token) && (ets_strncmp(p, token, tokenlen) == 0)) { return (uint8 *)p; }; }; return NULL; }
int strncmp(const char *s1, const char *s2, size_t len) { return ets_strncmp(s1, s2, len); }
static void ICACHE_FLASH_ATTR got_mdns_packet(void *arg, char *pusrdata, unsigned short len) { int i, j, stlen; char path[MAX_MDNS_PATH]; uint16_t * psr = (uint16_t*)pusrdata; uint16_t xactionid = HTONS( psr[0] ); uint16_t flags = HTONS( psr[1] ); uint16_t questions = HTONS( psr[2] ); uint16_t answers = HTONS( psr[3] ); uint8_t * dataptr = (uint8_t*)pusrdata + 12; uint8_t * dataend = dataptr + len; if( flags & 0x8000 ) { //Response //Unused; MDNS does not fit the browse model we want to use. } else { //Query for( i = 0; i < questions; i++ ) { uint8_t * namestartptr = dataptr; //Work our way through. dataptr = ParseMDNSPath( dataptr, path, &stlen ); if( dataend - dataptr < 10 ) return; if( !dataptr ) { return; } int pathlen = ets_strlen( path ); if( pathlen < 6 ) { continue; } if( strcmp( path + pathlen - 6, ".local" ) != 0 ) { continue; } uint16_t record_type = ( dataptr[0] << 8 ) | dataptr[1]; uint16_t record_class = ( dataptr[2] << 8 ) | dataptr[3]; const char * path_first_dot = path; const char * cpp = path; while( *cpp && *cpp != '.' ) cpp++; int dotlen = 0; if( *cpp == '.' ) { path_first_dot = cpp+1; dotlen = path_first_dot - path - 1; } else path_first_dot = 0; int found = 0; for( i = 0; i < MAX_MDNS_NAMES; i++ ) { //Handle [hostname].local, or [hostname].[service].local if( MDNSNames[i] && dotlen && ets_strncmp( MDNSNames[i], path, dotlen ) == 0 && dotlen == ets_strlen( MDNSNames[i] )) { found = 1; if( record_type == 0x0001 ) //A Name Lookup. SendOurARecord( namestartptr, xactionid, stlen ); else SendSpecificService( i, namestartptr, xactionid, stlen, 1 ); } } if( !found ) //Not a specific entry lookup... { //Is this a browse? if( ets_strcmp( path, "_services._dns-sd._udp.local" ) == 0 ) { SendAvailableServices( namestartptr, xactionid, stlen ); } else { //A specific service? for( i = 0; i < MAX_MDNS_SERVICES; i++ ) { const char * srv = MDNSServices[i]; if( !srv ) continue; int sl = ets_strlen( srv ); if( strncmp( path, srv, sl ) == 0 ) { SendSpecificService( i, namestartptr, xactionid, stlen, 0 ); } } } } } } }