-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathconnect.go
283 lines (236 loc) · 7.74 KB
/
connect.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
// Copyright 2015 Tony Bai.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package cmpp
import (
"bytes"
"crypto/md5"
"encoding/binary"
"errors"
"strconv"
"time"
cmpputils "github.com/bigwhite/gocmpp/utils"
)
// Packet length const for cmpp connect request and response packets.
const (
CmppConnReqPktLen uint32 = 4 + 4 + 4 + 6 + 16 + 1 + 4 //39d, 0x27
Cmpp2ConnRspPktLen uint32 = 4 + 4 + 4 + 1 + 16 + 1 //30d, 0x1e
Cmpp3ConnRspPktLen uint32 = 4 + 4 + 4 + 4 + 16 + 1 //33d, 0x21
)
// Errors for connect resp status.
var (
ErrnoConnInvalidStruct uint8 = 1
ErrnoConnInvalidSrcAddr uint8 = 2
ErrnoConnAuthFailed uint8 = 3
ErrnoConnVerTooHigh uint8 = 4
ErrnoConnOthers uint8 = 5
ConnRspStatusErrMap = map[uint8]error{
ErrnoConnInvalidStruct: errConnInvalidStruct,
ErrnoConnInvalidSrcAddr: errConnInvalidSrcAddr,
ErrnoConnAuthFailed: errConnAuthFailed,
ErrnoConnVerTooHigh: errConnVerTooHigh,
ErrnoConnOthers: errConnOthers,
}
errConnInvalidStruct = errors.New("connect response status: invalid protocol structure")
errConnInvalidSrcAddr = errors.New("connect response status: invalid source address")
errConnAuthFailed = errors.New("connect response status: auth failed")
errConnVerTooHigh = errors.New("connect response status: protocol version is too high")
errConnOthers = errors.New("connect response status: other errors")
)
func now() (string, uint32) {
s := time.Now().Format("0102150405")
i, _ := strconv.Atoi(s)
return s, uint32(i)
}
// CmppConnReqPkt represents a Cmpp2 or Cmpp3 connect request packet.
//
// when used in client side(pack), you should initialize it with
// correct SourceAddr(SrcAddr), Secret and Version.
//
// when used in server side(unpack), nothing needed to be initialized.
// unpack will fill the SourceAddr(SrcAddr), AuthSrc, Version, Timestamp
// and SeqId
//
type CmppConnReqPkt struct {
SrcAddr string
AuthSrc string
Version Type
Timestamp uint32
Secret string
SeqId uint32
}
// Cmpp2ConnRspPkt represents a Cmpp2 connect response packet.
//
// when used in server side(pack), you should initialize it with
// correct Status, AuthSrc, Secret and Version.
//
// when used in client side(unpack), nothing needed to be initialized.
// unpack will fill the Status, AuthImsg, Version and SeqId
//
type Cmpp2ConnRspPkt struct {
Status uint8
AuthIsmg string
Version Type
Secret string
AuthSrc string
SeqId uint32
}
// Cmpp3ConnRspPkt represents a Cmpp3 connect response packet.
//
// when used in server side(pack), you should initialize it with
// correct Status, AuthSrc, Secret and Version.
//
// when used in client side(unpack), nothing needed to be initialized.
// unpack will fill the Status, AuthImsg, Version and SeqId
//
type Cmpp3ConnRspPkt struct {
Status uint32
AuthIsmg string
Version Type
Secret string
AuthSrc string
SeqId uint32
}
// Pack packs the CmppConnReqPkt to bytes stream for client side.
// Before calling Pack, you should initialize a CmppConnReqPkt variable
// with correct SourceAddr(SrcAddr), Secret and Version.
func (p *CmppConnReqPkt) Pack(seqId uint32) ([]byte, error) {
var w = newPacketWriter(CmppConnReqPktLen)
// Pack header
w.WriteInt(binary.BigEndian, CmppConnReqPktLen)
w.WriteInt(binary.BigEndian, CMPP_CONNECT)
w.WriteInt(binary.BigEndian, seqId)
p.SeqId = seqId
var ts string
if p.Timestamp == 0 {
ts, p.Timestamp = now() //default: current time.
} else {
ts = cmpputils.TimeStamp2Str(p.Timestamp)
}
// Pack body
srcAddr := cmpputils.OctetString(p.SrcAddr, 6)
w.WriteString(srcAddr)
md5 := md5.Sum(bytes.Join([][]byte{[]byte(srcAddr),
make([]byte, 9),
[]byte(p.Secret),
[]byte(ts)},
nil))
p.AuthSrc = string(md5[:])
w.WriteString(p.AuthSrc)
w.WriteInt(binary.BigEndian, p.Version)
w.WriteInt(binary.BigEndian, p.Timestamp)
return w.Bytes()
}
// Unpack unpack the binary byte stream to a CmppConnReqPkt variable.
// Usually it is used in server side. After unpack, you will get SeqId, SourceAddr,
// AuthenticatorSource, Version and Timestamp.
func (p *CmppConnReqPkt) Unpack(data []byte) error {
var r = newPacketReader(data)
// Sequence Id
r.ReadInt(binary.BigEndian, &p.SeqId)
// Body: Source_Addr
var sa = make([]byte, 6)
r.ReadBytes(sa)
p.SrcAddr = string(sa)
// Body: AuthSrc
var as = make([]byte, 16)
r.ReadBytes(as)
p.AuthSrc = string(as)
// Body: Version
r.ReadInt(binary.BigEndian, &p.Version)
// Body: timestamp
r.ReadInt(binary.BigEndian, &p.Timestamp)
return r.Error()
}
// Pack packs the Cmpp2ConnRspPkt to bytes stream for server side.
// Before calling Pack, you should initialize a Cmpp2ConnRspPkt variable
// with correct Status,AuthenticatorSource, Secret and Version.
func (p *Cmpp2ConnRspPkt) Pack(seqId uint32) ([]byte, error) {
var w = newPacketWriter(Cmpp2ConnRspPktLen)
// pack header
w.WriteInt(binary.BigEndian, Cmpp2ConnRspPktLen)
w.WriteInt(binary.BigEndian, CMPP_CONNECT_RESP)
w.WriteInt(binary.BigEndian, seqId)
p.SeqId = seqId
// pack body
w.WriteInt(binary.BigEndian, p.Status)
md5 := md5.Sum(bytes.Join([][]byte{[]byte{p.Status},
[]byte(p.AuthSrc),
[]byte(p.Secret)},
nil))
p.AuthIsmg = string(md5[:])
w.WriteString(p.AuthIsmg)
w.WriteInt(binary.BigEndian, p.Version)
return w.Bytes()
}
// Unpack unpack the binary byte stream to a Cmpp2ConnRspPkt variable.
// Usually it is used in client side. After unpack, you will get SeqId, Status,
// AuthenticatorIsmg, and Version.
// Parameter data contains seqId in header and the whole packet body.
func (p *Cmpp2ConnRspPkt) Unpack(data []byte) error {
var r = newPacketReader(data)
// Sequence Id
r.ReadInt(binary.BigEndian, &p.SeqId)
// Body: Status
r.ReadInt(binary.BigEndian, &p.Status)
// Body: AuthenticatorISMG
var s = make([]byte, 16)
r.ReadBytes(s)
p.AuthIsmg = string(s)
// Body: Version
r.ReadInt(binary.BigEndian, &p.Version)
return r.Error()
}
// Pack packs the Cmpp3ConnRspPkt to bytes stream for server side.
// Before calling Pack, you should initialize a Cmpp3ConnRspPkt variable
// with correct Status,AuthenticatorSource, Secret and Version.
func (p *Cmpp3ConnRspPkt) Pack(seqId uint32) ([]byte, error) {
var w = newPacketWriter(Cmpp3ConnRspPktLen)
// pack header
w.WriteInt(binary.BigEndian, Cmpp3ConnRspPktLen)
w.WriteInt(binary.BigEndian, CMPP_CONNECT_RESP)
w.WriteInt(binary.BigEndian, seqId)
p.SeqId = seqId
// pack body
w.WriteInt(binary.BigEndian, p.Status)
var statusBuf = new(bytes.Buffer)
err := binary.Write(statusBuf, binary.BigEndian, p.Status)
if err != nil {
return nil, err
}
md5 := md5.Sum(bytes.Join([][]byte{statusBuf.Bytes(),
[]byte(p.AuthSrc),
[]byte(p.Secret)},
nil))
p.AuthIsmg = string(md5[:])
w.WriteString(p.AuthIsmg)
w.WriteInt(binary.BigEndian, p.Version)
return w.Bytes()
}
// Unpack unpack the binary byte stream to a Cmpp3ConnRspPkt variable.
// Usually it is used in client side. After unpack, you will get SeqId, Status,
// AuthenticatorIsmg, and Version.
// Parameter data contains seqId in header and the whole packet body.
func (p *Cmpp3ConnRspPkt) Unpack(data []byte) error {
var r = newPacketReader(data)
// Sequence Id
r.ReadInt(binary.BigEndian, &p.SeqId)
// Body: Status
r.ReadInt(binary.BigEndian, &p.Status)
// Body: AuthenticatorISMG
var s = make([]byte, 16)
r.ReadBytes(s)
p.AuthIsmg = string(s)
// Body: Version
r.ReadInt(binary.BigEndian, &p.Version)
return r.Error()
}