github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/golang.org/x/net/ipv4/header.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 "encoding/binary" 9 "fmt" 10 "net" 11 "runtime" 12 13 "golang.org/x/net/internal/socket" 14 ) 15 16 const ( 17 Version = 4 // protocol version 18 HeaderLen = 20 // header length without extension headers 19 maxHeaderLen = 60 // sensible default, revisit if later RFCs define new usage of version and header length fields 20 ) 21 22 type HeaderFlags int 23 24 const ( 25 MoreFragments HeaderFlags = 1 << iota // more fragments flag 26 DontFragment // don't fragment flag 27 ) 28 29 // A Header represents an IPv4 header. 30 type Header struct { 31 Version int // protocol version 32 Len int // header length 33 TOS int // type-of-service 34 TotalLen int // packet total length 35 ID int // identification 36 Flags HeaderFlags // flags 37 FragOff int // fragment offset 38 TTL int // time-to-live 39 Protocol int // next protocol 40 Checksum int // checksum 41 Src net.IP // source address 42 Dst net.IP // destination address 43 Options []byte // options, extension headers 44 } 45 46 func (h *Header) String() string { 47 if h == nil { 48 return "<nil>" 49 } 50 return fmt.Sprintf("ver=%d hdrlen=%d tos=%#x totallen=%d id=%#x flags=%#x fragoff=%#x ttl=%d proto=%d cksum=%#x src=%v dst=%v", h.Version, h.Len, h.TOS, h.TotalLen, h.ID, h.Flags, h.FragOff, h.TTL, h.Protocol, h.Checksum, h.Src, h.Dst) 51 } 52 53 // Marshal returns the binary encoding of h. 54 // 55 // The returned slice is in the format used by a raw IP socket on the 56 // local system. 57 // This may differ from the wire format, depending on the system. 58 func (h *Header) Marshal() ([]byte, error) { 59 if h == nil { 60 return nil, errNilHeader 61 } 62 if h.Len < HeaderLen { 63 return nil, errHeaderTooShort 64 } 65 hdrlen := HeaderLen + len(h.Options) 66 b := make([]byte, hdrlen) 67 b[0] = byte(Version<<4 | (hdrlen >> 2 & 0x0f)) 68 b[1] = byte(h.TOS) 69 flagsAndFragOff := (h.FragOff & 0x1fff) | int(h.Flags<<13) 70 switch runtime.GOOS { 71 case "darwin", "dragonfly", "netbsd": 72 socket.NativeEndian.PutUint16(b[2:4], uint16(h.TotalLen)) 73 socket.NativeEndian.PutUint16(b[6:8], uint16(flagsAndFragOff)) 74 case "freebsd": 75 if freebsdVersion < 1100000 { 76 socket.NativeEndian.PutUint16(b[2:4], uint16(h.TotalLen)) 77 socket.NativeEndian.PutUint16(b[6:8], uint16(flagsAndFragOff)) 78 } else { 79 binary.BigEndian.PutUint16(b[2:4], uint16(h.TotalLen)) 80 binary.BigEndian.PutUint16(b[6:8], uint16(flagsAndFragOff)) 81 } 82 default: 83 binary.BigEndian.PutUint16(b[2:4], uint16(h.TotalLen)) 84 binary.BigEndian.PutUint16(b[6:8], uint16(flagsAndFragOff)) 85 } 86 binary.BigEndian.PutUint16(b[4:6], uint16(h.ID)) 87 b[8] = byte(h.TTL) 88 b[9] = byte(h.Protocol) 89 binary.BigEndian.PutUint16(b[10:12], uint16(h.Checksum)) 90 if ip := h.Src.To4(); ip != nil { 91 copy(b[12:16], ip[:net.IPv4len]) 92 } 93 if ip := h.Dst.To4(); ip != nil { 94 copy(b[16:20], ip[:net.IPv4len]) 95 } else { 96 return nil, errMissingAddress 97 } 98 if len(h.Options) > 0 { 99 copy(b[HeaderLen:], h.Options) 100 } 101 return b, nil 102 } 103 104 // Parse parses b as an IPv4 header and stores the result in h. 105 // 106 // The provided b must be in the format used by a raw IP socket on the 107 // local system. 108 // This may differ from the wire format, depending on the system. 109 func (h *Header) Parse(b []byte) error { 110 if h == nil || b == nil { 111 return errNilHeader 112 } 113 if len(b) < HeaderLen { 114 return errHeaderTooShort 115 } 116 hdrlen := int(b[0]&0x0f) << 2 117 if len(b) < hdrlen { 118 return errExtHeaderTooShort 119 } 120 h.Version = int(b[0] >> 4) 121 h.Len = hdrlen 122 h.TOS = int(b[1]) 123 h.ID = int(binary.BigEndian.Uint16(b[4:6])) 124 h.TTL = int(b[8]) 125 h.Protocol = int(b[9]) 126 h.Checksum = int(binary.BigEndian.Uint16(b[10:12])) 127 h.Src = net.IPv4(b[12], b[13], b[14], b[15]) 128 h.Dst = net.IPv4(b[16], b[17], b[18], b[19]) 129 switch runtime.GOOS { 130 case "darwin", "dragonfly", "netbsd": 131 h.TotalLen = int(socket.NativeEndian.Uint16(b[2:4])) + hdrlen 132 h.FragOff = int(socket.NativeEndian.Uint16(b[6:8])) 133 case "freebsd": 134 if freebsdVersion < 1100000 { 135 h.TotalLen = int(socket.NativeEndian.Uint16(b[2:4])) 136 if freebsdVersion < 1000000 { 137 h.TotalLen += hdrlen 138 } 139 h.FragOff = int(socket.NativeEndian.Uint16(b[6:8])) 140 } else { 141 h.TotalLen = int(binary.BigEndian.Uint16(b[2:4])) 142 h.FragOff = int(binary.BigEndian.Uint16(b[6:8])) 143 } 144 default: 145 h.TotalLen = int(binary.BigEndian.Uint16(b[2:4])) 146 h.FragOff = int(binary.BigEndian.Uint16(b[6:8])) 147 } 148 h.Flags = HeaderFlags(h.FragOff&0xe000) >> 13 149 h.FragOff = h.FragOff & 0x1fff 150 optlen := hdrlen - HeaderLen 151 if optlen > 0 && len(b) >= hdrlen { 152 if cap(h.Options) < optlen { 153 h.Options = make([]byte, optlen) 154 } else { 155 h.Options = h.Options[:optlen] 156 } 157 copy(h.Options, b[HeaderLen:hdrlen]) 158 } 159 return nil 160 } 161 162 // ParseHeader parses b as an IPv4 header. 163 // 164 // The provided b must be in the format used by a raw IP socket on the 165 // local system. 166 // This may differ from the wire format, depending on the system. 167 func ParseHeader(b []byte) (*Header, error) { 168 h := new(Header) 169 if err := h.Parse(b); err != nil { 170 return nil, err 171 } 172 return h, nil 173 }