confused

L

leo

Hi,

I have a query with embedding nslookup in my program and its
behaviour. Here is the snippet. I have to collect the domainname from
a file and get the root server IP as command line args.
I then have to use nslookup to lookp the domain. my results of
nslookup seems to be different when run from a c prog and when run
from command line.

code snippet
while (fgets (domain, sizeof domain,
fileptr) != EOF)
{
if( (childpid = fork()) == 0)
{
printf("The domain is ");
fputs(domain, stdout);

//Call exec to the program
nslookup using execvp()
execl("/usr/bin/nslookup" ,
"nslookup" ,domain, argv[2],"-norecurse", NULL);
}
}
fclose( fileptr);
}

O/P for www.google.com is
#nslookup www.google.com 192.168.10.1 -norecurse

Server: 192.168.10.1
Address: 192.168.10.1#53

Non-authoritative answer:
www.google.com canonical name = www.l.google.com.
Name: www.l.google.com
Address: 74.125.91.147
Name: www.l.google.com
Address: 74.125.91.99
Name: www.l.google.com
Address: 74.125.91.104
Name: www.l.google.com
Address: 74.125.91.103


From the program
$ ./Program1 192.168.10.1
The domain is www.google.com
Server: 192.168.10.1
Address: 192.168.10.1#53

Non-authoritative answer:
Name: www.google.com\010
Address: 67.63.55.3

I see an additional \010 appended to the response which I have no
clue.

Any pointers on what could be the issue here?

Also is there any reference for constructing a DNS packet response to
nslookup ?

Thanks.
 
S

Stephen Sprunk

leo said:
I have a query with embedding nslookup in my program and its
behaviour.

That is almost always the wrong solution.
I see an additional \010 appended to the response which I have no
clue.

That is, in ASCII, a tab character. How it got into your output, I'm
not sure, but using tab characters at all is generally a bad idea.
Also is there any reference for constructing a DNS packet response to
nslookup ?

RFC 1035. However, if your system has DNS capability, you probably have
the function gethostbyname() or the improved getnameinfo(), and you are
almost certainly better off using those.

S
 
C

CBFalconer

Ian said:
leo wrote:

.... a question ...
You are confused, comp.unix.programmer would be a better place
to ask. When you do, drop the tabs from the code.

The reason being that you are using fork, childpid, and who knows
what else. These functions do not exist in standard C. Therefore
ask in a newsgroup that deals with them.

And drop the tabs from the code.
 
L

leo

... a question ...




The reason being that you are using fork, childpid, and who knows

Well I understand that there are other functions which can be used and
other forums which can answer specific queries.As far as I know I am
just trying to write a C code and thought this is the right forum to
post my questions related to C and not "C based on Standards only".
Well coming back to my question.

I have to use nslookup in this program. I just need help with the c
part of my program and not any specific function/utility which I am
using. I have got the issue of an extra characters which is getting
appended with the response and trying to fix it. Thanks Richard for
that response but why would a backspace get appended
.. I thought it was \010 which I assumed to be caused because of the
fgets function which is trying to screw up with the file content.

Is there any better function than fgets? I am using it to read a file
line by line.(the file contains list of domains). any other way to
read a file line by line and not fall in this trap?

And my mistake of not removing the tab. I noticed only after posting
it. Fork is being used so that I can treat the result and response of
fgets one by one since exec doesn't have a return value.

2nd Question
Anybody has an idea of how to create a dns response packet in C
probably using ldns package? I just need an example.
 
I

Ike Naar

code snippet
while (fgets (domain, sizeof domain, fileptr) != EOF)

This is strange; fgets never returns EOF; it returns a pointer to the
string (domain in this case) on successful completion. It returns a null
pointer on end-of-file or an error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top