Conversion between IP addresses (string and long int)

Conversion between IP addresses (string and long int)

Post by Mihai Crasnea » Tue, 30 Jul 1996 04:00:00



Hi !

Is there a function to convert a string IP address (ie x.x.x.x) to a
long int (ie 32 bits number) ?

I'm using AIX 3.2.3.

Thanks in advance.
---

 
 
 

Conversion between IP addresses (string and long int)

Post by Andrew Gier » Wed, 31 Jul 1996 04:00:00


 Mihai> Hi !  Is there a function to convert a string IP address (ie
 Mihai> x.x.x.x) to a long int (ie 32 bits number) ?

 Mihai> I'm using AIX 3.2.3.

inet_addr()

--


 
 
 

Conversion between IP addresses (string and long int)

Post by Dave Plon » Wed, 31 Jul 1996 04:00:00



> Is there a function to convert a string IP address (ie x.x.x.x) to a
> long int (ie 32 bits number) ?

I think you're looking for inet_addr(3).  Here's some fragments from
the HP-UX man page:

 SYNOPSIS
      #include <sys/socket.h>
      #include <netinet/in.h>
      #include <arpa/inet.h>

      unsigned long inet_addr(const char *cp);

 DESCRIPTION
      inet_addr()           Interpret character strings representing numbers
      inet_network()        expressed in the Internet standard ``dot''
                            notation.

                            inet_addr() returns numbers suitable for use as
                            Internet addresses.

 RETURN VALUE
      inet_addr() and inet_network() return -1 for malformed requests.

--

 
 
 

Conversion between IP addresses (string and long int)

Post by Steve Dempse » Wed, 31 Jul 1996 04:00:00



> Hi !

> Is there a function to convert a string IP address (ie x.x.x.x) to a
> long int (ie 32 bits number) ?

> I'm using AIX 3.2.3.

> Thanks in advance.
> ---


I can't speak for AIX specifically, but generally, all UNIX derivatives
should provide he following call:

        unsigned long inet_addr(char *ptr);

Just put make the string your dotted decimal address and you should get
back the 32 bit internet address.    Check it out!

--
                ^^^^
                0  0
-----------ooOO--()--OOoo-------------
            Steve Dempsey
      Lockheed Martin Corporation
    GES/Computer Systems Engineering
--------------------------------------

 
 
 

Conversion between IP addresses (string and long int)

Post by Lee Crite » Wed, 31 Jul 1996 04:00:00



> Is there a function to convert a string IP address (ie x.x.x.x) to a
> long int (ie 32 bits number) ?

> I'm using AIX 3.2.3.

Here's the tidbit of code I use.  The host name (char* host) and the
port number (int thePort) are passed into the function.

  if (host == NULL || strlen(host) < 1)
      // no name given at all, assume we connect to anything
      {
      s_use.sin_addr.s_addr = htonl(INADDR_ANY);
      }
  else
      {
      if (isdigit(host[0]))
          {
          // assume I was given a dotted quad
          s_use.sin_addr.s_addr = inet_addr(host);
          }
       else
          {
          // assume I was given a text name
          struct hostent* hp = ::gethostbyname(host);
          if (hp == 0)
              {
               // whatever error reporting you wish to do
              return(-1);
              }
          strncpy((char*)&s_use.sin_addr, hp->h_addr, hp->h_length);
          }
      }
  s_use.sin_family = AF_INET;
  s_use.sin_port = htons(thePort);

This should work for most conditions.  Of course, if your host name is
something like 9lives.com or 1way.net (starting with a numeric), then
you'll have problems.

Lee

 
 
 

Conversion between IP addresses (string and long int)

Post by Andrew Gier » Thu, 01 Aug 1996 04:00:00



 >> Is there a function to convert a string IP address (ie x.x.x.x) to
 >> a long int (ie 32 bits number) ?
 >>
 >> I'm using AIX 3.2.3.

 Lee> Here's the tidbit of code I use.  The host name (char* host) and
 Lee> the port number (int thePort) are passed into the function.
[code snipped]

 Lee> This should work for most conditions.  Of course, if your host
 Lee> name is something like 9lives.com or 1way.net (starting with a
 Lee> numeric), then you'll have problems.

The suggested way of dealing with that is to treat the name as a
name rather than a dotted-quad unless it actually does parse as a
dotted quad... that is unambiguous because you are guaranteed that
the top-level domain (.com, .net, .uk etc) will never be entirely
numeric. (ref: RFC1123 - Host Requirements: Applications).

The cleanest solution seems to be to try inet_addr, and if it fails
then use gethostbyname.

There's an example function in with the rest of the socket FAQ
code; see http://www.auroraonline.com/sock-faq

--

 
 
 

Conversion between IP addresses (string and long int)

Post by Patrick Horg » Tue, 06 Aug 1996 04:00:00




>           strncpy((char*)&s_use.sin_addr, hp->h_addr, hp->h_length);

Better make this memcpy since you can have 0 bytes in an address.

            memcpy((void*)&s_use.sin_addr, (const void *)hp->h_addr,hp->length);
--


   Opinions mine, not my employer's except by most bizarre coincidence.