// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package procfs

import (
	
	
	
	
	
	

	
)

const (
	blackholeRepresentation string = "*"
	blackholeIfaceName      string = "blackhole"
	routeLineColumns        int    = 11
)

// A NetRouteLine represents one line from net/route.
type NetRouteLine struct {
	Iface       string
	Destination uint32
	Gateway     uint32
	Flags       uint32
	RefCnt      uint32
	Use         uint32
	Metric      uint32
	Mask        uint32
	MTU         uint32
	Window      uint32
	IRTT        uint32
}

func ( FS) () ([]NetRouteLine, error) {
	return readNetRoute(.proc.Path("net", "route"))
}

func readNetRoute( string) ([]NetRouteLine, error) {
	,  := util.ReadFileNoStat()
	if  != nil {
		return nil, 
	}

	,  := parseNetRoute(bytes.NewReader())
	if  != nil {
		return nil, fmt.Errorf("failed to read net route from %s: %w", , )
	}
	return , nil
}

func parseNetRoute( io.Reader) ([]NetRouteLine, error) {
	var  []NetRouteLine

	 := bufio.NewScanner()
	.Scan()
	for .Scan() {
		 := strings.Fields(.Text())
		,  := parseNetRouteLine()
		if  != nil {
			return nil, 
		}
		 = append(, *)
	}
	return , nil
}

func parseNetRouteLine( []string) (*NetRouteLine, error) {
	if len() != routeLineColumns {
		return nil, fmt.Errorf("invalid routeline, num of digits: %d", len())
	}
	 := [0]
	if  == blackholeRepresentation {
		 = blackholeIfaceName
	}
	,  := strconv.ParseUint([1], 16, 32)
	if  != nil {
		return nil, 
	}
	,  := strconv.ParseUint([2], 16, 32)
	if  != nil {
		return nil, 
	}
	,  := strconv.ParseUint([3], 10, 32)
	if  != nil {
		return nil, 
	}
	,  := strconv.ParseUint([4], 10, 32)
	if  != nil {
		return nil, 
	}
	,  := strconv.ParseUint([5], 10, 32)
	if  != nil {
		return nil, 
	}
	,  := strconv.ParseUint([6], 10, 32)
	if  != nil {
		return nil, 
	}
	,  := strconv.ParseUint([7], 16, 32)
	if  != nil {
		return nil, 
	}
	,  := strconv.ParseUint([8], 10, 32)
	if  != nil {
		return nil, 
	}
	,  := strconv.ParseUint([9], 10, 32)
	if  != nil {
		return nil, 
	}
	,  := strconv.ParseUint([10], 10, 32)
	if  != nil {
		return nil, 
	}
	 := &NetRouteLine{
		Iface:       ,
		Destination: uint32(),
		Gateway:     uint32(),
		Flags:       uint32(),
		RefCnt:      uint32(),
		Use:         uint32(),
		Metric:      uint32(),
		Mask:        uint32(),
		MTU:         uint32(),
		Window:      uint32(),
		IRTT:        uint32(),
	}
	return , nil
}