This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Need some help with sockets..

Old posts that have not been replied to for several years.
Locked
H
HeLLFyRe

Need some help with sockets..

Post by HeLLFyRe »

OK This is what I am trying to do:
Whenever I say "<botnick> scan <host> <port>" the bot is supposed to open a tcp socket to <host> <port>.
However, I need to check if the socket has opened (ie if the port is open on that host 8))..
This is my script:

Code: Select all

proc scan_test { nick uhost handle channel text } {
set host [lindex "$text" 0]; set port [lindex "$text" 1]; set z [socket -async $host $port]; 
putquick "privmsg $channel :Scanning $host on $port.."
if {[fconfigure $z -error] == "connection refused"} { 
puthelp "privmsg $channel :Port Closed: $port"; 
} elseif {[fconfigure $z -error] == ""} { 
puthelp "privmsg $channel :Port Open: $port" 
}
close $z
}


bind pub - scan scan_test
Now.. On to the problem.. Even if a port is closed, fconfigure $z -error ALWAYS evaluates to "" (which should be the outcome of a SUCCESSFUL connection) Any ideas on what I can do? :(
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

If you want to do it all in one proc you should avoid using -async

Use catch to fetch the error message (if any) when opening the socket fails. Eg:

Code: Select all

if {[catch {socket $a $p} e]} {
    # $e is the error message
} {
    # connected...$e is the socket id
    # ...so close it and tell the user the port is open
    close $e
}
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Hi, about the async sockets... you can't check for the error until the socket has had some time to try to connect. Use 'fileevent writable' and 'fileevent readable' to set up a callback. Technically you should only have to use the 'writable' event, but the tcl docs are a bit unclear. Anyway, once the async socket is marked readable/writable, that means that it's either connected or in an error state.<br>
<br>
Also, you may want to use eggdrop's dns commands instead of calling 'socket' right away, because eggdrop's are nonblocking. Not an issue if you always use ip addresses of course.
Locked