Converting a hostname argv into an IP


 
Thread Tools Search this Thread
Top Forums Programming Converting a hostname argv into an IP
# 1  
Old 10-14-2005
Converting a hostname argv into an IP

Ok I've been stuck on this minor point forever and I simply cannot find a way to do this. When I use something like:

h = gethostbyname(argv[1]); // argument 2 is the host or IP

sin.sin_addr.s_addr = inet_addr(h->h_name);

it always comes up connecting to the address 255.255.255.255

I've tried the inet_ntoa function but everything I do with that keeps asking for
the in_addr type and I have no idea how to get that with gethostbyname. If anyone knows what I mean please help if you will...

Codeguru
# 2  
Old 10-14-2005
inet_addr(h->h_name)

You can't do that. h_name is the name. Look again at the docs for your structure, It can vary, but you probably have h_addr and may have a h_addr_list.
# 3  
Old 10-14-2005
I may notr understand your problem, but this works just fine for me:
Code:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>

int main(int argc, const  char **argv)
{

     struct hostent *hp;
     char **p;

     hp=gethostbyname(argv[1]);
     if (hp == NULL) {
          (void) printf("host information for %s no found \n", argv[1]);
          exit (3);
     }

     for (p = hp->h_addr_list; *p!=0;p++){
         struct in_addr in;
         char **q;

         (void)memcpy(&in.s_addr, *p, sizeof(in.s_addr));
         (void)printf("%s\t%s",inet_ntoa(in), hp->h_name);
         for (q=hp->h_aliases;*q != 0; q++)
              (void) printf("%s", *q);
        (void)putchar('\n');
     }
     return 0;
}

# 4  
Old 10-14-2005
Ok I put in that code and error checked it down to here

for (p = hp->h_addr_list; *p!=0;p++)

which gives me a segmentation fault.

I defined p as char **ipaddr and hp as struct hostent *hp. Can't see what
would give this error about it anyway. If h was null it would have exited the program already...

Codeguru

Last edited by Codeguru; 10-14-2005 at 03:42 PM..
# 5  
Old 10-14-2005
Codeguru, I have no idea what you are talking about. You seem to commenting on your own program which I cannot see. I can't even tell if it is working or not.

On the off chance that you might be complaining that Jim's code doesn't work, I tried that out. I had to add 2 include statements:
#include <stdlib.h>
#include <arpa/inet.h>
for exit() and inet_ntoa() respectively. I did not like the output so I added a space to a printf statement:
(void) printf(" %s", *q);

I do not see any other problems with Jim's code.
# 6  
Old 10-14-2005
Well, I've done all I know to do

Code:
/* Includes used for some general things */
#include <stdio.h>
#include <stdlib.h>

/* Includes used for most socket operations */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

/* Includes used for auxiliary calls: memset, etc. */
#include <strings.h>
#include <netdb.h>
#include <arpa/inet.h>

/* Defines */
#define SRV_ADDR "127.0.0.1"
#define SRV_PORT 65000
#define ECHO_DATA "Hello There - (1234567890)"
#define MAX_ECHO_LEN 180

/* The program */
int main(int argc, char **argv) {

	/* Types */
	//struct sockaddr_in sin;
      //char echo[MAX_ECHO_LEN];
	//int sock, len, i;
      //FILE *file;
      char **p;
      long int port;
      struct hostent *hp;
      
      printf("Error1\n");
      //return 0;
      /* The exe call goes like this: exe | IP address | port number | file 
         the |'s represent spaces */
      if (argc != 4)
      {
        
        printf("Invalid arguments.\n");
        return 0;
      }

      printf("Error2,%s\n",argv[1]);

      /* Sets the IP address to the first argument after the exe file call */
      hp = gethostbyname(argv[1]);

      if (hp = NULL)
      {
        printf("No IP address exists for hostname.\n");
        exit(1);
      }
      else
        printf("%s\n", hp->h_name);

      printf("Error3\n");

      for (p = hp->h_addr_list; *p!=0;p++){
        printf("Error3a\n");
	struct in_addr in;
	char **q;

        printf("Error3b\n");
	(void)memcpy(&in.s_addr, *p, sizeof(in.s_addr));
	(void)printf("%s\t%s",inet_ntoa(in), hp->h_name);
	for (q=hp->h_aliases;*q != 0; q++)
	  (void) printf("%s", *q);
        (void)putchar('\n');
      }
      printf("Error4\n");

 
      /* Sets the port number to the second argument after the exe file call */ 
      port = atoi(argv[2]);
 
      exit(0);
}

