github.phpd.cn/cilium/cilium@v1.6.12/pkg/proxy/dialer.go (about)

     1  // Copyright 2016-2017 Authors of Cilium
     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 proxy
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  	"os"
    21  	"syscall"
    22  	"time"
    23  )
    24  
    25  // ProxyKeepAlivePeriod is the time used for sending periodic keepalives on
    26  // proxy connections. Cross-reference with datapath PROXY_DEFAULT_LIFETIME.
    27  const ProxyKeepAlivePeriod = time.Duration(5) * time.Minute
    28  
    29  func setKeepAlive(c net.Conn) error {
    30  	if tcp, ok := c.(*net.TCPConn); ok {
    31  		if err := tcp.SetKeepAlive(true); err != nil {
    32  			return fmt.Errorf("unable to enable keepalive: %s", err)
    33  		}
    34  
    35  		if err := tcp.SetKeepAlivePeriod(ProxyKeepAlivePeriod); err != nil {
    36  			return fmt.Errorf("unable to set keepalive period: %s", err)
    37  		}
    38  	}
    39  
    40  	return nil
    41  }
    42  
    43  func ciliumDialer(identity int, network, address string) (net.Conn, error) {
    44  	addr, err := net.ResolveTCPAddr(network, address)
    45  	if err != nil {
    46  		return nil, fmt.Errorf("unable resolve address %s/%s: %s", network, address, err)
    47  	}
    48  
    49  	family := syscall.AF_INET
    50  	if addr.IP.To4() == nil {
    51  		family = syscall.AF_INET6
    52  	}
    53  
    54  	fd, err := syscall.Socket(family, syscall.SOCK_STREAM, 0)
    55  	if err != nil {
    56  		return nil, fmt.Errorf("unable to create socket: %s", err)
    57  	}
    58  
    59  	f := os.NewFile(uintptr(fd), addr.String())
    60  	defer f.Close()
    61  
    62  	c, err := net.FileConn(f)
    63  	if err != nil {
    64  		return nil, fmt.Errorf("unable to create FileConn: %s", err)
    65  	}
    66  
    67  	if identity != 0 {
    68  		setSocketMark(c, identity)
    69  	}
    70  
    71  	sockAddr, err := ipToSockaddr(family, addr.IP, addr.Port, addr.Zone)
    72  	if err != nil {
    73  		c.Close()
    74  		return nil, fmt.Errorf("unable to create sockaddr: %s", err)
    75  	}
    76  
    77  	if err := syscall.SetNonblock(fd, false); err != nil {
    78  		c.Close()
    79  		return nil, fmt.Errorf("unable to put socket in blocking mode: %s", err)
    80  	}
    81  
    82  	if err := syscall.Connect(fd, sockAddr); err != nil {
    83  		c.Close()
    84  		return nil, fmt.Errorf("unable to connect: %s", err)
    85  	}
    86  
    87  	if err := setKeepAlive(c); err != nil {
    88  		c.Close()
    89  		return nil, err
    90  	}
    91  
    92  	return c, nil
    93  }