Operating System - OpenVMS
1819575 Members
2774 Online
109603 Solutions
New Discussion

Re: FORTRAN using TCPIP gethostbyname

 
Frank_Gribbin
Occasional Contributor

FORTRAN using TCPIP gethostbyname

Does anyone have an example of using host name lookup in FORTRAN.

I've tried to use QIO with IO$_ACPCONTROL but get access violations. 

I've also tried to call the C gethostbyname function, but the return code is always zero.

It seems this question was asked to the openvms wizard a long time ago.

http://h71000.www7.hp.com/wizard/wiz_7265.html

I have a FORTRAN program talking to the remote server (python on a raspbrry pi) based on the example

http://h71000.www7.hp.com/wizard/wiz_2777.html

However it's frustrating to have the ip address coded into the program. I'm using FORTRAN because the existing application is FORTRAN. 

 

 

 

 

12 REPLIES 12
abrsvc
Respected Contributor

Re: FORTRAN using TCPIP gethostbyname

Post the code that gets the ACCVIO.  Usually this happens because the parameters are not passed correctly.  It may be a simple fix to your existing code.

Dan

Steven Schweda
Honored Contributor

Re: FORTRAN using TCPIP gethostbyname

   It's sure been a long time since I did anything with Fortran.  I seem
to have forgotten most of it.  The following is crude, but might be
helpful.  (I assume that some of those modern POINTER things might look
less goofy than deref(), for example.)  The QIO method would probably be
smarter.

alp $ fort ghbn
alp $ link ghbn
alp $ run ghbn
addr = 216.237.214.019


> Post the code [...]

   For us non-psychics, it's tough to guess what you did wrong without
seeing what you did.  (If I can post the above embarrassment, then you
should be able to reveal yours.)

 

      The file ghbn.for does not have a valid extension for an
      attachment and has been removed. jpg,gif,png are the valid
      extensions.

   These forums get stupider by the day.  Your Web browser may hate
this non-JPEG ".jpg" file, but it should be willing to save it.
Otherwise, it should be possible to use Wget to fetch the file.

Hoff
Honored Contributor

Re: FORTRAN using TCPIP gethostbyname

The answer that the Wizard gave is still correct.  You are using OpenVMS, which means that there simply are not examples of every call in every language.    What was available of for programming examples for OpenVMS largely disappeared off the net a decade or more ago, unfortunately.   This means learning the OpenVMS calling standard and argument passing, and how that applies to this and other calls (call by value, call by reference, descriptor by address, etc), and how to use examples of one call for some other call.    Other example calls from usenet newsgroups such as comp.os,vms, or from the OpenVMS Freeware, or elsewhere.

Here, you can also translate an application-specific logical name — not my favorite approach — or can read the IP address from a data file, and bypass the problem. 

For this case, using the OpenVMS APIs?    Fortran isn't going to want to call gethostbyname() call directly either, as that call returns a pointer to a structure.    Fortran pointers differ somewhat from how C pointers work.   (Might also need to call the CRTL initialization routine decc$crtl_init call here too, if you're heading down that path and planning to use jackets and calls into the C RTL from an application with a main program written in a language other than C.)   Which means you're either going to be using $qio calls (e.g. see 2.16.2 for the general sequence, though that example is in C, and also see some of the old discussions from comp.os.vms newsgroup) to get the data from TCP/IP Services, or you're going to be using a wrapper around the C call to dereference and pass the data to the Fortran code.   (There are examples of the latter approach posted.)

FWIW, consider avoiding the IPv4-only gethostbyname() and gethostbyaddr() calls in any new code.   The getaddrinfo() and getnameinfo() calls are the preferred approach in new code.  (While OpenVMS does now have these two C calls and related baggage, there are wrinkles, and unfortunately there does not appear to be any documented $qio function code associated with these.)

If you're just starting to learn IP networking, there are available resources.

The comp.os.vms usenet newsgroup gets more traffic than this place, too.   To access that usenet newsgroup, you can either use Google Groups (a front end which has its own issues, but the search usually works) or scrounge a newsreader for your preferred local operating system and register for a free Usenet account from one of the various providers, such as Eternal September.

Francisco Hermida
Occasional Advisor

Re: FORTRAN using TCPIP gethostbyname

Thank you Steven Schweda!, 

I was always wondering how to deal with C functions returning pointers from FORTRAN. deref() might be rough, but it is simple and it works!.

Steven Schweda
Honored Contributor

Re: FORTRAN using TCPIP gethostbyname

