-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.go
162 lines (143 loc) · 4.01 KB
/
tcp.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
// Copyright 2017 Inca Roads LLC. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
// Inbound TCP support
package main
import (
"bufio"
"fmt"
"io"
"net"
)
// TCPInboundHandler kicks off TCP single-upload request server
func TCPInboundHandler() {
fmt.Printf("Now handling inbound TCP on %s%s\n", ThisServerAddressIPv4, TTServerTCPPort)
ServerAddr, err := net.ResolveTCPAddr("tcp", TTServerTCPPort)
if err != nil {
fmt.Printf("Error resolving TCP port: \n%v\n", err)
return
}
ServerConn, err := net.ListenTCP("tcp", ServerAddr)
if err != nil {
fmt.Printf("Error listening on TCP port: \n%v\n", err)
return
}
defer ServerConn.Close()
for {
// Accept the TCP connection
conn, err := ServerConn.AcceptTCP()
if err != nil {
fmt.Printf("\nTCP: rror accepting TCP session: \n%v\n", err)
continue
}
// Create a reader on that connection
rdconn := bufio.NewReader(conn)
// Read the payload buffer format
payloadFormatLen := 1
payloadFormat := make([]byte, payloadFormatLen)
n, err := io.ReadFull(rdconn, payloadFormat)
if err != nil {
fmt.Printf("\nTCP: can't read format: \n%v\n", err)
conn.Close()
continue
}
if n != payloadFormatLen {
fmt.Printf("\nTCP: can't read format: %d/%d\n", n, payloadFormatLen)
conn.Close()
continue
}
if payloadFormat[0] != BuffFormatPBArray {
fmt.Printf("\n%s TCP request from %s ignored\n", LogTime(), ipv4(conn.RemoteAddr().String()))
buf1 := make([]byte, 1024)
n, err := rdconn.Read(buf1)
if err == nil || err == io.EOF || err == io.ErrUnexpectedEOF {
buf2 := append(payloadFormat, buf1[:n]...)
b := make([]byte, len(buf2))
var bl int
var ch, chPrev byte
for i := 0; i < len(buf2); i++ {
ch = buf2[i]
if ch < 32 || ch >= 127 {
if chPrev == ';' {
ch = ' '
} else {
ch = ';'
}
}
if ch != ' ' || chPrev != ' ' {
b[bl] = ch
bl++
}
chPrev = ch
}
if bl != 0 {
fmt.Printf("%s\n", string(b[:bl]))
}
}
conn.Close()
continue
}
// Read the number of array entries
payloadCountLen := 1
payloadCount := make([]byte, payloadCountLen)
n, err = io.ReadFull(rdconn, payloadCount)
if err != nil {
fmt.Printf("\nTCP: can't read count: \n%v\n", err)
conn.Close()
continue
}
if n != payloadCountLen {
fmt.Printf("\nTCP: can't read count: %d/%d\n", n, payloadCountLen)
conn.Close()
continue
}
if payloadCount[0] == 0 {
fmt.Printf("\nTCP: unsupported count: %d\n", payloadCount[0])
conn.Close()
continue
}
// Read the length array
payloadEntryLengthsLen := int(payloadCount[0])
payloadEntryLengths := make([]byte, payloadEntryLengthsLen)
n, err = io.ReadFull(rdconn, payloadEntryLengths)
if err != nil {
fmt.Printf("\nTCP: can't read entry_lengths: \n%v\n", err)
conn.Close()
continue
}
if n != int(payloadEntryLengthsLen) {
fmt.Printf("\nTCP: can't read entry_lengths: %d/%d\n", n, payloadEntryLengthsLen)
conn.Close()
continue
}
// Read the entries
payloadEntriesLen := 0
for i := 0; i < int(payloadEntryLengthsLen); i++ {
payloadEntriesLen += int(payloadEntryLengths[i])
}
payloadEntries := make([]byte, payloadEntriesLen)
n, err = io.ReadFull(rdconn, payloadEntries)
if err != nil {
fmt.Printf("\nTCP: can't read entries: \n%v\n", err)
conn.Close()
continue
}
if n != payloadEntriesLen {
fmt.Printf("\nTCP: can't read entries: %d/%d\n", n, payloadEntriesLen)
conn.Close()
continue
}
// Combine all that we've read
payload := append(payloadFormat, payloadCount...)
payload = append(payload, payloadEntryLengths...)
payload = append(payload, payloadEntries...)
// Initialize a new AppReq
AppReq := IncomingAppReq{}
AppReq.SvTransport = "device-tcp:" + ipv4(conn.RemoteAddr().String())
// Push it to be processed
go AppReqPushPayload(AppReq, payload, "device directly")
stats.Count.TCP++
// Close the connection
conn.Close()
}
}