helper.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. package ipv4
  5. import (
  6. "encoding/binary"
  7. "errors"
  8. "net"
  9. "unsafe"
  10. )
  11. var (
  12. errMissingAddress = errors.New("missing address")
  13. errMissingHeader = errors.New("missing header")
  14. errHeaderTooShort = errors.New("header too short")
  15. errBufferTooShort = errors.New("buffer too short")
  16. errInvalidConnType = errors.New("invalid conn type")
  17. errOpNoSupport = errors.New("operation not supported")
  18. errNoSuchInterface = errors.New("no such interface")
  19. errNoSuchMulticastInterface = errors.New("no such multicast interface")
  20. // See http://www.freebsd.org/doc/en/books/porters-handbook/freebsd-versions.html.
  21. freebsdVersion uint32
  22. nativeEndian binary.ByteOrder
  23. )
  24. func init() {
  25. i := uint32(1)
  26. b := (*[4]byte)(unsafe.Pointer(&i))
  27. if b[0] == 1 {
  28. nativeEndian = binary.LittleEndian
  29. } else {
  30. nativeEndian = binary.BigEndian
  31. }
  32. }
  33. func boolint(b bool) int {
  34. if b {
  35. return 1
  36. }
  37. return 0
  38. }
  39. func netAddrToIP4(a net.Addr) net.IP {
  40. switch v := a.(type) {
  41. case *net.UDPAddr:
  42. if ip := v.IP.To4(); ip != nil {
  43. return ip
  44. }
  45. case *net.IPAddr:
  46. if ip := v.IP.To4(); ip != nil {
  47. return ip
  48. }
  49. }
  50. return nil
  51. }