diff --git a/association.go b/association.go index 9b2aad55..6f5291dc 100644 --- a/association.go +++ b/association.go @@ -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) + } + return 0 +} + func getAssociationStateString(a uint32) string { switch a { case closed: @@ -335,6 +345,8 @@ func createAssociation(config Config) *Association { silentError: ErrSilentlyDiscard, stats: &associationStats{}, log: config.LoggerFactory.NewLogger("sctp"), + sourcePort: getPort(config.NetConn.LocalAddr()), + destinationPort: getPort(config.NetConn.RemoteAddr()), } a.name = fmt.Sprintf("%p", a) @@ -399,8 +411,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