@@ -274,23 +274,19 @@ static void setup_dns_hdr(uint8_t *buf, uint16_t answers)
274274static void add_answer (struct net_buf * query , enum dns_rr_type qtype ,
275275 uint32_t ttl , uint16_t addr_len , uint8_t * addr )
276276{
277- char * dot = query -> data + DNS_MSG_HEADER_SIZE ;
278- char * prev = NULL ;
277+ char * dot = query -> data + DNS_MSG_HEADER_SIZE + 1 ;
278+ char * prev = query -> data + DNS_MSG_HEADER_SIZE ;
279279 uint16_t offset ;
280280
281- while ((dot = strchr (dot , '.' ))) {
282- if (!prev ) {
283- prev = dot ++ ;
284- continue ;
285- }
281+ /* For the length of the first label. */
282+ query -> len += 1 ;
286283
284+ while ((dot = strchr (dot , '.' )) != NULL ) {
287285 * prev = dot - prev - 1 ;
288286 prev = dot ++ ;
289287 }
290288
291- if (prev ) {
292- * prev = strlen (prev ) - 1 ;
293- }
289+ * prev = strlen (prev + 1 );
294290
295291 /* terminator byte (0x00) */
296292 query -> len += 1 ;
@@ -322,14 +318,15 @@ static int create_answer(int sock,
322318 /* Prepare the response into the query buffer: move the name
323319 * query buffer has to get enough free space: dns_hdr + answer
324320 */
325- if ((net_buf_max_len (query ) - query -> len ) < (DNS_MSG_HEADER_SIZE +
321+ if ((net_buf_max_len (query ) - query -> len ) < (DNS_MSG_HEADER_SIZE + 1 +
326322 DNS_QTYPE_LEN + DNS_QCLASS_LEN +
327323 DNS_TTL_LEN + DNS_RDLENGTH_LEN +
328324 addr_len )) {
329325 return - ENOBUFS ;
330326 }
331327
332- memmove (query -> data + DNS_MSG_HEADER_SIZE , query -> data , query -> len );
328+ /* +1 for the initial label length */
329+ memmove (query -> data + DNS_MSG_HEADER_SIZE + 1 , query -> data , query -> len );
333330
334331 setup_dns_hdr (query -> data , 1 );
335332
@@ -641,7 +638,7 @@ static int dns_read(int sock,
641638 }
642639
643640 /* Handle only .local queries */
644- lquery = strrchr (result -> data + 1 , '.' );
641+ lquery = strrchr (result -> data , '.' );
645642 if (!lquery || memcmp (lquery , (const void * ){ ".local" }, 7 )) {
646643 continue ;
647644 }
@@ -654,9 +651,9 @@ static int dns_read(int sock,
654651 * We skip the first dot, and make sure there is dot after
655652 * matching hostname.
656653 */
657- if (!strncasecmp (hostname , result -> data + 1 , hostname_len ) &&
658- (result -> len - 1 ) >= hostname_len &&
659- & ( result -> data + 1 ) [hostname_len ] == lquery ) {
654+ if (!strncasecmp (hostname , result -> data , hostname_len ) &&
655+ (result -> len ) >= hostname_len &&
656+ & result -> data [hostname_len ] == lquery ) {
660657 NET_DBG ("%s %s %s to our hostname %s%s" , "mDNS" ,
661658 family == AF_INET ? "IPv4" : "IPv6" , "query" ,
662659 hostname , ".local" );
0 commit comments