home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 28
/
amigaformatcd28.iso
/
-seriously_amiga-
/
comms
/
other
/
rxsocket
/
examples
/
psu.rexx
< prev
next >
Wrap
OS/2 REXX Batch file
|
1998-05-09
|
2KB
|
110 lines
/*
A "not so good" macro to scan udp ports
Usage: psu <host> <fromPort/N> <toPort/N>"
*/
parse arg host low hi .
if ~show("L","rxsocket.library") then
if ~addLib("rxsocket.library",0,-30) then do
say "can't find rxsocket.library"
exit
end
/* Arguments controll */
if host=="" | host== "?" | low=="" | hi=="" then do
say "Usage: psu <host> <fromPort/N> <toPort/N>"
exit
end
if ~DataType(low,NUM) then do
say "psu: bad number fromPort"
exit
end
if ~DataType(hi,NUM) then do
say "psu: bad number toPort"
exit
end
if hi<low then do
say "psu: fromPort must be less than toPort"
exit
end
/* host resolve */
sin.ADDRADDR = resolve(host)
if sin.ADDRADDR==-1 then do
say "no host <" || host || ">"
exit
end
/* socket to send an empty message to test ports */
sock = socket("INET","DGRAM","IP")
if sock<0 then do
say "psu: no DGRAM socket (" || errno() || ")"
exit
end
/* socket to receive ICMP packet from stack */
rsock = socket("INET","RAW","ICMP")
if rsock<0 then do
say "psu: no ICMP socket (" || errno() || ")"
exit
end
maxRetry = 7
sin.ADDRFAMILY = "INET"
do sin.ADDRPORT = low to hi
/* We try maxRetry times */
/* If no ICMP_UNREACH/ICMP_UNREACH_PORT we suppose the port is open */
retry = 0
stop = 0
do while (retry<maxRetry) & ~stop
/* empty message just to test the port */
res = SendTo(sock,"",0,"SIN")
if res~=0 then do
say "psu: SendTo() error (" || errno() || ")"
exit
end
/* We wait for a ICMP packet for 5 secs */
wait.read.0=rsock
res = WaitSelect("WAIT",5)
if res~=0 then do
res = RecvFrom(rsock,"BUFF",256)
if res<0 then do
say "psu: RecvFrom() error (" || errno() || ")"
exit
end
/******************************************
*
* We get an ICMP packet. It consists of:
* - IP header
* - type
* - code
* - rest of packet
* We are looking for type=3 and code=3
*
******************************************/
parse var buff vhl +1 rest
hl = c2d(bitand(vhl,'F'x))*8
buff = c2x(buff)
parse var buff +hl type +2 code +2 rest
if (type=='03') & (code=='03') then stop = 1
end
retry = retry+1
end
if retry>=maxRetry then do
if GetServByPort("SERV",sin.ADDRPORT,"tcp") then
say "psu: servizio" serv.servname "(" || sin.ADDRPORT ||")" "aperto"
else say "psu: porta" sin.ADDRPORT "aperta"
end
end