File: Network.tcl

package info (click to toggle)
coccinella 0.96.20-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,184 kB
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (228 lines) | stat: -rw-r--r-- 6,091 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#  Network.tcl ---
#  
#      This file is part of The Coccinella application. 
#      It implements some network utilities.
#      
#  Copyright (c) 2003  Mats Bengtsson
#  
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#  
#  See the README file for license, bugs etc.
#  
# $Id: Network.tcl,v 1.21 2007-07-19 06:28:18 matben Exp $

package provide Network 1.0

namespace eval ::Network:: {
    
    variable debug 0
}

# Network::Open --
#
#       This is supposed to be a fairly general method of opening sockets
#       async.
#       
# Arguments:
#       nameOrIP
#       port
#       cmd         Specific callback proc for this open operation.
#                   'cmd {sock ip port status {msg {}}}'
#       args        -timeout secs,
#                   -ssl     boolean 
#                            Must not be mixed up with -tls which has a special
#                            meaning in XMPP
#       
# Results:
#       only via the callback cmd.

proc ::Network::Open {nameOrIP port cmd args} {
    global  errorCode
    
    variable debug
    variable opts
    
    if {$debug > 1} {
	puts "::Network::Open, nameOrIP=$nameOrIP, port=$port, cmd=$cmd, args=$args"
    }
    array set opts {
	-timeout 0
	-ssl     0
    }
    array set opts $args
    if {$opts(-ssl)} {
	set socketCmd {::tls::socket -request 0 -require 0}
    } else {
	set socketCmd socket
    }
    
    # Try opening socket async.
    if {[catch {eval $socketCmd {-async $nameOrIP $port}} sock]} {
	uplevel #0 $cmd [list {} $nameOrIP $port error $sock]
	return
    }
    
    # Write/read line by line.
    fconfigure $sock -buffering line
    
    # When socket writable the connection is opened.
    # Needs to be in nonblocking mode.
    fconfigure $sock -blocking 0
    
    # For nonlatin characters to work be sure to use Unicode/UTF-8.
    if {[info tclversion] >= 8.1} {
	catch {fconfigure $sock -encoding utf-8}
    }
    
    # If open socket in async mode, need to wait for fileevent.
    fileevent $sock writable   \
      [list [namespace current]::WhenSocketOpensInits $sock $nameOrIP  \
      $port $cmd $opts(-ssl)]
    
    # Set up timer event for timeouts.
    if {$opts(-timeout) > 0} {
	::Network::ScheduleKiller $sock $cmd
    }
    return
}

# Network::WhenSocketOpensInits --
#
#       Callback when socket is open; becomes writable.
#       
# Arguments:
#       sock
#       nameOrIP
#       port
#       cmd         Specific callback proc for this open operation.
# Results:
#       only via the callback cmd.

proc ::Network::WhenSocketOpensInits {sock nameOrIP port cmd tls} {
    variable killerId    
    variable debug    
    
    if {$debug > 1} {
	puts "::Network::WhenSocketOpensInits, nameOrIP=$nameOrIP, port=$port"
    }
    
    # No more event handlers here. See also below...
    fileevent $sock writable {}
    
    #  Cancel timeout killer.
    if {[info exists killerId($sock)]} {
	after cancel $killerId($sock)
	unset -nocomplain killerId($sock)
    }

    if {[catch {eof $sock} iseof] || $iseof} {
	catch {close $sock}
	set msg "Failed to open network socket to $nameOrIP."
	uplevel #0 $cmd [list $sock $nameOrIP $port error $msg]
	return
    }
    
    # Detecting failure to connect, at least needed on linux.
    # The jabber server gives: <stream:error>Invalid XML</stream:error>
    if {0 && [catch {puts -nonewline $sock { }} msg]} {
	set msg "Failed to open network socket to $nameOrIP."
	uplevel #0 $cmd [list $sock $nameOrIP $port error $msg]
	return
    }
    
    # Check if something went wrong first.
    if {[catch {fconfigure $sock -sockname} sockname]} {
	uplevel #0 $cmd [list $sock $nameOrIP $port error $sockname]
	return
    }
    
    # Do SSL handshake.
    if {$tls} {
	fconfigure $sock -blocking 1
	if {[catch {::tls::handshake $sock} msg]} {
	    catch {close $sock}
	    uplevel #0 $cmd [list $sock $nameOrIP $port error $msg]
	    return
	}
	fconfigure $sock -blocking 0
    }
           
    # Evaluate our callback procedure.
    uplevel #0 $cmd [list $sock $nameOrIP $port ok]
}

# ScheduleKiller, Kill --
#
#       Cancel 'OpenConnection' process if timeout.
#
# Arguments:
#       sock
#       cmd         Specific callback proc for this open operation if we get
#                   a timeout.
#       
# Results:

proc ::Network::ScheduleKiller {sock cmd} {  
    variable killerId    
    variable opts

    if {[info exists killerId($sock)]} {
	after cancel $killerId($sock)
    }
    set killerId($sock) [after [expr {1000 * $opts(-timeout)}]   \
      [list [namespace current]::Kill $sock $cmd]]
}

proc ::Network::Kill {sock cmd} {    
    variable killerId    

    catch {close $sock}
    if {[info exists killerId($sock)]} {
	after cancel $killerId($sock)
	unset -nocomplain killerId($sock)
    }
    
    # Evaluate our callback procedure.
    uplevel #0 $cmd [list $sock {} {} timeout]
}

# Network::KillAll --
#
#       Kills all pending open states.

proc ::Network::KillAll { } {
    variable killerId

    foreach sock [array names killerId] {
	catch {close $sock}
	after cancel $killerId($sock)
    }
    unset -nocomplain killerId
}

# Network::GetThisPublicIP --
#
#       Returns our own ip number unless set own NAT address.

proc ::Network::GetThisPublicIP { } {
    global  this prefs
    
    if {$prefs(setNATip) && ($prefs(NATip) ne "")} {
	return $prefs(NATip)
    } else {
	return $this(ipnum)
    }
}

#-------------------------------------------------------------------------------