inet.af/netstack@v0.0.0-20220214151720-7585b01ddccf/tcpip/transport/internal/network/endpoint_state.go (about)

     1  // Copyright 2021 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package network
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"inet.af/netstack/tcpip"
    21  	"inet.af/netstack/tcpip/stack"
    22  	"inet.af/netstack/tcpip/transport"
    23  )
    24  
    25  // Resume implements tcpip.ResumableEndpoint.Resume.
    26  func (e *Endpoint) Resume(s *stack.Stack) {
    27  	e.mu.Lock()
    28  	defer e.mu.Unlock()
    29  
    30  	e.stack = s
    31  
    32  	for m := range e.multicastMemberships {
    33  		if err := e.stack.JoinGroup(e.netProto, m.nicID, m.multicastAddr); err != nil {
    34  			panic(fmt.Sprintf("e.stack.JoinGroup(%d, %d, %s): %s", e.netProto, m.nicID, m.multicastAddr, err))
    35  		}
    36  	}
    37  
    38  	info := e.Info()
    39  
    40  	switch state := e.State(); state {
    41  	case transport.DatagramEndpointStateInitial, transport.DatagramEndpointStateClosed:
    42  	case transport.DatagramEndpointStateBound:
    43  		if len(info.ID.LocalAddress) != 0 && !e.isBroadcastOrMulticast(info.RegisterNICID, e.effectiveNetProto, info.ID.LocalAddress) {
    44  			if e.stack.CheckLocalAddress(info.RegisterNICID, e.effectiveNetProto, info.ID.LocalAddress) == 0 {
    45  				panic(fmt.Sprintf("got e.stack.CheckLocalAddress(%d, %d, %s) = 0, want != 0", info.RegisterNICID, e.effectiveNetProto, info.ID.LocalAddress))
    46  			}
    47  		}
    48  	case transport.DatagramEndpointStateConnected:
    49  		var err tcpip.Error
    50  		multicastLoop := e.ops.GetMulticastLoop()
    51  		e.connectedRoute, err = e.stack.FindRoute(info.RegisterNICID, info.ID.LocalAddress, info.ID.RemoteAddress, e.effectiveNetProto, multicastLoop)
    52  		if err != nil {
    53  			panic(fmt.Sprintf("e.stack.FindRoute(%d, %s, %s, %d, %t): %s", info.RegisterNICID, info.ID.LocalAddress, info.ID.RemoteAddress, e.effectiveNetProto, multicastLoop, err))
    54  		}
    55  	default:
    56  		panic(fmt.Sprintf("unhandled state = %s", state))
    57  	}
    58  }