File: loopback.go

package info (click to toggle)
ipp-usb 0.9.23-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 480 kB
  • sloc: sh: 42; makefile: 35
file content (33 lines) | stat: -rw-r--r-- 658 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* ipp-usb - HTTP reverse proxy, backed by IPP-over-USB connection to device
 *
 * Copyright (C) 2020 and up by Alexander Pevzner (pzz@apevzner.com)
 * See LICENSE for license terms and conditions
 *
 * Loopback interface index discovery
 */

package main

import (
	"errors"
	"fmt"
	"net"
)

// Loopback returns index of loopback interface
func Loopback() (int, error) {
	interfaces, err := net.Interfaces()
	if err == nil {
		for _, iface := range interfaces {
			if (iface.Flags & net.FlagLoopback) != 0 {
				return iface.Index, nil
			}
		}
	}

	if err == nil {
		err = errors.New("not found")
	}

	return 0, fmt.Errorf("Loopback discovery: %s", err)
}