Skip to content

Commit

Permalink
initialize source port and destination port if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoArregui committed Mar 1, 2024
1 parent 32ef4a1 commit 2c5c79c
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 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

Check warning on line 111 in association.go

View check run for this annotation

Codecov / codecov/patch

association.go#L111

Added line #L111 was not covered by tests
}

func getAssociationStateString(a uint32) string {
switch a {
case closed:
Expand Down Expand Up @@ -303,6 +313,7 @@ func createAssociation(config Config) *Association {
}

tsn := globalMathRandomGenerator.Uint32()

a := &Association{
netConn: config.NetConn,
maxReceiveBufferSize: maxReceiveBufferSize,
Expand Down Expand Up @@ -335,6 +346,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)
Expand Down Expand Up @@ -399,8 +412,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
}

Check warning on line 417 in association.go

View check run for this annotation

Codecov / codecov/patch

association.go#L416-L417

Added lines #L416 - L417 were not covered by tests
if a.destinationPort == 0 {
a.destinationPort = defaultSCTPSrcDstPort
}

Check warning on line 420 in association.go

View check run for this annotation

Codecov / codecov/patch

association.go#L419-L420

Added lines #L419 - L420 were not covered by tests
outbound.sourcePort = a.sourcePort
outbound.destinationPort = a.destinationPort

Expand Down

0 comments on commit 2c5c79c

Please sign in to comment.