net-connect question

Started by sprink, August 07, 2005, 06:57:41 PM

Previous topic - Next topic

sprink

Hi, i'm fairly new to newlisp so I thought i''d make a script to validate if a list of anonymous proxy servers in a file are connectable (seems easy enough), but my script seems to stall on the ones that aren't. Here's my code:



(define (check-list file)
  (set 'in-file (open file "read"))
  (while
    (read-line in-file)
    (check-state (current-line))
  )
  (close in-file)
)

(define (check-state server)
  (if
    (= (net-connect server 8080) nil)
    (println (current-line) " is NOT online")
    (println (current-line) " is online")
  )
)


(check-list "servers.txt")
(exit)


Is there some way to set like a timeout limit on the net-connect so it doesn't hang for ever on the ones that aren't connectable?



i.e. So it can scan a list of ips and quickly tell which are useable.

pjot

#1
Hi,



This question has been asked before:



---------------



http://www.alh.net/newlisp/phpbb/viewtopic.php?t=685">http://www.alh.net/newlisp/phpbb/viewtopic.php?t=685



---------------



Maybe it's better to perform a 'ping' first, to check if the proxyserver is online. If the proxyserver is out of your network segment and not routed then even the ping will not return.



Peter

Dmi

#2
How about calling "nmap" with list of hosts/ports and parsing it's output.

Yes, nmap isn't part of newLisp, but instead it designed to scan ports ;-)

The code will looks like  "Get a list of local IPs" in "code snippets" page
WBR, Dmi

newdep

#3
The ones that arn't connectable will directly return 'nil and wont

hang the session.  Those who can will return a handle, but a handle does

not say its accessable..!



this is quick enough.. not too fast but it works..without hangs..

http://www.nodep.nl/downloads/newlisp/portscan.lsp">http://www.nodep.nl/downloads/newlisp/portscan.lsp



Try to send someting or read someting for the handle with a (net-select..)



Norman.
-- (define? (Cornflakes))

sprink

#4
well, first off, thanks for the replys. But I still haven't quite got the solution i'm looking for working. From your replys i'm assuming my best bet is to use threads for each connection (so I can continue to check the rest without waiting for timeouts)

or use net-select. But I can't seem to get net-select to work the way it says in the manual.



This is my little script updated from the last version, trying to implement the net-select function.



(define (check-list file)
  (set 'in-file (open file "read"))
  (while
    (read-line in-file)
    (check-state (current-line))
  )
  (close in-file)
)

(define (check-state server)
  (if (= (net-lookup server) nil)
    (println (current-line) " does not resolve.")
    (begin
      (set 'sock (net-connect server 8080))
      (if (= (net-select sock "write" 1000) nil)
        (begin
          (println (current-line) " is not connectable on port 8080.")
          (net-close sock))
        (begin
          (println (current-line) " connection made.")
          (net-close sock))
      )
    )
  )
)

(if (= (main-args 2) nil)
  (begin
    (println "Please supply a proxy list as a argument.")
    (exit)
  )
)

(check-list (main-args 2))
(exit)


from reading the manaul net-select looks like it should return either true or nil after the 1000 mili-seconds. Am I mistaken or am I using net-select the wrong way?

Dmi

#5
what is the advantage of this script?

net-connect is bloking either, so, if port is filtered out by "drop" policy (as usual :) it will not return anything until long timeout.

So, if U still want to use net-connect, threads will be a good choice.



btw, nil in newLisp is equivalent of "false", and

(if (= (net-lookup server) nil) ...

is equivalent to

(if (net-lookup server) ...
WBR, Dmi

sprink

#6
Quote from: "Dmi"
btw, nil in newLisp is equivalent of "false", and

(if (= (net-lookup server) nil) ...

is equivalent to

(if (net-lookup server) ...


(if (net-lookip server))

isn't equivalent to

(if (= (net-lookup server) nil).



perhaps you mean !=



And newdep suggested using net-select, which seems like the easier route. That's why I decided to try and use it.

Dmi

#7
... about nil: of course !=



about select: net-select operates on descriptor, given from net-connect (or net-listen, but it's not our case).

It's in your code:


(set 'sock (net-connect server 8080))
(if (= (net-select sock "write" 1000) nil)


But net-connect blocks on filtered ports (I just have checked for that).

So, net-select runs always _after_ net-connect, and after it's timeout.
WBR, Dmi

sprink

#8
ohhhhh. hehe Dmi. I understand now. Thanks for clearing that up. :)

newdep

#9
A net-select on a net-connect is always true for "r" "w" "a" on a opened socket! That wont help in this case..



Norman.
-- (define? (Cornflakes))