Skip to content

Commit

Permalink
fix: reuse UDP connection when sending INVITE
Browse files Browse the repository at this point in the history
  - this also helps with handling empheral port
  • Loading branch information
emiago committed Nov 24, 2024
1 parent 0397de3 commit 600dc94
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions diago.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,6 @@ func NewDiago(ua *sipgo.UserAgent, opts ...DiagoOption) *Diago {
sipgo.WithClientNAT(),
}

tran, hasUDP := dg.getTransport("udp")
if !hasUDP {
tran, _ = dg.getTransport("")
}

if ip := net.ParseIP(tran.BindHost); ip != nil && !ip.IsUnspecified() {
opts = append(opts, sipgo.WithClientHostname(ip.String()))
// Reuse UDP port and avoid extra connections
if hasUDP {
opts = append(opts, sipgo.WithClientPort(tran.BindPort))
}
}
dg.client, _ = sipgo.NewClient(ua, opts...)
}

Expand Down Expand Up @@ -556,20 +544,6 @@ func (dg *Diago) InviteBridge(ctx context.Context, recipient sip.Uri, bridge *Br
inviteReq := sip.NewRequest(sip.INVITE, recipient)
inviteReq.SetTransport(sip.NetworkToUpper(transport))

// via := &sip.ViaHeader{
// ProtocolName: "SIP",
// ProtocolVersion: "2.0",
// Transport: inviteReq.Transport(),
// Host: dg.client.Hostname(),
// Port: 0,
// Params: sip.NewParams(),
// }
// via.Params.Add("branch", sip.GenerateBranchN(16))
// via.Params.Add("rport", "")
// if tran.Transport == "udp" {
// via.Port = tran.BindPort

// inviteReq.PrependHeader(via)
for _, h := range opts.Headers {
inviteReq.AppendHeader(h)
}
Expand Down Expand Up @@ -650,7 +624,22 @@ func (dg *Diago) InviteBridge(ctx context.Context, recipient sip.Uri, bridge *Br
}
}

dialog, err := dialogCli.WriteInvite(ctx, inviteReq)
// Build here request
if err := sipgo.ClientRequestBuild(dg.client, inviteReq); err != nil {
return nil, err
}

// reuse UDP listener
via := inviteReq.Via()
if transport == "udp" && via.Port == 0 {
via.Host = tran.BindHost
via.Port = tran.BindPort
}

dialog, err := dialogCli.WriteInvite(ctx, inviteReq, func(c *sipgo.Client, req *sip.Request) error {
// Do nothing
return nil
})
if err != nil {
sess.Close()
return nil, err
Expand Down

0 comments on commit 600dc94

Please sign in to comment.