the /proc/net/{t,u}dp{,6} files are network byte order for ipv4 and for ipv6 the address is four words consisting of four bytes each. In each of those four words the four bytes are written in reverse order.
(hexIP string)
| 138 | // the /proc/net/{t,u}dp{,6} files are network byte order for ipv4 and for ipv6 the address is four words consisting of four bytes each. In each of those four words the four bytes are written in reverse order. |
| 139 | |
| 140 | func parseIP(hexIP string) (net.IP, error) { |
| 141 | var byteIP []byte |
| 142 | byteIP, err := hex.DecodeString(hexIP) |
| 143 | if err != nil { |
| 144 | return nil, fmt.Errorf("%w: Cannot parse socket field in %q: %w", ErrFileParse, hexIP, err) |
| 145 | } |
| 146 | switch len(byteIP) { |
| 147 | case 4: |
| 148 | return net.IP{byteIP[3], byteIP[2], byteIP[1], byteIP[0]}, nil |
| 149 | case 16: |
| 150 | i := net.IP{ |
| 151 | byteIP[3], byteIP[2], byteIP[1], byteIP[0], |
| 152 | byteIP[7], byteIP[6], byteIP[5], byteIP[4], |
| 153 | byteIP[11], byteIP[10], byteIP[9], byteIP[8], |
| 154 | byteIP[15], byteIP[14], byteIP[13], byteIP[12], |
| 155 | } |
| 156 | return i, nil |
| 157 | default: |
| 158 | return nil, fmt.Errorf("%w: Unable to parse IP %s: %v", ErrFileParse, hexIP, nil) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // parseNetIPSocketLine parses a single line, represented by a list of fields. |
| 163 | func parseNetIPSocketLine(fields []string, isUDP bool) (*netIPSocketLine, error) { |
no outgoing calls
no test coverage detected
searching dependent graphs…