I need to write a couple of programs to exchange commands between systems.
I chose to use TCP/IP sockets because it is supposedly less cumbersome than
starting commands through SNA DDM files (customer has a secure network).
Most of the socket programs I have been able to write in ILE-RPG (after
reading a couple of articles in the News400), but when it came to getting
the IP address for a host name using the gethostbyname API, things got too
hard for me and I haven't been able to properly execute this API.
Does anyone have a sample (RPG-IV) program that retrieves the host's IP
address based on a host name?
Thanks in advance,
Frank
There is a code samples in the ibm red book "Who Knew You
Could Do That with RPG IV?" www.redbooks.ibm.com
HTH
Dmitriy
* Sent from AltaVista http://www.altavista.com Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping. Smart is Beautiful
Sounds like fun. Here's how I do a host lookup in RPG, hope it helps:
* prototype:
D resolve_ip PR 10I 0
D Host 256A const
D IP 10U 0
D IP S 10U 0
D Char10 S 10A
D Msg S 50A
** demonstration code:
c if resolve_ip('www.as400.ibm.com': IP) < 0
c eval Msg = 'No such host found!'
c else
c move IP char10
c eval Msg = 'IP resolves to:' + char10
c endif
c dsply Msg
c eval *inlr = *on
**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
** This procedure returns the IP address of a hostname, or a
** human-readable IP address for input into the connect(),
** bind(), etc APIs
**
** Returns 0 upon success, -1 upon lookup failure
**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P resolve_ip B EXPORT
D resolve_ip PI 10I 0
D HostParm 256A const
D IP 10U 0
**
** Host Database Entry (for DNS lookups, etc)
**
** (this is a partial implementation... didn't try to
** figure out how to deal with all possible addresses
** or all possible aliases for a host in RPG)
**
** struct hostent {
** char *h_name;
** char **h_aliases;
** int h_addrtype;
** int h_length;
** char **h_addr_list;
** };
**
** #define h_addr h_addr_list[0]
**
D p_hostent S *
D hostent DS Based(p_hostent)
D h_name *
D h_aliases *
D h_addrtype 5I 0
D h_length 5I 0
D h_addrlist *
D p_h_addr S * Based(h_addrlist)
D h_addr S 10U 0 Based(p_h_addr)
** --------------------------------------------------------------------
** gethostbyname() -- Resolves a domain name to an IP address
**
** struct hostent *gethostbyname(char *host_name)
**
** struct hostent {
** char *h_name;
** char **h_aliases;
** int h_addrtype;
** int h_length;
** char **h_addr_list;
** };
**
** Returns a pointer to a host entry structure. The aliases and
** address list items in the structure are pointers to arrays of
** pointers, which are null terminated.
**
** Note: The strings & arrays used in C are often variable length,
** null-terminated entities. Be careful to only use bytes from
** the returned pointers (in the hostent data structure) to
** the first null (x'00') character.
** --------------------------------------------------------------------
D GetHostNam PR * extProc('gethostbyname')
D HostName 256A
** --------------------------------------------------------------------
** inet_addr()--Converts an address from dotted-decimal format
** to a 32-bit IP address.
**
** unsigned long inet_addr (char *address_string)
**
** Converts an IP address from format 192.168.0.100 to an
** unsigned long, such as hex x'C0A80064'.
**
** returns INADDR_NON on error.
**
** KNOWN BUG: Due to the fact that this can't return a negative value,
** it returns x'FFFFFFFF' on error. However, x'FFFFFFFF'
** is also the correct IP for the valid address of
** "255.255.255.255". (which is "worldwide broadcast")
** A reasonable workaround is to check for 255.255.255.255
** beforehand, and translate it manually rather than
** calling inet_addr.
** --------------------------------------------------------------------
D INADDR_NON C CONST(4294967295)
D inet_addr PR 10U 0 ExtProc('inet_addr')
D char_addr 16A
D Dotted S 16A
D Host S 257A
c eval Host = %trimr(HostParm) + x'00'
c eval Dotted = %trimr(%subst(Host:1:15))+x'00'
C* First check to see if this is just an IP address:
C*
c eval IP = inet_addr(Dotted)
C* If its not an IP address, try a DNS/Hosts lookup:
C if IP = INADDR_NON
c eval p_hostent = gethostnam(Host)
c if p_hostent = *NULL
C return -1
c endif
c eval IP = h_addr
c endif
c return 0
P E
--
[Please post replies to the newsgroup, I do not check this E-mail address]