net-connect question

For the Compleat Fan
Locked
sprink
Posts: 4
Joined: Fri Aug 05, 2005 8:26 pm

net-connect question

Post by 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:

Code: Select all

(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
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Hi,

This question has been asked before:

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

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
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

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
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

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

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

Norman.
-- (define? (Cornflakes))

sprink
Posts: 4
Joined: Fri Aug 05, 2005 8:26 pm

Post by sprink »

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.

Code: Select all

(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
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

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
Posts: 4
Joined: Fri Aug 05, 2005 8:26 pm

Post by sprink »

Dmi wrote: 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
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

... 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:

Code: Select all

(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
Posts: 4
Joined: Fri Aug 05, 2005 8:26 pm

Post by sprink »

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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

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))

Locked