midrange.com code scratchpad
Name:
Chk_Port
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
06/22/2011 06:47:01 pm
IP:
Logged
Description:
Check a host/port combination for online/offline status
Code:
  1.      H DFTACTGRP(*NO) ACTGRP(*NEW)
  2.       /copy socket_h
  3.       /copy errno_h
  4.       /copy signal_h
  5.  
  6.      D translate       PR                  ExtPgm('QDCXLATE')
  7.      D   length                       5P 0 const
  8.      D   data                     32766A   options(*varsize)
  9.      D   table                       10A   const
  10.      D init_signals    PR
  11.      D got_alarm       PR
  12.      D   signo                       10I 0 value
  13.  
  14.      D msg             S             50A
  15.      D sock            S             10I 0
  16.      D port            S              5U 0
  17.      D addrlen         S             10I 0
  18.      D addr            s             20I 0
  19.      D ch              S              1A
  20.      D host            s             32A
  21.      D addr_port       s              6A
  22.      D port_status     s              1A
  23.      D file            s             32A
  24.      D p_Connto        S               *
  25.      D RC              S             10I 0
  26.      D Request         S             60A
  27.      D ReqLen          S             10I 0
  28.      D RecBuf          S             50A
  29.      D RecLen          S             10I 0
  30.  
  31.      C*************************************************
  32.      C* The user will supply a hostname and port
  33.      C*  name as parameters to our program...
  34.      C*************************************************
  35.      c     *entry        plist
  36.      c                   parm                    host
  37.      c                   parm                    addr_port
  38.      c                   parm                    port_status
  39.  
  40.      c                   eval      port = %INT(addr_port)
  41.  
  42.      c                   eval      *inlr = *on
  43.  
  44.  
  45.      C*************************************************
  46.      C* Get the 32-bit network IP address for the host
  47.      C*  that was supplied by the user:
  48.      C*************************************************
  49.      c                   eval      addr = inet_addr(%trim(host))
  50.      c                   if        addr = INADDR_NONE
  51.      c                   eval      p_hostent = gethostbyname(%trim(host))
  52.      c                   if        p_hostent = *NULL
  53.      c                   eval      msg = 'Unable to find that host!'
  54.      c*                   dsply                   msg
  55.      c                   return
  56.      c                   endif
  57.      c                   eval      addr = h_addr
  58.      c                   endif
  59.  
  60.      C*************************************************
  61.      C* Create a socket
  62.      C*************************************************
  63.      c                   eval      sock = socket(AF_INET: SOCK_STREAM:
  64.      c                                           IPPROTO_IP)
  65.      c                   if        sock < 0
  66.      c                   eval      msg = 'Error calling socket()!'
  67.      c*end                   dsply                   msg
  68.      c                   return
  69.      c                   endif
  70.  
  71.      C*************************************************
  72.      C* Create a socket address structure that
  73.      C*   describes the host & port we wanted to
  74.      C*   connect to
  75.      C*************************************************
  76.      c                   eval      addrlen = %size(sockaddr)
  77.      c                   alloc     addrlen       p_connto
  78.  
  79.      c                   eval      p_sockaddr = p_connto
  80.      c                   eval      sin_family = AF_INET
  81.      c                   eval      sin_addr = addr
  82.      c                   eval      sin_port = port
  83.      c                   eval      sin_zero = *ALLx'00'
  84.  
  85.      C*************************************************
  86.      C* Connect to the requested host
  87.      C*************************************************
  88.      C/FREE
  89.            init_signals();
  90.  
  91.            alarm(5);
  92.            if connect(sock: p_connto: addrlen) < 0;
  93.                   msg = 'unable to connect to server!';
  94.                 //  dsply  msg;
  95.                   callp close(sock);
  96.                   alarm(0);
  97.                   port_status = '0';
  98.                   return;
  99.            else;
  100.                   msg = 'Host is online!';
  101.                   port_status = '1';
  102.                  // dsply msg;
  103.            endif;
  104.       /end-free
  105.      C*************************************************
  106.      C*  We're done, so close the socket.
  107.      C*   do a dsply with input to pause the display
  108.      C*   and then end the program
  109.      C*************************************************
  110.      c                   callp     close(sock)
  111.      c*                   dsply                   pause             1
  112.      c                   return
  113.      C*************************************************
  114.      C*  We're going to use signals to indicate timeouts
  115.      C*   so we have to enable signals in the program
  116.      C*************************************************
  117.      P init_signals    B
  118.      D init_signals    PI
  119.      D act             ds                  likeds(sigaction_t)
  120.       /free
  121.           Qp0sEnableSignals();
  122.           sigemptyset(act.sa_mask);
  123.           sigaddset(act.sa_mask: SIGALRM);
  124.           act.sa_handler   = %paddr(got_alarm);
  125.           act.sa_flags     = 0;
  126.           act.sa_sigaction = *NULL;
  127.           sigaction(SIGALRM: act: *omit);
  128.       /end-free
  129.      P                 E
  130.      C*************************************************
  131.      C*  We need a stub to handle the alarm
  132.      C*************************************************
  133.      P got_alarm       B
  134.      D got_alarm       PI
  135.      D   signo                       10I 0 value
  136.       /free
  137.          // Do nothing. The connect() API will return
  138.          //  EINTR ("interrupted by signal") when the
  139.          //  signal is received.
  140.       /end-free
  141.      P                 E
  142.      P                                                        
© 2004-2019 by midrange.com generated in 0.007s valid xhtml & css