Browse code

dns: minor fixes: long lines, unlikely(), end of msg checks

- check if the rdlength is valid (points inside the answer),
before skipping over a record
- added unlikely() for error checks
- cosmetic: broken some long lines

Andrei Pelinescu-Onciul authored on 30/03/2009 13:50:36
Showing 1 changed files
... ...
@@ -523,22 +523,22 @@ struct rdata* get_record(char* name, int type, int flags)
523 523
 	fullname_rd=0;
524 524
 
525 525
 	size=res_search(name, C_IN, type, buff.buff, sizeof(buff));
526
-	if (size<0) {
526
+	if (unlikely(size<0)) {
527 527
 		DBG("get_record: lookup(%s, %d) failed\n", name, type);
528 528
 		goto not_found;
529 529
 	}
530
-	else if (size > sizeof(buff)) size=sizeof(buff);
530
+	else if (unlikely(size > sizeof(buff))) size=sizeof(buff);
531 531
 	head=rd=0;
532 532
 	last=crt=&head;
533 533
 	
534 534
 	p=buff.buff+DNS_HDR_SIZE;
535 535
 	end=buff.buff+size;
536
-	if (p>=end) goto error_boundary;
536
+	if (unlikely(p>=end)) goto error_boundary;
537 537
 	qno=ntohs((unsigned short)buff.hdr.qdcount);
538 538
 
539 539
 	for (r=0; r<qno; r++){
540 540
 		/* skip the name of the question */
541
-		if ((p=dns_skipname(p, end))==0) {
541
+		if (unlikely((p=dns_skipname(p, end))==0)) {
542 542
 			LOG(L_ERR, "ERROR: get_record: skipname==0\n");
543 543
 			goto error;
544 544
 		}
... ...
@@ -547,7 +547,7 @@ struct rdata* get_record(char* name, int type, int flags)
547 547
 		for (;(p<end && (*p)); p++);
548 548
 		p+=1+2+2; /* skip the ending  '\0, QCODE and QCLASS */
549 549
 	#endif
550
-		if (p>end) {
550
+		if (unlikely(p>end)) {
551 551
 			LOG(L_ERR, "ERROR: get_record: p>=end\n");
552 552
 			goto error;
553 553
 		}
... ...
@@ -562,20 +562,21 @@ again:
562 562
 			goto error;
563 563
 		}
564 564
 #else
565
-		if ((skip=dn_expand(buff.buff, end, p, rec_name, MAX_DNS_NAME-1))==-1){
565
+		if (unlikely((skip=dn_expand(buff.buff, end, p, rec_name,
566
+							MAX_DNS_NAME-1))==-1)){
566 567
 			LOG(L_ERR, "ERROR: get_record: dn_expand(rec_name) failed\n");
567 568
 			goto error;
568 569
 		}
569 570
 #endif
570 571
 		p+=skip;
571 572
 		rec_name_len=strlen(rec_name);
572
-		if (rec_name_len>255){
573
+		if (unlikely(rec_name_len>255)){
573 574
 			LOG(L_ERR, "ERROR: get_record: dn_expand(rec_name): name too"
574 575
 					" long  (%d)\n", rec_name_len);
575 576
 			goto error;
576 577
 		}
577 578
 		/* check if enough space is left for type, class, ttl & size */
578
-		if ((p+2+2+4+2)>end) goto error_boundary;
579
+		if (unlikely((p+2+2+4+2)>end)) goto error_boundary;
579 580
 		/* get type */
580 581
 		memcpy((void*) &rtype, (void*)p, 2);
581 582
 		rtype=ntohs(rtype);
... ...
@@ -592,6 +593,7 @@ again:
592 592
 		memcpy((void*)&rdlength, (void*)p, 2);
593 593
 		rdlength=ntohs(rdlength);
594 594
 		p+=2;
595
+		if (unlikely((p+rdlength)>end)) goto error_boundary;
595 596
 		if ((flags & RES_ONLY_TYPE) && (rtype!=type)){
596 597
 			/* skip */
597 598
 			p+=rdlength;
... ...
@@ -599,7 +601,8 @@ again:
599 599
 		}
600 600
 		/* expand the "type" record  (rdata)*/
601 601
 		
602
-		rd=(struct rdata*) local_malloc(sizeof(struct rdata)+rec_name_len+1-1);
602
+		rd=(struct rdata*) local_malloc(sizeof(struct rdata)+rec_name_len+
603
+										1-1);
603 604
 		if (rd==0){
604 605
 			LOG(L_ERR, "ERROR: get_record: out of memory\n");
605 606
 			goto error;
... ...
@@ -615,13 +618,13 @@ again:
615 615
 		if ((search_list_used==1)&&(fullname_rd==0)&&
616 616
 				(rec_name_len>=name_len)&&
617 617
 				(strncasecmp(rec_name, name, name_len)==0)) {
618
-			/* now we have record which's name is the same (up-to the name_len
619
-			 * with the searched one
620
-			 * if the length is the same - we found full match, no fake cname
621
-			 *   needed, just clear the flag
622
-			 * if the length of the name differs - it has matched using search list
623
-			 *   remember the rd, so we can create fake CNAME record when all answers
624
-			 *   are used and no better match found
618
+			/* now we have record whose name is the same (up-to the
619
+			 * name_len with the searched one):
620
+			 * if the length is the same - we found full match, no fake
621
+			 *  cname needed, just clear the flag
622
+			 * if the length of the name differs - it has matched using
623
+			 *  search list remember the rd, so we can create fake CNAME
624
+			 *  record when all answers are used and no better match found
625 625
 			 */
626 626
 			if (rec_name_len==name_len)
627 627
 				search_list_used=0;
... ...
@@ -638,7 +641,7 @@ again:
638 638
 			case T_SRV:
639 639
 				srv_rd= dns_srv_parser(buff.buff, end, p);
640 640
 				rd->rdata=(void*)srv_rd;
641
-				if (srv_rd==0) goto error_parse;
641
+				if (unlikely(srv_rd==0)) goto error_parse;
642 642
 				
643 643
 				/* insert sorted into the list */
644 644
 				for (crt=&head; *crt; crt= &((*crt)->next)){
... ...
@@ -652,34 +655,35 @@ again:
652 652
 						goto skip;
653 653
 					}
654 654
 				}
655
-				last=&(rd->next); /*end of for => this will be the last elem*/
655
+				last=&(rd->next); /*end of for => this will be the last
656
+									element*/
656 657
 			skip:
657 658
 				/* insert here */
658 659
 				rd->next=*crt;
659 660
 				*crt=rd;
660
-				
661 661
 				break;
662 662
 			case T_A:
663 663
 				rd->rdata=(void*) dns_a_parser(p,end);
664
-				if (rd->rdata==0) goto error_parse;
665
-				*last=rd; /* last points to the last "next" or the list head*/
664
+				if (unlikely(rd->rdata==0)) goto error_parse;
665
+				*last=rd; /* last points to the last "next" or the list
666
+							 	head*/
666 667
 				last=&(rd->next);
667 668
 				break;
668 669
 			case T_AAAA:
669 670
 				rd->rdata=(void*) dns_aaaa_parser(p,end);
670
-				if (rd->rdata==0) goto error_parse;
671
+				if (unlikely(rd->rdata==0)) goto error_parse;
671 672
 				*last=rd;
672 673
 				last=&(rd->next);
673 674
 				break;
674 675
 			case T_CNAME:
675 676
 				rd->rdata=(void*) dns_cname_parser(buff.buff, end, p);
676
-				if(rd->rdata==0) goto error_parse;
677
+				if(unlikely(rd->rdata==0)) goto error_parse;
677 678
 				*last=rd;
678 679
 				last=&(rd->next);
679 680
 				break;
680 681
 			case T_NAPTR:
681 682
 				rd->rdata=(void*) dns_naptr_parser(buff.buff, end, p);
682
-				if(rd->rdata==0) goto error_parse;
683
+				if(unlikely(rd->rdata==0)) goto error_parse;
683 684
 				*last=rd;
684 685
 				last=&(rd->next);
685 686
 				break;
... ...
@@ -697,7 +701,8 @@ again:
697 697
 		flags&=~RES_AR;
698 698
 		answers_no=ntohs((unsigned short)buff.hdr.nscount);
699 699
 #ifdef RESOLVE_DBG
700
-		DBG("get_record: skipping %d NS (p=%p, end=%p)\n", answers_no, p, end);
700
+		DBG("get_record: skipping %d NS (p=%p, end=%p)\n", answers_no, p,
701
+				end);
701 702
 #endif
702 703
 		for (r=0; (r<answers_no) && (p<end); r++){
703 704
 			/* skip over the ns records */
... ...
@@ -706,13 +711,14 @@ again:
706 706
 				goto error;
707 707
 			}
708 708
 			/* check if enough space is left for type, class, ttl & size */
709
-			if ((p+2+2+4+2)>end) goto error_boundary;
709
+			if (unlikely((p+2+2+4+2)>end)) goto error_boundary;
710 710
 			memcpy((void*)&rdlength, (void*)p+2+2+4, 2);
711 711
 			p+=2+2+4+2+ntohs(rdlength);
712 712
 		}
713 713
 		answers_no=ntohs((unsigned short)buff.hdr.arcount);
714 714
 #ifdef RESOLVE_DBG
715
-		DBG("get_record: parsing %d ARs (p=%p, end=%p)\n", answers_no, p, end);
715
+		DBG("get_record: parsing %d ARs (p=%p, end=%p)\n", answers_no, p,
716
+				end);
716 717
 #endif
717 718
 		goto again; /* add also the additional records */
718 719
 	}
... ...
@@ -723,7 +729,7 @@ again:
723 723
 	 */
724 724
 	if ((search_list_used==1)&&(fullname_rd!=0)) {
725 725
 		rd=(struct rdata*) local_malloc(sizeof(struct rdata)+name_len+1-1);
726
-		if (rd==0){
726
+		if (unlikely(rd==0)){
727 727
 			LOG(L_ERR, "ERROR: get_record: out of memory\n");
728 728
 			goto error;
729 729
 		}
... ...
@@ -735,13 +741,15 @@ again:
735 735
 		rd->name[name_len]=0;
736 736
 		rd->name_len=name_len;
737 737
 		/* alloc sizeof struct + space for the null terminated name */
738
-		rd->rdata=(void*)local_malloc(sizeof(struct cname_rdata)-1+head->name_len+1);
739
-		if(rd->rdata==0){
738
+		rd->rdata=(void*)local_malloc(sizeof(struct cname_rdata)-1+
739
+										head->name_len+1);
740
+		if(unlikely(rd->rdata==0)){
740 741
 			LOG(L_ERR, "ERROR: get_record: out of memory\n");
741 742
 			goto error_rd;
742 743
 		}
743 744
 		((struct cname_rdata*)(rd->rdata))->name_len=fullname_rd->name_len;
744
-		memcpy(((struct cname_rdata*)(rd->rdata))->name, fullname_rd->name, fullname_rd->name_len);
745
+		memcpy(((struct cname_rdata*)(rd->rdata))->name, fullname_rd->name,
746
+				fullname_rd->name_len);
745 747
 		((struct cname_rdata*)(rd->rdata))->name[head->name_len]=0;
746 748
 		head=rd;
747 749
 	}