> I was always wondering how to deal with C functions returning pointers
> from FORTRAN. deref() might be rough, but it is simple and it works!.

   Glad to help.  (It's been so long that I can't remember where I stole
that idea.  It seems unlikely that I invented it.)  Of course, "deref"
is really a misnomer, because the real trick is the %val() in the
caller.  deref() simply returns what it gets.

   It'd be nice if someone (perhaps someone who knows something about
pointers in Fortran) could explain how to deal with a C function like
getaddrinfo(), which returns pointers in arguments rather than as the
function value.  An alternative approach would be to write a
Fortran-friendly C wrapper for getaddrinfo(), but that would require a
Fortran user to have a C compiler, too.

Mike Kier
Valued Contributor

Re: FORTRAN using TCPIP gethostbyname

Steven, I cobbled up the start of a routine that performs the appropriate pointer stuff in Fortran (using the DEC-specific POINTER (pointer,pointee), not the Fortran 95 Pointer types.  It needs more work to turn it into a callable function, and I don't have the IPV6 bits implemented, but it may work as a jumping off point or just give you ideas for your own approach.

I'll try to attach it here.

Mike

example use:

$ mcr []F_GETADDRINFO localhost
[LOCALHOST]
AI_Flags = 0
AI_Family = INET
AI_SockType = SOCK_STREAM
AI_Protocol = IP_TCP
AI_CannonName = [LOCALHOST]
SIN_Len = 16
SIN_Family = INET
SIN_Port = 0
IPV4 Address = 127.0.0.1

AI_Flags = 0
AI_Family = INET
AI_SockType = SOCK_DGRAM
AI_Protocol = IP_UDP
AI_CannonName = [LOCALHOST]
SIN_Len = 16
SIN_Family = INET
SIN_Port = 0
IPV4 Address = 127.0.0.1

Practice Random Acts of VMS Marketing
Steven Schweda
Honored Contributor

Re: FORTRAN using TCPIP gethostbyname

> Steven, I cobbled up the start of a routine [...]

   Thanks for the helpful cobbling (including the better FORMAT).  My
more stone-age variation may be attached (depending on today's forum
vagaries).

alp $ fort gai
alp $ link gai
alp $ r gai
  getaddrinfo() = 0
  ai_flags = 0
  ai_family = 2
  ai_socktype = 1
  ai_protocol = 6
  ai_addrlen = 16
  ai_canonname_p = 0
  ai_addr_p = 2196368
  ai_next_p = 2390728
addr = 216.237.214.19

Steven Schweda
Honored Contributor

Re: FORTRAN using TCPIP gethostbyname

> Post the code [...]

   Still a good idea.

> I've also tried to call the C gethostbyname function, but the return
> code is always zero.

   I can do that, too, but with my weak psychic powers, I can't say that
your (invisible) code has the same defect as my demo.  Works:

alp $ fort ghbn_1
alp $ link ghbn_1
alp $ run ghbn_1
  decc$gethostbyname() = 270352

   Fails:

alp $ fort ghbn_2
alp $ link ghbn_2
alp $ run ghbn_2
  decc$gethostbyname() = 0

   The (working) program:

alp $ type ghbn_1.for
c     Example (lame) calling gethostbyname().

      program ghbn

      implicit none

      character* 256 name
      integer* 4 he_p

      integer* 4 decc$gethostbyname

c     NUL-terminate the name.
       name = 'ftp.hpe.com'// char(0)

c     Get the address of a hostent structure.
      he_p = decc$gethostbyname( %ref( name))

      type *, " decc$gethostbyname() = ", he_p

      end

   How to make it fail:

alp $ gdiff ghbn_1.for ghbn_2.for
5c5
<       implicit none
---
> ccc       implicit none
10c10
<       integer* 4 decc$gethostbyname
---
> ccc       integer* 4 decc$gethostbyname

    That is, failing to declare decc$gethostbyname as an integer is an
easy way to get an apparently bad result.  "implicit none" was, I claim,
the best thing ever to happen to FORTRAN/Fortran.

Steven Schweda
Honored Contributor

Re: FORTRAN using TCPIP gethostbyname

> I've tried to use QIO with IO$_ACPCONTROL but get access violations.

> Post the code [...]

   Still a good idea.

   Attached should be still more code in desperate need of refinement,
this time using $QIOW to do a GETHOSTBYNAME look-up.

alp $ fort qghbn
alp $ link qghbn
alp $ run qghbn
sys$assign() =    00000001
sys$qiow() =      00000001
sys$qiow( iosb) = 00000001
  addr_len =       4
addr = 216.237.214.19

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.