星期三, 11月 01, 2023

2002-06-04 如何由已知的 Host Name 取得該 Host 的 IP address ?


如何由已知的 Host Name 取得該 Host 的 IP address ?

可藉由 C Library 的 inet_ntoa 及gethostbyname 取得。


File  : QRPGLESRC 
Member: GETHOSTADR
Type  : RPGLE
Usage : CRTBNDRPG GETHOSTADR


     hdebug dftactgrp( *no ) bnddir( 'QC2LE' )

     D inet_ntoa       PR              *   ExtProc('inet_ntoa')
     D  internet_addr                10U 0 value

     D GetHostByName   PR              *   extproc('gethostbyname')
     D   host_name                     *   value options(*string)

     DSysInfDS        SDS
     D CurDevice             244    253

     DMsg              S             52
     DIP               S             10U 0
     Dhost             S             64

     D p_hostent       S               *
     D hostentn        DS                  Based(p_hostent)
     D   h_name                        *
     D   h_aliases                     *
     D   h_addrtype                  10I 0
     D   h_length                    10I 0
     D   h_addr_list                   *
     D p_h_addr        S               *   Based(h_addr_list)
     D h_addr          S             10U 0 Based(p_h_addr)

      *****************************************************************
     C     *Entry        PList
     C                   Parm                    P#IPNAME         48
     C                   Parm                    P#IP             64
     C*************************************************
     C* Get the 32-bit network IP address for the host
     C*  that was supplied by the user:
     C*************************************************
     c                   eval      host     = P#IPNAME
     c                   eval      p_hostent  = gethostbyname(%trim(host))
     c                   if        p_hostent  = *NULL
     c                   eval      msg = 'Unable to find that host!'
     c                   dsply                   msg
     c                   return
     c                   endif
     c                   eval      IP       = h_addr
     c*                  endif

     C                   Eval      P#IP = %str(inet_ntoa(IP))
     C     X'00':X'40'   XLate     P#IP          P#IP

     C                   SetOn                                        Lr
     C                   Return


File  : QCLSRC
Member: GETHOSTADC
Type  : CLP
Usage : CRTCLPGM GETHOSTADC
        CALL PGM(GETHOSTADC) PARM('xxx.xxx.xxx.xxx')
        xxx.xxx.xxx.xxx -> Host Name


   PGM        PARM(&IP)
   DCL        VAR(&IP) TYPE(*CHAR) LEN(48)
   DCL        VAR(&HOST) TYPE(*CHAR) LEN(64)
   CALL       PGM(GETHOSTADR) PARM(&IP &HOST)
   SNDPGMMSG  MSG('The TCP/IP address for host name ' || +
                &IP *BCAT 'is ' || &HOST)
   ENDPGM



沒有留言: