-
Notifications
You must be signed in to change notification settings - Fork 38
/
pdu.go
270 lines (227 loc) · 5.79 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
package smpp34
import (
"bytes"
"encoding/binary"
"io"
"reflect"
)
const (
PduLenErr PduReadErr = "Invalid PDU length"
)
type PduReadErr string
type PduCmdIdErr string
type Pdu interface {
Fields() map[string]Field
MandatoryFieldsList() []string
GetField(string) Field
GetHeader() *Header
TLVFields() map[uint16]*TLVField
Writer() []byte
SetField(f string, v interface{}) error
SetTLVField(t, l int, v []byte) error
SetSeqNum(uint32)
Ok() bool
}
func (p PduReadErr) Error() string {
return string(p)
}
func (p PduCmdIdErr) Error() string {
return string(p)
}
func ParsePdu(data []byte) (Pdu, error) {
if len(data) < 16 {
return nil, PduLenErr
}
header := ParsePduHeader(data[:16])
if int(header.Length) != len(data) {
return nil, PduLenErr
}
switch header.Id {
case SUBMIT_SM:
n, err := NewSubmitSm(header, data[16:])
return Pdu(n), err
case SUBMIT_SM_RESP:
n, err := NewSubmitSmResp(header, data[16:])
return Pdu(n), err
case DELIVER_SM:
n, err := NewDeliverSm(header, data[16:])
return Pdu(n), err
case DELIVER_SM_RESP:
n, err := NewDeliverSmResp(header, data[16:])
return Pdu(n), err
case BIND_TRANSCEIVER, BIND_RECEIVER, BIND_TRANSMITTER:
n, err := NewBind(header, data[16:])
return Pdu(n), err
case BIND_TRANSCEIVER_RESP, BIND_RECEIVER_RESP, BIND_TRANSMITTER_RESP:
n, err := NewBindResp(header, data[16:])
return Pdu(n), err
case ENQUIRE_LINK:
n, err := NewEnquireLink(header)
return Pdu(n), err
case ENQUIRE_LINK_RESP:
n, err := NewEnquireLinkResp(header)
return Pdu(n), err
case UNBIND:
n, err := NewUnbind(header)
return Pdu(n), err
case UNBIND_RESP:
n, err := NewUnbindResp(header)
return Pdu(n), err
case GENERIC_NACK:
n, err := NewGenericNack(header)
return Pdu(n), err
case QUERY_SM:
n, err := NewQuerySm(header, data[16:])
return Pdu(n), err
case QUERY_SM_RESP:
n, err := NewQuerySmResp(header, data[16:])
return Pdu(n), err
default:
return nil, PduCmdIdErr(header.Id.Error())
}
}
func ParsePduHeader(data []byte) *Header {
return NewPduHeader(
unpackUi32(data[:4]),
CMDId(unpackUi32(data[4:8])),
CMDStatus(unpackUi32(data[8:12])),
unpackUi32(data[12:16]),
)
}
func create_pdu_fields(fieldNames []string, r *bytes.Buffer) (map[string]Field, map[uint16]*TLVField, error) {
fields := make(map[string]Field)
eof := false
for _, k := range fieldNames {
switch k {
case SERVICE_TYPE, SOURCE_ADDR, DESTINATION_ADDR, SCHEDULE_DELIVERY_TIME, VALIDITY_PERIOD, SYSTEM_ID, PASSWORD, SYSTEM_TYPE, ADDRESS_RANGE, MESSAGE_ID, FINAL_DATE, MESSAGE_STATE, ERROR_CODE:
// Review this for fields that could be 1 or 17 int in length (E.g: FINAL_DATE)
t, err := r.ReadBytes(0x00)
if err == io.EOF {
eof = true
} else if err != nil {
return nil, nil, err
}
if len(t) == 0 {
fields[k] = NewVariableField(t)
} else {
fields[k] = NewVariableField(t[:len(t)-1])
}
case SOURCE_ADDR_TON, SOURCE_ADDR_NPI, DEST_ADDR_TON, DEST_ADDR_NPI, ESM_CLASS, PROTOCOL_ID, PRIORITY_FLAG, REGISTERED_DELIVERY, REPLACE_IF_PRESENT_FLAG, DATA_CODING, SM_DEFAULT_MSG_ID, INTERFACE_VERSION, ADDR_TON, ADDR_NPI:
t, err := r.ReadByte()
if err == io.EOF {
eof = true
} else if err != nil {
return nil, nil, err
}
fields[k] = NewFixedField(t)
case SM_LENGTH:
// Short Message Length
t, err := r.ReadByte()
if err == io.EOF {
eof = true
} else if err != nil {
return nil, nil, err
}
fields[k] = NewFixedField(t)
// Short Message
p := make([]byte, t)
_, err = r.Read(p)
if err == io.EOF {
eof = true
} else if err != nil {
return nil, nil, err
}
fields[SHORT_MESSAGE] = NewSMField(p)
case SHORT_MESSAGE:
continue
}
}
// Optional Fields
tlvs := map[uint16]*TLVField{}
var err error
if !eof {
tlvs, err = parse_tlv_fields(r)
if err != nil {
return nil, nil, err
}
}
return fields, tlvs, nil
}
func parse_tlv_fields(r *bytes.Buffer) (map[uint16]*TLVField, error) {
tlvs := map[uint16]*TLVField{}
for {
p := make([]byte, 4)
_, err := r.Read(p)
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
// length
l := unpackUi16(p[2:4])
// Get Value
v := make([]byte, l)
_, err = r.Read(v)
if err != nil {
return nil, err
}
tlvs[unpackUi16(p[0:2])] = &TLVField{
unpackUi16(p[0:2]),
unpackUi16(p[2:4]),
v,
}
}
return tlvs, nil
}
func validate_pdu_field(f string, v interface{}) bool {
switch f {
case SOURCE_ADDR_TON, SOURCE_ADDR_NPI, DEST_ADDR_TON, DEST_ADDR_NPI, ESM_CLASS, PROTOCOL_ID, PRIORITY_FLAG, REGISTERED_DELIVERY, REPLACE_IF_PRESENT_FLAG, DATA_CODING, SM_DEFAULT_MSG_ID, INTERFACE_VERSION, ADDR_TON, ADDR_NPI, SM_LENGTH, MESSAGE_STATE, ERROR_CODE:
if validate_pdu_field_type(0x00, v) {
return true
}
case SERVICE_TYPE, SOURCE_ADDR, DESTINATION_ADDR, SCHEDULE_DELIVERY_TIME, VALIDITY_PERIOD, SYSTEM_ID, PASSWORD, SYSTEM_TYPE, ADDRESS_RANGE, MESSAGE_ID, SHORT_MESSAGE, FINAL_DATE:
if validate_pdu_field_type("string", v) {
return true
} else if f == SHORT_MESSAGE && validate_pdu_field_type([]byte{0x00}, v) {
return true
}
}
return false
}
func validate_pdu_field_type(t interface{}, v interface{}) bool {
if reflect.TypeOf(t) == reflect.TypeOf(v) {
return true
}
return false
}
func included_check(a []string, v string) bool {
for _, k := range a {
if k == v {
return true
}
}
return false
}
func unpackUi32(b []byte) (n uint32) {
n = binary.BigEndian.Uint32(b)
return
}
func packUi32(n uint32) (b []byte) {
b = make([]byte, 4)
binary.BigEndian.PutUint32(b, n)
return
}
func unpackUi16(b []byte) (n uint16) {
n = binary.BigEndian.Uint16(b)
return
}
func packUi16(n uint16) (b []byte) {
b = make([]byte, 2)
binary.BigEndian.PutUint16(b, n)
return
}
func packUi8(n uint8) (b []byte) {
b = make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(n))
return b[1:]
}