The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to read the data from an excel sheet and use those data as variable in the unix c Anne Grace UNIX for Advanced & Expert Users 1 03-03-2008 04:21 AM
how to verify that copied data to remote system is identical with local data. ynilesh Shell Programming and Scripting 3 01-31-2008 05:55 AM
Howto capture data from rs232port andpull data into oracle database-9i automatically boss UNIX for Dummies Questions & Answers 1 09-22-2007 11:35 PM
Using loop reading a file,retrieving data from data base. Sonu4lov Shell Programming and Scripting 1 01-19-2007 12:38 AM
Convert Binary data to ascii data krishna UNIX for Advanced & Expert Users 4 11-05-2004 01:12 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-20-2008
Registered User
 

Join Date: Jun 2004
Posts: 28
How to Get the hostent data

Hi,
I am trying to write a program, to find the open port on any system. Rightnow, my programm is working fine on the same system ip.

Now, i want to enhanced it to get any IP address and perform the task.
Problem is that, i have only IP address, like "111:111:111:111".
From this how to get the struct hostent *he;
I have tried with the following code

//he = gethostbyname( compName );
unsigned int addr = inet_addr("111:111:111:111");
hostent *he = gethostbyaddr((char *) &addr, 4, AF_INET);
But getgostbyaddress is returning NULL.
Any body help me.
Reply With Quote
Forum Sponsor
  #2  
Old 04-20-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
111:111:111:111 is not a valid IPv4 address. Try 127.0.0.1 instead, for a start. (That's localhost so it will connect to your own box anyway.)
Reply With Quote
  #3  
Old 04-20-2008
Registered User
 

Join Date: Jun 2004
Posts: 28
That IP addess is as exapmle. i am giving my IP address which given by ipconfig command. Assume it 72.14.253.125 .
Reply With Quote
  #4  
Old 04-20-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Seems you should put the return value from inet_addr in addr.s_addr.

./ettercap-0.5.0/src/ec_inet.c - Google Code Search
Reply With Quote
  #5  
Old 04-21-2008
Registered User
 

Join Date: Jun 2004
Posts: 28
I tried the code fromm link.
struct in_addr addr;

addr.s_addr = inet_addr( "121.243.2231.133" );
he = gethostbyaddr((char *)&addr, sizeof(struct in_addr), AF_INET);

Again he is NULL>\.
Reply With Quote
  #6  
Old 04-21-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Again, that is not a valid IP address.

I wrote a quick test program and got it to run successfully.

Code:
vnix$ ./a.out 72.14.253.125 
in_addr: 72.14.253.125
po-in-f125.google.com
Code:
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <errno.h>

int main (int argc, char *argv[])
{
  struct in_addr addr;
  struct hostent *he;
  /* printf ("sizeof struct in_addr: %i\n", sizeof(struct in_addr)); */
  printf ("in_addr: %s\n", argv[1]);
  addr.s_addr = inet_addr(argv[1]);
  if (addr.s_addr == 0 || addr.s_addr == -1)
    {
      perror ("inet_addr");
      return 1;
    }
  /* else */
  he = gethostbyaddr (&addr, 4, AF_INET);
  if (he == NULL)
    {
      perror ("gethostbyaddr");
      switch (h_errno)
	{
	case HOST_NOT_FOUND: printf ("HOST_NOT_FOUND\n"); break;
	case NO_ADDRESS: printf ("NO_ADDRESS\n"); break;
	  /* case NO_DATA: print ("NO_DATA\n"); break; */
	case NO_RECOVERY: printf ("NO_RECOVERY\n"); break;
	case TRY_AGAIN: printf ("TRY_AGAIN\n"); break;
	}
      return 1;
    }
  /* else */
  printf ("%s\n", he->h_name);
  return 0;
}
I might have left in a bit too many includes from other stuff but anyway, this compiles and runs. (Pardon the weird indentation, I didn't start configuring Emacs' indentation to my liking just for this.)
Reply With Quote
  #7  
Old 04-21-2008
Registered User
 

Join Date: Jun 2004
Posts: 28
yes, u are right. It was a typo. Thanks to point out it.
Now it's working fine...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0