home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / comms / other / rxsocket / examples / psu.rexx < prev    next >
OS/2 REXX Batch file  |  1998-05-09  |  2KB  |  110 lines

  1. /*
  2.      A "not so good" macro to scan udp ports
  3.  
  4.      Usage: psu <host> <fromPort/N> <toPort/N>"
  5. */
  6.  
  7. parse arg host low hi .
  8.  
  9. if ~show("L","rxsocket.library") then
  10.     if ~addLib("rxsocket.library",0,-30) then do
  11.         say "can't find rxsocket.library"
  12.         exit
  13.     end
  14.  
  15. /* Arguments controll */
  16. if host=="" | host== "?" | low=="" | hi=="" then do
  17.     say "Usage: psu <host> <fromPort/N> <toPort/N>"
  18.     exit
  19. end
  20.  
  21. if ~DataType(low,NUM) then do
  22.     say "psu: bad number fromPort"
  23.     exit
  24. end
  25.  
  26. if ~DataType(hi,NUM) then do
  27.     say "psu: bad number toPort"
  28.     exit
  29. end
  30.  
  31. if hi<low then do
  32.     say "psu: fromPort must be less than toPort"
  33.     exit
  34. end
  35.  
  36. /* host resolve */
  37. sin.ADDRADDR = resolve(host)
  38. if sin.ADDRADDR==-1 then do
  39.     say "no host <" || host || ">"
  40.     exit
  41. end
  42.  
  43. /* socket to send an empty message to test ports */
  44. sock = socket("INET","DGRAM","IP")
  45. if sock<0 then do
  46.     say "psu: no DGRAM socket (" || errno() || ")"
  47.     exit
  48. end
  49.  
  50. /* socket to receive ICMP packet from stack */
  51. rsock = socket("INET","RAW","ICMP")
  52. if rsock<0 then do
  53.     say "psu: no ICMP socket (" || errno() || ")"
  54.     exit
  55. end
  56.  
  57. maxRetry = 7
  58. sin.ADDRFAMILY = "INET"
  59. do sin.ADDRPORT = low to hi
  60.  
  61.     /* We try maxRetry times */
  62.     /* If no ICMP_UNREACH/ICMP_UNREACH_PORT we suppose the port is open */
  63.     retry = 0
  64.     stop = 0
  65.     do while (retry<maxRetry) & ~stop
  66.  
  67.         /* empty message just to test the port */
  68.         res = SendTo(sock,"",0,"SIN")
  69.         if res~=0 then do
  70.             say "psu: SendTo() error (" || errno() || ")"
  71.             exit
  72.         end
  73.  
  74.         /* We wait for a ICMP packet for 5 secs */
  75.         wait.read.0=rsock
  76.         res = WaitSelect("WAIT",5)
  77.         if res~=0 then do
  78.             res = RecvFrom(rsock,"BUFF",256)
  79.             if res<0 then do
  80.                 say "psu: RecvFrom() error (" || errno() || ")"
  81.                 exit
  82.             end
  83.  
  84.             /******************************************
  85.              *
  86.              * We get an ICMP packet. It consists of:
  87.              * - IP header
  88.              * - type
  89.              * - code
  90.               * - rest of packet
  91.              * We are looking for type=3 and code=3
  92.              *
  93.               ******************************************/
  94.             parse var buff vhl +1 rest
  95.             hl = c2d(bitand(vhl,'F'x))*8
  96.             buff = c2x(buff)
  97.             parse var buff +hl type +2 code +2 rest
  98.             if (type=='03') & (code=='03') then stop = 1
  99.         end
  100.         retry = retry+1
  101.     end
  102.  
  103.     if retry>=maxRetry then do
  104.         if GetServByPort("SERV",sin.ADDRPORT,"tcp") then
  105.             say "psu: servizio" serv.servname "(" || sin.ADDRPORT ||")" "aperto"
  106.         else say "psu: porta" sin.ADDRPORT "aperta"
  107.     end
  108.  
  109. end
  110.