github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/ipv4/helper.go (about)

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ipv4
     6  
     7  import (
     8  	"errors"
     9  	"net"
    10  	"runtime"
    11  
    12  	"github.com/Andyfoo/golang/x/net/internal/socket"
    13  )
    14  
    15  var (
    16  	errInvalidConn              = errors.New("invalid connection")
    17  	errMissingAddress           = errors.New("missing address")
    18  	errMissingHeader            = errors.New("missing header")
    19  	errNilHeader                = errors.New("nil header")
    20  	errHeaderTooShort           = errors.New("header too short")
    21  	errExtHeaderTooShort        = errors.New("extension header too short")
    22  	errInvalidConnType          = errors.New("invalid conn type")
    23  	errNoSuchInterface          = errors.New("no such interface")
    24  	errNoSuchMulticastInterface = errors.New("no such multicast interface")
    25  	errNotImplemented           = errors.New("not implemented on " + runtime.GOOS + "/" + runtime.GOARCH)
    26  
    27  	// See https://www.freebsd.org/doc/en/books/porters-handbook/versions.html.
    28  	freebsdVersion  uint32
    29  	compatFreeBSD32 bool // 386 emulation on amd64
    30  )
    31  
    32  // See golang.org/issue/30899.
    33  func adjustFreeBSD32(m *socket.Message) {
    34  	// FreeBSD 12.0-RELEASE is affected by https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236737
    35  	if 1200086 <= freebsdVersion && freebsdVersion < 1201000 {
    36  		l := (m.NN + 4 - 1) &^ (4 - 1)
    37  		if m.NN < l && l <= len(m.OOB) {
    38  			m.NN = l
    39  		}
    40  	}
    41  }
    42  
    43  func boolint(b bool) int {
    44  	if b {
    45  		return 1
    46  	}
    47  	return 0
    48  }
    49  
    50  func netAddrToIP4(a net.Addr) net.IP {
    51  	switch v := a.(type) {
    52  	case *net.UDPAddr:
    53  		if ip := v.IP.To4(); ip != nil {
    54  			return ip
    55  		}
    56  	case *net.IPAddr:
    57  		if ip := v.IP.To4(); ip != nil {
    58  			return ip
    59  		}
    60  	}
    61  	return nil
    62  }
    63  
    64  func opAddr(a net.Addr) net.Addr {
    65  	switch a.(type) {
    66  	case *net.TCPAddr:
    67  		if a == nil {
    68  			return nil
    69  		}
    70  	case *net.UDPAddr:
    71  		if a == nil {
    72  			return nil
    73  		}
    74  	case *net.IPAddr:
    75  		if a == nil {
    76  			return nil
    77  		}
    78  	}
    79  	return a
    80  }