forked from emiago/sipgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog_client.go
558 lines (469 loc) · 14.2 KB
/
dialog_client.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package sipgo
import (
"context"
"errors"
"fmt"
"sync"
"github.com/emiago/sipgo/sip"
"github.com/icholy/digest"
)
type DialogClient struct {
c *Client
dialogs sync.Map // TODO replace with typed version
contactHDR sip.ContactHeader
ua DialogUA
}
// NewDialogClient provides handle for managing UAC dialog
// Contact hdr is default to be provided for correct invite. It is not used if you provided hdr as part of request,
// but contact hdr must be present so this makes sure correct dialog is established.
// In case handling different transports you should have multiple instances per transport
func NewDialogClient(client *Client, contactHDR sip.ContactHeader) *DialogClient {
s := &DialogClient{
c: client,
dialogs: sync.Map{},
contactHDR: contactHDR,
ua: DialogUA{
Client: client,
ContactHDR: contactHDR,
},
}
return s
}
func (s *DialogClient) dialogsLen() int {
leftItems := 0
s.dialogs.Range(func(key, value any) bool {
leftItems++
return true
})
return leftItems
}
func (s *DialogClient) loadDialog(id string) *DialogClientSession {
val, ok := s.dialogs.Load(id)
if !ok || val == nil {
return nil
}
t := val.(*DialogClientSession)
return t
}
func (s *DialogClient) MatchRequestDialog(req *sip.Request) (*DialogClientSession, error) {
return s.matchDialogRequest(req)
}
func (s *DialogClient) matchDialogRequest(req *sip.Request) (*DialogClientSession, error) {
id, err := sip.UACReadRequestDialogID(req)
if err != nil {
return nil, errors.Join(err, ErrDialogOutsideDialog)
}
dt := s.loadDialog(id)
if dt == nil {
return nil, ErrDialogDoesNotExists
}
return dt, nil
}
// Invite sends INVITE request and creates early dialog session.
// This is actually not yet dialog (ID is empty)
// You need to call WaitAnswer after for establishing dialog
// For passing custom Invite request use WriteInvite
func (c *DialogClient) Invite(ctx context.Context, recipient sip.Uri, body []byte, headers ...sip.Header) (*DialogClientSession, error) {
dt, err := c.ua.Invite(ctx, recipient, body, headers...)
if err != nil {
return nil, err
}
dt.OnClose = func() {
c.dialogs.Delete(dt.ID)
}
dt.OnDialog = func(id string) {
c.dialogs.Store(id, dt)
}
// req := sip.NewRequest(sip.INVITE, recipient)
// if body != nil {
// req.SetBody(body)
// }
// for _, h := range headers {
// req.AppendHeader(h)
// }
// return c.WriteInvite(ctx, req)
return dt, err
}
func (c *DialogClient) WriteInvite(ctx context.Context, inviteRequest *sip.Request) (*DialogClientSession, error) {
dt, err := c.ua.WriteInvite(ctx, inviteRequest)
if err != nil {
return nil, err
}
dt.OnClose = func() {
c.dialogs.Delete(dt.ID)
}
dt.OnDialog = func(id string) {
c.dialogs.Store(id, dt)
}
// cli := c.c
// if inviteRequest.Contact() == nil {
// // Set contact only if not exists
// inviteRequest.AppendHeader(&c.contactHDR)
// }
// tx, err := cli.TransactionRequest(ctx, inviteRequest)
// if err != nil {
// return nil, err
// }
// ctx, cancel := context.WithCancel(context.Background())
// dtx := &DialogClientSession{
// Dialog: Dialog{
// InviteRequest: inviteRequest,
// lastCSeqNo: inviteRequest.CSeq().SeqNo,
// state: atomic.Int32{},
// stateCh: make(chan sip.DialogState, 3),
// ctx: ctx,
// cancel: cancel,
// },
// dc: c,
// inviteTx: tx,
// }
// return dtx, nil
return dt, err
}
func (c *DialogClient) ReadBye(req *sip.Request, tx sip.ServerTransaction) error {
dt, err := c.matchDialogRequest(req)
if err != nil {
return err
}
return dt.ReadBye(req, tx)
}
// Experiment
// ReadRefer reads REFER (Transfer action) and parses referURI if dialog exists.
// Returned dialog you should use to pass NOTIFY and BYE if your new INVITE dialog is successful
func (c *DialogClient) ReadRefer(req *sip.Request, tx sip.ServerTransaction, referUri *sip.Uri) (*DialogClientSession, error) {
dt, err := c.matchDialogRequest(req)
if err != nil {
return nil, err
}
return dt.ReadRefer(req, tx, referUri)
}
type DialogClientSession struct {
Dialog
// dc *DialogClient
inviteTx sip.ClientTransaction
ua *DialogUA
// OnClose triggers when user calls Close
OnClose func()
// OnDialog triggers when SIP Dialog is only created after successful answer. Use it for caching dialog
OnDialog func(id string)
}
func (s *DialogClientSession) ReadBye(req *sip.Request, tx sip.ServerTransaction) error {
s.setState(sip.DialogStateEnded)
res := sip.NewResponseFromRequest(req, 200, "OK", nil)
if err := tx.Respond(res); err != nil {
return err
}
defer s.Close() // Delete our dialog always
defer s.inviteTx.Terminate() // Terminates Invite transaction
// select {
// case <-tx.Done():
// return tx.Err()
// }
return nil
}
func (s *DialogClientSession) ReadRefer(req *sip.Request, tx sip.ServerTransaction, referUri *sip.Uri) (*DialogClientSession, error) {
cseq := req.CSeq().SeqNo
if cseq <= s.lastCSeqNo {
return nil, ErrDialogOutsideDialog
}
referToHdr := req.GetHeader("Refer-to")
if referToHdr == nil {
return nil, fmt.Errorf("no Refer-to header present")
}
if err := sip.ParseUri(referToHdr.Value(), referUri); err != nil {
return nil, err
}
res := sip.NewResponseFromRequest(req, 202, "Accepted", nil)
if err := tx.Respond(res); err != nil {
return nil, err
}
// Now dialog should do invite
// And implicit subscription should be done
// invite := sip.NewRequest(sip.INVITE, *referUri)
// invite.SetBody(s.InviteRequest.Body())
// invite
// // s.TransactionRequest(context.TODO(), invite)
// c.WriteInvite(context.TODO(), invite)
return s, nil
// Dial until current dialog is canceled. Therefore we pass s.Context
// ctx, cancel := context.WithTimeout(s.Context(), 30*time.Second)
// defer cancel()
// c.Invite(ctx, referUri)
// s.setState(sip.DialogStateEnded)
// res := sip.NewResponseFromRequest(req, 200, "OK", nil)
// if err := tx.Respond(res); err != nil {
// return err
// }
// defer s.Close() // Delete our dialog always
// defer s.inviteTx.Terminate() // Terminates Invite transaction
// // select {
// // case <-tx.Done():
// // return tx.Err()
// // }
// return nil
}
// TransactionRequest is doing client DIALOG request based on RFC
// https://www.rfc-editor.org/rfc/rfc3261#section-12.2.1
// This ensures that you have proper request done within dialog. You should avoid setting any Dialog header (cseq, from, to, callid)
func (s *DialogClientSession) TransactionRequest(ctx context.Context, req *sip.Request) (sip.ClientTransaction, error) {
cseq := req.CSeq()
if cseq == nil {
cseq = &sip.CSeqHeader{
SeqNo: s.InviteRequest.CSeq().SeqNo,
MethodName: req.Method,
}
req.AppendHeader(cseq)
}
// For safety make sure we are starting with our last dialog cseq num
cseq.SeqNo = s.lastCSeqNo
if !req.IsAck() && !req.IsCancel() {
// Do cseq increment within dialog
cseq.SeqNo = s.lastCSeqNo + 1
}
// Check record route header
if s.InviteResponse != nil {
cont := s.InviteResponse.Contact()
if cont != nil {
req.Recipient = cont.Address
}
if rr := s.InviteResponse.RecordRoute(); rr != nil {
if rr.Address.UriParams.Has("lr") {
req.AppendHeader(&sip.RouteHeader{
Address: rr.Address,
})
} else {
req.SetDestination(rr.Address.HostPort())
}
}
}
s.lastCSeqNo = cseq.SeqNo
// Keep any request inside dialog
if h, invH := req.From(), s.InviteRequest.From(); h == nil {
req.AppendHeader(sip.HeaderClone(invH))
}
if h, invH := req.To(), s.InviteResponse.To(); h == nil && invH != nil {
req.AppendHeader(sip.HeaderClone(invH))
}
if h, invH := req.CallID(), s.InviteRequest.CallID(); h == nil {
req.AppendHeader(sip.HeaderClone(invH))
}
// Passing option to avoid CSEQ apply
return s.ua.Client.TransactionRequest(ctx, req, ClientRequestBuild)
}
func (s *DialogClientSession) WriteRequest(req *sip.Request) error {
// Check Record-Route Header
if s.InviteResponse != nil {
// Record Route handling
if rr := s.InviteResponse.RecordRoute(); rr != nil {
req.SetDestination(rr.Address.HostPort())
}
}
return s.ua.Client.WriteRequest(req)
}
// Close must be always called in order to cleanup some internal resources
// Consider that this will not send BYE or CANCEL or change dialog state
func (s *DialogClientSession) Close() error {
if s.OnClose != nil {
s.OnClose()
}
// s.ua.dialogs.Delete(s.ID)
// s.setState(sip.DialogStateEnded)
// ctx, _ := context.WithTimeout(context.Background(), sip.Timer_B)
// return s.Bye(ctx)
return nil
}
type AnswerOptions struct {
OnResponse func(res *sip.Response) error
// For digest authentication
Username string
Password string
}
// WaitAnswer waits for success response or returns ErrDialogResponse in case non 2xx
// Canceling context while waiting 2xx will send Cancel request
// Returns errors:
// - ErrDialogResponse in case non 2xx response
// - any internal in case waiting answer failed for different reasons
func (s *DialogClientSession) WaitAnswer(ctx context.Context, opts AnswerOptions) error {
tx, inviteRequest := s.inviteTx, s.InviteRequest
var r *sip.Response
var err error
for {
select {
case r = <-tx.Responses():
// just pass
case <-ctx.Done():
// Send cancel
defer tx.Terminate()
if err := tx.Cancel(); err != nil {
return errors.Join(err, ctx.Err())
}
return ctx.Err()
case <-tx.Done():
// tx.Err() can be empty
return errors.Join(fmt.Errorf("transaction terminated"), tx.Err())
}
if opts.OnResponse != nil {
if err := opts.OnResponse(r); err != nil {
return err
}
}
s.InviteResponse = r
if r.IsSuccess() {
break
}
if r.IsProvisional() {
continue
}
if (r.StatusCode == sip.StatusProxyAuthRequired) && opts.Password != "" {
h := r.GetHeader("Proxy-Authorization")
if h == nil {
tx.Terminate()
digopts := digest.Options{
Method: sip.INVITE.String(),
URI: inviteRequest.Recipient.Addr(),
Username: opts.Username,
Password: opts.Password,
}
// First build this request
if err := digestProxyAuthApply(inviteRequest, r, digopts); err != nil {
return err
}
// Remove Via from original request and send it through dialog transaction
// This keeps transaction within dialog
inviteRequest.RemoveHeader("Via")
tx, err = s.TransactionRequest(ctx, inviteRequest)
if err != nil {
return err
}
continue
}
}
if r.StatusCode == sip.StatusUnauthorized && opts.Password != "" {
h := inviteRequest.GetHeader("Authorization")
if h == nil {
tx.Terminate()
digopts := digest.Options{
Method: sip.INVITE.String(),
URI: inviteRequest.Recipient.Addr(),
Username: opts.Username,
Password: opts.Password,
}
// First build this request
if err := digestAuthApply(inviteRequest, r, digopts); err != nil {
return err
}
// Remove Via from original request and send it through dialog transaction
// This keeps transaction within dialog
inviteRequest.RemoveHeader("Via")
tx, err = s.TransactionRequest(ctx, inviteRequest)
if err != nil {
return err
}
continue
}
}
return &ErrDialogResponse{Res: r}
}
id, err := sip.MakeDialogIDFromResponse(r)
if err != nil {
return err
}
s.inviteTx = tx
s.InviteResponse = r
s.ID = id
s.setState(sip.DialogStateEstablished)
if s.OnDialog != nil {
s.OnDialog(id)
}
// s.ua.dialogs.Store(id, s)
return nil
}
// Ack sends ack. Use WriteAck for more customizing
func (s *DialogClientSession) Ack(ctx context.Context) error {
ack := sip.NewAckRequest(s.InviteRequest, s.InviteResponse, nil)
return s.WriteAck(ctx, ack)
}
func (s *DialogClientSession) WriteAck(ctx context.Context, ack *sip.Request) error {
if err := s.WriteRequest(ack); err != nil {
// Make sure we close our error
// s.Close()
return err
}
s.setState(sip.DialogStateConfirmed)
return nil
}
// Bye sends bye and terminates session. Use WriteBye if you want to customize bye request
func (s *DialogClientSession) Bye(ctx context.Context) error {
bye := newByeRequestUAC(s.InviteRequest, s.InviteResponse, nil)
return s.WriteBye(ctx, bye)
}
func (s *DialogClientSession) WriteBye(ctx context.Context, bye *sip.Request) error {
defer s.Close()
state := s.state.Load()
// In case dialog terminated
if sip.DialogState(state) == sip.DialogStateEnded {
return nil
}
// In case dialog was not updated
if sip.DialogState(state) != sip.DialogStateConfirmed {
return fmt.Errorf("Dialog not confirmed. ACK not send?")
}
tx, err := s.TransactionRequest(ctx, bye)
if err != nil {
return err
}
defer s.inviteTx.Terminate() // Terminates INVITE in all cases
defer tx.Terminate() // Terminates current transaction
// Wait 200
select {
case res := <-tx.Responses():
if res.StatusCode != 200 {
return ErrDialogResponse{res}
}
s.setState(sip.DialogStateEnded)
return nil
case <-tx.Done():
return tx.Err()
case <-ctx.Done():
return ctx.Err()
}
}
// newByeRequestUAC creates bye request from established dialog
// https://datatracker.ietf.org/doc/html/rfc3261#section-15.1.1
// NOTE: it does not copy Via header neither increases CSEQ. This is left to dialog transaction request
func newByeRequestUAC(inviteRequest *sip.Request, inviteResponse *sip.Response, body []byte) *sip.Request {
recipient := &inviteRequest.Recipient
cont := inviteResponse.Contact()
if cont != nil {
// BYE is subsequent request
recipient = &cont.Address
}
byeRequest := sip.NewRequest(
sip.BYE,
*recipient.Clone(),
)
byeRequest.SipVersion = inviteRequest.SipVersion
if len(inviteRequest.GetHeaders("Route")) > 0 {
sip.CopyHeaders("Route", inviteRequest, byeRequest)
}
maxForwardsHeader := sip.MaxForwardsHeader(70)
byeRequest.AppendHeader(&maxForwardsHeader)
if h := inviteRequest.From(); h != nil {
byeRequest.AppendHeader(sip.HeaderClone(h))
}
if h := inviteResponse.To(); h != nil {
byeRequest.AppendHeader(sip.HeaderClone(h))
}
if h := inviteRequest.CallID(); h != nil {
byeRequest.AppendHeader(sip.HeaderClone(h))
}
if h := inviteRequest.CSeq(); h != nil {
byeRequest.AppendHeader(sip.HeaderClone(h))
}
cseq := byeRequest.CSeq()
cseq.MethodName = sip.BYE
byeRequest.SetBody(body)
byeRequest.SetTransport(inviteRequest.Transport())
byeRequest.SetSource(inviteRequest.Source())
return byeRequest
}