github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/tcpip/transport/icmp/endpoint_state.go (about)

     1  // Copyright 2018 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 icmp
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"github.com/nicocha30/gvisor-ligolo/pkg/tcpip"
    22  	"github.com/nicocha30/gvisor-ligolo/pkg/tcpip/stack"
    23  	"github.com/nicocha30/gvisor-ligolo/pkg/tcpip/transport"
    24  )
    25  
    26  // saveReceivedAt is invoked by stateify.
    27  func (p *icmpPacket) saveReceivedAt() int64 {
    28  	return p.receivedAt.UnixNano()
    29  }
    30  
    31  // loadReceivedAt is invoked by stateify.
    32  func (p *icmpPacket) loadReceivedAt(nsec int64) {
    33  	p.receivedAt = time.Unix(0, nsec)
    34  }
    35  
    36  // afterLoad is invoked by stateify.
    37  func (e *endpoint) afterLoad() {
    38  	stack.StackFromEnv.RegisterRestoredEndpoint(e)
    39  }
    40  
    41  // beforeSave is invoked by stateify.
    42  func (e *endpoint) beforeSave() {
    43  	e.freeze()
    44  }
    45  
    46  // Resume implements tcpip.ResumableEndpoint.Resume.
    47  func (e *endpoint) Resume(s *stack.Stack) {
    48  	e.thaw()
    49  
    50  	e.net.Resume(s)
    51  
    52  	e.stack = s
    53  	e.ops.InitHandler(e, e.stack, tcpip.GetStackSendBufferLimits, tcpip.GetStackReceiveBufferLimits)
    54  
    55  	switch state := e.net.State(); state {
    56  	case transport.DatagramEndpointStateInitial, transport.DatagramEndpointStateClosed:
    57  	case transport.DatagramEndpointStateBound, transport.DatagramEndpointStateConnected:
    58  		var err tcpip.Error
    59  		info := e.net.Info()
    60  		info.ID.LocalPort = e.ident
    61  		info.ID, err = e.registerWithStack(info.NetProto, info.ID)
    62  		if err != nil {
    63  			panic(fmt.Sprintf("e.registerWithStack(%d, %#v): %s", info.NetProto, info.ID, err))
    64  		}
    65  		e.ident = info.ID.LocalPort
    66  	default:
    67  		panic(fmt.Sprintf("unhandled state = %s", state))
    68  	}
    69  }