Pārlūkot izejas kodu

- added cname parser

Andrei Pelinescu-Onciul 23 gadi atpakaļ
vecāks
revīzija
4c55b6cf86
3 mainītis faili ar 59 papildinājumiem un 23 dzēšanām
  1. 40 15
      resolve.c
  2. 5 0
      resolve.h
  3. 14 8
      test/dns_query.c

+ 40 - 15
resolve.c

@@ -73,7 +73,7 @@ struct srv_rdata* dns_srv_parser( unsigned char* msg, unsigned char* end,
 	if ((rdata+6)>end) goto error;
 	srv=(struct srv_rdata*)local_malloc(sizeof(struct srv_rdata));
 	if (srv==0){
-		LOG(L_ERR, "ERROR: dns_srv_parser: outof memory\n");
+		LOG(L_ERR, "ERROR: dns_srv_parser: out of memory\n");
 		goto error;
 	}
 	
@@ -84,9 +84,9 @@ struct srv_rdata* dns_srv_parser( unsigned char* msg, unsigned char* end,
 	srv->priority=ntohs(srv->priority);
 	srv->weight=ntohs(srv->weight);
 	srv->port=ntohs(srv->port);
-	if ((len=dn_expand(msg, end, rdata, srv->name, MAX_DNS_NAME))==-1)
+	if ((len=dn_expand(msg, end, rdata, srv->name, MAX_DNS_NAME-1))==-1)
 		goto error;
-	/* add terminatng 0 ? */
+	/* add terminating 0 ? (warning: len=compressed name len) */
 	return srv;
 error:
 	if (srv) local_free(srv);
@@ -95,6 +95,29 @@ error:
 
 
 
+/* parses a CNAME record into a cname_rdata structure */
+struct cname_rdata* dns_cname_parser( unsigned char* msg, unsigned char* end,
+									  unsigned char* rdata)
+{
+	struct cname_rdata* cname;
+	int len;
+	
+	cname=0;
+	cname=(struct cname_rdata*)local_malloc(sizeof(struct cname_rdata));
+	if(cname==0){
+		LOG(L_ERR, "ERROR: dns_cname_parser: out of memory\n");
+		goto error;
+	}
+	if ((len=dn_expand(msg, end, rdata, cname->name, MAX_DNS_NAME-1))==-1)
+		goto error;
+	return cname;
+error:
+	if (cname) local_free(cname);
+	return 0;
+}
+
+
+
 /* parses an A record rdata into an a_rdata structure
  * returns 0 on error or a dyn. alloc'ed a_rdata struct
  */
@@ -236,22 +259,16 @@ struct rdata* get_record(char* name, int type)
 		rdlength=ntohs(rdlength);
 		p+=2;
 		/* check for type */
+		/*
 		if (rtype!=type){
 			LOG(L_ERR, "WARNING: get_record: wrong type in answer (%d!=%d)\n",
 					rtype, type);
 			p+=rdlength;
 			continue;
 		}
-		/* expand the "type" record  (rdata)*/
-		/* print it */
-		/*
-		printf("\ntype=%d class= %d, ttl= %d, rdlength= %d\n",
-				rtype, class, ttl, rdlength);
-		for (i=0;i<rdlength;i++){
-			printf("%x ", *(p+i));
-		}
-		printf("\n");
 		*/
+		/* expand the "type" record  (rdata)*/
+		
 		rd=(struct rdata*) local_malloc(sizeof(struct rdata));
 		if (rd==0){
 			LOG(L_ERR, "ERROR: get_record: out of memory\n");
@@ -260,7 +277,8 @@ struct rdata* get_record(char* name, int type)
 		rd->type=rtype;
 		rd->class=class;
 		rd->ttl=ttl;
-		switch(type){
+		rd->next=0;
+		switch(rtype){
 			case T_SRV:
 				srv_rd= dns_srv_parser(buff.buff, end, p);
 				rd->rdata=(void*)srv_rd;
@@ -294,10 +312,17 @@ struct rdata* get_record(char* name, int type)
 				*crt=rd;
 				crt=&(rd->next);
 				break;
+			case T_CNAME:
+				rd->rdata=(void*) dns_cname_parser(buff.buff, end, p);
+				if(rd->rdata==0) goto error_parse;
+				*crt=rd;
+				crt=&(rd->next);
+				break;
 			default:
-				LOG(L_ERR, "BUG: get_record: unknown type %d\n", type);
+				LOG(L_ERR, "WARNING: get_record: unknown type %d\n", rtype);
 				rd->rdata=0;
-				goto error;
+				*crt=rd;
+				crt=&(rd->next);
 		}
 		
 		p+=rdlength;

+ 5 - 0
resolve.h

@@ -55,6 +55,11 @@ struct aaaa_rdata {
 	unsigned char ip6[16];
 };
 
+/* cname rec. struct*/
+struct cname_rdata {
+	char name[MAX_DNS_NAME];
+};
+
 
 
 struct rdata* get_record(char* name, int type);

+ 14 - 8
test/dns_query.c

@@ -126,29 +126,35 @@ int main(int argc, char** argv)
 			switch(l->type){
 				case T_SRV:
 					srv=(struct srv_rdata*)l->rdata;
-					printf("SRV  type= %d class=%d  ttl=%d\n",
+					printf("SRV    type= %d class=%d  ttl=%d\n",
 							l->type, l->class, l->ttl);
-					printf("     prio= %d weight=%d port=%d\n",
+					printf("       prio= %d weight=%d port=%d\n",
 								srv->priority, srv->weight, srv->port);
-					printf("     name= [%s]\n", srv->name);
+					printf("       name= [%s]\n", srv->name);
+					break;
+				case T_CNAME:
+					printf("CNAME  type= %d class=%d  ttl=%d\n",
+							l->type, l->class, l->ttl);
+					printf("       name=[%s]\n", 
+								((struct cname_rdata*)l->rdata)->name);
 					break;
 				case T_A:
 					ip=(struct a_rdata*)l->rdata;
-					printf("A    type= %d class=%d  ttl=%d\n",
+					printf("A      type= %d class=%d  ttl=%d\n",
 								l->type, l->class, l->ttl);
-					printf("     ip= %d.%d.%d.%d\n",
+					printf("       ip= %d.%d.%d.%d\n",
 								ip->ip[0], ip->ip[1], ip->ip[2], ip->ip[3]);
 					break;
 				case T_AAAA:
-					printf("AAAA  type= %d class=%d  ttl=%d\n",
+					printf("AAAA    type= %d class=%d  ttl=%d\n",
 							l->type, l->class, l->ttl);
-					printf("      ip6= ");
+					printf("        ip6= ");
 					for(r=0;r<16;r++) 
 						printf("%x ", ((struct aaaa_rdata*)l->rdata)->ip6[r]);
 					printf("\n");
 					break;
 				default:
-					printf("UNKN  type= %d class=%d  ttl=%d\n",
+					printf("UNKN    type= %d class=%d  ttl=%d\n",
 								l->type, l->class, l->ttl);
 					printf("       rdata=%p\n", l->rdata);
 			}