-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
292 lines (252 loc) · 6.87 KB
/
connection.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package main
import (
"context"
"fmt"
"math/rand"
"net"
"os"
"strconv"
"strings"
"sync"
"syscall"
"time"
"golang.org/x/sys/unix"
)
var randomNum int
var data = make(chan string, 1)
const (
tcpPort = "8080"
udpPort = "3000"
)
func Connection() {
if len(os.Args) < 3 {
fmt.Println("Usage: go run p2p.go <server|client> <port|address:port>")
return
}
mode := os.Args[1]
arg := os.Args[2]
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // Ensure cancellation is called to free resources if main exits
fmt.Println("Hello, playground")
// Start TCP server in a goroutine and broadcast the TCP server's address
var wg sync.WaitGroup
wg.Add(1)
go broadcasting(ctx, cancel, &wg)
// Wait for the broadcasting goroutine to finish
wg.Wait()
// Start the TCP server and connect to the peer
if mode == "server" {
startServer(arg)
} else if mode == "client" {
startClient()
} else {
fmt.Println("Invalid mode. Use 'server' or 'client'.")
}
// select{}
fmt.Println("Main function exiting.")
}
// TCP server
func setReusePort(conn *net.TCPListener) error {
file, err := conn.File()
if err != nil {
return err
}
defer file.Close()
fd := int(file.Fd())
err = unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
if err != nil {
return err
}
return nil
}
func handleConnection(conn net.Conn) {
defer conn.Close()
buffer := make([]byte, 1024)
for {
n, err := conn.Read(buffer)
if err != nil {
fmt.Println("Connection closed:", err)
return
}
fmt.Println("Received:", string(buffer[:n]))
conn.Write([]byte("Message received"))
}
}
func startServer(port string) {
addr, err := net.ResolveTCPAddr("tcp", ":"+port)
if err != nil {
fmt.Println("Error resolving address:", err)
return
}
listener, err := net.ListenTCP("tcp", addr)
if err != nil {
fmt.Println("Error starting server:", err)
return
}
if err := setReusePort(listener); err != nil {
fmt.Println("Error setting SO_REUSEPORT:", err)
return
}
fmt.Println("Server started on port", port)
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting connection:", err)
continue
}
go handleConnection(conn)
}
}
func startClient() {
preData := <-data
parts := strings.Split(preData, ":")
if len(parts) < 2 {
fmt.Println("Invalid data format")
return
}
ipWithSubnet := parts[0]
port_remote := parts[1]
// Split the IP address and subnet
ipParts := strings.Split(ipWithSubnet, "/")
if len(ipParts) < 2 {
fmt.Println("Invalid IP format")
return
}
ip := ipParts[0] // This is the IP address without the subnet
fmt.Println("IP: ", ip)
fmt.Println("Port: ", port_remote)
fmt.Println("IP address & port:", ip + ":" + port_remote)
address := ip + ":" + port_remote
conn, err := net.Dial("tcp", address)
if err != nil {
fmt.Println("Error connecting to server:", err)
return
}
defer conn.Close()
fmt.Println("Connected to server:", address)
for {
_, err := conn.Write([]byte("Hello from client"))
if err != nil {
fmt.Println("Error writing to connection:", err)
return
}
buffer := make([]byte, 1024)
n, err := conn.Read(buffer)
if err != nil {
fmt.Println("Error reading from connection:", err)
return
}
fmt.Println("Received:", string(buffer[:n]))
time.Sleep(2 * time.Second)
}
}
// UDP Broadcasting
func broadcasting(ctx context.Context, cancel context.CancelFunc, wg *sync.WaitGroup) {
defer wg.Done()
// Generate random number
rand.New(rand.NewSource(time.Now().UnixNano()))
randomNum = rand.Intn(90) + 10
// Create the socket
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
if err != nil {
fmt.Printf("Error creating socket: %s\n", err)
os.Exit(1)
}
// Enable SO_REUSEPORT to allow multiple applications to bind to the same port
if err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
fmt.Printf("Error setting SO_REUSEPORT: %s\n", err)
syscall.Close(fd)
os.Exit(1)
}
// Enable SO_BROADCAST to allow broadcasting
if err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_BROADCAST, 1); err != nil {
fmt.Printf("Error setting SO_BROADCAST: %s\n", err)
syscall.Close(fd)
os.Exit(1)
}
// Bind the socket
addr := &syscall.SockaddrInet4{Port: 3000}
copy(addr.Addr[:], net.IPv4zero.To4())
if err := syscall.Bind(fd, addr); err != nil {
fmt.Printf("Error binding socket: %s\n", err)
syscall.Close(fd)
os.Exit(1)
}
// Convert the file descriptor to a net.PacketConn to use with Go's net package
file := os.NewFile(uintptr(fd), "udp")
conn, err := net.FilePacketConn(file)
if err != nil {
fmt.Printf("Error converting file descriptor to net.PacketConn: %s\n", err)
syscall.Close(fd)
file.Close()
os.Exit(1)
}
defer conn.Close()
// Set up the broadcast address
bcastAddr := &net.UDPAddr{IP: net.IPv4bcast, Port: 3000}
// Start a goroutine to broadcast messages
addrs, err := net.InterfaceAddrs()
if err != nil {
fmt.Println("Error getting machine IP address:", err)
os.Exit(1)
}
go func() {
for {
select {
case <-ctx.Done(): // Checks if the context has been cancelled
fmt.Println("Stopping broadcasting due to cancellation.")
return
default:
message := "pokemonGo-land:" + addrs[1].String() + ":"+ tcpPort +":"+ strconv.Itoa(randomNum) + ":end"
// Let's broadcast the TCP connection to the client
_, err := conn.WriteTo([]byte(message), bcastAddr)
if err != nil {
fmt.Printf("Error broadcasting: %s\n", err)
continue
}
fmt.Println("Broadcasted message:", message)
time.Sleep(1 * time.Second)
}
}
}()
// Read from the UDP connection
buffer := make([]byte, 1024)
fmt.Println("Listening on UDP port 3000")
for {
select {
case <-ctx.Done():
fmt.Println("Stopping reading due to cancellation.")
return
default:
n, addr, err := conn.ReadFrom(buffer)
if err != nil {
fmt.Printf("Error reading from UDP: %s\n", err)
continue
}
message := string(buffer[:n])
// Check if the message contains the random number
randomNumber, _ := strconv.Atoi(message[strings.Index(message, ":")+21 : strings.Index(message, ":end")])
fmt.Println(randomNum)
fmt.Println(randomNumber)
if randomNumber == randomNum {
continue
}
// Get the IP from the message and save it to data channel
data <- message[15 : len(message)-4]
fmt.Printf("Received '%s' from %s\n", string(buffer[:n]), addr.String())
// send a message to other clients to stop broadcasting and listening
for i:= 0; i < 2; i++ {
message := "pokemonGo-land:" + addrs[1].String() + ":"+ tcpPort +":"+ strconv.Itoa(randomNum) + ":end"
// Let's broadcast the TCP connection to the client
_, err := conn.WriteTo([]byte(message), bcastAddr)
if err != nil {
fmt.Printf("Error broadcasting: %s\n", err)
continue
}
fmt.Println("Broadcasted message:", message)
time.Sleep(1 * time.Second)
}
cancel()
}
}
}