The code below is suppose to print out the TXT Resource Records i have in my zone file.
When i execute the code only with BLOCK 1 (BLOCK 2 not present) i get the name, Type, Class,TTL and Data Length for each of the 3 TXT RRs i have.
But when i execute the code only with BLOCK 2, i only get the answer for the first TXT RR and then an error message: ns_parserr: Message to long.
Can somebody plese help me with this problem.
Thanks in advance.
int rrnum; /* resource record number */
ns_rr rr; /* expanded resource record */
for(rrnum = 0; rrnum < ns_msg_count(handle, ns_s_an); rrnum++)
{
//from section ns_s_an(ANSWER) take out answer number rrnum and put it in rr
if (ns_parserr(&handle, ns_s_an, rrnum, &rr)) {
fprintf(stderr, "ns_parserr: %s\n", strerror(errno));
}
if (ns_rr_type(rr)==ns_t_txt){
//BLOCK 1
char *cp;
cp=(char *)ns_rr_name(rr);
printf("CP->%s\n",(char *)cp);
int i1=ns_rr_type(rr);
printf("Type->%d\n", i1);
int i2=ns_rr_class(rr);
printf("Class->%d\n", i2);
int i3=ns_rr_ttl(rr);
printf("TTL->%d\n", i3);
int i4=ns_rr_rdlen(rr);
printf("Data Length->%d\n\n", i4);
//BLOCK 2
u_char const *rdata=ns_rr_rdata(rr);
printf("Data->%s\n",(u_char *)rdata);
char *rdatatemp;
rdatatemp=(char *)rdata;
int len=strlen(rdata);
printf("%d\n",len);
rdatatemp[strlen(rdata)-2]='\0';
printf("Data->%s\n",(u_char *)rdatatemp);
}
}
This is the result i get with the two blocks on:
vanco@vanco-laptop:~/Desktop$ gcc d2ip.c /usr/lib/libresolv.a
vanco@vanco-laptop:~/Desktop$ ./a.out www.example.com
CP->www.example.com
Type->16
Class->1
TTL->10800
Data Length->41
Data->(ver=dgw1 pre=8 id=0 name=www.example.com�
43
Data->(ver=dgw1 pre=8 id=0 name=www.example.com
ns_parserr: Message too long
You're modifying the memory pointed to by the u_char const *
returned by ns_rr_rdata(rr);
via circumventing the const
using a cast
u_char const *rdata=ns_rr_rdata(rr);
...
char *rdatatemp;
rdatatemp=(char *)rdata;
...
rdatatemp[strlen(rdata)-2]='\0';
You need to allocate a new char array and copy from rdata