Skip to content

Commit

Permalink
Set TP max_packet_size according to interface MTU
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiraux committed Mar 10, 2020
1 parent bff6ea1 commit c00e05d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
31 changes: 31 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,37 @@ func NewDefaultConnection(address string, serverName string, resumptionTicket []
c = NewConnection(serverName, QuicVersion, QuicALPNToken, scid, dcid, udpConn, resumptionTicket)
}

var headerOverhead = 8
if useIPv6 {
headerOverhead += 40
} else {
headerOverhead += 20
}

lAddr := udpConn.LocalAddr().(*net.UDPAddr)
itfs, err := net.Interfaces()
if err != nil {
return nil, err
}

findMTU:
for _, e := range itfs {
addrs , err := e.Addrs()
if err != nil {
return nil, err
}
for _, a := range addrs {
switch ipNet := a.(type) {
case *net.IPNet:
if ipNet.IP.Equal(lAddr.IP) {
c.InterfaceMTU = e.MTU
c.TLSTPHandler.MaxPacketSize = uint64(c.InterfaceMTU - headerOverhead)
break findMTU
}
}
}
}

c.UseIPv6 = useIPv6
c.Host = udpAddr
return c, nil
Expand Down
3 changes: 3 additions & 0 deletions transport_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ func (h *TLSTransportParameterHandler) GetExtensionData() ([]byte, error) {
addParameter(InitialMaxStreamsUni, h.QuicTransportParameters.MaxUniStreams)
addParameter(IdleTimeout, h.QuicTransportParameters.IdleTimeout)
addParameter(ActiveConnectionIdLimit, h.QuicTransportParameters.ActiveConnectionIdLimit)
if h.QuicTransportParameters.MaxPacketSize > 0 {
addParameter(MaxPacketSize, h.QuicTransportParameters.MaxPacketSize)
}
for _, p := range h.QuicTransportParameters.AdditionalParameters {
parameters = append(parameters, p)
}
Expand Down

0 comments on commit c00e05d

Please sign in to comment.