-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
165 lines (143 loc) · 3.68 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
package main
import (
"flag"
"log"
"os"
"sync"
"time"
"github.com/netsec-ethz/scion-apps/pkg/pan"
smp "github.com/netsys-lab/scion-path-discovery/api"
"github.com/netsys-lab/scion-path-discovery/packets"
"github.com/netsys-lab/scion-path-discovery/pathselection"
"github.com/scionproto/scion/go/lib/snet"
"github.com/sirupsen/logrus"
)
var localAddr *string = flag.String("l", "localhost:9999", "Set the local address")
var remoteAddr *string = flag.String("r", "18-ffaa:1:ef8,[127.0.0.1]:12345", "Set the remote address")
var isServer *bool = flag.Bool("s", false, "Run as Server (otherwise, client)")
func main() {
// peers := []string{"peer1", "peer2", "peer3"} // Later real addresses
flag.Parse()
logrus.SetLevel(logrus.DebugLevel)
mpSock := smp.NewPanSock(*localAddr, nil, &smp.PanSocketOptions{
Transport: "QUIC",
})
err := mpSock.Listen()
if err != nil {
log.Fatal("Failed to listen PanSock: ", err)
os.Exit(1)
}
if *isServer {
i := 1
for {
remote, err := mpSock.WaitForPeerConnect()
if err != nil {
log.Fatal("Failed to connect in-dialing peer: ", err)
os.Exit(1)
}
log.Printf("Connected to %s", remote.String())
addr, _ := pan.ResolveUDPAddr(*localAddr)
addr.Port = addr.Port + uint16(i)*32
mps := smp.NewPanSock(addr.String(), nil, &smp.PanSocketOptions{
Transport: "QUIC",
})
err = mps.Listen()
if err != nil {
log.Fatal("Failed to listen mps: ", err)
os.Exit(1)
}
mps.SetPeer(remote)
disjointSel := smp.NewDisjointPathSelectionSocket(mps, 2, 2)
go func(dj *smp.DisjointPathselection) {
metricsTicker := time.NewTicker(1 * time.Second)
for {
select {
case <-metricsTicker.C:
_, err := disjointSel.UpdatePathSelection()
if err != nil {
logrus.Error("[DisjointPathSelection] Failed to update path selection ", err)
os.Exit(1)
}
}
}
}(disjointSel)
pathset, err := disjointSel.InitialPathset()
if err != nil {
log.Fatal("Failed to obtain pathset", err)
os.Exit(1)
}
pathset.Address = *remote
err = mps.Connect(&pathset, nil)
if err != nil {
log.Fatal("Failed to connect MPPeerSock", err)
os.Exit(1)
}
i++
go func(mps *smp.PanSocket) {
WriteAllConns(mps)
logrus.Warn("Done writing all conns to ", mps.Peer.String())
}(mps)
}
} else {
peerAddr, err := snet.ParseUDPAddr(*remoteAddr)
if err != nil {
log.Fatalf("Failed to parse remote addr %s, err: %v", *remoteAddr, err)
os.Exit(1)
}
mpSock.SetPeer(peerAddr)
paths, _ := mpSock.GetAvailablePaths()
pathset := pathselection.WrapPathset(paths)
pathset.Address = *peerAddr
err = mpSock.Connect(&pathset, nil)
if err != nil {
log.Fatal("Failed to connect MPPeerSock", err)
os.Exit(1)
}
for {
mpSock.Disconnect()
new, err := mpSock.WaitForPeerConnect()
if err != nil {
log.Fatal("Failed to wait for back MPPeerSock", err)
os.Exit(1)
}
logrus.Info("Got conn back from remote ", new.String())
ReadAllConns(mpSock)
}
}
}
func ReadAllConns(mps *smp.PanSocket) {
var wg sync.WaitGroup
conns := mps.UnderlaySocket.GetConnections()
for _, c := range conns {
wg.Add(1)
go func(c packets.UDPConn) {
bts := make([]byte, packets.PACKET_SIZE)
for {
_, err := c.Read(bts)
if err != nil {
logrus.Error(err)
wg.Done()
}
}
}(c)
}
wg.Wait()
}
func WriteAllConns(mps *smp.PanSocket) {
var wg sync.WaitGroup
conns := mps.UnderlaySocket.GetConnections()
for _, c := range conns {
wg.Add(1)
go func(c packets.UDPConn) {
bts := make([]byte, packets.PACKET_SIZE)
for {
_, err := c.Write(bts)
if err != nil {
logrus.Error(err)
wg.Done()
}
}
}(c)
}
wg.Wait()
}