forked from Philio/GoSMPP
-
Notifications
You must be signed in to change notification settings - Fork 4
/
smpp.go
291 lines (279 loc) · 6.93 KB
/
smpp.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
// GoSMPP - An SMPP library for Go
// Copyright 2010 Phil Bayfield
// This software is licensed under a Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License
// Further information on this license can be found here: http://creativecommons.org/licenses/by-sa/2.0/uk/
package smpp
// Imports
import (
"bufio"
"errors"
"fmt"
"net"
"strconv"
)
// Used for all outbound connections
type smpp struct {
conn net.Conn
reader *bufio.Reader
writer *bufio.Writer
connected bool
bound bool
async bool
sequence uint32
}
// Connect to server
func (smpp *smpp) connect(host string, port int) (err error) {
// Create TCP connection
smpp.conn, err = net.Dial("tcp", host+":"+strconv.Itoa(port))
if err != nil {
return
}
smpp.connected = true
// Setup buffered reader/writer
smpp.reader = bufio.NewReader(smpp.conn)
smpp.writer = bufio.NewWriter(smpp.conn)
return
}
// Close connection
func (smpp *smpp) close() (err error) {
err = smpp.conn.Close()
smpp.connected = false
return
}
// Send bind request (called via NewTransmitter/NewReceiver/NewTransceiver) always synchronous
func (smpp *smpp) bind(cmd, rcmd SMPPCommand, params Params) (err error) {
// Sequence number starts at 1
smpp.sequence++
// PDU header
hdr := new(PDUHeader)
hdr.CmdLength = 23 // Min length
hdr.CmdId = cmd
hdr.CmdStatus = STATUS_ESME_ROK
hdr.Sequence = smpp.sequence
// Create bind PDU
pdu := new(PDUBind)
// Mising params cause panic, this provides a clean error/exit
paramOK := false
defer func() {
if !paramOK && recover() != nil {
err = errors.New("Bind: Panic, invalid params")
return
}
}()
// Populate params
pdu.SystemId = params["systemId"].(string)
pdu.Password = params["password"].(string)
pdu.SystemType = params["systemType"].(string)
pdu.IfVersion = SMPP_INTERFACE_VER
pdu.AddrTon = params["addrTon"].(SMPPTypeOfNumber)
pdu.AddrNpi = params["addrNpi"].(SMPPNumericPlanIndicator)
pdu.AddressRange = params["addressRange"].(string)
// Add length of strings to pdu length
hdr.CmdLength += uint32(len(pdu.SystemId))
hdr.CmdLength += uint32(len(pdu.Password))
hdr.CmdLength += uint32(len(pdu.SystemType))
hdr.CmdLength += uint32(len(pdu.AddressRange))
// Params were fine 'disable' the recover
paramOK = true
// Send PDU
pdu.setHeader(hdr)
err = pdu.write(smpp.writer)
if err != nil {
return
}
// Get response
_, err = smpp.GetResp(rcmd, smpp.sequence)
return
}
// Set async commands on/offer (trancsceiver is always async)
func (smpp *smpp) Async(async bool) {
smpp.async = async
}
// Send unbind request
func (smpp *smpp) Unbind() (sequence uint32, err error) {
// Check connected and bound
if !smpp.connected || !smpp.bound {
err = errors.New("Unbind: A bound connection is required to unbind")
return
}
// Increment sequence number
smpp.sequence++
// PDU header
hdr := new(PDUHeader)
hdr.CmdLength = 16
hdr.CmdId = CMD_UNBIND
hdr.CmdStatus = STATUS_ESME_ROK
hdr.Sequence = smpp.sequence
// Create bind PDU
pdu := new(PDUUnbind)
pdu.setHeader(hdr)
// Send PDU
err = pdu.write(smpp.writer)
if err != nil {
return
}
// If not async get the response
if smpp.async {
sequence = smpp.sequence
} else {
_, err = smpp.GetResp(CMD_UNBIND_RESP, smpp.sequence)
}
return
}
// Get response PDU
func (smpp *smpp) GetResp(cmd SMPPCommand, sequence uint32) (rpdu PDU, err error) {
// Read the header
hdr := new(PDUHeader)
err = hdr.read(smpp.reader)
fmt.Printf("Header: %#v\n", hdr)
if err != nil {
return nil, err
}
// Has packet been read
pduRead := false
// Defer reading rest of packet from buffer on error
defer func() {
if err != nil && !pduRead && hdr.CmdLength > 16 {
p := make([]byte, hdr.CmdLength-16)
smpp.reader.Read(p)
}
}()
// Check cmd and/or sequence if not 0
if cmd != CMD_NONE && hdr.CmdId != cmd {
err = errors.New("Get Response: Invalid command")
return nil, err
}
// Check sequence number if not 0
if sequence > 0 && hdr.Sequence != sequence {
err = errors.New("Get Response: Invalid sequence number")
return nil, err
}
// Check for error response
if hdr.CmdStatus != STATUS_ESME_ROK {
err = errors.New("Get Response: PDU contains an error")
return nil, err
}
// Set PDU as read (to disable the deferred read)
pduRead = true
// Get response PDU
switch hdr.CmdId {
// Default unhandled PDU
default:
err = errors.New("Get Response: Unknown or unhandled PDU received")
return nil, err
// Bind responses
case CMD_BIND_RECEIVER_RESP, CMD_BIND_TRANSMITTER_RESP, CMD_BIND_TRANSCEIVER_RESP:
rpdu = new(PDUBindResp)
rpdu.setHeader(hdr)
err = rpdu.read(smpp.reader)
if err != nil {
return nil, err
}
// Set connection as bound
smpp.bound = true
// Unbind response
case CMD_UNBIND_RESP:
rpdu = new(PDUUnbindResp)
rpdu.setHeader(hdr)
err = rpdu.read(smpp.reader)
if err != nil {
return nil, err
}
// Set connection as unbound and disconnect
smpp.bound = false
smpp.close()
// SubmitSM response
case CMD_SUBMIT_SM_RESP:
rpdu = new(PDUSubmitSMResp)
rpdu.setHeader(hdr)
err = rpdu.read(smpp.reader)
if err != nil {
return nil, err
}
// SubmitMulti response
case CMD_SUBMIT_MULTI_RESP:
rpdu = new(PDUSubmitMultiResp)
rpdu.setHeader(hdr)
err = rpdu.read(smpp.reader)
if err != nil {
return nil, err
}
}
return
}
// Create a new Transmitter
func NewTransmitter(host string, port int, params Params) (tx *Transmitter, err error) {
// Merge params with defaults
allParams := mergeParams(params, defaultsBind)
// Create new transmitter
tx = new(Transmitter)
// Connect to server
err = tx.connect(host, port)
if err != nil {
return nil, err
}
// Close connection on error
defer func() {
if err != nil {
tx.close()
}
}()
// Bind with server
err = tx.bind(CMD_BIND_TRANSMITTER, CMD_BIND_TRANSMITTER_RESP, allParams)
if err != nil {
return nil, err
}
return
}
// Create a new Receiver
func NewReceiver(host string, port int, params Params) (rx *Receiver, err error) {
// Merge params with defaults
allParams := mergeParams(params, defaultsBind)
// Create new receiver
rx = new(Receiver)
// Connect to server
err = rx.connect(host, port)
if err != nil {
return nil, err
}
// Close connection on error
defer func() {
if err != nil {
rx.close()
}
}()
// Bind with server
err = rx.bind(CMD_BIND_RECEIVER, CMD_BIND_RECEIVER_RESP, allParams)
if err != nil {
return nil, err
}
return
}
// Create a new Transceiver
func NewTransceiver(host string, port int, params Params) (trx *Transceiver, err error) {
// Merge params with defaults
allParams := mergeParams(params, defaultsBind)
// Create new receiver
trx = new(Transceiver)
// Connect to server
err = trx.connect(host, port)
if err != nil {
return nil, err
}
// Close connection on error
defer func() {
if err != nil {
trx.close()
}
}()
// Bind with server
err = trx.bind(CMD_BIND_TRANSCEIVER, CMD_BIND_TRANSCEIVER_RESP, allParams)
if err != nil {
return nil, err
}
return
}
// Create a new Server
func NewServer() {
return
}