4 namespace eval excentis {
13 namespace eval ByteBlower {
21 package require excentis_basic
48 proc NatDevice.IP.Get { publicPort nattedPort publicUdpPort natUdpPort { publicIpAddress null } } {
50 if { [ $publicPort Info -implements WirelessEndpoint ]} {
51 error "Public port cannot be a WirelessEndpoint!"
55 set nattedPortIsWE [ $nattedPort Info -implements WirelessEndpoint ]
58 set publicportIP [ $publicPort Layer3.IPv4.Get ]
59 set publicportL25List [ $publicPort Layer2_5.Vlan.Get ]
60 if { [ string equal $publicIpAddress "null" ]} {
61 set publicIpAddress [ $publicportIP Ip.Get ]
64 if { ! $nattedPortIsWE} {
65 set nattedPortL3 [ $nattedPort Layer3.IPv4.Get ]
66 set nattedPortIP [ $nattedPortL3 Ip.Get ]
67 set nattedPortL2 [ $nattedPort Layer2.EthII.Get ]
68 set nattedPortMac [ $nattedPortL2 Mac.Get ]
69 set nattedPortL25List [ $nattedPort Layer2_5.Vlan.Get ]
71 set nattedPortIP [ [ [ $nattedPort Device.Info.Get ] Network.Info.Get ] IPv4.Get ]
75 set stream [ $nattedPort Tx.Stream.Add ]
76 $stream InterFrameGap.Set 10ms
77 $stream NumberOfFrames.Set 100
79 set txFrame [ $stream Frame.Add ]
81 if { ! $nattedPortIsWE} {
83 set mac [ $nattedPortL3 Protocol.Arp [ $publicportIP Ip.Get ] ]
85 puts "== NATTED PORT DEST MAC : $mac =="
89 for {
set i [
expr [
llength $nattedPortL25List] - 1]} { $i >= 0} {
incr i -1} {
90 set nattedPortL25 [
lindex $nattedPortL25List $i]
93 $txFrame Bytes.Set $srcFrame
96 $stream Destination.Address.Set $publicIpAddress
97 $stream Destination.Port.Set $publicUdpPort
98 $stream Source.Port.Set $natUdpPort
99 $txFrame Payload.Set [
string repeat "AA" [
expr 256 - 42 ] ]
103 set capture [ $publicPort Rx.Capture.Basic.Add ]
104 set publicportVlanCount 0
105 set vlanFilters [ list ]
106 foreach publicportL25 $publicportL25List {
107 incr publicportVlanCount
108 set vlanId [ $publicportL25 ID.Get ]
109 lappend vlanFilters "vlan $vlanId"
112 if { $publicportVlanCount > 0} {
113 set vlanFilter [
join $vlanFilters " and " ]
114 set filter "$vlanFilter and dst host $publicIpAddress and udp"
116 set filter "dst host $publicIpAddress and udp"
119 $capture Filter.Set $filter
123 if { $nattedPortIsWE} {
126 set ts [ $nattedPort Start ]
127 set mpTs [ [ $nattedPort Parent.Get ] Timestamp.Get ]
128 set timeToWait [
expr int((double($ts) - double($mpTs)) / 1000000) ]
136 if { ! $nattedPortIsWE} {
143 if { $nattedPortIsWE} {
144 $nattedPort Result.Clear
148 set captureResult [ $capture Result.Get ]
149 set capturedFrames [ $captureResult Frames.Get ]
150 if { [ llength $capturedFrames ] == 0} {
154 error "NAT failed, no packets received on public port for NATted port $nattedPort"
156 set capturedFrame [
lindex $capturedFrames 0 ]
157 set bytes [ $capturedFrame Bytes.Get ]
162 set lentype [
string range $bytes 24 27 ]
163 scan $lentype "%x" lentypeDec
164 if { $lentypeDec <= 1500} {
165 set ethernetLength $lentypeDec
166 set ethernetPayload [
string range $bytes 44 [
expr 44 + $lentypeDec * 2 - 1 ] ]
168 set ethernetType $lentype
169 set ethernetPayload [
string range $bytes 28 end ]
172 set ethernetPayload [
string range $ethernetPayload [
expr 2 * 4 * $publicportVlanCount] end ]
174 scan [
string range $ethernetPayload 0 1 ] "%x" temp
175 set ipIhl [
expr $temp & 15 ]
178 scan [
string range $ethernetPayload 4 7 ] "%x" ipLength
180 set ipSa [
string range $ethernetPayload 24 31 ]
181 set ipPayload [
string range $ethernetPayload [
expr $ipIhl * 4 * 2 ] [
expr $ipLength * 2 - 1 ] ]
184 scan [
string range $ipPayload 0 3 ] "%x" udpSp
187 set natPublicPort $udpSp
194 return [ list $natPublicIP $natPublicPort ]