Skip to content

Commit

Permalink
Initialize source port and destination 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 c1165a7
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 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 @@ -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)
Expand Down Expand Up @@ -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
}

Check warning on line 416 in association.go

View check run for this annotation

Codecov / codecov/patch

association.go#L415-L416

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

Check warning on line 419 in association.go

View check run for this annotation

Codecov / codecov/patch

association.go#L418-L419

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

Expand Down

0 comments on commit c1165a7

Please sign in to comment.