helper.go raw

   1  // Copyright 2013 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 ipv6
   6  
   7  import (
   8  	"errors"
   9  	"net"
  10  	"runtime"
  11  )
  12  
  13  var (
  14  	errInvalidConn     = errors.New("invalid connection")
  15  	errMissingAddress  = errors.New("missing address")
  16  	errHeaderTooShort  = errors.New("header too short")
  17  	errInvalidConnType = errors.New("invalid conn type")
  18  	errNotImplemented  = errors.New("not implemented on " + runtime.GOOS + "/" + runtime.GOARCH)
  19  )
  20  
  21  func boolint(b bool) int {
  22  	if b {
  23  		return 1
  24  	}
  25  	return 0
  26  }
  27  
  28  func netAddrToIP16(a net.Addr) net.IP {
  29  	switch v := a.(type) {
  30  	case *net.UDPAddr:
  31  		if ip := v.IP.To16(); ip != nil && ip.To4() == nil {
  32  			return ip
  33  		}
  34  	case *net.IPAddr:
  35  		if ip := v.IP.To16(); ip != nil && ip.To4() == nil {
  36  			return ip
  37  		}
  38  	}
  39  	return nil
  40  }
  41  
  42  func opAddr(a net.Addr) net.Addr {
  43  	switch a.(type) {
  44  	case *net.TCPAddr:
  45  		if a == nil {
  46  			return nil
  47  		}
  48  	case *net.UDPAddr:
  49  		if a == nil {
  50  			return nil
  51  		}
  52  	case *net.IPAddr:
  53  		if a == nil {
  54  			return nil
  55  		}
  56  	}
  57  	return a
  58  }
  59