Browse code

- added cname parser

Andrei Pelinescu-Onciul authored on 27/08/2002 17:32:58
Showing 3 changed files
... ...
@@ -73,7 +73,7 @@ struct srv_rdata* dns_srv_parser( unsigned char* msg, unsigned char* end,
73 73
 	if ((rdata+6)>end) goto error;
74 74
 	srv=(struct srv_rdata*)local_malloc(sizeof(struct srv_rdata));
75 75
 	if (srv==0){
76
-		LOG(L_ERR, "ERROR: dns_srv_parser: outof memory\n");
76
+		LOG(L_ERR, "ERROR: dns_srv_parser: out of memory\n");
77 77
 		goto error;
78 78
 	}
79 79
 	
... ...
@@ -84,9 +84,9 @@ struct srv_rdata* dns_srv_parser( unsigned char* msg, unsigned char* end,
84 84
 	srv->priority=ntohs(srv->priority);
85 85
 	srv->weight=ntohs(srv->weight);
86 86
 	srv->port=ntohs(srv->port);
87
-	if ((len=dn_expand(msg, end, rdata, srv->name, MAX_DNS_NAME))==-1)
87
+	if ((len=dn_expand(msg, end, rdata, srv->name, MAX_DNS_NAME-1))==-1)
88 88
 		goto error;
89
-	/* add terminatng 0 ? */
89
+	/* add terminating 0 ? (warning: len=compressed name len) */
90 90
 	return srv;
91 91
 error:
92 92
 	if (srv) local_free(srv);
... ...
@@ -95,6 +95,29 @@ error:
95 95
 
96 96
 
97 97
 
98
+/* parses a CNAME record into a cname_rdata structure */
99
+struct cname_rdata* dns_cname_parser( unsigned char* msg, unsigned char* end,
100
+									  unsigned char* rdata)
101
+{
102
+	struct cname_rdata* cname;
103
+	int len;
104
+	
105
+	cname=0;
106
+	cname=(struct cname_rdata*)local_malloc(sizeof(struct cname_rdata));
107
+	if(cname==0){
108
+		LOG(L_ERR, "ERROR: dns_cname_parser: out of memory\n");
109
+		goto error;
110
+	}
111
+	if ((len=dn_expand(msg, end, rdata, cname->name, MAX_DNS_NAME-1))==-1)
112
+		goto error;
113
+	return cname;
114
+error:
115
+	if (cname) local_free(cname);
116
+	return 0;
117
+}
118
+
119
+
120
+
98 121
 /* parses an A record rdata into an a_rdata structure
99 122
  * returns 0 on error or a dyn. alloc'ed a_rdata struct
100 123
  */
... ...
@@ -236,22 +259,16 @@ struct rdata* get_record(char* name, int type)
236 236
 		rdlength=ntohs(rdlength);
237 237
 		p+=2;
238 238
 		/* check for type */
239
+		/*
239 240
 		if (rtype!=type){
240 241
 			LOG(L_ERR, "WARNING: get_record: wrong type in answer (%d!=%d)\n",
241 242
 					rtype, type);
242 243
 			p+=rdlength;
243 244
 			continue;
244 245
 		}
245
-		/* expand the "type" record  (rdata)*/
246
-		/* print it */
247
-		/*
248
-		printf("\ntype=%d class= %d, ttl= %d, rdlength= %d\n",
249
-				rtype, class, ttl, rdlength);
250
-		for (i=0;i<rdlength;i++){
251
-			printf("%x ", *(p+i));
252
-		}
253
-		printf("\n");
254 246
 		*/
247
+		/* expand the "type" record  (rdata)*/
248
+		
255 249
 		rd=(struct rdata*) local_malloc(sizeof(struct rdata));
256 250
 		if (rd==0){
257 251
 			LOG(L_ERR, "ERROR: get_record: out of memory\n");
... ...
@@ -260,7 +277,8 @@ struct rdata* get_record(char* name, int type)
260 260
 		rd->type=rtype;
261 261
 		rd->class=class;
262 262
 		rd->ttl=ttl;
263
-		switch(type){
263
+		rd->next=0;
264
+		switch(rtype){
264 265
 			case T_SRV:
265 266
 				srv_rd= dns_srv_parser(buff.buff, end, p);
266 267
 				rd->rdata=(void*)srv_rd;
... ...
@@ -294,10 +312,17 @@ struct rdata* get_record(char* name, int type)
294 294
 				*crt=rd;
295 295
 				crt=&(rd->next);
296 296
 				break;
297
+			case T_CNAME:
298
+				rd->rdata=(void*) dns_cname_parser(buff.buff, end, p);
299
+				if(rd->rdata==0) goto error_parse;
300
+				*crt=rd;
301
+				crt=&(rd->next);
302
+				break;
297 303
 			default:
298
-				LOG(L_ERR, "BUG: get_record: unknown type %d\n", type);
304
+				LOG(L_ERR, "WARNING: get_record: unknown type %d\n", rtype);
299 305
 				rd->rdata=0;
300
-				goto error;
306
+				*crt=rd;
307
+				crt=&(rd->next);
301 308
 		}
302 309
 		
303 310
 		p+=rdlength;
... ...
@@ -55,6 +55,11 @@ struct aaaa_rdata {
55 55
 	unsigned char ip6[16];
56 56
 };
57 57
 
58
+/* cname rec. struct*/
59
+struct cname_rdata {
60
+	char name[MAX_DNS_NAME];
61
+};
62
+
58 63
 
59 64
 
60 65
 struct rdata* get_record(char* name, int type);
... ...
@@ -126,29 +126,35 @@ int main(int argc, char** argv)
126 126
 			switch(l->type){
127 127
 				case T_SRV:
128 128
 					srv=(struct srv_rdata*)l->rdata;
129
-					printf("SRV  type= %d class=%d  ttl=%d\n",
129
+					printf("SRV    type= %d class=%d  ttl=%d\n",
130 130
 							l->type, l->class, l->ttl);
131
-					printf("     prio= %d weight=%d port=%d\n",
131
+					printf("       prio= %d weight=%d port=%d\n",
132 132
 								srv->priority, srv->weight, srv->port);
133
-					printf("     name= [%s]\n", srv->name);
133
+					printf("       name= [%s]\n", srv->name);
134
+					break;
135
+				case T_CNAME:
136
+					printf("CNAME  type= %d class=%d  ttl=%d\n",
137
+							l->type, l->class, l->ttl);
138
+					printf("       name=[%s]\n", 
139
+								((struct cname_rdata*)l->rdata)->name);
134 140
 					break;
135 141
 				case T_A:
136 142
 					ip=(struct a_rdata*)l->rdata;
137
-					printf("A    type= %d class=%d  ttl=%d\n",
143
+					printf("A      type= %d class=%d  ttl=%d\n",
138 144
 								l->type, l->class, l->ttl);
139
-					printf("     ip= %d.%d.%d.%d\n",
145
+					printf("       ip= %d.%d.%d.%d\n",
140 146
 								ip->ip[0], ip->ip[1], ip->ip[2], ip->ip[3]);
141 147
 					break;
142 148
 				case T_AAAA:
143
-					printf("AAAA  type= %d class=%d  ttl=%d\n",
149
+					printf("AAAA    type= %d class=%d  ttl=%d\n",
144 150
 							l->type, l->class, l->ttl);
145
-					printf("      ip6= ");
151
+					printf("        ip6= ");
146 152
 					for(r=0;r<16;r++) 
147 153
 						printf("%x ", ((struct aaaa_rdata*)l->rdata)->ip6[r]);
148 154
 					printf("\n");
149 155
 					break;
150 156
 				default:
151
-					printf("UNKN  type= %d class=%d  ttl=%d\n",
157
+					printf("UNKN    type= %d class=%d  ttl=%d\n",
152 158
 								l->type, l->class, l->ttl);
153 159
 					printf("       rdata=%p\n", l->rdata);
154 160
 			}