forked from vponomarev/libsmpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
600 lines (507 loc) · 13.4 KB
/
encode.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
package libsmpp
import (
"encoding/binary"
"fmt"
"github.com/vponomarev/libsmpp/const"
)
func ReadCString(b []byte, maxLen int, fieldName string) (data string, l int, err error) {
if (maxLen < 1) || (maxLen > len(b)) {
maxLen = len(b)
}
for l = 0; l < maxLen; l++ {
if b[l] == 0 {
if l > 0 {
data = string(b[0:l])
} else {
data = ""
}
// Skip trailing 0x00
l++
return
}
}
// No terminator found, copy the least part of the line and raise an error
data = string(b[0:maxLen])
err = fmt.Errorf("No CString terminator for field [%s]", fieldName)
return
}
func (p *SMPPPacket) DecodeHDR(b []byte) error {
if len(b) < 16 {
return fmt.Errorf("Header is too short (%d, expecting 16 or mode bytes)", len(b))
}
p.Hdr.Len = binary.BigEndian.Uint32(b[0:])
p.Hdr.ID = binary.BigEndian.Uint32(b[4:])
p.Hdr.Status = binary.BigEndian.Uint32(b[8:])
p.Hdr.Seq = binary.BigEndian.Uint32(b[12:])
if p.Hdr.Len > MaxSMPPPacketSize {
return fmt.Errorf("Packet body is too large (%d, allowed only %d bytes)", p.Hdr.Len, MaxSMPPPacketSize)
}
if p.Hdr.Len < 16 {
return fmt.Errorf("Packet body is too short (%d)", p.Hdr.Len)
}
return nil
}
// Encode ENQUIRE_LINK
func (s *SMPPSession) EncodeEnquireLinkRAW(seq uint32) []byte {
buf := make([]byte, 16)
binary.BigEndian.PutUint32(buf, 16)
binary.BigEndian.PutUint32(buf[4:], libsmpp.CMD_ENQUIRE_LINK)
binary.BigEndian.PutUint32(buf[8:], 0)
binary.BigEndian.PutUint32(buf[12:], seq)
return buf
}
// Encode ENQUIRE_LINK_RESP
func (s *SMPPSession) EncodeEnquireLinkRespRAW(seq uint32) []byte {
buf := make([]byte, 16)
binary.BigEndian.PutUint32(buf, 16)
binary.BigEndian.PutUint32(buf[4:], libsmpp.CMD_ENQUIRE_LINK_RESP)
binary.BigEndian.PutUint32(buf[8:], 0)
binary.BigEndian.PutUint32(buf[12:], seq)
return buf
}
// Encode BindResp
func (s *SMPPSession) EncodeBindRespRAW(ID uint32, seq uint32, status uint32, systemID string) []byte {
buf := make([]byte, MaxSMPPPacketSize)
pl := uint32(16 + len(systemID) + 1)
binary.BigEndian.PutUint32(buf, pl)
binary.BigEndian.PutUint32(buf[4:], ID+0x80000000)
binary.BigEndian.PutUint32(buf[8:], status)
binary.BigEndian.PutUint32(buf[12:], seq)
copy(buf[16:16+len(systemID)], []byte(systemID))
buf[pl] = 0
return buf[0:pl]
}
// Decode BindResp
func (s *SMPPSession) DecodeBindResp(p *SMPPPacket) (state uint32, SystemID string, err error) {
state = p.Hdr.Status
if p.Hdr.Len == 16 {
return
}
SystemID, _, err = ReadCString(p.Body, len(p.Body), "SystemID")
return
}
// Generate SubmitSM Resp packet
func (s *SMPPSession) EncodeSubmitSmResp(p SMPPPacket, status uint32, msgID string) (pr SMPPPacket) {
pr = SMPPPacket{
Hdr: SMPPHeader{
ID: libsmpp.CMD_SUBMIT_SM_RESP,
Status: status,
Seq: p.Hdr.Seq,
},
BodyLen: uint32(len(msgID) + 1),
SeqComplete: true,
}
pr.Body = make([]byte, len(msgID)+1)
copy(pr.Body, msgID)
return
}
// Generate DeliverSM Resp packet
func (s *SMPPSession) EncodeDeliverSmResp(p SMPPPacket, status uint32) (pr SMPPPacket) {
pr = SMPPPacket{
Hdr: SMPPHeader{
ID: libsmpp.CMD_DELIVER_SM_RESP,
Status: status,
Seq: p.Hdr.Seq,
},
BodyLen: 1,
SeqComplete: true,
}
pr.Body = make([]byte, 1)
pr.Body[0] = 0
return
}
// Generate GENERIC_NACK
func (s *SMPPSession) EncodeGenericNack(p SMPPPacket, status uint32) (pr SMPPPacket) {
pr = SMPPPacket{
Hdr: SMPPHeader{
ID: libsmpp.CMD_GENERIC_NACK,
Status: status,
Seq: p.Hdr.Seq,
},
BodyLen: 0,
SeqComplete: true,
}
return
}
func (s *SMPPSession) DecodeBind(p *SMPPPacket) error {
// Validate correct command ID
switch p.Hdr.ID {
case libsmpp.CMD_BIND_RECEIVER:
s.Cs.sm = CSMPPRX
case libsmpp.CMD_BIND_TRANSMITTER:
s.Cs.sm = CSMPPTX
case libsmpp.CMD_BIND_TRANSCIEVER:
s.Cs.sm = CSMPPTRX
default:
return fmt.Errorf("Unsupported bind commaind ID [%d]", p.Hdr.ID)
}
s.Bind.ConnMode = s.Cs.sm
// Decode systemID
var l, offset int
var erx error
offset = 0
s.Bind.SystemID, l, erx = ReadCString(p.Body[offset:], 16, "SystemID")
offset += l
if erx != nil {
return erx
}
s.Bind.Password, l, erx = ReadCString(p.Body[offset:], 9, "Password")
offset += l
if erx != nil {
return erx
}
s.Bind.SystemType, l, erx = ReadCString(p.Body[offset:], 13, "SystemType")
offset += l
if erx != nil {
return erx
}
if offset >= int(p.BodyLen) {
return fmt.Errorf("Invalid packet, no data for InterfaceVersion")
}
// Interface version
s.Bind.IVersion = uint(p.Body[offset])
offset++
if offset >= int(p.BodyLen) {
return fmt.Errorf("Invalid packet, no data for AddrTON")
}
// AddrTON
s.Bind.AddrTON = uint(p.Body[offset])
offset++
if offset > int(p.BodyLen) {
return fmt.Errorf("Invalid packet, no data for AddrNPI")
}
// AddrNPI
s.Bind.AddrNPI = uint(p.Body[offset])
offset++
if offset >= int(p.BodyLen) {
return fmt.Errorf("Invalid packet, no data for AddressRange")
}
s.Bind.AddrRange, l, erx = ReadCString(p.Body[offset:], 13, "AddressRange")
offset += l
if erx != nil {
return erx
}
if offset != int(p.BodyLen) {
return fmt.Errorf("Invalid packet body len [HDR: %d, Context: %d]", p.BodyLen, offset)
}
return nil
}
// Generate BIND packet
func (s *SMPPSession) EncodeBind(m ConnSMPPMode, b SMPPBind) (p SMPPPacket, err error) {
var cmdid uint32
switch m {
case CSMPPTX:
cmdid = libsmpp.CMD_BIND_TRANSMITTER
case CSMPPRX:
cmdid = libsmpp.CMD_BIND_RECEIVER
case CSMPPTRX:
cmdid = libsmpp.CMD_BIND_TRANSCIEVER
default:
return SMPPPacket{}, fmt.Errorf("Invalid connection mode")
}
//
// Validate max entity len
if len(b.SystemID) > 15 {
return SMPPPacket{}, fmt.Errorf("Invalid length of [SystemID]: %d, maxLen = 16", len(b.SystemID))
}
if len(b.Password) > 8 {
return SMPPPacket{}, fmt.Errorf("Invalid length of [Password]: %d, maxLen = 9", len(b.Password))
}
if len(b.SystemType) > 12 {
return SMPPPacket{}, fmt.Errorf("Invalid length of [SystemType]: %d, maxLen = 13", len(b.SystemType))
}
if len(b.AddrRange) > 40 {
return SMPPPacket{}, fmt.Errorf("Invalid length of [AddressRange]: %d, maxLen = 41", len(b.SystemType))
}
//
// Generate packet
buf := make([]byte, MaxSMPPPacketSize)
offset := 0
// SystemID
copy(buf, b.SystemID)
offset += len(b.SystemID)
buf[offset] = 0
offset++
copy(buf[offset:], b.Password)
offset += len(b.Password)
buf[offset] = 0
offset++
copy(buf[offset:], b.SystemType)
offset += len(b.SystemType)
buf[offset] = 0
offset++
buf[offset] = byte(b.IVersion)
offset++
buf[offset] = byte(b.AddrTON)
offset++
buf[offset] = byte(b.AddrNPI)
offset++
copy(buf[offset:], b.AddrRange)
offset += len(b.AddrRange)
buf[offset] = 0
offset++
p = SMPPPacket{
Hdr: SMPPHeader{ID: cmdid},
BodyLen: uint32(offset),
Body: make([]byte, offset),
}
copy(p.Body, buf[0:offset])
return p, nil
}
// Input:
// ss SMPPSubmit - structure for SubmitSM/DeliverSM
// Output:
// p SMPPPacket - encoded SMPP packet
// c uint32 - SMPP Error code if present
// err error - error description
func (s *SMPPSession) EncodeSubmitDeliverSm(CMD uint32, ss SMPPSubmit) (p SMPPPacket, err error) {
// Validate max entity len
if len(ss.ServiceType) > 5 {
return SMPPPacket{}, fmt.Errorf("Invalid length of [service_type]: %d, maxLen = 5", len(ss.ServiceType))
}
if len(ss.Source.Addr) > 20 {
return SMPPPacket{}, fmt.Errorf("Invalid length of [source_addr]: %d, maxLen = 20", len(ss.Source.Addr))
}
if len(ss.Dest.Addr) > 20 {
return SMPPPacket{}, fmt.Errorf("Invalid length of [dest_addr]: %d, maxLen = 20", len(ss.Dest.Addr))
}
if len(ss.ScheduledDeliveryTime) > 16 {
return SMPPPacket{}, fmt.Errorf("Invalid length of [scheduled_delivery_time]: %d, maxLen = 16", len(ss.ScheduledDeliveryTime))
}
if len(ss.ValidityPeriod) > 16 {
return SMPPPacket{}, fmt.Errorf("Invalid length of [validity_period]: %d, maxLen = 16", len(ss.ValidityPeriod))
}
if len(ss.ShortMessages) > 254 {
return SMPPPacket{}, fmt.Errorf("Invalid length of [short_message]: %d, maxLen = 254", len(ss.ShortMessages))
}
switch CMD {
case libsmpp.CMD_SUBMIT_SM, libsmpp.CMD_DELIVER_SM:
p.Hdr.ID = CMD
default:
return SMPPPacket{}, fmt.Errorf("Invalid CommandID: %x", CMD)
}
//
// Generate packet
buf := make([]byte, MaxSMPPPacketSize)
offset := 0
// ServiceType
copy(buf, ss.ServiceType)
offset += len(ss.ServiceType)
buf[offset] = 0
offset++
// Source Address TON
buf[offset] = ss.Source.TON
offset++
// Source Address NPI
buf[offset] = ss.Source.NPI
offset++
// Source Addr
copy(buf[offset:], ss.Source.Addr)
offset += len(ss.Source.Addr)
buf[offset] = 0
offset++
// Dest Address TON
buf[offset] = ss.Dest.TON
offset++
// Dest Address NPI
buf[offset] = ss.Dest.NPI
offset++
// Dest Addr
copy(buf[offset:], ss.Dest.Addr)
offset += len(ss.Dest.Addr)
buf[offset] = 0
offset++
// ESM Class
buf[offset] = ss.ESMClass
offset++
// Protocol ID
buf[offset] = ss.ProtocolID
offset++
// Priority Flag
buf[offset] = ss.PriorityFlag
offset++
// Scheduled Delivery Time
copy(buf[offset:], ss.ScheduledDeliveryTime)
offset += len(ss.ScheduledDeliveryTime)
buf[offset] = 0
offset++
// Validity Period
copy(buf[offset:], ss.ValidityPeriod)
offset += len(ss.ValidityPeriod)
buf[offset] = 0
offset++
// Registered Delivery
buf[offset] = ss.RegisteredDelivery
offset++
// Replace If Present
buf[offset] = ss.ReplaceIfPresent
offset++
// Data Coding
buf[offset] = ss.DataCoding
offset++
// SM Default MSG ID
buf[offset] = ss.SmDefaultMsgID
offset++
// SM Length
buf[offset] = uint8(len(ss.ShortMessages))
offset++
// Short Message
copy(buf[offset:], ss.ShortMessages)
offset += len(ss.ShortMessages)
// Publish TLV if it's set
if ss.TLV != nil {
for tK, tV := range ss.TLV {
binary.BigEndian.PutUint16(buf[offset:], uint16(tK))
offset += 2
binary.BigEndian.PutUint16(buf[offset:], tV.Len)
offset += 2
copy(buf[offset:offset+int(tV.Len)], tV.Data)
offset += int(tV.Len)
}
}
p.Body = buf[0:offset]
p.BodyLen = uint32(offset)
// DONE!
return
}
func (s *SMPPSession) EncodeSubmitSm(ss SMPPSubmit) (p SMPPPacket, err error) {
return s.EncodeSubmitDeliverSm(libsmpp.CMD_SUBMIT_SM, ss)
}
func (s *SMPPSession) EncodeDeliverSm(ss SMPPSubmit) (p SMPPPacket, err error) {
return s.EncodeSubmitDeliverSm(libsmpp.CMD_DELIVER_SM, ss)
}
func (s *SMPPSession) DecodeSubmitDeliverSm(p *SMPPPacket) (ss SMPPSubmit, err error) {
// Validate correct command ID
switch p.Hdr.ID {
case libsmpp.CMD_SUBMIT_SM, libsmpp.CMD_DELIVER_SM:
default:
err = fmt.Errorf("Unsupported command ID [%d]", p.Hdr.ID)
return
}
// serviceType
var l, offset int
offset = 0
ss.ServiceType, l, err = ReadCString(p.Body[offset:], 6, "service_type")
offset += l
if err != nil {
return
}
if uint32(offset+3) > p.BodyLen {
err = fmt.Errorf("Invalid packet, no data for Source Addr Info")
return
}
// Source Addr TON
ss.Source.TON = p.Body[offset]
offset++
// Source Addr NPI
ss.Source.NPI = p.Body[offset]
offset++
// Source Addr NPI
ss.Source.Addr, l, err = ReadCString(p.Body[offset:], 21, "source_addr")
offset += l
if err != nil {
return
}
if uint32(offset+3) > p.BodyLen {
err = fmt.Errorf("Invalid packet, no data for Dest Addr Info")
return
}
// Dest Addr TON
ss.Dest.TON = p.Body[offset]
offset++
// Dest Addr NPI
ss.Dest.NPI = p.Body[offset]
offset++
// Dest Addr
ss.Dest.Addr, l, err = ReadCString(p.Body[offset:], 21, "dest_addr")
offset += l
if err != nil {
return
}
if uint32(offset+4) > p.BodyLen {
err = fmt.Errorf("Invalid packet, no data for Scheduled Delivery Time")
return
}
ss.ESMClass = p.Body[offset]
offset++
ss.ProtocolID = p.Body[offset]
offset++
ss.PriorityFlag = p.Body[offset]
offset++
// Scheduled delivery time
ss.ScheduledDeliveryTime, l, err = ReadCString(p.Body[offset:], 21, "scheduled_delivery_time")
offset += l
if err != nil {
return
}
if uint32(offset+1) > p.BodyLen {
err = fmt.Errorf("Invalid packet, no data for Validity Period")
return
}
// Validity period
ss.ValidityPeriod, l, err = ReadCString(p.Body[offset:], 21, "validity_period")
offset += l
if err != nil {
return
}
if uint32(offset+4) > p.BodyLen {
err = fmt.Errorf("Invalid packet, no data for sm_length")
return
}
ss.RegisteredDelivery = p.Body[offset]
offset++
ss.ReplaceIfPresent = p.Body[offset]
offset++
ss.DataCoding = p.Body[offset]
offset++
ss.SmDefaultMsgID = p.Body[offset]
offset++
ss.SmLength = p.Body[offset]
offset++
// Load message body
if ss.SmLength > 0 {
if uint32(offset)+uint32(ss.SmLength) > p.BodyLen {
err = fmt.Errorf("Invalid packet, no data for short_message")
return
}
ss.ShortMessages = string(p.Body[offset : offset+int(ss.SmLength)])
offset += int(ss.SmLength)
}
// Check if there is TLV
if offset == len(p.Body) {
// NO TLV!
return
}
// Init TLV structure
ss.TLV = make(map[TLVCode]TLVStruct)
// Scan TLV
for offset < len(p.Body) {
// Check for minimum TLV header size
if offset+4 > len(p.Body) {
// ERROR
err = fmt.Errorf("TLV header is truncated")
return
}
//
tlvTag := TLVCode(binary.BigEndian.Uint16(p.Body[offset:]))
tlvLen := binary.BigEndian.Uint16(p.Body[offset+2:])
// Check if tlvLen is not so large
if tlvLen > MaxSMPPPacketSize {
err = fmt.Errorf("TLV len is too large")
return
}
// Check for TLV body size
if tlvLen > 0 && (offset+4+int(tlvLen) > len(p.Body)) {
err = fmt.Errorf("TLV body is truncated")
return
}
// Add TLV into map
ss.TLV[tlvTag] = TLVStruct{
Data: p.Body[offset+4 : offset+4+int(tlvLen)],
Len: tlvLen,
}
// Switch to next TLV field
offset += 4 + int(tlvLen)
}
return
}