@@ -274,23 +274,19 @@ static void setup_dns_hdr(uint8_t *buf, uint16_t answers)
274
274
static void add_answer (struct net_buf * query , enum dns_rr_type qtype ,
275
275
uint32_t ttl , uint16_t addr_len , uint8_t * addr )
276
276
{
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 ;
279
279
uint16_t offset ;
280
280
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 ;
286
283
284
+ while ((dot = strchr (dot , '.' )) != NULL ) {
287
285
* prev = dot - prev - 1 ;
288
286
prev = dot ++ ;
289
287
}
290
288
291
- if (prev ) {
292
- * prev = strlen (prev ) - 1 ;
293
- }
289
+ * prev = strlen (prev + 1 );
294
290
295
291
/* terminator byte (0x00) */
296
292
query -> len += 1 ;
@@ -322,14 +318,15 @@ static int create_answer(int sock,
322
318
/* Prepare the response into the query buffer: move the name
323
319
* query buffer has to get enough free space: dns_hdr + answer
324
320
*/
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 +
326
322
DNS_QTYPE_LEN + DNS_QCLASS_LEN +
327
323
DNS_TTL_LEN + DNS_RDLENGTH_LEN +
328
324
addr_len )) {
329
325
return - ENOBUFS ;
330
326
}
331
327
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 );
333
330
334
331
setup_dns_hdr (query -> data , 1 );
335
332
@@ -641,7 +638,7 @@ static int dns_read(int sock,
641
638
}
642
639
643
640
/* Handle only .local queries */
644
- lquery = strrchr (result -> data + 1 , '.' );
641
+ lquery = strrchr (result -> data , '.' );
645
642
if (!lquery || memcmp (lquery , (const void * ){ ".local" }, 7 )) {
646
643
continue ;
647
644
}
@@ -654,9 +651,9 @@ static int dns_read(int sock,
654
651
* We skip the first dot, and make sure there is dot after
655
652
* matching hostname.
656
653
*/
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 ) {
660
657
NET_DBG ("%s %s %s to our hostname %s%s" , "mDNS" ,
661
658
family == AF_INET ? "IPv4" : "IPv6" , "query" ,
662
659
hostname , ".local" );
0 commit comments