243 lines
7.9 KiB
Go
243 lines
7.9 KiB
Go
// submit.go CMPP Submit/Deliver 包体(纯 CMPP 2.0 规范实现)。
|
|
// 字段定长按 2.0 规范表:Dest_terminal_Id 21B/个(单包最多 100 个号码),
|
|
// Msg_Content 长度 Msg_Length 为 1 字节(Octet String,≤140)。
|
|
package cmpp
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// 消息编码格式(Msg_Fmt)。
|
|
const (
|
|
MsgFmtASCII uint8 = 0x00
|
|
MsgFmtGBK uint8 = 0x0F // CMPP2.0 规范 Msg_Fmt=15(十进制,GB2312/GBK);历史笔误 0x15(=21)非标值——上游网关无法识别,GBK 按 Latin-1 落库致后台乱码(2026-09-13 上游 CMPP 日志 msgFmt=21 实证)
|
|
MsgFmtUCS2 uint8 = 0x08
|
|
)
|
|
|
|
// SubmitReq 提交短信请求。
|
|
type SubmitReq struct {
|
|
MsgID uint64 // 8B;0 表示由网关分配
|
|
PkTotal, PkNumber uint8
|
|
RegisteredDelivery uint8 // 1 要求状态报告
|
|
MsgLevel uint8
|
|
ServiceID string // ≤10
|
|
FeeUserType uint8
|
|
MsgFmt uint8 // 消息编码
|
|
TPUdhi uint8 // 1=Msg_Content 含用户数据头(长短信分片,见 split.go)
|
|
MsgSrc string // 企业代码 6B
|
|
FeeType string // "01"
|
|
FeeCode string // 6B
|
|
SrcID string // 源号码 ≤21
|
|
DestTerminalIDs []string // 接收号码(≤100 个,每个 ≤21)
|
|
Content []byte
|
|
}
|
|
|
|
// ErrTooManyDest 接收号码超限。
|
|
var ErrTooManyDest = errors.New("cmpp: 接收号码超过 100 个")
|
|
|
|
// MaxDestPerSubmit 单包号码上限(CMPP 2.0:Dest_Usr_tl ≤100;合包切包口径)。
|
|
const MaxDestPerSubmit = 100
|
|
|
|
// ErrContentTooLong 消息内容超 140 字节(CMPP 2.0 Msg_Length 为 1B)。
|
|
var ErrContentTooLong = errors.New("cmpp: 消息内容超过 140 字节")
|
|
|
|
// appendFixed 定长字段(左对齐右补零,超长截断)。
|
|
func appendFixed(b []byte, s string, n int) []byte {
|
|
fixed := make([]byte, n)
|
|
copy(fixed, s)
|
|
return append(b, fixed...)
|
|
}
|
|
|
|
// Marshal 编码 Submit 包体(CMPP 2.0 布局)。
|
|
// 根因修复(Day 39 复盘):此前误写 3.0 独有的 Fee_terminal_type/Dest_terminal_type
|
|
// 两个 1B 字段,真实网关按 2.0 解析时 Msg_Length 错位读到 0 —— "空数据"不下发
|
|
// (抓包 temp86.pcap 实证;模拟网关与 Marshal 同源同错,自测未暴露)。
|
|
// 2.0 布局:TP_pid@44 TP_udhi@45 Msg_Fmt@46 Msg_Length@138 DestUsrTl@116。
|
|
func (s *SubmitReq) Marshal() ([]byte, error) {
|
|
if len(s.DestTerminalIDs) == 0 || len(s.DestTerminalIDs) > 100 {
|
|
return nil, ErrTooManyDest
|
|
}
|
|
if len(s.Content) > 140 {
|
|
return nil, ErrContentTooLong
|
|
}
|
|
pkTotal, pkNumber := s.PkTotal, s.PkNumber
|
|
if pkTotal == 0 {
|
|
pkTotal = 1
|
|
}
|
|
if pkNumber == 0 { // Pk_number 语义 1..Pk_total,0 兜底为 1(网关拒收 0)
|
|
pkNumber = 1
|
|
}
|
|
b := make([]byte, 0, 128+21*len(s.DestTerminalIDs)+len(s.Content))
|
|
var u64 [8]byte
|
|
binary.BigEndian.PutUint64(u64[:], s.MsgID)
|
|
b = append(b, u64[:]...)
|
|
b = append(b, pkTotal, pkNumber, s.RegisteredDelivery, s.MsgLevel)
|
|
b = appendFixed(b, s.ServiceID, 10)
|
|
b = append(b, s.FeeUserType)
|
|
b = appendFixed(b, "", 21) // Fee_terminal_Id
|
|
b = append(b, 0, s.TPUdhi, s.MsgFmt) // TP_pid=0 / TP_udhi(长短信=1) / Msg_Fmt
|
|
b = appendFixed(b, s.MsgSrc, 6)
|
|
b = appendFixed(b, s.FeeType, 2)
|
|
b = appendFixed(b, s.FeeCode, 6)
|
|
b = appendFixed(b, "", 17) // ValId_Time
|
|
b = appendFixed(b, "", 17) // At_Time
|
|
b = appendFixed(b, s.SrcID, 21)
|
|
b = append(b, uint8(len(s.DestTerminalIDs)))
|
|
for _, d := range s.DestTerminalIDs {
|
|
b = appendFixed(b, d, 21)
|
|
}
|
|
b = append(b, uint8(len(s.Content))) // Msg_Length:CMPP 2.0 为 1B(≤140)
|
|
b = append(b, s.Content...)
|
|
b = appendFixed(b, "", 8) // Reserve
|
|
return b, nil
|
|
}
|
|
|
|
// SubmitResp 提交应答:Msg_Id(8B) + Result(1B)。
|
|
type SubmitResp struct {
|
|
MsgID uint64
|
|
Result uint8 // 0 成功
|
|
}
|
|
|
|
// Marshal 编码 SubmitResp。
|
|
func (r *SubmitResp) Marshal() []byte {
|
|
b := make([]byte, 9)
|
|
binary.BigEndian.PutUint64(b[0:8], r.MsgID)
|
|
b[8] = r.Result
|
|
return b
|
|
}
|
|
|
|
// ParseSubmitResp 解析 SubmitResp。
|
|
func ParseSubmitResp(body []byte) (*SubmitResp, error) {
|
|
if len(body) < 9 {
|
|
return nil, fmt.Errorf("cmpp: submit_resp 包体 %d 字节不足 9", len(body))
|
|
}
|
|
return &SubmitResp{MsgID: binary.BigEndian.Uint64(body[0:8]), Result: body[8]}, nil
|
|
}
|
|
|
|
// ReportInfo 状态报告(Registered_Delivery=1 的 Deliver 消息体)。
|
|
type ReportInfo struct {
|
|
MsgID uint64 // 对应提交的 msg_id
|
|
Stat string // 7B:DELIVRD/EXPIRED/DELETED/UNDELIV/REJECTD
|
|
SubmitTime string // MMDDHHMMSS
|
|
DoneTime string // MMDDHHMMSS
|
|
DestTerminalID string // 21B
|
|
SMSCSequence uint32
|
|
}
|
|
|
|
// IsSuccess 状态是否送达。
|
|
func (r *ReportInfo) IsSuccess() bool { return r.Stat == "DELIVRD" }
|
|
|
|
// Deliver 网关下发(短消息或状态报告)。
|
|
type Deliver struct {
|
|
MsgID uint64
|
|
DestID string // 服务代码/目的号码
|
|
ServiceID string
|
|
MsgFmt uint8
|
|
SrcTerminal string // 用户号码
|
|
IsReport bool // Registered_Delivery:1=状态报告 0=短消息
|
|
Content []byte // 短消息内容(报告时为原始报告体)
|
|
Report *ReportInfo
|
|
}
|
|
|
|
// deliverFixedLen Deliver 定长部分(CMPP 2.0:无 Src_terminal_type,Msg_Length 1B;Msg_Content 计 0 字节)。
|
|
const deliverFixedLen = 8 + 21 + 10 + 1 + 1 + 1 + 21 + 1 + 1 + 8
|
|
|
|
// MarshalDeliver 编码 Deliver 包体(网关/模拟器侧使用)。
|
|
func MarshalDeliver(d *Deliver) ([]byte, error) {
|
|
content := d.Content
|
|
if d.IsReport && d.Report != nil {
|
|
content = MarshalReport(d.Report)
|
|
}
|
|
b := make([]byte, 0, deliverFixedLen+len(content))
|
|
var u64 [8]byte
|
|
binary.BigEndian.PutUint64(u64[:], d.MsgID)
|
|
b = append(b, u64[:]...)
|
|
b = appendFixed(b, d.DestID, 21)
|
|
b = appendFixed(b, d.ServiceID, 10)
|
|
b = append(b, 0, 0, d.MsgFmt)
|
|
b = appendFixed(b, d.SrcTerminal, 21)
|
|
var rd uint8
|
|
if d.IsReport {
|
|
rd = 1
|
|
}
|
|
b = append(b, rd)
|
|
b = append(b, uint8(len(content))) // Msg_Length:CMPP 2.0 为 1B(≤140)
|
|
b = append(b, content...)
|
|
b = appendFixed(b, "", 8) // Reserved
|
|
return b, nil
|
|
}
|
|
|
|
// ParseDeliver 解析 Deliver 包体(CMPP 2.0:Registered_Delivery@63/Msg_Length@64 1B)。
|
|
func ParseDeliver(body []byte) (*Deliver, error) {
|
|
if len(body) < deliverFixedLen {
|
|
return nil, fmt.Errorf("cmpp: deliver 包体 %d 字节不足 %d", len(body), deliverFixedLen)
|
|
}
|
|
d := &Deliver{
|
|
MsgID: binary.BigEndian.Uint64(body[0:8]),
|
|
DestID: trimRightZero(body[8:29]),
|
|
ServiceID: trimRightZero(body[29:39]),
|
|
MsgFmt: body[41],
|
|
SrcTerminal: trimRightZero(body[42:63]),
|
|
IsReport: body[63] == 1,
|
|
}
|
|
contentLen := int(body[64]) // CMPP 2.0:Msg_Length 1B
|
|
if len(body) < deliverFixedLen+contentLen {
|
|
return nil, fmt.Errorf("cmpp: deliver content %d 字节越界", contentLen)
|
|
}
|
|
d.Content = body[65 : 65+contentLen]
|
|
if d.IsReport {
|
|
rep, err := ParseReport(d.Content)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
d.Report = rep
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
// reportFixedLen 状态报告定长:8+7+10+10+21+4 = 60。
|
|
const reportFixedLen = 60
|
|
|
|
// MarshalReport 编码状态报告体。
|
|
func MarshalReport(r *ReportInfo) []byte {
|
|
b := make([]byte, reportFixedLen)
|
|
binary.BigEndian.PutUint64(b[0:8], r.MsgID)
|
|
copy(b[8:15], r.Stat)
|
|
copy(b[15:25], r.SubmitTime)
|
|
copy(b[25:35], r.DoneTime)
|
|
copy(b[35:56], r.DestTerminalID)
|
|
binary.BigEndian.PutUint32(b[56:60], r.SMSCSequence)
|
|
return b
|
|
}
|
|
|
|
// ParseReport 解析状态报告体。
|
|
func ParseReport(content []byte) (*ReportInfo, error) {
|
|
if len(content) < reportFixedLen {
|
|
return nil, fmt.Errorf("cmpp: 状态报告体 %d 字节不足 %d", len(content), reportFixedLen)
|
|
}
|
|
return &ReportInfo{
|
|
MsgID: binary.BigEndian.Uint64(content[0:8]),
|
|
Stat: trimRightZero(content[8:15]),
|
|
SubmitTime: trimRightZero(content[15:25]),
|
|
DoneTime: trimRightZero(content[25:35]),
|
|
DestTerminalID: trimRightZero(content[35:56]),
|
|
SMSCSequence: binary.BigEndian.Uint32(content[56:60]),
|
|
}, nil
|
|
}
|
|
|
|
// DeliverResp 下发应答:Msg_Id(8B) + Result(1B)。
|
|
type DeliverResp struct {
|
|
MsgID uint64
|
|
Result uint8
|
|
}
|
|
|
|
// Marshal 编码 DeliverResp。
|
|
func (r *DeliverResp) Marshal() []byte {
|
|
b := make([]byte, 9)
|
|
binary.BigEndian.PutUint64(b[0:8], r.MsgID)
|
|
b[8] = r.Result
|
|
return b
|
|
}
|