SerializeTo writes the serialized form of this layer into the SerializationBuffer, implementing gopacket.SerializableLayer. See the docs for gopacket.SerializableLayer for more info.
(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions)
| 137 | // SerializationBuffer, implementing gopacket.SerializableLayer. |
| 138 | // See the docs for gopacket.SerializableLayer for more info. |
| 139 | func (ipv6 *IPv6) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error { |
| 140 | var jumbo bool |
| 141 | var err error |
| 142 | |
| 143 | payload := b.Bytes() |
| 144 | pLen := len(payload) |
| 145 | if pLen > ipv6MaxPayloadLength { |
| 146 | jumbo = true |
| 147 | if opts.FixLengths { |
| 148 | // We need to set the length later because the hop-by-hop header may |
| 149 | // not exist or else need padding, so pLen may yet change |
| 150 | addIPv6JumboOption(ipv6) |
| 151 | } else if ipv6.HopByHop == nil { |
| 152 | return fmt.Errorf("Cannot fit payload length of %d into IPv6 packet", pLen) |
| 153 | } else { |
| 154 | _, ok, err := getIPv6HopByHopJumboLength(ipv6.HopByHop) |
| 155 | if err != nil { |
| 156 | return err |
| 157 | } |
| 158 | if !ok { |
| 159 | return errors.New("Missing jumbo length hop-by-hop option") |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | hbhAlreadySerialized := false |
| 165 | if ipv6.HopByHop != nil { |
| 166 | for _, l := range b.Layers() { |
| 167 | if l == LayerTypeIPv6HopByHop { |
| 168 | hbhAlreadySerialized = true |
| 169 | break |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | if ipv6.HopByHop != nil && !hbhAlreadySerialized { |
| 174 | if ipv6.NextHeader != IPProtocolIPv6HopByHop { |
| 175 | // Just fix it instead of throwing an error |
| 176 | ipv6.NextHeader = IPProtocolIPv6HopByHop |
| 177 | } |
| 178 | err = ipv6.HopByHop.SerializeTo(b, opts) |
| 179 | if err != nil { |
| 180 | return err |
| 181 | } |
| 182 | payload = b.Bytes() |
| 183 | pLen = len(payload) |
| 184 | if opts.FixLengths && jumbo { |
| 185 | err := setIPv6PayloadJumboLength(payload) |
| 186 | if err != nil { |
| 187 | return err |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if !jumbo && pLen > ipv6MaxPayloadLength { |
| 193 | return errors.New("Cannot fit payload into IPv6 header") |
| 194 | } |
| 195 | bytes, err := b.PrependBytes(40) |
| 196 | if err != nil { |
nothing calls this directly
no test coverage detected