-
Notifications
You must be signed in to change notification settings - Fork 89
/
transaction.go
233 lines (200 loc) · 5.8 KB
/
transaction.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
// transaction package implements SIP Transaction Layer
package transaction
import (
"fmt"
"strings"
"time"
"github.com/ghettovoice/gosip/log"
"github.com/ghettovoice/gosip/sip"
)
const (
T1 = 500 * time.Millisecond
T2 = 4 * time.Second
T4 = 5 * time.Second
Timer_A = T1
Timer_B = 64 * T1
Timer_D = 32 * time.Second
Timer_E = T1
Timer_F = 64 * T1
Timer_G = T1
Timer_H = 64 * T1
Timer_I = T4
Timer_J = 64 * T1
Timer_K = T4
Timer_1xx = 200 * time.Millisecond
Timer_L = 64 * T1
Timer_M = 64 * T1
)
type TxError interface {
error
Key() TxKey
Terminated() bool
Timeout() bool
Transport() bool
}
type TxTerminatedError struct {
Err error
TxKey TxKey
TxPtr string
}
func (err *TxTerminatedError) Unwrap() error { return err.Err }
func (err *TxTerminatedError) Terminated() bool { return true }
func (err *TxTerminatedError) Timeout() bool { return false }
func (err *TxTerminatedError) Transport() bool { return false }
func (err *TxTerminatedError) Key() TxKey { return err.TxKey }
func (err *TxTerminatedError) Error() string {
if err == nil {
return "<nil>"
}
fields := log.Fields{
"transaction_key": "???",
"transaction_ptr": "???",
}
if err.TxKey != "" {
fields["transaction_key"] = err.TxKey
}
if err.TxPtr != "" {
fields["transaction_ptr"] = err.TxPtr
}
return fmt.Sprintf("transaction.TxTerminatedError<%s>: %s", fields, err.Err)
}
type TxTimeoutError struct {
Err error
TxKey TxKey
TxPtr string
}
func (err *TxTimeoutError) Unwrap() error { return err.Err }
func (err *TxTimeoutError) Terminated() bool { return false }
func (err *TxTimeoutError) Timeout() bool { return true }
func (err *TxTimeoutError) Transport() bool { return false }
func (err *TxTimeoutError) Key() TxKey { return err.TxKey }
func (err *TxTimeoutError) Error() string {
if err == nil {
return "<nil>"
}
fields := log.Fields{
"transaction_key": "???",
"transaction_ptr": "???",
}
if err.TxKey != "" {
fields["transaction_key"] = err.TxKey
}
if err.TxPtr != "" {
fields["transaction_ptr"] = err.TxPtr
}
return fmt.Sprintf("transaction.TxTimeoutError<%s>: %s", fields, err.Err)
}
type TxTransportError struct {
Err error
TxKey TxKey
TxPtr string
}
func (err *TxTransportError) Unwrap() error { return err.Err }
func (err *TxTransportError) Terminated() bool { return false }
func (err *TxTransportError) Timeout() bool { return false }
func (err *TxTransportError) Transport() bool { return true }
func (err *TxTransportError) Key() TxKey { return err.TxKey }
func (err *TxTransportError) Error() string {
if err == nil {
return "<nil>"
}
fields := log.Fields{
"transaction_key": "???",
"transaction_ptr": "???",
}
if err.TxKey != "" {
fields["transaction_key"] = err.TxKey
}
if err.TxPtr != "" {
fields["transaction_ptr"] = err.TxPtr
}
return fmt.Sprintf("transaction.TxTransportError<%s>: %s", fields, err.Err)
}
// MakeServerTxKey creates server commonTx key for matching retransmitting requests - RFC 3261 17.2.3.
func MakeServerTxKey(msg sip.Message) (TxKey, error) {
var sep = "__"
firstViaHop, ok := msg.ViaHop()
if !ok {
return "", fmt.Errorf("'Via' header not found or empty in message '%s'", msg.Short())
}
cseq, ok := msg.CSeq()
if !ok {
return "", fmt.Errorf("'CSeq' header not found in message '%s'", msg.Short())
}
method := cseq.MethodName
if method == sip.ACK || method == sip.CANCEL {
method = sip.INVITE
}
var isRFC3261 bool
branch, ok := firstViaHop.Params.Get("branch")
if ok && branch.String() != "" &&
strings.HasPrefix(branch.String(), sip.RFC3261BranchMagicCookie) &&
strings.TrimPrefix(branch.String(), sip.RFC3261BranchMagicCookie) != "" {
isRFC3261 = true
} else {
isRFC3261 = false
}
// RFC 3261 compliant
if isRFC3261 {
var port sip.Port
if firstViaHop.Port == nil {
port = sip.DefaultPort(firstViaHop.Transport)
} else {
port = *firstViaHop.Port
}
return TxKey(strings.Join([]string{
branch.String(), // branch
firstViaHop.Host, // sent-by Host
fmt.Sprint(port), // sent-by Port
string(method), // request Method
}, sep)), nil
}
// RFC 2543 compliant
from, ok := msg.From()
if !ok {
return "", fmt.Errorf("'From' header not found in message '%s'", msg.Short())
}
fromTag, ok := from.Params.Get("tag")
if !ok {
return "", fmt.Errorf("'tag' param not found in 'From' header of message '%s'", msg.Short())
}
callId, ok := msg.CallID()
if !ok {
return "", fmt.Errorf("'Call-ID' header not found in message '%s'", msg.Short())
}
return TxKey(strings.Join([]string{
// TODO: how to match core.Response in Send method to server tx? currently disabled
// msg.Recipient().String(), // request-uri
fromTag.String(), // from tag
callId.String(), // Call-ID
string(method), // cseq method
fmt.Sprint(cseq.SeqNo), // cseq num
firstViaHop.String(), // top Via
}, sep)), nil
}
// MakeClientTxKey creates client commonTx key for matching responses - RFC 3261 17.1.3.
func MakeClientTxKey(msg sip.Message) (TxKey, error) {
var sep = "__"
cseq, ok := msg.CSeq()
if !ok {
return "", fmt.Errorf("'CSeq' header not found in message '%s'", msg.Short())
}
method := cseq.MethodName
if method == sip.ACK || method == sip.CANCEL {
method = sip.INVITE
}
firstViaHop, ok := msg.ViaHop()
if !ok {
return "", fmt.Errorf("'Via' header not found or empty in message '%s'", msg.Short())
}
branch, ok := firstViaHop.Params.Get("branch")
if !ok || len(branch.String()) == 0 ||
!strings.HasPrefix(branch.String(), sip.RFC3261BranchMagicCookie) ||
len(strings.TrimPrefix(branch.String(), sip.RFC3261BranchMagicCookie)) == 0 {
return "", fmt.Errorf("'branch' not found or empty in 'Via' header of message '%s'", msg.Short())
}
return TxKey(strings.Join([]string{
branch.String(),
string(method),
}, sep)), nil
}