golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/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  	"golang.org/x/net/internal/socket"
    13  )
    14  
    15  var (
    16  	errInvalidConn       = errors.New("invalid connection")
    17  	errMissingAddress    = errors.New("missing address")
    18  	errNilHeader         = errors.New("nil header")
    19  	errHeaderTooShort    = errors.New("header too short")
    20  	errExtHeaderTooShort = errors.New("extension header too short")
    21  	errInvalidConnType   = errors.New("invalid conn type")
    22  	errNotImplemented    = errors.New("not implemented on " + runtime.GOOS + "/" + runtime.GOARCH)
    23  
    24  	// See https://www.freebsd.org/doc/en/books/porters-handbook/versions.html.
    25  	freebsdVersion  uint32
    26  	compatFreeBSD32 bool // 386 emulation on amd64
    27  )
    28  
    29  // See golang.org/issue/30899.
    30  func adjustFreeBSD32(m *socket.Message) {
    31  	// FreeBSD 12.0-RELEASE is affected by https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236737
    32  	if 1200086 <= freebsdVersion && freebsdVersion < 1201000 {
    33  		l := (m.NN + 4 - 1) &^ (4 - 1)
    34  		if m.NN < l && l <= len(m.OOB) {
    35  			m.NN = l
    36  		}
    37  	}
    38  }
    39  
    40  func boolint(b bool) int {
    41  	if b {
    42  		return 1
    43  	}
    44  	return 0
    45  }
    46  
    47  func netAddrToIP4(a net.Addr) net.IP {
    48  	switch v := a.(type) {
    49  	case *net.UDPAddr:
    50  		if ip := v.IP.To4(); ip != nil {
    51  			return ip
    52  		}
    53  	case *net.IPAddr:
    54  		if ip := v.IP.To4(); ip != nil {
    55  			return ip
    56  		}
    57  	}
    58  	return nil
    59  }
    60  
    61  func opAddr(a net.Addr) net.Addr {
    62  	switch a.(type) {
    63  	case *net.TCPAddr:
    64  		if a == nil {
    65  			return nil
    66  		}
    67  	case *net.UDPAddr:
    68  		if a == nil {
    69  			return nil
    70  		}
    71  	case *net.IPAddr:
    72  		if a == nil {
    73  			return nil
    74  		}
    75  	}
    76  	return a
    77  }