Apparently when I try to printf hp->h_name it's giving a segmentation fault and I have no idea why since there I'm using gethostbyname on the first argument. I even print out the first argument string and it is always what I entered but when I run my program, it doesn't assign anything useful to hp but your program works fine. If you can see anything I missed please let me know, thanks...

Codeguru

PS- This code of mine is severely gutted but the attempt is the same here, I' trying to get the host ip from the hostname then the next two arguments are simply the port and the file that I'm trying to send and echo but all of the echo code is taken out due to error checking on this problem.
# 7  
Old 10-14-2005
Your primary error:
if (hp = NULL)

You want:
if (hp == NULL)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

ARGV how to use it?

So i am trying to read in file readFile <GivenFile> modFile looking for a regular file under the directories in the GivenFile and print them out is my over all goal. basically I am looking for anything that looks like a directory in the given file and printing it out. Since I am trying to do... (2 Replies)
Discussion started by: squidGreen
2 Replies

2. UNIX for Advanced & Expert Users

Hostname -f hostname: Unknown host

deleted (0 Replies)
Discussion started by: hce
0 Replies

3. UNIX for Advanced & Expert Users

O argv, argv, wherefore art thou argv?

All of my machines (various open source derivatives on x86 and amd64) store argv above the stack (at a higher memory address). I am curious to learn if any systems store argv below the stack (at a lower memory address). I am particularly interested in proprietary Unices, such as Solaris, HP-UX,... (9 Replies)
Discussion started by: alister
9 Replies

4. Emergency UNIX and Linux Support

HP UX - ILO Console hostname different than Machine Hostname...

Hi All, So we added a new HP-UX 11.31 machine. Copied OS via Ignite-UX (DVD)over from this machine called machine_a. It was supposed to be named machine_c. And it is when you log in...however when I'm in the ILO console before logging in, it says: It should say: What gives? And how do... (4 Replies)
Discussion started by: zixzix01
4 Replies

5. Programming

help with C, argv

when i run my program, i have a parameter, that i want to set the value to another string i am using int main(int argc, char **argv) { char my_str=argv; printf("%s",my_str); return 0; } and i get Segmentation fault ran using ./my_prog /usr/share/dict/words hello1 ... (2 Replies)
Discussion started by: omega666
2 Replies

6. Programming

ARGV help in C

Hi, Can somehelp help how to list file in a dir? (5 Replies)
Discussion started by: Learnerabc
5 Replies

7. Shell Programming and Scripting

if #argv = (this OR that) then...

this is in one of my scripts... if ($#argv == 0) then echo 'blah bla' exit 0 endif I want it to be something like this... if ($#argv == 0 OR $argv >=3) echo 'blah bla' exit 0 endif so when the arguments are none, or greater than three I want this "if then" to take over. how? I... (5 Replies)
Discussion started by: ajp7701
5 Replies

8. Programming

help for argv argc

Hi C experts, I have the following code for adding command line option for a program int main (argc, argv) int argc; char *argv; { char *mem_type; //memory type char *name; //name of the memory int addr; //address bits int data; ... (5 Replies)
Discussion started by: return_user
5 Replies

9. UNIX for Dummies Questions & Answers

Solaris - unknown hostname - how can I change hostname?

Hello, I am new to Solaris. I am using stand alone Solaris 10.0 for test/study purpose and connecting to internet via an ADSL modem which has DHCP server. My Solaris is working on VMWare within winXP. My WinXP and Solaris connects to internet by the same ADSL modem via its DHCP at the same... (1 Reply)
Discussion started by: XNOR
1 Replies

10. Programming

argv

I have a program which I wish to modify. It used to be run from the command line, but now I wish to change this so it can be used as a function. The program has complex argument processing so I want to pass my paramters to as if it were being called by the OS as a program. I have tried to... (2 Replies)
Discussion started by: mbb
2 Replies
Login or Register to Ask a Question