c - strlen() Refuses to Read String from struct hostent * -


I am working through a small tutorial about creating a basic packet sniffer for Linux. I am getting everything, and now I want to add an IP-to-host mapping.

Before I add this function:

  Zero IPtoHostname (char * ipaddress, char * hostname) {struct hostent * host; In_addr_t ip = inet_addr (ipaddress); If (hostname) {puts ("can not allocate memory ..."); Exit (-1); } Host = gethostbyaddr ((Four *) and IP, 32, AF_INET); Hostname = Stardup ​​(host-> H_NA); }  

It basically takes a string IP address ("192.168.28.18") ipaddress and the hostname of that IP ("who.cares.com" ) Fills in hostname .

What happens strlen Refus to give me anything (I know that strdup works, and I have tested this myself) and segfaults. I have used GDB, and the string ends in an empty character and it is not a null.

I have also used a raw string assignment with a string string:

  Zero IPtoHostname (char * ipaddress, char * hostname) {Static Stretch Hostent * Host ; In_addr_t ip = inet_addr (ipaddress); If (hostname) {puts ("can not allocate memory ..."); Exit (-1); } Host = gethostbyaddr ((Four *) and IP, 32, AF_INET); Hostname = host-> H_name;     

Div class = "post-text" itemprop = "text">

Nothing is strlen with you pass in four ** hostnames Then, set hostname-> hostname> hostname *, assuming that you are straining your IPTO hostname outside. You are setting a local copy of your hostname pointer.

So you have something like this:

  char myip [] = "123.45.67.89"; Char * myhost = NULL; IPToHostName (MIPE, MAHOST); / * It sets its local copy of myhost, which is on the stack * / / * At this point, the myhost is still empty !! * /  

If you change it like a code below, then maybe you will do whatever you want.

  Zero IPtoHostname (char * ipaddress, char) ** Hostname) {emphasis (hostname); / * You will need to include assert.h for this - if the hostname is the null * / struct host * host then it will cancel your program in debug mode; In_addr_t ip = inet_addr (ipaddress); If (hostname) {puts ("can not allocate memory ..."); Exit (-1); } Host = gethostbyaddr ((Four *) and IP, 32, AF_INET); * Hostname = strdup (host-> h_name); } Four mimes [] = "123.45.67.8 9"; Char * myhost = NULL; IPHostNames (MIP, and MAHOST);  

Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -