× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



On Thu, 11 Jul 2002, McBride, Lane (Artco) wrote:
>
> I am having a problem with a sub-procedure I am trying to create to get a
> host name by providing an IP address.  If anybody can spot a problem or lend
> any insights, I would appreciate it.

There are 3 problems with this code, that I can see.   One of them is
obvious, the other two will lead to the occasional "weird failures" that
will be difficult to debug.

First, the obvious one:

>
> D GetHostByAddr   PR              *   ExtProc('gethostbyaddr')
> D  IP_Address                     *
> D  AddrLen                      10I 0 Value
> D  AddrType                     10I 0 Value

The IP_Address must be passed by VALUE, since that's what the API is
expecting.  You can, if you like, pass an unsigned integer by reference,
since that way you'll be passing a pointer to an integer... but the
way you're doing it, you're passing a pointer to a pointer, and that's
not what the API wants.


The first not-so-obvious error:

>
> C                   Eval      IP32Addr = inet_addr(%Addr(InAddress))
>

You didn't null-terminate InAddress.  So, when the byte after InAddress
happens to be a zero, this code will work.  However if not, this code
could accidentally look up a different IP.

I bet you're saying "But!  I used options(*string)!".  That's nice, but
since you're passing %addr(), options(*string) won't work. Instead your
code should be fixed to read:

  C                   Eval      IP32Addr = inet_addr(%trim(InAddress))

This way, I'm removing any unwanted spaces, and I'm letting the
options(*string) all a null to the end to terminate it.  That way, I'm
conforming exactly to the specs.

Also, it's not strictly necessary, but you REALLY SHOULD be checking to
see if inet_addr returns an error.  Otherwise, you'll end up passing the
address 255.255.255.255 to the DNS server and doing a look up on it if
the IP address was not valid to begin with.

Next problem:

> C                   Return    Host

Host is a fixed 64 byte string that's based on pHostName.   pHostName
points to a VARIABLE-LENGTH host name, which is null terminated.

Let's say the returned host name is 'www.yahoo.com'  That's 14 bytes long.
gethostbyaddr() would request 15 bytes of main storage to put the string
'www.yahoo.com' + X'00'  (the x'00' is the null-terminator) into memory.
Now, when you read it, you're reading those 15 bytes, PLUS you're reading
another 49 bytes past the end of it.

If you're lucky, those 49 bytes will be empty, and you won't notice any
problem.   But, if they're not, you'll get garbage on the end of the
string.

And, worst case scenario, if those 49 bytes happen to be memory that's
allocated to a different job, your program will crash as soon as you
try to use Host for something.

Instead, what you want to do is only use the memory starting at the
pHostName pointer, and reading up until the null-terminator.   RPG
provides the %str() function to make that easy for us.

  C                   Return    %str(pHostName)

Will solve that problem nicely.  (It also eliminates the need for
the Host variable)

Good Luck!




As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 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.