forked from ajankovic/smpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smpp.go
443 lines (407 loc) · 11.9 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Package smpp implements SMPP protocol v3.4.
//
// It allows easier creation of SMPP clients and servers by providing utilities for PDU and session handling.
// In order to do any kind of interaction you first need to create an SMPP [Session](https://godoc.org/github.com/sorokinmax/smpp#Session). Session is the main carrier of the protocol and enforcer of the specification rules.
//
// Naked session can be created with:
//
// // You must provide already established connection and configuration struct.
// sess := smpp.NewSession(conn, conf)
//
// But it's much more convenient to use helpers that would do the binding with the remote SMSC and return you session prepared for sending:
//
// // Bind with remote server by providing config structs.
// sess, err := smpp.BindTRx(sessConf, bindConf)
//
// And once you have the session it can be used for sending PDUs to the binded peer.
//
// sm := smpp.SubmitSm{
// SourceAddr: "11111111",
// DestinationAddr: "22222222",
// ShortMessage: "Hello from SMPP!",
// }
// // Session can then be used for sending PDUs.
// resp, err := sess.Send(p)
//
// Session that is no longer used must be closed:
//
// sess.Close()
//
// If you want to handle incoming requests to the session specify SMPPHandler in session configuration when creating new session similarly to HTTPHandler from _net/http_ package:
//
// conf := smpp.SessionConf{
// Handler: smpp.HandlerFunc(func(ctx *smpp.Context) {
// switch ctx.CommandID() {
// case pdu.UnbindID:
// ubd, err := ctx.Unbind()
// if err != nil {
// t.Errorf(err.Error())
// }
// resp := ubd.Response()
// if err := ctx.Respond(resp, pdu.StatusOK); err != nil {
// t.Errorf(err.Error())
// }
// }
// }),
// }
//
// Detailed examples for SMPP client and server can be found in the examples dir.
package smpp
import (
"context"
"net"
"time"
"github.com/sorokinmax/smpp/pdu"
)
const (
// Version of the supported SMPP Protocol. Only supporting 3.4 for now.
Version = 0x34
// SequenceStart is the starting reference for sequence number.
SequenceStart = 0x00000001
// SequenceEnd s sequence number upper boundary.
SequenceEnd = 0x7FFFFFFF
)
// BindConf is the configuration for binding to smpp servers.
type BindConf struct {
// Bind will be attempted to this addr.
Addr string
// Mandatory fields for binding PDU.
SystemID string
Password string
SystemType string
AddrTon int
AddrNpi int
AddrRange string
}
func bind(req pdu.PDU, sc SessionConf, bc BindConf) (*Session, error) {
conn, err := net.Dial("tcp", bc.Addr)
if err != nil {
return nil, err
}
sess := NewSession(conn, sc)
timeout := sc.WindowTimeout
if timeout == 0 {
timeout = time.Second * 5
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
_, err = sess.Send(ctx, req)
if err != nil {
return sess, err
}
return sess, nil
}
// BindTx binds transmitter session.
func BindTx(sc SessionConf, bc BindConf) (*Session, error) {
return bind(&pdu.BindTx{
SystemID: bc.SystemID,
Password: bc.Password,
SystemType: bc.SystemType,
InterfaceVersion: Version,
AddrTon: bc.AddrTon,
AddrNpi: bc.AddrNpi,
AddressRange: bc.AddrRange,
}, sc, bc)
}
// BindRx binds receiver session.
func BindRx(sc SessionConf, bc BindConf) (*Session, error) {
return bind(&pdu.BindRx{
SystemID: bc.SystemID,
Password: bc.Password,
SystemType: bc.SystemType,
InterfaceVersion: Version,
AddrTon: bc.AddrTon,
AddrNpi: bc.AddrNpi,
AddressRange: bc.AddrRange,
}, sc, bc)
}
// BindTRx binds transreceiver session.
func BindTRx(sc SessionConf, bc BindConf) (*Session, error) {
return bind(&pdu.BindTRx{
SystemID: bc.SystemID,
Password: bc.Password,
SystemType: bc.SystemType,
InterfaceVersion: Version,
AddrTon: bc.AddrTon,
AddrNpi: bc.AddrNpi,
AddressRange: bc.AddrRange,
}, sc, bc)
}
// Unbind session will initiate session unbinding and close the session.
// First it will try to notify peer with unbind request.
// If there was any error during unbinding an error will be returned.
// Session will be closed even if there was an error during unbind.
func Unbind(ctx context.Context, sess *Session) error {
defer func() {
sess.Close()
}()
_, err := sess.Send(ctx, pdu.Unbind{})
if err != nil {
return err
}
return nil
}
// SendGenericNack is a helper function for sending GenericNack PDU.
func SendGenericNack(ctx context.Context, sess *Session, p *pdu.GenericNack) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}
// SendBindRx is a helper function for sending BindRx PDU.
func SendBindRx(ctx context.Context, sess *Session, p *pdu.BindRx) (*pdu.BindRxResp, error) {
var tresp *pdu.BindRxResp
resp, err := sess.Send(ctx, p)
if resp != nil {
tresp = resp.(*pdu.BindRxResp)
}
if err != nil {
return tresp, err
}
return tresp, nil
}
// SendBindRxResp is a helper function for sending BindRxResp PDU.
func SendBindRxResp(ctx context.Context, sess *Session, p *pdu.BindRxResp) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}
// SendBindTx is a helper function for sending BindTx PDU.
func SendBindTx(ctx context.Context, sess *Session, p *pdu.BindTx) (*pdu.BindTxResp, error) {
var tresp *pdu.BindTxResp
resp, err := sess.Send(ctx, p)
if resp != nil {
tresp = resp.(*pdu.BindTxResp)
}
if err != nil {
return tresp, err
}
return tresp, nil
}
// SendBindTxResp is a helper function for sending BindTxResp PDU.
func SendBindTxResp(ctx context.Context, sess *Session, p *pdu.BindTxResp) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}
// SendQuerySm is a helper function for sending QuerySm PDU.
func SendQuerySm(ctx context.Context, sess *Session, p *pdu.QuerySm) (*pdu.QuerySmResp, error) {
var tresp *pdu.QuerySmResp
resp, err := sess.Send(ctx, p)
if resp != nil {
tresp = resp.(*pdu.QuerySmResp)
}
if err != nil {
return tresp, err
}
return tresp, nil
}
// SendQuerySmResp is a helper function for sending QuerySmResp PDU.
func SendQuerySmResp(ctx context.Context, sess *Session, p *pdu.QuerySmResp) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}
// SendSubmitSm is a helper function for sending SubmitSm PDU.
func SendSubmitSm(ctx context.Context, sess *Session, p *pdu.SubmitSm) (*pdu.SubmitSmResp, error) {
var tresp *pdu.SubmitSmResp
resp, err := sess.Send(ctx, p)
if resp != nil {
tresp = resp.(*pdu.SubmitSmResp)
}
if err != nil {
return tresp, err
}
return tresp, nil
}
// SendSubmitSmResp is a helper function for sending SubmitSmResp PDU.
func SendSubmitSmResp(ctx context.Context, sess *Session, p *pdu.SubmitSmResp) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}
// SendDeliverSm is a helper function for sending DeliverSm PDU.
func SendDeliverSm(ctx context.Context, sess *Session, p *pdu.DeliverSm) (*pdu.DeliverSmResp, error) {
var tresp *pdu.DeliverSmResp
resp, err := sess.Send(ctx, p)
if resp != nil {
tresp = resp.(*pdu.DeliverSmResp)
}
if err != nil {
return tresp, err
}
return tresp, nil
}
// SendDeliverSmResp is a helper function for sending DeliverSmResp PDU.
func SendDeliverSmResp(ctx context.Context, sess *Session, p *pdu.DeliverSmResp) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}
// SendUnbind is a helper function for sending Unbind PDU.
func SendUnbind(ctx context.Context, sess *Session, p *pdu.Unbind) (*pdu.UnbindResp, error) {
var tresp *pdu.UnbindResp
resp, err := sess.Send(ctx, p)
if resp != nil {
tresp = resp.(*pdu.UnbindResp)
}
if err != nil {
return tresp, err
}
return tresp, nil
}
// SendUnbindResp is a helper function for sending UnbindResp PDU.
func SendUnbindResp(ctx context.Context, sess *Session, p *pdu.UnbindResp) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}
// SendReplaceSm is a helper function for sending ReplaceSm PDU.
func SendReplaceSm(ctx context.Context, sess *Session, p *pdu.ReplaceSm) (*pdu.ReplaceSmResp, error) {
var tresp *pdu.ReplaceSmResp
resp, err := sess.Send(ctx, p)
if resp != nil {
tresp = resp.(*pdu.ReplaceSmResp)
}
if err != nil {
return tresp, err
}
return tresp, nil
}
// SendReplaceSmResp is a helper function for sending ReplaceSmResp PDU.
func SendReplaceSmResp(ctx context.Context, sess *Session, p *pdu.ReplaceSmResp) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}
// SendCancelSm is a helper function for sending CancelSm PDU.
func SendCancelSm(ctx context.Context, sess *Session, p *pdu.CancelSm) (*pdu.CancelSmResp, error) {
var tresp *pdu.CancelSmResp
resp, err := sess.Send(ctx, p)
if resp != nil {
tresp = resp.(*pdu.CancelSmResp)
}
if err != nil {
return tresp, err
}
return tresp, nil
}
// SendCancelSmResp is a helper function for sending CancelSmResp PDU.
func SendCancelSmResp(ctx context.Context, sess *Session, p *pdu.CancelSmResp) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}
// SendBindTRx is a helper function for sending BindTRx PDU.
func SendBindTRx(ctx context.Context, sess *Session, p *pdu.BindTRx) (*pdu.BindTRxResp, error) {
var tresp *pdu.BindTRxResp
resp, err := sess.Send(ctx, p)
if resp != nil {
tresp = resp.(*pdu.BindTRxResp)
}
if err != nil {
return tresp, err
}
return tresp, nil
}
// SendBindTRxResp is a helper function for sending BindTRxResp PDU.
func SendBindTRxResp(ctx context.Context, sess *Session, p *pdu.BindTRxResp) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}
// SendOutbind is a helper function for sending Outbind PDU.
func SendOutbind(ctx context.Context, sess *Session, p *pdu.Outbind) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}
// SendEnquireLink is a helper function for sending EnquireLink PDU.
func SendEnquireLink(ctx context.Context, sess *Session, p *pdu.EnquireLink) (*pdu.EnquireLinkResp, error) {
var tresp *pdu.EnquireLinkResp
resp, err := sess.Send(ctx, p)
if resp != nil {
tresp = resp.(*pdu.EnquireLinkResp)
}
if err != nil {
return tresp, err
}
return tresp, nil
}
// SendEnquireLinkResp is a helper function for sending EnquireLinkResp PDU.
func SendEnquireLinkResp(ctx context.Context, sess *Session, p *pdu.EnquireLinkResp) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}
// SendSubmitMulti is a helper function for sending SubmitMulti PDU.
func SendSubmitMulti(ctx context.Context, sess *Session, p *pdu.SubmitMulti) (*pdu.SubmitMultiResp, error) {
var tresp *pdu.SubmitMultiResp
resp, err := sess.Send(ctx, p)
if resp != nil {
tresp = resp.(*pdu.SubmitMultiResp)
}
if err != nil {
return tresp, err
}
return tresp, nil
}
// SendSubmitMultiResp is a helper function for sending SubmitMultiResp PDU.
func SendSubmitMultiResp(ctx context.Context, sess *Session, p *pdu.SubmitMultiResp) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}
// SendAlertNotification is a helper function for sending AlertNotification PDU.
func SendAlertNotification(ctx context.Context, sess *Session, p *pdu.AlertNotification) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}
// SendDataSm is a helper function for sending DataSm PDU.
func SendDataSm(ctx context.Context, sess *Session, p *pdu.DataSm) (*pdu.DataSmResp, error) {
var tresp *pdu.DataSmResp
resp, err := sess.Send(ctx, p)
if resp != nil {
tresp = resp.(*pdu.DataSmResp)
}
if err != nil {
return tresp, err
}
return tresp, nil
}
// SendDataSmResp is a helper function for sending DataSmResp PDU.
func SendDataSmResp(ctx context.Context, sess *Session, p *pdu.DataSmResp) error {
_, err := sess.Send(ctx, p)
if err != nil {
return err
}
return nil
}