-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
196 lines (172 loc) · 4.32 KB
/
main.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
package main
import (
"bufio"
"flag"
"io"
logger "log"
"net"
"os"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
const (
snaplen = 1 << 16
promiscuous = true
timeout = pcap.BlockForever
maxTTL = 1 * time.Hour
)
var (
rx = flag.String("rx", "DioFromCell", "name/ip of receiving nic")
tx = flag.String("tx", "DioToCell", "name/ip of transmitting nic")
nsrcip = flag.String("nsrc", "192.168.1.200", "new source ip address for transmitting")
nip net.IP
htx, hrx *pcap.Handle
ltx, lrx localini
enter = bufio.NewScanner(os.Stdin)
log *logger.Logger
)
// E is a conveniecy for error printing
type E struct {
error
string
}
type udppacket struct {
eth layers.Ethernet
ip layers.IPv4
udp layers.UDP
payload []byte
}
func init() {
flag.Parse()
log = logger.New(os.Stdout, "[UDPFIX]", logger.LstdFlags)
nip = net.ParseIP(*nsrcip)
if nip == nil {
log.Printf("Must provide new source ip.\n")
flag.Usage()
os.Exit(1)
}
nip = nip.To4()
if err := ltx.set(*tx); err != nil {
log.Fatalf("error setting local transmitting interface: %+v", E{err, err.Error()})
}
if err := lrx.set(*rx); err != nil {
log.Fatalf("error setting local receiving interface: %+v", E{err, err.Error()})
}
}
func main() {
var err error
// open tx rx sniffers
htx, err := pcap.OpenLive(ltx.DevName, snaplen, promiscuous, timeout)
if err != nil {
log.Fatalf("could not open capture device (transmitting): %+v", E{err, err.Error()})
}
defer htx.Close()
hrx, err = pcap.OpenLive(lrx.DevName, snaplen, promiscuous, timeout)
if err != nil {
log.Fatalf("could not open capture device (receiving): %+v", E{err, err.Error()})
}
defer hrx.Close()
// set capture filters
bpftx := "udp and (ip src host " + ltx.IP.String() + ")"
if err := htx.SetBPFFilter(bpftx); err != nil {
log.Fatalf("error with tx bpf: %+v\n", E{err, err.Error()})
}
bpfrx := "udp and (ip dst host " + lrx.IP.String() + ") and (ip src host not " + nip.String() + ")"
if err := hrx.SetBPFFilter(bpfrx); err != nil {
log.Fatalf("error with rx bpf: %+v\n", E{err, err.Error()})
}
// start rx sniffing loop
m := newTTLmap(maxTTL)
go func() {
rxip := layers.IPv4{}
rxudp := layers.UDP{}
decoded := make([]gopacket.LayerType, 0, 2)
parser := gopacket.NewDecodingLayerParser(
layers.LayerTypeEthernet,
&layers.Ethernet{},
&rxip,
&rxudp,
)
parser.IgnoreUnsupported = true
for {
p, _, err := hrx.ZeroCopyReadPacketData()
if err != nil {
if err == io.EOF {
return
}
continue
}
if err := parser.DecodeLayers(p, &decoded); err != nil {
continue
}
m.putPort(newMKey(rxip.SrcIP, rxudp.SrcPort), rxudp.DstPort)
}
}()
inpacket := udppacket{}
decoded := make([]gopacket.LayerType, 0, 10)
parser := gopacket.NewDecodingLayerParser(
layers.LayerTypeEthernet,
&inpacket.eth,
&inpacket.ip,
&inpacket.udp,
)
parser.IgnoreUnsupported = true
c := make(chan udppacket, 100)
go func() {
for {
p, _, err := htx.ReadPacketData()
if err != nil {
if err == io.EOF {
return
}
continue
}
// payload := getpayload(inpacket.payload)
if err := parser.DecodeLayers(p, &decoded); err != nil {
log.Println("decode err: ", err)
}
c <- udppacket{
eth: inpacket.eth,
ip: inpacket.ip,
udp: inpacket.udp,
payload: inpacket.udp.LayerPayload(),
}
}
}()
// done channel
done := make(chan bool)
go func() {
done <- enter.Scan()
}()
buff := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
}
log.Printf("receiving on: %s transmitting on: %s new source address: %s\n", lrx.IP.String(), ltx.IP.String(), nip.String())
for {
select {
case p := <-c:
p.ip.SrcIP = nip
sport, ok := m.getPort(newMKey(p.ip.DstIP, p.udp.DstPort))
if !ok || sport.port == 0 {
continue
}
p.udp.SrcPort = sport.port
p.udp.SetNetworkLayerForChecksum(&p.ip)
if err := gopacket.SerializeLayers(buff, opts, &p.eth, &p.ip, &p.udp, gopacket.Payload(p.payload)); err != nil {
log.Println("serial err: ", err)
continue
}
if err := htx.WritePacketData(buff.Bytes()); err != nil {
log.Println("sending err: ", err)
continue
}
case <-done:
log.Println("exiting...")
return
}
}
}