pax_global_header00006660000000000000000000000064146013624000014506gustar00rootroot0000000000000052 comment=ba1a5b918e7c3b4f9c092dc11a6aa9fb2a449d7f prometheus-homeplug-exporter-0.4.0/000077500000000000000000000000001460136240000173665ustar00rootroot00000000000000prometheus-homeplug-exporter-0.4.0/.gitignore000066400000000000000000000000431460136240000213530ustar00rootroot00000000000000go.sum homeplug_exporter vendor/*/ prometheus-homeplug-exporter-0.4.0/.promu.yml000066400000000000000000000011561460136240000213340ustar00rootroot00000000000000go: version: 1.14 cgo: true repository: path: github.com/NightTsarina/homeplug_exporter build: binaries: - name: homeplug_exporter flags: -a -tags netgo static: true ldflags: | -X github.com/prometheus/common/version.Version={{.Version}} -X github.com/prometheus/common/version.Revision={{.Revision}} -X github.com/prometheus/common/version.Branch={{.Branch}} -X github.com/prometheus/common/version.BuildUser={{user}}@{{host}} -X github.com/prometheus/common/version.BuildDate={{date "20060102-15:04:05"}} tarball: files: - LICENSE prometheus-homeplug-exporter-0.4.0/Dockerfile000066400000000000000000000004501460136240000213570ustar00rootroot00000000000000FROM golang:1.14-buster AS builder COPY ./ /go/src/github.com/nighttsarina/homeplug_exporter/ WORKDIR /go/src/github.com/nighttsarina/homeplug_exporter/ RUN make FROM scratch COPY --from=builder /go/src/github.com/nighttsarina/homeplug_exporter /go/bin/ ENTRYPOINT ["/go/bin/homeplug_exporter"] prometheus-homeplug-exporter-0.4.0/LICENSE000066400000000000000000000021711460136240000203740ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2020 brandond (Brad Davidson) 2024 Martina Ferrari Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. prometheus-homeplug-exporter-0.4.0/Makefile000066400000000000000000000033211460136240000210250ustar00rootroot00000000000000# Copyright 2015 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. GO := GO111MODULE=on go PROMU := $(shell $(GO) env GOPATH)/bin/promu pkgs = $(shell $(GO) list ./... | grep -v /vendor/) PREFIX ?= $(shell pwd) BIN_DIR ?= $(shell pwd) DOCKER_IMAGE_NAME ?= homeplug-exporter DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD)) all: format build test test: @echo ">> running tests" @$(GO) test -short $(pkgs) style: @echo ">> checking code style" @! gofmt -d $(shell find . -path ./vendor -prune -o -name '*.go' -print) | grep '^' format: @echo ">> formatting code" @$(GO) fmt $(pkgs) vet: @echo ">> vetting code" @$(GO) vet $(pkgs) build: promu @echo ">> building binaries" @$(PROMU) build --prefix $(PREFIX) tarball: promu @echo ">> building release tarball" @$(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR) docker: @echo ">> building docker image" @docker build -t "$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" . promu: @GOOS=$(shell uname -s | tr A-Z a-z) \ GOARCH=$(subst x86_64,amd64,$(patsubst i%86,386,$(shell uname -m))) \ $(GO) get -u github.com/prometheus/promu .PHONY: all style format build test vet tarball docker promu prometheus-homeplug-exporter-0.4.0/README.md000066400000000000000000000104641460136240000206520ustar00rootroot00000000000000# HomePlug exporter for Prometheus Prometheus exporter for [HomePlug](https://en.wikipedia.org/wiki/HomePlug) Power Line Communication (PLC) devices. It collects and exports metrics about the HomePlug network and stations. Tested with TP-Link and Devolo devices but should work with any device using a Qualcomm Atheros chipset such as QCA6410, QCA7000, and QCA7420. Currently it does not support other chipsets as it uses Atheros-specific messages to gather statistics. # Running Command-line flags: ``` usage: homeplug_exporter [] Flags: -h, --help Show context-sensitive help (also try --help-long and --help-man). --telemetry.address=":9702" Address on which to expose metrics. --telemetry.endpoint="/metrics" Path under which to expose metrics. --interface=INTERFACE Interface to search for Homeplug devices. --destaddr=local Destination MAC address for Homeplug devices. Accepts 'local', 'all', and 'broadcast' as aliases. --log.level=info Only log messages with the given severity or above. One of: [debug, info, warn, error] --log.format=logfmt Output format of log messages. One of: [logfmt, json] --version Show application version. ``` The `destaddr` parameter specifies the MAC address of a HomePlug device or one of these aliases: * `all`, `broadcast` A synonym for the Ethernet broadcast address: `ff:ff:ff:ff:ff:ff`. All devices, whether local, remote, or foreign[^1] recognize messages sent to this address. * `local` (default) A synonym for the Qualcomm Atheros vendor specific Local Management Address (LMA): `00:b0:52:00:00:01`. All local Atheros devices recognize this address but remote and foreign devices do not. Note that the default destination (`local`) will only find devices on the near side of a PLC connection. Once a reply is obtained from a device, the exporter directly queries all other devices connected to the same HomePlug network. [^1]: A "local device" is any device at the near end of a PLC connection. A "remote device" is any device at the far end of a PLC connection. A "foreign device" is any device not manufactured by Atheros. ## Permissions The exporter needs to access raw sockets, so it needs to be run as root, or with `cap_net_raw` capability. To avoid running it as root, set the capabilities on the binary as follows: ``` sudo setcap cap_net_raw=eip /path/to/binary ``` ## Using Docker **NOTE:** The HomePlug protocol uses raw Ethernet frames, and must be run with `--net=host` on the same layer 2 network segment as at least one HomePlug device. ``` docker build -t homeplug_exporter . docker run --rm --detach --name=homeplug_exporter --net=host homeplug_exporter ``` # Metrics ``` # HELP homeplug_exporter_build_info A metric with a constant '1' value labeled by version, revision, branch, and goversion from which homeplug_exporter was built. # TYPE homeplug_exporter_build_info gauge homeplug_exporter_build_info{branch="main",goversion="go1.22.1",revision="",version="0.4.0"} 1 # HELP homeplug_network_info Logical network information # TYPE homeplug_network_info gauge homeplug_network_info{cco_addr="de:ad:be:ef:00:01",cco_tei="1",device_addr="de:ad:be:ef:00:01",nid="52:de:ad:be:ef:00:01",role="CCO",snid="6",tei="1"} 1 homeplug_network_info{cco_addr="de:ad:be:ef:00:01",cco_tei="1",device_addr="de:ad:be:ef:00:02",nid="52:de:ad:be:ef:00:01",role="STA",snid="6",tei="2"} 1 # HELP homeplug_station_rx_rate_bytes Average PHY Rx data rate # TYPE homeplug_station_rx_rate_bytes gauge homeplug_station_rx_rate_bytes{device_addr="de:ad:be:ef:00:01",nid="52:de:ad:be:ef:00:01",peer_addr="de:ad:be:ef:00:02"} 1.86e+07 homeplug_station_rx_rate_bytes{device_addr="de:ad:be:ef:00:02",nid="52:de:ad:be:ef:00:01",peer_addr="de:ad:be:ef:00:01"} 1.18e+06 # HELP homeplug_station_tx_rate_bytes Average PHY Tx data rate # TYPE homeplug_station_tx_rate_bytes gauge homeplug_station_tx_rate_bytes{device_addr="de:ad:be:ef:00:01",nid="52:de:ad:be:ef:00:01",peer_addr="de:ad:be:ef:00:02"} 1.18e+06 homeplug_station_tx_rate_bytes{device_addr="de:ad:be:ef:00:02",nid="52:de:ad:be:ef:00:01",peer_addr="de:ad:be:ef:00:01"} 1.86e+07 ``` prometheus-homeplug-exporter-0.4.0/build.sh000077500000000000000000000002741460136240000210270ustar00rootroot00000000000000#!/bin/bash set -euo pipefail TOPLEVEL=`git rev-parse --show-toplevel` REPONAME=`basename ${TOPLEVEL}` TAG=${1:-latest} docker build --force-rm -t ${USER}/${REPONAME}:${TAG} ${TOPLEVEL} prometheus-homeplug-exporter-0.4.0/go.mod000066400000000000000000000017661460136240000205060ustar00rootroot00000000000000module github.com/nighttsarina/homeplug_exporter go 1.21 toolchain go1.22.1 require ( github.com/go-kit/log v0.2.1 github.com/mdlayher/ethernet v0.0.0-20190606142754-0394541c37b7 github.com/mdlayher/raw v0.0.0-20191009151244-50f2db8cc065 github.com/prometheus/client_golang v1.19.0 github.com/prometheus/common v0.51.1 gopkg.in/alecthomas/kingpin.v2 v2.2.6 ) require ( github.com/alecthomas/kingpin/v2 v2.4.0 // indirect github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/prometheus/client_model v0.6.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect github.com/xhit/go-str2duration/v2 v2.1.0 // indirect golang.org/x/net v0.22.0 // indirect golang.org/x/sys v0.18.0 // indirect google.golang.org/protobuf v1.33.0 // indirect ) prometheus-homeplug-exporter-0.4.0/homeplug_exporter.go000066400000000000000000000275021460136240000234730ustar00rootroot00000000000000package main import ( "errors" "fmt" "io" "net" "net/http" "os" "strconv" "sync" "time" "github.com/alecthomas/kingpin/v2" "github.com/go-kit/log" "github.com/go-kit/log/level" "github.com/mdlayher/ethernet" "github.com/mdlayher/raw" "github.com/prometheus/client_golang/prometheus" versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/promlog" "github.com/prometheus/common/promlog/flag" "github.com/prometheus/common/version" ) const ( namespace = "homeplug" etherType = 0x88E1 ) var ( hpVersion = [...]byte{0x00} nwInfoReq = [...]byte{0xA0, 0x38} nwInfoCnf = [...]byte{0xA0, 0x39} hpVendor = [...]byte{0x00, 0xB0, 0x52} stRole = [...]string{"STA", "PCO", "CCO"} listeningAddress = kingpin.Flag("telemetry.address", "Address on which to expose metrics.").Default(":9702").String() metricsEndpoint = kingpin.Flag("telemetry.endpoint", "Path under which to expose metrics.").Default("/metrics").String() interfaceName = kingpin.Flag("interface", "Interface to search for Homeplug devices.").String() destAddress = MacAddress(kingpin.Flag("destaddr", "Destination MAC address for Homeplug devices. Accepts 'local', 'all', and 'broadcast' as aliases."). Default("local").HintOptions("local", "all", "broadcast")) logger log.Logger ) type Exporter struct { iface *net.Interface conn *raw.Conn dest net.HardwareAddr mutex sync.Mutex txRate *prometheus.Desc rxRate *prometheus.Desc network *prometheus.Desc } func NewExporter(iface *net.Interface, conn *raw.Conn, dest net.HardwareAddr) *Exporter { return &Exporter{ iface: iface, conn: conn, dest: dest, txRate: prometheus.NewDesc( prometheus.BuildFQName(namespace, "station", "tx_rate_bytes"), "Average PHY Tx data rate", []string{"device_addr", "nid", "peer_addr"}, nil), rxRate: prometheus.NewDesc( prometheus.BuildFQName(namespace, "station", "rx_rate_bytes"), "Average PHY Rx data rate", []string{"device_addr", "nid", "peer_addr"}, nil), network: prometheus.NewDesc( prometheus.BuildFQName(namespace, "network", "info"), "Logical network information", []string{"device_addr", "nid", "snid", "tei", "role", "cco_addr", "cco_tei"}, nil), } } func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { ch <- e.txRate ch <- e.rxRate ch <- e.network } func (e *Exporter) Collect(ch chan<- prometheus.Metric) { e.mutex.Lock() defer e.mutex.Unlock() err := e.collect(ch) if err != nil { level.Error(logger).Log("msg", "Error scraping Homeplug", "err", err) } } func (e *Exporter) collect(ch chan<- prometheus.Metric) error { netinfos, err := get_homeplug_netinfo(e.iface, e.conn, e.dest) if err != nil { return err } for _, info := range netinfos { if len(info.Networks) == 0 { continue } for _, network := range info.Networks { ch <- prometheus.MustNewConstMetric(e.network, prometheus.GaugeValue, 1, info.Address.String(), network.NetworkID.String(), strconv.FormatUint(uint64(network.ShortID), 10), strconv.FormatUint(uint64(network.TEI), 10), stRole[network.Role], network.CCoAddress.String(), strconv.FormatUint(uint64(network.CCoTEI), 10)) } network0 := info.Networks[0] for _, station := range info.Stations { ch <- prometheus.MustNewConstMetric(e.txRate, prometheus.GaugeValue, float64(uint64(station.TxRate)*1024*1024/8), info.Address.String(), network0.NetworkID.String(), station.Address.String()) ch <- prometheus.MustNewConstMetric(e.rxRate, prometheus.GaugeValue, float64(uint64(station.RxRate)*1024*1024/8), info.Address.String(), network0.NetworkID.String(), station.Address.String()) } } return nil } type HomeplugNetworkInfo struct { Address net.HardwareAddr Networks []HomeplugNetworkStatus Stations []HomeplugStationStatus } func (n *HomeplugNetworkInfo) UnmarshalBinary(b []byte) error { o := 0 var num_networks = int(b[o]) o++ for i := 0; i < num_networks; i++ { var ns HomeplugNetworkStatus size, err := (&ns).UnmarshalBinary(b[o:]) if err != nil { return err } n.Networks = append(n.Networks, ns) o += size } var num_stations = int(b[o]) o++ for i := 0; i < num_stations; i++ { var ss HomeplugStationStatus size, err := (&ss).UnmarshalBinary(b[o:]) if err != nil { return err } n.Stations = append(n.Stations, ss) o += size } return nil } type HomeplugNetworkStatus struct { NetworkID net.HardwareAddr ShortID uint8 TEI uint8 Role uint8 CCoAddress net.HardwareAddr CCoTEI uint8 } func (s *HomeplugNetworkStatus) UnmarshalBinary(b []byte) (int, error) { if len(b) < 17 { return 0, io.ErrUnexpectedEOF } s.NetworkID = b[0:7] s.ShortID = b[7] s.TEI = b[8] s.Role = b[9] s.CCoAddress = b[10:16] s.CCoTEI = b[16] return 17, nil } type HomeplugStationStatus struct { Address net.HardwareAddr TEI uint8 BridgedAddress net.HardwareAddr TxRate uint8 RxRate uint8 } func (s *HomeplugStationStatus) UnmarshalBinary(b []byte) (int, error) { if len(b) < 15 { return 0, io.ErrUnexpectedEOF } s.Address = b[0:6] s.TEI = b[6] s.BridgedAddress = b[7:13] s.TxRate = b[13] s.RxRate = b[14] return 15, nil } type HomeplugFrame struct { Version [1]byte MMEType [2]byte Vendor [3]byte Payload []byte } func (h *HomeplugFrame) MarshalBinary() ([]byte, error) { b := make([]byte, h.length()) _, err := h.read(b) return b, err } func (h *HomeplugFrame) read(b []byte) (int, error) { b[0] = h.Version[0] b[1] = h.MMEType[1] b[2] = h.MMEType[0] b[3] = h.Vendor[0] b[4] = h.Vendor[1] b[5] = h.Vendor[2] copy(b[6:], h.Payload[:]) return len(b), nil } func (h *HomeplugFrame) length() int { return 6 + len(h.Payload) } func (h *HomeplugFrame) UnmarshalBinary(b []byte) error { if len(b) < 6 { return io.ErrUnexpectedEOF } bb := make([]byte, len(b)-6) copy(bb[:], b[6:]) h.Version[0] = b[0] h.MMEType[1] = b[1] h.MMEType[0] = b[2] h.Vendor[0] = b[3] h.Vendor[1] = b[4] h.Vendor[2] = b[5] h.Payload = bb return nil } func main() { promlogConfig := &promlog.Config{} flag.AddFlags(kingpin.CommandLine, promlogConfig) kingpin.Version(version.Print("homeplug_exporter")) kingpin.HelpFlag.Short('h') kingpin.Parse() logger = promlog.New(promlogConfig) level.Info(logger).Log("msg", "Starting homeplug_exporter", "version", version.Info()) level.Info(logger).Log("msg", "Build context", "build_context", version.BuildContext()) iface, err := get_interface_or_default(*interfaceName) if err != nil { level.Error(logger).Log("msg", "Failed to get interface", "err", err) os.Exit(1) } conn, err := raw.ListenPacket(iface, etherType, nil) if err != nil { level.Error(logger).Log("msg", "Failed to listen", "err", err) os.Exit(1) } exporter := NewExporter(iface, conn, *destAddress) prometheus.MustRegister(exporter) prometheus.MustRegister(versioncollector.NewCollector("homeplug_exporter")) level.Info(logger).Log("msg", "Collector parameters", "destaddr", destAddress, "interface", iface.Name) level.Info(logger).Log("msg", "Starting HTTP server", "telemetry.address", *listeningAddress, "telemetry.endpoint", *metricsEndpoint) http.Handle(*metricsEndpoint, promhttp.Handler()) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(` Homeplug Exporter

Homeplug Exporter

Metrics

`)) }) if err := http.ListenAndServe(*listeningAddress, nil); err != nil { level.Error(logger).Log("msg", "Failed to bind HTTP server", "err", err) os.Exit(1) } } func get_homeplug_netinfo(iface *net.Interface, conn *raw.Conn, dest net.HardwareAddr) ([]HomeplugNetworkInfo, error) { seen := make(map[string]bool, 0) ni := make([]HomeplugNetworkInfo, 0) ch := make(chan HomeplugNetworkInfo, 1) go read_homeplug(iface, conn, ch) err := write_homeplug(iface, conn, dest) if err != nil { return nil, fmt.Errorf("write_homeplug failed: %w", err) } ChanLoop: for { select { case n := <-ch: addr := n.Address.String() if seen[addr] { continue } ni = append(ni, n) seen[addr] = true // Query each remote station directly. for _, station := range n.Stations { if err := write_homeplug(iface, conn, station.Address); err != nil { return nil, fmt.Errorf("write_homeplug failed: %w", err) } } case <-time.After(time.Second): break ChanLoop } } if len(ni) == 0 { return ni, nil } return ni, nil } func write_homeplug(iface *net.Interface, conn *raw.Conn, dest net.HardwareAddr) error { h := &HomeplugFrame{ Version: hpVersion, MMEType: nwInfoReq, Vendor: hpVendor, } b, err := h.MarshalBinary() if err != nil { return fmt.Errorf("failed to marshal homeplug frame: %w", err) } f := ðernet.Frame{ Destination: dest, Source: iface.HardwareAddr, EtherType: etherType, Payload: b, } a := &raw.Addr{ HardwareAddr: dest, } b, err = f.MarshalBinary() if err != nil { return fmt.Errorf("failed to marshal ethernet frame: %w", err) } _, err = conn.WriteTo(b, a) if err != nil { return fmt.Errorf("failed to send message: %w", err) } return nil } func read_homeplug(iface *net.Interface, conn *raw.Conn, ch chan<- HomeplugNetworkInfo) { b := make([]byte, iface.MTU) for { conn.SetReadDeadline(time.Now().Add(time.Second)) n, addr, err := conn.ReadFrom(b) if err != nil { level.Debug(logger).Log("msg", "Failed to receive message", "err", err) break } var f ethernet.Frame err = (&f).UnmarshalBinary(b[:n]) if err != nil { level.Error(logger).Log("msg", "Failed to unmarshal ethernet frame", "err", err, "from", addr) continue } var h HomeplugFrame err = (&h).UnmarshalBinary(f.Payload) if err != nil { level.Error(logger).Log("msg", "Failed to unmarshal homeplug frame", "err", err) continue } level.Debug(logger).Log( "msg", "Received homeplug frame", "from", addr, "version", fmt.Sprintf("%#x", h.Version), "mme_type", fmt.Sprintf("%#x", h.MMEType), "vendor", fmt.Sprintf("%#x", h.Vendor), "payload", fmt.Sprintf("[% x]", h.Payload), ) if h.MMEType != nwInfoCnf { level.Error(logger).Log("msg", "Got unhandled MME type", "mme_type", h.MMEType) continue } hni := HomeplugNetworkInfo{Address: f.Source} if err := (&hni).UnmarshalBinary(h.Payload); err != nil { level.Error(logger).Log("msg", "Failed to unmarshal network info frame", "err", err) continue } if len(hni.Networks) == 0 { level.Error(logger).Log("msg", "Ignoring isolated device", "device_addr", hni.Address) continue } for _, network := range hni.Networks { level.Debug(logger).Log( "msg", "Network found", "device_addr", hni.Address, "nid", network.NetworkID, "snid", network.ShortID, "tei", network.TEI, "role", stRole[network.Role], "cco_addr", network.CCoAddress, "cco_tei", network.CCoTEI, ) } for _, station := range hni.Stations { level.Debug(logger).Log( "msg", "Connected station found", "device_addr", hni.Address, "peer_addr", station.Address, "bda", station.BridgedAddress, "tx_rate", station.TxRate, "rx_rate", station.RxRate, ) } ch <- hni } } func get_interface_or_default(name string) (*net.Interface, error) { if *interfaceName == "" { ifaces, err := net.Interfaces() if err != nil { return nil, err } for _, iface := range ifaces { if iface.Flags&net.FlagUp == 0 { continue } if iface.Flags&net.FlagLoopback != 0 { continue } return &iface, nil } } else { iface, err := net.InterfaceByName(*interfaceName) if err != nil { return nil, err } return iface, nil } return nil, &net.OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errors.New("invalid network interface")} } prometheus-homeplug-exporter-0.4.0/macaddress.go000066400000000000000000000016441460136240000220300ustar00rootroot00000000000000// vim:ts=2:sw=2:et:ai:sts=2 package main import ( "errors" "net" "github.com/alecthomas/kingpin/v2" ) var ( bcastMAC = net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff} localMAC = net.HardwareAddr{0x00, 0xb0, 0x52, 0x00, 0x00, 0x01} ) type macAddressValue net.HardwareAddr func (m *macAddressValue) Set(s string) error { if s == "broadcast" || s == "all" { *m = (macAddressValue)(bcastMAC) } else if s == "local" { *m = (macAddressValue)(localMAC) } else { v, err := net.ParseMAC(s) if err != nil { return err } else if len(v) != 6 { return errors.New("invalid address length") } *m = (macAddressValue)(v) } return nil } func (m *macAddressValue) Get() interface{} { return *m } func (m *macAddressValue) String() string { return string(*m) } func MacAddress(s kingpin.Settings) (target *net.HardwareAddr) { target = &net.HardwareAddr{} s.SetValue((*macAddressValue)(target)) return }