-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdu.go
514 lines (455 loc) · 12.2 KB
/
pdu.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
package zkm
import (
"bytes"
"encoding/binary"
"fmt"
)
type Id uint32
type Status uint32
const pduHeaderPartSize = 4
const (
GenericNack Id = 0x80000000
BindReceiver Id = 0x00000001
BindReceiverResp Id = 0x80000001
BindTransmitter Id = 0x00000002
BindTransmitterResp Id = 0x80000002
QuerySm Id = 0x00000003
QuerySmResp Id = 0x80000003
SubmitSm Id = 0x00000004
SubmitSmResp Id = 0x80000004
DeliverSm Id = 0x00000005
DeliverSmResp Id = 0x80000005
Unbind Id = 0x00000006
UnbindResp Id = 0x80000006
ReplaceSm Id = 0x00000007
ReplaceSmResp Id = 0x80000007
CancelSm Id = 0x00000008
CancelSmResp Id = 0x80000008
BindTransceiver Id = 0x00000009
BindTransceiverResp Id = 0x80000009
Outbind Id = 0x0000000B
EnquireLink Id = 0x00000015
EnquireLinkResp Id = 0x80000015
SubmitMulti Id = 0x00000021
SubmitMultiResp Id = 0x80000021
AlertNotification Id = 0x00000102
DataSm Id = 0x00000103
DataSmResp Id = 0x80000103
)
const (
EsmeROk Status = 0x00000000
EsmeRInvMsgLen Status = 0x00000001
EsmeRInvCmdLen Status = 0x00000002
EsmeRInvCmdId Status = 0x00000003
EsmeRInvBndSts Status = 0x00000004
EsmeRAlyBnd Status = 0x00000005
EsmeRInvPrtFlg Status = 0x00000006
EsmeRInvRegDlvFlg Status = 0x00000007
EsmeRSysErr Status = 0x00000008
EsmeRInvSrcAdr Status = 0x0000000a
EsmeRInvDstAdr Status = 0x0000000b
EsmeRInvMsgId Status = 0x0000000c
EsmeRBindFail Status = 0x0000000d
EsmeRInvPaswd Status = 0x0000000e
EsmeRInvSysId Status = 0x0000000f
EsmeRCancelFail Status = 0x00000011
EsmeRReplaceFail Status = 0x00000013
EsmeRMsgQFul Status = 0x00000014
EsmeRInvSerTyp Status = 0x00000015
EsmeRInvNumDests Status = 0x00000033
EsmeRInvDLName Status = 0x00000034
EsmeRInvDestFlag Status = 0x00000040
EsmeRInvSubRep Status = 0x00000042
EsmeRInvEsmClass Status = 0x00000043
EsmeRCntSubDL Status = 0x00000044
EsmeRSubmitFail Status = 0x00000045
EsmeRInvSrcTon Status = 0x00000048
EsmeRInvSrcNpi Status = 0x00000049
EsmeRInvDstTon Status = 0x00000050
EsmeRInvDstNpi Status = 0x00000051
EsmeRInvSysTyp Status = 0x00000053
EsmeRInvRepFlag Status = 0x00000054
EsmeRInvNumMsgs Status = 0x00000055
EsmeRThrottled Status = 0x00000058
EsmeRInvSched Status = 0x00000061
EsmeRInvExpire Status = 0x00000062
EsmeRInvDftMsgId Status = 0x00000063
EsmeRxTAppn Status = 0x00000064
EsmeRxPAppn Status = 0x00000065
EsmeRxRAppn Status = 0x00000066
EsmeRQueryFail Status = 0x00000067
EsmeRInvOptParStream Status = 0x000000c0
EsmeROptParNotAllwd Status = 0x000000c1
EsmeRInvParLen Status = 0x000000c2
EsmeRMissingOptParam Status = 0x000000c3
EsmeRInvOptParamVal Status = 0x000000c4
EsmeRDeliveryFailure Status = 0x000000fe
EsmeRUnknownErr Status = 0x000000ff
)
type Pdu struct {
id Id
status Status
seq uint32
mandatoryParams *mandatoryParams
optionalParams *optionalParams
raw []byte
}
func NewEmptyPdu() *Pdu {
return &Pdu{
id: 0,
mandatoryParams: newMandatoryParams(0),
optionalParams: newOptionalParams(),
raw: nil,
}
}
func NewPdu(id Id) *Pdu {
return &Pdu{
id: id,
mandatoryParams: newMandatoryParams(id),
optionalParams: newOptionalParams(),
raw: nil,
}
}
func (pdu *Pdu) Id() Id {
return pdu.id
}
func (pdu *Pdu) Status() Status {
return pdu.status
}
func (pdu *Pdu) SetStatus(status Status) {
pdu.status = status
if pdu.raw != nil {
rawStatus := make([]byte, pduHeaderPartSize)
binary.BigEndian.PutUint32(rawStatus, uint32(status))
copy(pdu.raw[2*pduHeaderPartSize:], rawStatus)
}
}
func (pdu *Pdu) Seq() uint32 {
return pdu.seq
}
func (pdu *Pdu) SetSeq(seq uint32) {
pdu.seq = seq
if pdu.raw != nil {
rawSeq := make([]byte, pduHeaderPartSize)
binary.BigEndian.PutUint32(rawSeq, seq)
copy(pdu.raw[3*pduHeaderPartSize:], rawSeq)
}
}
func (pdu *Pdu) String() string {
return fmt.Sprintf("id:%v status:%v seq:%v", pdu.Id(), pdu.Status(), pdu.Seq())
}
func (pdu *Pdu) IsReq() bool {
return pdu.id&0x80000000 == 0
}
func (pdu *Pdu) CreateResp(status Status) (*Pdu, error) {
var resp *Pdu
switch pdu.id {
case BindReceiver:
resp = NewPdu(BindReceiverResp)
case BindTransmitter:
resp = NewPdu(BindTransmitterResp)
case QuerySm:
resp = NewPdu(QuerySmResp)
case SubmitSm:
resp = NewPdu(SubmitSmResp)
case DeliverSm:
resp = NewPdu(DeliverSmResp)
case Unbind:
resp = NewPdu(UnbindResp)
case ReplaceSm:
resp = NewPdu(ReplaceSmResp)
case CancelSm:
resp = NewPdu(CancelSmResp)
case BindTransceiver:
resp = NewPdu(BindTransceiverResp)
case EnquireLink:
resp = NewPdu(EnquireLinkResp)
case SubmitMulti:
resp = NewPdu(SubmitMultiResp)
case DataSm:
resp = NewPdu(DataSmResp)
default:
return nil, fmt.Errorf("cann't create resp for cmd id [%v]", pdu.Id())
}
resp.SetSeq(pdu.Seq())
resp.SetStatus(status)
return resp, nil
}
func (pdu *Pdu) Serialize() []byte {
if pdu.raw != nil {
return pdu.raw
}
buff := bytes.Buffer{}
b := make([]byte, pduHeaderPartSize)
binary.BigEndian.PutUint32(b, pdu.Len())
buff.Write(b)
binary.BigEndian.PutUint32(b, uint32(pdu.id))
buff.Write(b)
binary.BigEndian.PutUint32(b, uint32(pdu.status))
buff.Write(b)
binary.BigEndian.PutUint32(b, pdu.seq)
buff.Write(b)
buff.Write(pdu.mandatoryParams.serialize())
buff.Write(pdu.optionalParams.serialize())
pdu.raw = buff.Bytes()
return pdu.raw
}
func (pdu *Pdu) Deserialize(raw []byte) error {
buff := bytes.NewBuffer(raw)
b := make([]byte, pduHeaderPartSize)
n, err := buff.Read(b)
if n != pduHeaderPartSize {
return err
}
l := binary.BigEndian.Uint32(b)
if int(l) != len(raw) {
return fmt.Errorf("bad pdu length: in field - %v, received - %v", l, len(raw))
}
n, err = buff.Read(b)
if n != pduHeaderPartSize {
return err
}
pdu.id = Id(binary.BigEndian.Uint32(b))
n, err = buff.Read(b)
if n != pduHeaderPartSize {
return err
}
pdu.status = Status(binary.BigEndian.Uint32(b))
n, err = buff.Read(b)
if n != pduHeaderPartSize {
return err
}
pdu.seq = binary.BigEndian.Uint32(b)
pdu.mandatoryParams = newMandatoryParams(pdu.id)
if err = pdu.mandatoryParams.deserialize(buff); err != nil {
return err
}
pdu.optionalParams = newOptionalParams()
if err = pdu.optionalParams.deserialize(buff); err != nil {
return err
}
pdu.raw = raw
return nil
}
func (pdu *Pdu) SetMain(name Name, value interface{}) error {
p, err := pdu.mandatoryParams.get(name)
if err != nil {
return err
}
pdu.raw = nil
return p.value().set(value)
}
func (pdu *Pdu) GetMainAsRaw(name Name) ([]byte, error) {
p, err := pdu.mandatoryParams.get(name)
if err != nil {
return nil, err
}
return p.value().raw(), nil
}
func (pdu *Pdu) GetMainAsString(name Name) (string, error) {
p, err := pdu.mandatoryParams.get(name)
if err != nil {
return "", err
}
return p.value().String(), nil
}
func (pdu *Pdu) GetMainAsUint32(name Name) (uint32, error) {
p, err := pdu.mandatoryParams.get(name)
if err != nil {
return 0, err
}
return p.value().uint32()
}
func (pdu *Pdu) SetOpt(tag Tag, value interface{}) error {
pdu.raw = nil
return pdu.optionalParams.add(tag, value)
}
func (pdu *Pdu) RemoveOpt(tag Tag) {
pdu.optionalParams.remove(tag)
}
func (pdu *Pdu) GetOptAsRaw(tag Tag) ([]byte, error) {
p, err := pdu.optionalParams.get(tag)
if err != nil {
return nil, err
}
return p.value().raw(), nil
}
func (pdu *Pdu) GetOptAsString(tag Tag) (string, error) {
p, err := pdu.optionalParams.get(tag)
if err != nil {
return "", err
}
return p.value().String(), nil
}
func (pdu *Pdu) GetOptAsUint32(tag Tag) (uint32, error) {
p, err := pdu.optionalParams.get(tag)
if err != nil {
return 0, err
}
return p.value().uint32()
}
func (pdu *Pdu) Len() uint32 {
return 4*pduHeaderPartSize + pdu.mandatoryParams.len() + pdu.optionalParams.len()
}
func (id Id) String() string {
switch id {
case GenericNack:
return "GenericNack"
case BindReceiver:
return "BindReceiver"
case BindReceiverResp:
return "BindReceiverResp"
case BindTransmitter:
return "BindTransmitter"
case BindTransmitterResp:
return "BindTransmitterResp"
case QuerySm:
return "QuerySm"
case QuerySmResp:
return "QuerySmResp"
case SubmitSm:
return "SubmitSm"
case SubmitSmResp:
return "SubmitSmResp"
case DeliverSm:
return "DeliverSm"
case DeliverSmResp:
return "DeliverSmResp"
case Unbind:
return "Unbind"
case UnbindResp:
return "UnbindResp"
case ReplaceSm:
return "ReplaceSm"
case ReplaceSmResp:
return "ReplaceSmResp"
case CancelSm:
return "CancelSm"
case CancelSmResp:
return "CancelSmResp"
case BindTransceiver:
return "BindTransceiver"
case BindTransceiverResp:
return "BindTransceiverResp"
case Outbind:
return "Outbind"
case EnquireLink:
return "EnquireLink"
case EnquireLinkResp:
return "EnquireLinkResp"
case SubmitMulti:
return "SubmitMulti"
case SubmitMultiResp:
return "SubmitMultiResp"
case AlertNotification:
return "AlertNotification"
case DataSm:
return "DataSm"
case DataSmResp:
return "DataSmResp"
default:
return "Unknown"
}
}
func (s Status) String() string {
switch s {
case EsmeROk:
return "ok"
case EsmeRInvMsgLen:
return "invalid message length"
case EsmeRInvCmdLen:
return "invalid command length"
case EsmeRInvCmdId:
return "invalid command id"
case EsmeRInvBndSts:
return "incorrect bind status for given command"
case EsmeRAlyBnd:
return "already in bound state"
case EsmeRInvPrtFlg:
return "invalid priority flag"
case EsmeRInvRegDlvFlg:
return "invalid registered delivery flag"
case EsmeRSysErr:
return "system error"
case EsmeRInvSrcAdr:
return "invalid source address"
case EsmeRInvDstAdr:
return "invalid destination address"
case EsmeRInvMsgId:
return "invalid message id"
case EsmeRBindFail:
return "bind failed"
case EsmeRInvPaswd:
return "invalid password"
case EsmeRInvSysId:
return "invalid system id"
case EsmeRCancelFail:
return "cancelsm failed"
case EsmeRReplaceFail:
return "replacesm failed"
case EsmeRMsgQFul:
return "message queue full"
case EsmeRInvSerTyp:
return "invalid service type"
case EsmeRInvNumDests:
return "invalid number of destinations"
case EsmeRInvDLName:
return "invalid distribution list name"
case EsmeRInvDestFlag:
return "invalid destination flag"
case EsmeRInvSubRep:
return "invalid 'submit with replace' request"
case EsmeRInvEsmClass:
return "invalid esm class field data"
case EsmeRCntSubDL:
return "cannot submit to distribution list"
case EsmeRSubmitFail:
return "submitsm or submitmulti failed"
case EsmeRInvSrcTon:
return "invalid source address ton"
case EsmeRInvSrcNpi:
return "invalid source address npi"
case EsmeRInvDstTon:
return "invalid destination address ton"
case EsmeRInvDstNpi:
return "invalid destination address npi"
case EsmeRInvSysTyp:
return "invalid system type field"
case EsmeRInvRepFlag:
return "invalid replace_if_present flag"
case EsmeRInvNumMsgs:
return "invalid number of messages"
case EsmeRThrottled:
return "throttling error"
case EsmeRInvSched:
return "invalid scheduled delivery time"
case EsmeRInvExpire:
return "invalid message validity period (expiry time)"
case EsmeRInvDftMsgId:
return "predefined message invalid or not found"
case EsmeRxTAppn:
return "esme receiver temporary app error code"
case EsmeRxPAppn:
return "esme receiver permanent app error code"
case EsmeRxRAppn:
return "esme receiver reject message error code"
case EsmeRQueryFail:
return "querysm request failed"
case EsmeRInvOptParStream:
return "error in the optional part of the pdu body"
case EsmeROptParNotAllwd:
return "optional parameter not allowed"
case EsmeRInvParLen:
return "invalid parameter length"
case EsmeRMissingOptParam:
return "expected optional parameter missing"
case EsmeRInvOptParamVal:
return "invalid optional parameter value"
case EsmeRDeliveryFailure:
return "delivery failure (used for datasmresp)"
case EsmeRUnknownErr:
return "unknown error"
default:
return "unknown"
}
}