|
On Mon, 16 Apr 2001, ram babu wrote:
> Hi,
>
> I'm new to this forum.
Hi! welcome to the forum. :)
> I'm having trouble with gethostbyname
> API.Basically i'm trying to get IP address for the Host name through
> DNS lookup.Specified DNS server address through CFGTCP option
> '12'.Also i'm not using host table entries.
It doesn't matter if you use the host table or DNS, gethostbyname()
will still work.
> I'm able to get Host name
> for the IP address by gethostbyaddr API.I wonder why this code was not
> working for me.I'm using following code to get IP address for the Host
> name.I appreciate your help.Please advise me.
This is because you've got a data structure called "HostEntData" that
you're trying to retrieve the IP address through. However, this data
structure is not correct.
I'll elaborate below:
> HDEBUG(*YES)
> DGetHostByName PR * ExtProc('gethostbyname')
> D HostName * Value
> D*
> D Host@ S *
> D HostEntData@ S * Inz
> D*
> DHostEnt DS Align Based(Host@)
> D HName@ *
> D HAliases@ *
> D HAddrType 10I 0
> D HLength 10I 0
> D HAddrList@ *
> D*
> DHostEntData DS Align Based(HostEntData@)
> D HName 256A
> D HAliasesArr@ * Dim(64)
> D HAliasesArr 256A Dim(64)
> D HAddrArr@ * Dim(101)
When you set "HostEntData@" to the value that gethostbyname will
assign to "HName@", you'll be putting "HName" in the area of memory
pointed to be "HName@". That's probably what you want to do, but
you do need to be cautious, because the data placed there by
gethostbyname isn't necessarily 256 bytes long. It might be 1 byte,
it might be 6000 bytes... you don't know. :)
After that, nothing else in the HostEntData structure makes no sense
to me. HAliasesArr@ should be placed in the area of memory pointed
to by HAliases@. Instead, however, you have it always set to be
256 bytes after HName@. HAliasesArr@ has nothing to do with HName,
it shouldn't be based on it.
Next, each element of HAliasesArr needs to contain the data pointed to
by the corresponding entry in HAliasesArr@. But, you're not doing that.
>
> D HAddrArr 10U 0 Dim(100)
> D OpenFlag 10I 0
> D F0@ *
> D FileP0 260A
> D Reserved0 150A
> D F1@ *
> D FileP1 260A
> D Reserved1 150A
> D F2@ *
> D FileP2 260A
> D Reserved2 150A
This data (above) is also in the same data structure. "HostEntData".
I don't know why... Maybe you just forgot to put the "S" in the
D-spec? At any rate, this means that it'll also be based on
"HName@", which it shouldn't be.
> D*
> DServerIP@ S *
> DServerIP S 256A Based(ServerIP@)
> DServerName S 256A
> DServer@ S *
> D*
> D*Get h_errno
> D*
> DGetH_ErrNo PR * ExtProc('__h_errno')
> D h_ErrnoPtr S *
> D h_errno S 10I 0 Based(h_ErrnoPtr)
> D*
> DIPaddr S 10U 0
> D*
> c
> c *entry plist
> c parm HostName 256
It's important to note here that "HostName" is 256 bytes, however, by
default the OS/400 command line will only allocate 32 bytes for a parm
on the "CALL" command.
Therefore, if you're going to use CALL from the command line, or from
QCMDEXC, or from a SBMJOB command, you'll have problems. To fix this,
make sure your program is called by another program, or is called using
a "*CMD" object as a front-end.
> c*
> C eval ServerName = %trim(HostName)+X'00'
> C eval Server@ =%addr(ServerName)
> C eval Host@ = GetHostByName(Server@)
> C eval h_ErrnoPtr = GetH_ErrNo
> C*
> c if h_errno = 0
> c Eval HostEntData@ = HName@
> c Eval IPaddr = HAddrArr(1)
> c Endif
> c*
> c dump
> c Seton lr
> c
It's probably not a good idea to check "h_errno", unless you know
that an error occurred. (At least, that's the way I was taught to
use it). Instead, check to see if "Host@" is *NULL. If Host@ is
*NULL, you know an error occurred, THEN check h_errno.
Here's a "fixed" version of your code...
HDEBUG(*YES) BNDDIR('QC2LE')
DGetHostByName PR * ExtProc('gethostbyname')
D HostName * Value
D*
D Host@ S *
D HostEntData@ S * Inz
D*
DHostEnt DS Align Based(Host@)
D HName@ *
D HAliases@ *
D HAddrType 10I 0
D HLength 10I 0
D HAddrList@ *
d*
D HName S 256A
D HAliasesArr@ S * DIM(64) based(HAliases@)
D HAliasesArr S 256A DIM(64)
D HAddrArr@ S * DIM(100) based(HAddrList@)
D HAddrArr S 10U 0 DIM(100)
D*
D HAddrVal S 10U 0 based(HAddrVal@)
D HAddrVal@ S *
D*
DServerIP@ S *
DServerIP S 256A Based(ServerIP@)
DServerName S 256A
DServer@ S *
D*
D*Get h_errno
D*
DGetH_ErrNo PR * ExtProc('__h_errno')
D h_ErrnoPtr S *
D h_errno S 10I 0 Based(h_ErrnoPtr)
D*
DIPaddr S 10U 0
D*
c Seton lr
c
C*
C* Note that you cannot pass HostName from the command line using
C* a CALL command, from a SBMJOB command or from QCMDEXC. To
C* call this you need a *CMD front-end or another program to
C* call it.
C*
C* If you just wish to test this from the command line, change
C* Hostname to be only 32 bytes long.
C*
c *entry plist
c parm HostName 256
c*
C eval ServerName = %trim(HostName)+X'00'
C eval Server@ =%addr(ServerName)
C eval Host@ = GetHostByName(Server@)
C if Host@ = *NULL
C eval h_ErrnoPtr = GetH_ErrNo
c dump
c return
c endif
C* Get host name pointed to by "HName@"
c eval HName = %str(HName@)
C* Get host aliases pointed to by an array of pointers:
c clear HAliasesArr
c do 64 I 3 0
c if HAliasesArr@(I) = *NULL
c leave
c endif
c eval HAliasesArr(I) = %str(HAliasesArr@(I))
c enddo
C* Get IP Addresses pointed to by an array of pointers:
c clear HAddrArr
c do 100 I
c if HAddrArr@(I) = *NULL
c leave
c endif
c eval HAddrVal@ = HAddrArr@(I)
c eval HAddrArr(I) = HAddrVal
c enddo
C* This is the first IP in the array:
c Eval IPaddr = HAddrArr(1)
c dump
Hope that helps...
+---
| This is the RPG/400 Mailing List!
| To submit a new message, send your mail to RPG400-L@midrange.com.
| To subscribe to this list send email to RPG400-L-SUB@midrange.com.
| To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.