/* ups.rexx - a very stupid udp ports scanner (but it works...:-) */ l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then exit if AddLibrary("rexxsupport.library","rxsocket.library","rxlibnet.library")~=0 then exit prg=ProgramName("NOEXT") if ~RMH_ReadArgs("HOST/A,FROM/N,TO/N,T=TIMEOUT/K/N,I=INTERFACE/K") then do call PrintFault() exit end if parm.1.flag then from=parm.1.value else from=1 if parm.2.flag then high=parm.2.value else if parm.1.flag then high=from else high=100 if from<1 | high>65535 | from>high then call err "bad ports sequence" from"..."high,1 if parm.3.flag then secs=parm.3.value else secs=5 if secs<1 then call err "bad timeout value '"secs"'",1 if parm.4.flag then dev=parm.4.value else do dev=suitable() if dev="" then call err "no suitable device found",1 end sin.addrAddr=resolve(parm.0.value) if sin.addrAddr=-1 then call err "host <"parm.0.value"> not found",1 cxaddr=c2x(addr2c(sin.addrAddr)) us=socket(inet,dgram) if us<0 then call err "can't create socket" fstring="icmp[0]=3" filter=MiamiPCapCompile(fstring,dev) if filter="" then call err PCAPERR,1 s=2**AllocSignal() say dev s pf=MiamiCreatePF(dev,s) if IsDotAddr(parm.0.value) then say "Scanning <"parm.0.value"> on '"dev"'" from"..."high else say "Scanning <"parm.0.value"> ["sin.addrAddr"] on '"dev"'" from"..."high todo=0 do i=from to high sin.addrport=i if sendto(us,"D0A"x,,"SIN")<0 then ports.i=0 else ports.i=1 todo=todo+ports.i if and(SetSignal(0,s),s)>0 then do p=MiamiPFNext(pf) do while p~="" if MiamiPCapMatch(filter,p) then do t=c2d(substr(p,21,1)) c=c2d(substr(p,22,1)) /* addr=substr(p,45,4) if cxaddr~=c2x(addr) then say cxaddr c2x(addr)*/ if t=3 & c~=3 then call err "error on icmp packet type:"t "code:"c,1 pp=c2d(substr(p,51,2)) ports.pp=0 todo=todo-1 end p=MiamiPFNext(pf) end end end secs=todo%3 if secs<5 then secs=5 say "Sending completed, waiting for" todo "returns for" secs "seconds" tim=CreateTimer() ts=TimerSignal(tim) call StartTimer(tim,secs) sigs=or(s,ts,2**12) do while todo>0 res=wait(sigs) if and(res,s)>0 then do p=MiamiPFNext(pf) do while p~="" if MiamiPCapMatch(filter,p) then do pp=c2d(substr(p,51,2)) ports.pp=0 todo=todo-1 end p=MiamiPFNext(pf) end end else leave end do i=from to high if ports.i then say i end exit err: procedure expose prg parse arg msg,ntdoerr if ntdoerr~=1 then msg=msg "("ErrorString()")" say prg":" msg exit suitable: procedure res=QueryInterfaces("IN") do i=0 to res-1 if in.i.family=2 & in.i.up=1 & in.i.LoopBack=0 then return in.i.name end return ""