|
Hi Andrew,
The problem is in the code. I'm surprised that it ever works, since there
are many problems with the way it was done. I'll point them out, if
you're interested... then I'll give you a suggested replacement at
the bottom...
On Mon, 3 Dec 2001, Andrew Borts wrote:
> hdebug dftactgrp( *no ) bnddir( 'QC2LE' )
The inet_addr prototype is okay, but if you do it this way, you'll need
to manually null-terminate the string. Unless you're stuck at an old
release of OS/400 (pre V4R2, I think) then I'd recommend saving yourself
some trouble, and using OPTIONS(*STRING) here:
> Dinet_addr PR 10U 0 ExtProc('inet_addr')
> D IPAddr * Value
>
This ("gethostbyaddr") prototype is wrong. AddrLen and AddrType are
supposed to be full integers. (i.e. they should be 10I 0, not 5I 0).
We can simplify the code greatly by changing the "HostEnt" parameter
(which is misnamed, IMHO) to be a 10U 0 passed by reference... Of
course, that'll limit us to using this code for IP Addresses, but that's
all it works with now anyway...
> DGetHostByAddr PR * ExtProc('gethostbyaddr')
> D HostEnt * Value
> D AddrLen 5I 0 Value
> D AddrType 5I 0 Value
>
> DSysInfDS SDS
> D CurDevice 244 253
>
> DRcvVar DS
> D Protocol 859 859
> D IPAddress 878 892
>
> DRcvLen S 9B 0
> DFormat S 8
> DDevice S 10
> DIPAddr S *
> DHostEnt S *
> DHostAddr S *
> DAddrLen S 5I 0
> DAddrType S 5I 0
> DHostEntS DS Based(HostEntP)
> D HostNPtr *
>
> DHostName S 64 Based(HostNameP)
>
> DHostAddrS DS Based(HostAddrP)
> D IP32Addr 10U 0
>
> DAF_INET S 5I 0 Inz(2)
> DSpaces S 64 Inz(*Blanks)
>
> DAPIError DS
> D NbrBytes 1 4B 0 Inz(15)
> D CPFID 9 15
> *****************************************************************
> C *Entry PList
> C Parm P#IPAddress 15
> C Parm P#IPName 64
>
> * Get IP-name
> c Eval IPAddress = P#IPAddress
This code makes no sense to me. Why would we want to set "HostEntP" to
point to the address of an uninitialized pointer? Whomever wrote this
was obviously confused.
> C Eval HostEntP = %Addr(HostEnt)
This is the same problem. It makes no sense whatsoever.
> C Eval HostAddrP = %Addr(HostAddr)
Two flaws with the call to inet_addr, here.
1) As written, IPAddress is not null-terminated. We either need to
increase the size of IPAddress to 16 bytes, and concatenate a
x'00' ("null") character to the end, or we need to change the
prototype for inet_addr to use options(*string)
2) IP32Addr is presently in the same area of memory as HostAddr, which
is a pointer. After this runs, HostAddr will become an invalid
pointer. Luckily, IP32Addr is only 4 bytes long, where a pointer
is 16 bytes, so we're not writing into unallocated memory, we're
just randomly corrupting a pointer :)
> C Eval IP32Addr = inet_addr(%Addr(IPAddress))
AddrLen and Addrtype should be 10I 0. Though, it wont matter for this
example, since neither "4" nor "2" will require more than 5 digits :)
> C Eval AddrLen = %Size(HostAddrS)
> C Eval AddrType = AF_INET
The prototype for GetHostByAddr is wrong, as I explained earlier.
And "HostEntP" was already set above, why are we setting it again?
(Except that the author was clearly confused above, and was setting
something that he didnt need to)
> C Eval HostEntP = GetHostByAddr(HostAddrP:
> C AddrLen:
> C AddrType)
> C HostEntP IfEq *NULL
> C Eval P#IPName = Spaces
> C Else
Here we copy 64 bytes of memory starting at "HostNPtr". However, how do
we know that HostNPtr contained 64 bytes of memory or more? We only want
to copy up til the trailing null (x'00') character, not just whatever
happens to be in memory after that. RPG makes it easy for us by
giving us the %str() BIF. But, even without that, we'd want to scan for
x'00', and copy up to that point, not just whatever the next 64 bytes
would be.
> C Eval HostNameP = HostNPtr
> C Eval P#IPName = HostName
This wouldn't be necessary if the above portion was coded correctly.
Furthermore, this won't always work -- beyond the trailing x'00', the
data is undefined. We don't know what we'll get. The original author
appears to have gotten x'00' in his tests, so he's replacing it with
spaces... but we can't rely on that always being the case!!
> C X'00':X'40' XLate P#IPName P#IPName
> C EndIf
> * Return parameters
> C Eval P#IPAddress = IPAddress
> C SetOn Lr
> C Return
So, having pointed out everything that I dont' like about that code, I'll
show you how I WOULD do it. Note that my code is a lot simpler, and IMHO
is easier to read.
I tried to keep the same variable names where I could:
hdebug dftactgrp( *no ) bnddir( 'QC2LE' )
Dinet_addr PR 10U 0 ExtProc('inet_addr')
D IPAddr * Value options(*string)
DGetHostByAddr PR * ExtProc('gethostbyaddr')
D HostEnt 10U 0
D AddrLen 10I 0 Value
D AddrType 10I 0 Value
D HostEntP S *
D hostent DS Based(HostEntP)
D h_name *
D h_aliases *
D h_addrtype 5I 0
D h_length 5I 0
D h_addrlist *
DIP32Addr S 10U 0
DAF_INET C Const(2)
*****************************************************************
C *Entry PList
C Parm P#IPAddress 15
C Parm P#IPName 64
* convert IP to binary format:
C Eval IP32Addr = inet_addr(P#IPAddress)
* do reverse DNS lookup:
C eval HostEntP = gethostbyaddr(IP32Addr:
c %size(IP32Addr):
c AF_INET)
C* set results:
c if HostEntP = *NULL
c eval P#IPName = *blanks
c else
c eval P#IPName = %str(h_name)
c endif
C SetOn Lr
C Return
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.