gethostbyname strange phenomena

Started by
5 comments, last by P-Dog 21 years, 9 months ago
Here''s the situation. In C, normally when one wants to get an IP adddress they would do a gethostbyname call and use the returning structure to access the ip field. However... on some systems one can alter the /etc/hosts file to change the hostname (mild change, let''s say change your server name from xname to xname.server.com). Once this is changed, gethostbyname seems to not work anymore. Can anyone think of anyways to get around this, or an explanation of why? Thanks.
Advertisement
I don''t understand the problem. You want to bypass a hosts file lookup and go straight to DNS? That can be fooled also.

What exactly are you trying to prevent? People who are trying to fool your program, or people who mess up their own system on ''accident''?
How about people who are trying to mess up their own system.
Well, on NT / W2K systems the /windows/system32/drivers/etc directory is secured and only administrators can modify files in there. Are you talking about a specific computer?

Or are you looking for a way to protect this change on any computer that your software runs on? If that''s the case, I think you''re out of luck. And besides, if they don''t do it by changing the hosts file, they can always fool with the IP routing to either make your connection fail or route to a different machine.

I''m assuming you''re trying to do something that someone would want to disable on purpose? What exactly are you looking to do that you have to worry about hosts files?
Hhaha, I should have mentioned something vital first... this is on Sun/Solaris workstations.

Basically, the actual complete problem (Which i didn''t mention before) is this:

There is some password encrypted software that needs to access a centralized server for the password. Software still runs on client side tho.

This wasn''t any problem until one of the client side machines decided to change their loghost name in the host file (/etc/hosts). Example: previous name would be like ''computername'' and the new changed loghost name would be ''computername.theirserver.com''

After this change, gethostbyname returns null everytime we want to find a hostname (especially important if we want to get that password from the centralized server).

I guess I''m not looking for a way to protect this kind of change; it''s more I''m looking for a way to still successfully get a host''s ip address afterwards.

Maybe it''s not enough coffee for me, but if I am still unclear, please let me know. Thanks, btw.
In this situation, it can be assumed that that central server isn''t going to be randomly changing it''s IP. You could always include some option for the user to manually enter the IP address and avoid the hostname look up all together (Of course, you would want to store it, unless you really don''t like this person. Then you can store it on a stickynote.)
simply, its on you to change the host the software looks for. if the host changes its name (computername is a netbios name and bad anyway) you need to change your software to reflect this. there is nothing you can do about it. ppl change stuff, complain to the guy who changed it, i assume this is at an office. if its not and the guy owns the computer, then its YOUR problem (it is in either case).

you could use the ip address instead, no host name problems then. though its more likly the ip address will change if DHCP is being used on the network.

if this is on your machine and it cant reach an dns servers thus ALL host lookups fail. call tech support and get the pc fixed. changing the hostname dont affect the dns queries.

most likly they change /etc/hosts to match the dns name so things are consistent.

basically to me it sounds like an admin was told to switch things for consistency and forgot to tell you so you could update things.

i also assume that theirserver.com is actually the domain for the network or company. if this is a work releated thing, speak to managment or tech support or the admin. find out why things changed. was this the only pc that changes? did other pcs change? was this the only one not in the x.x.x format? or was this the only one that you could access in the x format? can you get the admin to add both names to the host table?

in any case the host table is a local thing for dns servers so they dont have to go to another server to get the ip. you could set your machine up to query its dns server first (ie run a dns server on localhost and have it forward to the proper dns server) then add the host entry you want. of course this assumes the ip dont change which brings it full circle to just use the ip if its static (make sure its static from the network admins).

in any case, its most likly yoru problem and you need to modify your softare to deal with the name change.

This topic is closed to new replies.

Advertisement