Skip to content

Commit

Permalink
Initialize source and dest port if possible
Browse files Browse the repository at this point in the history
if association is initialized with a net.Conn take the ports from there
  • Loading branch information
hugoArregui committed Mar 1, 2024
1 parent 32ef4a1 commit 90d37b4
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions association.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ const (
acceptChSize = 16
)

func getPort(addr net.Addr) uint16 {
switch addr := addr.(type) {
case *net.UDPAddr:
return uint16(addr.Port)
case *net.TCPAddr:
return uint16(addr.Port)

Check warning on line 109 in association.go

View check run for this annotation

Codecov / codecov/patch

association.go#L108-L109

Added lines #L108 - L109 were not covered by tests
}
return 0
}

func getAssociationStateString(a uint32) string {
switch a {
case closed:
Expand Down Expand Up @@ -337,6 +347,10 @@ func createAssociation(config Config) *Association {
log: config.LoggerFactory.NewLogger("sctp"),
}

if config.NetConn != nil {
a.sourcePort = getPort(config.NetConn.LocalAddr())
a.destinationPort = getPort(config.NetConn.RemoteAddr())
}
a.name = fmt.Sprintf("%p", a)

// RFC 4690 Sec 7.2.1
Expand Down Expand Up @@ -399,8 +413,12 @@ func (a *Association) sendInit() error {

outbound := &packet{}
outbound.verificationTag = a.peerVerificationTag
a.sourcePort = defaultSCTPSrcDstPort
a.destinationPort = defaultSCTPSrcDstPort
if a.sourcePort == 0 {
a.sourcePort = defaultSCTPSrcDstPort
}
if a.destinationPort == 0 {
a.destinationPort = defaultSCTPSrcDstPort
}
outbound.sourcePort = a.sourcePort
outbound.destinationPort = a.destinationPort

Expand Down

0 comments on commit 90d37b4

Please sign in to comment.