200 lines
5.6 KiB
Go
200 lines
5.6 KiB
Go
// bind.go SMPP bind_transceiver 握手与 submit/deliver 消息体(Day 34)。
|
|
package smpp
|
|
|
|
import "fmt"
|
|
|
|
// Bind bind_transceiver 包体:system_id + password + system_type + interface_version +
|
|
// addr_ton + addr_npi + address_range(均为 C 字符串/单字节)。
|
|
type Bind struct {
|
|
SystemID string // ≤16
|
|
Password string // ≤9(测试用;生产建议更长,由网关约束)
|
|
SystemType string // 常用 "SMPP" 或空
|
|
InterfaceVersion byte // 0x34
|
|
}
|
|
|
|
// Marshal 编码 Bind 包体。
|
|
func (b *Bind) Marshal() []byte {
|
|
v := b.InterfaceVersion
|
|
if v == 0 {
|
|
v = 0x34
|
|
}
|
|
out := []byte{}
|
|
out = writeCString(out, b.SystemID)
|
|
out = writeCString(out, b.Password)
|
|
out = writeCString(out, b.SystemType)
|
|
out = append(out, v, 0x00, 0x00) // interface_version + TON + NPI
|
|
out = writeCString(out, "") // address_range
|
|
return out
|
|
}
|
|
|
|
// ParseBind 解析 Bind 包体(网关/模拟器侧)。
|
|
func ParseBind(body []byte) (systemID, password, systemType string, err error) {
|
|
var off int
|
|
systemID, off, err = readCString(body, 0)
|
|
if err != nil {
|
|
return "", "", "", err
|
|
}
|
|
password, off, err = readCString(body, off)
|
|
if err != nil {
|
|
return "", "", "", err
|
|
}
|
|
systemType, _, err = readCString(body, off)
|
|
if err != nil {
|
|
return "", "", "", err
|
|
}
|
|
return systemID, password, systemType, nil
|
|
}
|
|
|
|
// BindResp 包体:system_id(C 字符串)。
|
|
func BindResp(systemID string) []byte {
|
|
return writeCString(nil, systemID)
|
|
}
|
|
|
|
// ParseBindResp 解析 BindResp(取 system_id;status 在头)。
|
|
func ParseBindResp(body []byte) (systemID string, err error) {
|
|
systemID, _, err = readCString(body, 0)
|
|
return systemID, err
|
|
}
|
|
|
|
// Addr 地址三元组(TON/NPI/号码)。
|
|
type Addr struct {
|
|
Ton, Npi uint8
|
|
Address string
|
|
}
|
|
|
|
// SubmitReq submit_sm 包体(基础字段)。
|
|
type SubmitReq struct {
|
|
ServiceType string
|
|
Source Addr
|
|
Dest Addr
|
|
ESMClass uint8 // 0=普通;回执短消息时 0x04
|
|
RegisteredDelivery uint8 // 1=要求回执
|
|
DataCoding uint8 // 0=MC 缺省 8=UCS2
|
|
ShortMessage []byte
|
|
}
|
|
|
|
// Marshal 编码 submit_sm 包体。
|
|
func (s *SubmitReq) Marshal() []byte {
|
|
out := []byte{}
|
|
out = writeCString(out, s.ServiceType)
|
|
out = append(out, s.Source.Ton, s.Source.Npi)
|
|
out = writeCString(out, s.Source.Address)
|
|
out = append(out, s.Dest.Ton, s.Dest.Npi)
|
|
out = writeCString(out, s.Dest.Address)
|
|
out = append(out, s.ESMClass, 0x00, 0x00) // protocol_id, priority_flag
|
|
out = writeCString(out, "") // schedule_delivery_time
|
|
out = writeCString(out, "") // validity_period
|
|
out = append(out, s.RegisteredDelivery, 0x00, s.DataCoding, 0x00)
|
|
if len(s.ShortMessage) > 255 {
|
|
s.ShortMessage = s.ShortMessage[:255]
|
|
}
|
|
out = append(out, uint8(len(s.ShortMessage)))
|
|
out = append(out, s.ShortMessage...)
|
|
return out
|
|
}
|
|
|
|
// SubmitResp: message_id(C 字符串;status 在头)。
|
|
func SubmitResp(messageID string) []byte {
|
|
return writeCString(nil, messageID)
|
|
}
|
|
|
|
// ParseSubmitResp 解析 submit_sm_resp。
|
|
func ParseSubmitResp(body []byte) (messageID string, err error) {
|
|
messageID, _, err = readCString(body, 0)
|
|
return messageID, err
|
|
}
|
|
|
|
// Deliver deliver_sm 下发(上行短信或状态报告)。
|
|
type Deliver struct {
|
|
Source Addr
|
|
Dest Addr
|
|
ESMClass uint8 // 0x04=状态报告
|
|
ShortMessage []byte
|
|
}
|
|
|
|
// IsReport 是否为状态报告。
|
|
func (d *Deliver) IsReport() bool { return d.ESMClass&0x04 != 0 }
|
|
|
|
// MarshalDeliver 编码 deliver_sm(网关/模拟器侧)。
|
|
func (d *Deliver) Marshal() []byte {
|
|
s := &SubmitReq{Source: d.Source, Dest: d.Dest, ESMClass: d.ESMClass, ShortMessage: d.ShortMessage}
|
|
return s.Marshal()
|
|
}
|
|
|
|
// ParseDeliver 解析 deliver_sm。
|
|
func ParseDeliver(body []byte) (*Deliver, error) {
|
|
off := 0
|
|
readStr := func() (string, error) {
|
|
var s string
|
|
var err error
|
|
s, off, err = readCString(body, off)
|
|
return s, err
|
|
}
|
|
if _, err := readStr(); err != nil { // service_type
|
|
return nil, err
|
|
}
|
|
if off+2 > len(body) {
|
|
return nil, fmt.Errorf("smpp: deliver 源地址越界")
|
|
}
|
|
src := Addr{Ton: body[off], Npi: body[off+1]}
|
|
off += 2
|
|
addr, err := readStr()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
src.Address = addr
|
|
if off+2 > len(body) {
|
|
return nil, fmt.Errorf("smpp: deliver 目的地址越界")
|
|
}
|
|
dst := Addr{Ton: body[off], Npi: body[off+1]}
|
|
off += 2
|
|
dstAddr, err := readStr()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
dst.Address = dstAddr
|
|
if off+3 > len(body) {
|
|
return nil, fmt.Errorf("smpp: deliver 固定字段越界")
|
|
}
|
|
esm := body[off]
|
|
off += 3 // esm_class + protocol_id + priority_flag
|
|
for i := 0; i < 2; i++ { // schedule_delivery_time / validity_period
|
|
if _, err := readStr(); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
// registered_delivery + replace_if_present + data_coding + sm_default_msg_id
|
|
if off+4 > len(body) {
|
|
return nil, fmt.Errorf("smpp: deliver 可选字段越界")
|
|
}
|
|
off += 4
|
|
if off >= len(body) {
|
|
return nil, fmt.Errorf("smpp: deliver sm_length 越界")
|
|
}
|
|
n := int(body[off])
|
|
off++
|
|
if off+n > len(body) {
|
|
return nil, fmt.Errorf("smpp: deliver short_message %d 字节越界", n)
|
|
}
|
|
return &Deliver{Source: src, Dest: dst, ESMClass: esm, ShortMessage: body[off : off+n]}, nil
|
|
}
|
|
|
|
// ReportStat 从状态报告短消息文本提取 stat 字段。
|
|
// 文本惯例:id:XX sub:001 dlvrd:001 submit date:... done date:... stat:DELIVRD err:000 text:...
|
|
func ReportStat(shortMessage []byte) string {
|
|
text := string(shortMessage)
|
|
const key = "stat:"
|
|
for i := 0; i+len(key) <= len(text); i++ {
|
|
if text[i:i+len(key)] == key {
|
|
rest := text[i+len(key):]
|
|
for j := 0; j < len(rest); j++ {
|
|
if rest[j] == ' ' || rest[j] == '\n' {
|
|
return rest[:j]
|
|
}
|
|
}
|
|
return rest
|
|
}
|
|
}
|
|
return ""
|
|
}
|