-
Notifications
You must be signed in to change notification settings - Fork 37
/
construct_main_routines.go
200 lines (162 loc) · 4.96 KB
/
construct_main_routines.go
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
/*
Copyright 2020 The Board of Trustees of The Leland Stanford Junior University
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package lzr
import (
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
"log"
"io"
"bufio"
"os"
"time"
"fmt"
)
var (
handle *pcap.Handle
snapshot_len int32 = 1024
promiscuous bool = false
err error
source_mac string
dest_mac string
QUEUE_SIZE int32 = 200000000
)
func InitParams() {
source_mac = getSourceMacAddr()
dest_mac = getHostMacAddr()
}
func ConstructWritingQueue( workers int ) chan *packet_metadata {
writingQueue := make(chan *packet_metadata, QUEUE_SIZE)
return writingQueue
}
func ConstructIncomingRoutine( workers int ) chan *packet_metadata {
//routine to read in from ZMap
incoming := make(chan *packet_metadata, QUEUE_SIZE)
go func() {
reader := bufio.NewReader(os.Stdin)
for {
//Read from ZMap
input, err := reader.ReadString(byte('\n'))
if err != nil && err == io.EOF {
fmt.Fprintln(os.Stderr,"Finished Reading Input")
close(incoming)
return
}
var packet *packet_metadata
if ReadZMap () {
packet = convertFromZMapToPacket( input )
} else {
packet = convertFromInputListToPacket( input )
}
if packet == nil {
continue
}
incoming <- packet
}
}()
return incoming
}
func ConstructPcapRoutine( workers int ) chan *packet_metadata {
//routine to read in from pcap
pcapIncoming := make(chan *packet_metadata, QUEUE_SIZE)
pcapdQueue := make(chan *gopacket.Packet, QUEUE_SIZE)
// Open device
handle, err = pcap.OpenLive(getDevice(), snapshot_len, promiscuous, pcap.BlockForever)//1*time.Second)
if err != nil {
panic(err)
log.Fatal(err)
}
//set to filter out zmap syn packets (just syn)
err := handle.SetBPFFilter("tcp[tcpflags] != tcp-syn")
if err != nil {
panic(err)
log.Fatal(err)
}
for i := 0; i < workers; i ++ {
go func(i int) {
for {
select {
case data := <-pcapdQueue:
packet := convertToPacketM( data )
if packet == nil {
continue
}
if dest_mac == "" {
saveHostMacAddr( packet )
}
pcapIncoming <- packet
}
}
}(i)
}
go func() {
defer handle.Close()
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for {
pcapPacket, _ := packetSource.NextPacket()
pcapdQueue <- &pcapPacket
}
}()
return pcapIncoming
}
func PollTimeoutRoutine( ipMeta * pState, timeoutQueue chan *packet_metadata, retransmitQueue chan *packet_metadata,
workers int, timeoutT int, timeoutR int ) chan *packet_metadata {
TIMEOUT_T := time.Duration(timeoutT)*time.Second
TIMEOUT_R := time.Duration(timeoutR)*time.Second
timeoutIncoming := make(chan *packet_metadata, QUEUE_SIZE)
//spawn off appropriate routines to poll from timeout & retransmit Queues at specified intervals
timeoutAlg( ipMeta, timeoutQueue, timeoutIncoming, TIMEOUT_T )
timeoutAlg( ipMeta, retransmitQueue, timeoutIncoming, TIMEOUT_R )
return timeoutIncoming
}
//peek at front of q and sleep until processing
func timeoutAlg( ipMeta * pState, queue chan *packet_metadata, timeoutIncoming chan *packet_metadata,
timeout time.Duration) {
go func() {
tdif := time.Duration(timeout)
for {
select {
case packet := <-queue:
tdif = (time.Now()).Sub( packet.Timestamp )
//if top of the Q is early, put routine to sleep until
if tdif < timeout {
//fmt.Println("sleeping:",timeout-tdif)
time.Sleep(timeout-tdif)
}
p, ok := ipMeta.find( packet )
//if no longer in map
if !ok {
//fmt.Println("not found")
continue
}
//if state hasnt changed
if p.ExpectedRToLZR != packet.ExpectedRToLZR {
//fmt.Println("state hasnt changed")
continue
} else {
//fmt.Println("will deal with")
timeoutIncoming <-packet
}
}
}
}()
}
// TimeoutQueueStuff TODO:need to move
func ConstructRetransmitQueue( workers int ) chan *packet_metadata {
retransmitQueue := make(chan *packet_metadata, QUEUE_SIZE)
return retransmitQueue
}
// TimeoutQueueStuff TODO:need to move
func ConstructTimeoutQueue( workers int ) chan *packet_metadata {
timeoutQueue := make(chan *packet_metadata, QUEUE_SIZE)
return timeoutQueue
}