Operating System - OpenVMS
1748113 Members
3438 Online
108758 Solutions
New Discussion

Re: FORTRAN using TCPIP gethostbyname

 
Frank_Gribbin
Occasional Contributor

Re: FORTRAN using TCPIP gethostbyname

Thanks to everyone who replied. I'm sorry about being silent so long. I'd expected that the system would notify me when there were updates. Anyway I got on with my own solution, which was to write a C wrapper to gethostbyname that returned the address in a form my FORTRAN could access simply.

In the hope this may be of use to someone else or provoke more comments.

I wasn't aware of getaddrinfo. I'm using an old version of TCP/IP, with an older manual!

HP TCP/IP Services for OpenVMS Alpha Version V5.4 - ECO 7
on a AlphaStation 255/233 running OpenVMS V7.3-2

To run the example

$ @Test4

You'll want to specify a host name that exists

It seems I can't attach code examples, so I put in-line

TEST.FOR

PROGRAM TEST4
*+
*
* Program : TEST4
*
* Purpose : Test host name lookup
*
* Method :
*
* Called :
*
*
*-
* History :
* 03-Aug-2016 FJG Original version
*
** Include files **
*
INCLUDE 'sys$library:ucx$inetdef.for'
*
** Arguments **
* Given
* Modified
* Returned
*
** External routines **
*
INTEGER*4 get_host_address
EXTERNAL get_host_address
*
** Local parameters **
*
PARAMETER IBUFL=80

** Local variables **
*
CHARACTER*128 HOST_NAME
INTEGER*4 HOST_ADDRESS
*

PRINT *,'Entering program test4'

HOST_NAME = 'lpas3'! //CHAR(0)

HOST_ADDRESS = get_host_address(%REF(HOST_NAME), %VAL(5))

IF (HOST_ADDRESS .NE. 0) THEN
PRINT 300, 'Host address = ',HOST_ADDRESS,
& 'Host name =', HOST_NAME
300 FORMAT(A20, Z8, A20, A40)
ELSE
PRINT *, 'Host name not resolved'
ENDIF

PRINT *,'Exiting program test4'

=================================================================================

TEST4C.C

 

/*
* Test C function to lookup host name and return IP address
* to be linked with TEST4.FOR
*/

/*
* INCLUDE FILES:
*/

#include <in.h> /* define internet related constants, */
/* functions, and structures */
#include <inet.h> /* define network address info */

#include <netdb.h> /* define network database library info */

#include <socket.h> /* define BSD 4.x socket api */
#include <stdio.h> /* define standard i/o functions */
#include <stdlib.h> /* define standard library functions */
#include <string.h> /* define string handling functions */

#include <unixio.h> /* define unix i/o */


/*
* NAMED CONSTANTS:
*/


/*
* FORWARD REFERENCES:
*/

struct hostent * gethname(char *, int );
int get_host_address(char *, int);

int get_host_address(char * name, int length)
{
printf("Entering get_host_address\nName = %s\nlength = %d\n", name, length);

struct hostent* my_host_ent;
struct in_addr my_in_addr;
int host_address;
char terminated_host_name[length+1];

strncpy(terminated_host_name, name, length);
printf("copied string\n");
terminated_host_name[length] = 0; //terminate the string

printf("terminated_host_name = %.80s\n", terminated_host_name);

my_host_ent=gethostbyname(terminated_host_name);

printf("my_host_ent = %d\n", my_host_ent);
if (my_host_ent)
{
printf("h-name = %.80s\n", my_host_ent->h_name);
printf("h-length = %d\n", my_host_ent->h_length);
printf("h_addr = %X\n", my_host_ent->h_addr);
printf("h_addrtype = %X\n", my_host_ent->h_addrtype);
memcpy( &my_in_addr, my_host_ent->h_addr, sizeof(struct in_addr) );
printf("my_in_addr = %.80s\n",inet_ntoa(my_in_addr));
host_address = my_in_addr.S_un.S_addr;
printf("host_address = %X\n",host_address);
}
printf("Exiting get_host_address\n");

return(host_address);
}

===================================================================================

TEST4.COM

$ ! build, link, run TEST4 - program to test bind lookup
$ CC/PREFIX=ALL TEST4C.C
$ FOR TEST4
$ LINK/MAP/FULL TEST4,TEST4C
$ RUN TEST4
$!
$ EXIT

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Steven Schweda
Honored Contributor

Re: FORTRAN using TCPIP gethostbyname

> [...] a C wrapper [...]

> HOST_ADDRESS = get_host_address(%REF(HOST_NAME), %VAL(5))

   That was pretty painful.  If you're going through all the work of
using a C wrapper, then it might as well be more convenient to use than
something with unnatural argument passing, and where the victim needs to
count characters.  (Nowadays, computers can do jobs like that.)  A more
(Fortran-)victim-friendly approach may be attached.  (It's always hard
to guess ahead of time which attachments will work in this (lame)
forum.)

alp $ cc ghbn_5c
alp $ fort ghbn_5f
alp $ link ghbn_5f, ghbn_5c
alp $ run ghbn_5f
Entering program test5
Entering get_host_address
Name = ftp.hpe.com

length = 11
my_host_ent = 389040
h-name = ftp.hpe.com
h-length = 4
h_addr = 1246DC
h_addrtype = 2
my_in_addr = 216.237.214.19
host_address = 13D6EDD8
Exiting get_host_address
     Host address = 13D6EDD8 Host name =ftp.hpe.com

Exiting program test5


    Note that I let the C function add the NUL terminator directly to the
Fortran name string.  It could make a local copy and NUL-terminate that,
but I was willing to trust the victim to leave room for it.  As usual,
many things are possible (and many of those don't make the victim count
characters).

Steven Schweda
Honored Contributor

Re: FORTRAN using TCPIP gethostbyname

> [...] I'm sorry about being silent so long. [...]

   I assumed that you had died.

> [...] I'd expected that the system would notify me when there were
> updates. [...]

   You need to check the "Email me when someone replies" box when
posting, or, before you get logged out automatically, the Options menu
(in every posting) offers a "Subscribe" action.

> It seems I can't attach code examples, [...]

      http://community.hpe.com/t5/x/x/td-p/6878990

As shown here, changing the file name to one of the approved (picture)
types seems to work, unless you expect your Web browser to show you the
result.  On the bright side, "Save Link As" lets you fix the name while
you save the file.  Of course, "I can't" is not a useful problem
description.

   Back when I was still trying to use these forums from a Web browser
on VMS (Alpha), a Stream_LF file was required for attachment, but I've
mostly switched to Firefox on a Mac.