diff --git a/.changeset/sip-room-name-limit.md b/.changeset/sip-room-name-limit.md new file mode 100644 index 000000000..5795684d7 --- /dev/null +++ b/.changeset/sip-room-name-limit.md @@ -0,0 +1,5 @@ +--- +"github.com/livekit/protocol": patch +--- + +Keep the room name of a SIP dispatch rule within the room name limit when the phone number is long. Add WithMaxRoomNameLength to set the limit. diff --git a/sip/sip.go b/sip/sip.go index a82ddceb0..11b692169 100644 --- a/sip/sip.go +++ b/sip/sip.go @@ -270,6 +270,42 @@ func SelectDispatchRule(rules []*livekit.SIPDispatchRuleInfo, req *rpc.EvaluateS return ValidateDispatchRulesIter(iters.Slice(rules), opts...) } +// DefaultMaxRoomNameLength is the default limit of the server for a room name, in bytes. +const DefaultMaxRoomNameLength = 256 + +type evaluateDispatchRuleOpts struct { + MaxRoomNameLength int +} + +type EvaluateDispatchRuleOpt func(opt *evaluateDispatchRuleOpts) + +// WithMaxRoomNameLength sets the limit for the room name that a dispatch rule builds, in bytes. +// Use the limit of the server that the call joins. Zero or less means no limit. +// The default is DefaultMaxRoomNameLength. +func WithMaxRoomNameLength(n int) EvaluateDispatchRuleOpt { + return func(opt *evaluateDispatchRuleOpts) { + opt.MaxRoomNameLength = n + } +} + +// noRoomNameLimit is the limit that fitRoomNumber takes when the room name has no limit. +const noRoomNameLimit = math.MaxInt + +// fitRoomNumber returns num for use in a room name, in at most limit bytes. +// The user part of a SIP URI has no length limit, and some carriers put long data in it. +// A long num is cut, and a hash of the full value keeps the result unique. +func fitRoomNumber(num string, limit int) string { + const hashLen = 16 + if len(num) <= limit || limit <= hashLen { + // It fits, or there is no space to shorten it in a useful way. + return num + } + h := sha256.Sum256([]byte(num)) + // The cut can split a multi-byte character. + head := strings.ToValidUTF8(num[:limit-hashLen-1], "") + return head + "_" + hex.EncodeToString(h[:hashLen/2]) +} + // GetPinAndRoom returns a room name/prefix and the pin for a dispatch rule. Just a convenience wrapper. func GetPinAndRoom(info *livekit.SIPDispatchRuleInfo) (room, pin string, err error) { // TODO: Could probably add methods on SIPDispatchRuleInfo struct instead. @@ -899,10 +935,21 @@ func InboundTrunkAuthPrompt(trunk *livekit.SIPInboundTrunkInfo) (*rpc.GetSIPTrun } // EvaluateDispatchRule checks a selected Dispatch Rule against the provided request. -func EvaluateDispatchRule(projectID string, trunk *livekit.SIPInboundTrunkInfo, rule *livekit.SIPDispatchRuleInfo, req *rpc.EvaluateSIPDispatchRulesRequest) (*rpc.EvaluateSIPDispatchRulesResponse, error) { +func EvaluateDispatchRule(projectID string, trunk *livekit.SIPInboundTrunkInfo, rule *livekit.SIPDispatchRuleInfo, req *rpc.EvaluateSIPDispatchRulesRequest, opts ...EvaluateDispatchRuleOpt) (*rpc.EvaluateSIPDispatchRulesResponse, error) { if rule == nil { return nil, errors.New("no dispatch rule") } + opt := evaluateDispatchRuleOpts{MaxRoomNameLength: DefaultMaxRoomNameLength} + for _, fnc := range opts { + fnc(&opt) + } + // numLimit returns the space for the number in a room name with this prefix and suffix. + numLimit := func(pref, suffix string) int { + if opt.MaxRoomNameLength <= 0 { + return noRoomNameLimit + } + return opt.MaxRoomNameLength - len(pref) - len(suffix) + } trunk.Upgrade() rule.Upgrade() call := req.SIPCall() @@ -982,21 +1029,23 @@ func EvaluateDispatchRule(projectID string, trunk *livekit.SIPInboundTrunkInfo, // TODO: Remove "_" if the prefix is empty for consistency with Callee dispatch rule. // TODO: Do we need to escape specific characters in the number? // TODO: Include actual SIP call ID in the room name? - room = from - if pref := rule.DispatchRuleIndividual.GetRoomPrefix(); pref != "" { - room = pref + "_" + from + var pref, suffix string + if p := rule.DispatchRuleIndividual.GetRoomPrefix(); p != "" { + pref = p + "_" } if !rule.DispatchRuleIndividual.NoRandomness { - room += "_" + guid.New("") + suffix = "_" + guid.New("") } + room = pref + fitRoomNumber(from, numLimit(pref, suffix)) + suffix case *livekit.SIPDispatchRule_DispatchRuleCallee: - room = to - if pref := rule.DispatchRuleCallee.GetRoomPrefix(); pref != "" { - room = pref + "_" + to + var pref, suffix string + if p := rule.DispatchRuleCallee.GetRoomPrefix(); p != "" { + pref = p + "_" } if rule.DispatchRuleCallee.Randomize { - room += "_" + guid.New("") + suffix = "_" + guid.New("") } + room = pref + fitRoomNumber(to, numLimit(pref, suffix)) + suffix } attrs[livekit.AttrSIPDispatchRuleID] = rule.GetSipDispatchRuleId() resp := &rpc.EvaluateSIPDispatchRulesResponse{ diff --git a/sip/sip_test.go b/sip/sip_test.go index 5462ac4f9..e4313eda9 100644 --- a/sip/sip_test.go +++ b/sip/sip_test.go @@ -19,8 +19,10 @@ import ( "net/netip" "regexp" "strconv" + "strings" "testing" "time" + "unicode/utf8" "github.com/dennwc/iters" "google.golang.org/protobuf/proto" @@ -1672,3 +1674,66 @@ func TestEvaluateDispatchRuleAgentSplit(t *testing.T) { prototest.Equals(t, conf, res.RoomConfig) }) } + +func TestEvaluateDispatchRuleLongNumber(t *testing.T) { + // Some carriers put long data in the user part of the From URI. + long := "+15551234567&&" + strings.Repeat("a1b2c3d4", 30) + tr := &livekit.SIPInboundTrunkInfo{SipTrunkId: "trunk"} + eval := func(t *testing.T, rule *livekit.SIPDispatchRule, from, to string, opts ...EvaluateDispatchRuleOpt) string { + t.Helper() + res, err := EvaluateDispatchRule("p_123", tr, &livekit.SIPDispatchRuleInfo{SipDispatchRuleId: "rule", Rule: rule}, &rpc.EvaluateSIPDispatchRulesRequest{ + SipCallId: "call-id", + CallingNumber: from, + CallingHost: "sip.example.com", + CalledNumber: to, + }, opts...) + require.NoError(t, err) + require.Equal(t, rpc.SIPDispatchResult_ACCEPT, res.Result) + return res.RoomName + } + individual := func(pref string, noRandom bool) *livekit.SIPDispatchRule { + return &livekit.SIPDispatchRule{Rule: &livekit.SIPDispatchRule_DispatchRuleIndividual{ + DispatchRuleIndividual: &livekit.SIPDispatchRuleIndividual{RoomPrefix: pref, NoRandomness: noRandom}, + }} + } + callee := func(pref string, random bool) *livekit.SIPDispatchRule { + return &livekit.SIPDispatchRule{Rule: &livekit.SIPDispatchRule_DispatchRuleCallee{ + DispatchRuleCallee: &livekit.SIPDispatchRuleCallee{RoomPrefix: pref, Randomize: random}, + }} + } + + t.Run("short number is not changed", func(t *testing.T) { + require.Equal(t, "pref_+15551234567", eval(t, individual("pref", true), "+15551234567", "+3333")) + require.Equal(t, "pref_+3333", eval(t, callee("pref", false), "+15551234567", "+3333")) + }) + t.Run("individual", func(t *testing.T) { + room := eval(t, individual("pref", false), long, "+3333") + require.LessOrEqual(t, len(room), DefaultMaxRoomNameLength) + require.True(t, strings.HasPrefix(room, "pref_+15551234567&&"), room) + }) + t.Run("individual without randomness", func(t *testing.T) { + room := eval(t, individual("pref", true), long, "+3333") + require.Len(t, room, DefaultMaxRoomNameLength) + // The same caller gets the same room, and a different caller a different one. + require.Equal(t, room, eval(t, individual("pref", true), long, "+3333")) + require.NotEqual(t, room, eval(t, individual("pref", true), long+"x", "+3333")) + }) + t.Run("callee", func(t *testing.T) { + room := eval(t, callee("pref", true), "+15551234567", long) + require.LessOrEqual(t, len(room), DefaultMaxRoomNameLength) + require.True(t, strings.HasPrefix(room, "pref_+15551234567&&"), room) + }) + t.Run("custom limit", func(t *testing.T) { + room := eval(t, individual("pref", true), long, "+3333", WithMaxRoomNameLength(64)) + require.Len(t, room, 64) + require.True(t, strings.HasPrefix(room, "pref_+15551234567&&"), room) + }) + t.Run("no limit", func(t *testing.T) { + require.Equal(t, "pref_"+long, eval(t, individual("pref", true), long, "+3333", WithMaxRoomNameLength(0))) + }) + t.Run("multi-byte characters", func(t *testing.T) { + room := eval(t, individual("", true), strings.Repeat("é", 200), "+3333") + require.LessOrEqual(t, len(room), DefaultMaxRoomNameLength) + require.True(t, utf8.ValidString(room)) + }) +}