(p []byte)
| 10 | ) |
| 11 | |
| 12 | func FindProtocol(p []byte) (layers.IPProtocol, error) { |
| 13 | version, err := FindIPVersion(p) |
| 14 | if err != nil { |
| 15 | return 0, err |
| 16 | } |
| 17 | switch version { |
| 18 | case 4: |
| 19 | if len(p) < ipv4MinHeaderLen { |
| 20 | return 0, fmt.Errorf("IPv4 packet should have at least %d bytes, got %d bytes", ipv4MinHeaderLen, len(p)) |
| 21 | } |
| 22 | // Protocol is in the 10th byte of IPv4 header |
| 23 | return layers.IPProtocol(p[9]), nil |
| 24 | case 6: |
| 25 | if len(p) < ipv6HeaderLen { |
| 26 | return 0, fmt.Errorf("IPv6 packet should have at least %d bytes, got %d bytes", ipv6HeaderLen, len(p)) |
| 27 | } |
| 28 | // Next header is in the 7th byte of IPv6 header |
| 29 | return layers.IPProtocol(p[6]), nil |
| 30 | default: |
| 31 | return 0, fmt.Errorf("unknown ip version %d", version) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func FindIPVersion(p []byte) (uint8, error) { |
| 36 | if len(p) == 0 { |
nothing calls this directly
no test coverage detected