<commit_before>// Copyright (c) 2014, The Tor Project, Inc.
// See LICENSE for licensing information

package natpmp

import (
	"net"
	"syscall"
	"unsafe"
)

var iphlpapi = syscall.NewLazyDLL("Iphlpapi.dll")
var procGetBestRoute = iphlpapi.NewProc("GetBestRoute")

// mibIPForwardRow is MIBIPFORWARDROW from windows.
type mibIPForwardRow struct {
	dwForwardDest      uint32
	dwForwardMask      uint32
	dwForwardPolicy    uint32
	dwForwardNextHop   uint32
	dwForwardIfIndex   uint32
	dwForwardType      uint32
	dwForwardProto     uint32
	dwForwardAge       uint32
	dwForwardNextHopAS uint32
	dwForwardMetric1   uint32
	dwForwardMetric2   uint32
	dwForwardMetric3   uint32
	dwForwardMetric4   uint32
	dwForwardMetric5   uint32
}

func getGateway() (net.IP, error) {
	// This routine uses "unsafe".  Yolo, swag, 420 blaze it.
	if err := iphlpapi.Load(); err != nil {
		return nil, err
	}
	if err := procGetBestRoute.Find(); err != nil {
		return nil, err
	}

	var dwDestAddr, dwSourceAddr uintptr // 0.0.0.0
	row := mibIPForwardRow{}
	r0, _, e1 := syscall.Syscall(procGetBestRoute.Addr(), 3, dwDestAddr, dwSourceAddr, uintptr(unsafe.Pointer(&row)))
	if r0 != 0 { // r0 != NO_ERROR
		return nil, e1
	}

	// Ok, row should have what windows thinks is the best route to "0.0.0.0"
	// now, which will be the default gateway, per the documentation this is in
	// network byte order.  Assume host byte order is little endian because
	// this is windows.
	a := row.dwForwardNextHop<commit_msg>Display a useful error when getGateway() fails on Windows.<commit_after>// Copyright (c) 2014, The Tor Project, Inc.
// See LICENSE for licensing information

package natpmp

import (
	"net"
	"syscall"
	"unsafe"
)

var iphlpapi = syscall.NewLazyDLL("Iphlpapi.dll")
var procGetBestRoute = iphlpapi.NewProc("GetBestRoute")

// mibIPForwardRow is MIBIPFORWARDROW from windows.
type mibIPForwardRow struct {
	dwForwardDest      uint32
	dwForwardMask      uint32
	dwForwardPolicy    uint32
	dwForwardNextHop   uint32
	dwForwardIfIndex   uint32
	dwForwardType      uint32
	dwForwardProto     uint32
	dwForwardAge       uint32
	dwForwardNextHopAS uint32
	dwForwardMetric1   uint32
	dwForwardMetric2   uint32
	dwForwardMetric3   uint32
	dwForwardMetric4   uint32
	dwForwardMetric5   uint32
}

func getGateway() (net.IP, error) {
	// This routine uses "unsafe".  Yolo, swag, 420 blaze it.
	if err := iphlpapi.Load(); err != nil {
		return nil, err
	}
	if err := procGetBestRoute.Find(); err != nil {
		return nil, err
	}

	var dwDestAddr, dwSourceAddr uintptr // 0.0.0.0
	row := mibIPForwardRow{}
	r0, _, _ := syscall.Syscall(procGetBestRoute.Addr(), 3, dwDestAddr, dwSourceAddr, uintptr(unsafe.Pointer(&row)))
	if r0 != 0 { // r0 != NO_ERROR
		return nil, syscall.Errno(r0)
	}

	// Ok, row should have what windows thinks is the best route to "0.0.0.0"
	// now, which will be the default gateway, per the documentation this is in
	// network byte order.  Assume host byte order is little endian because
	// this is windows.
	a := row.dwForwardNextHop
{'fraction_non_alphanumeric': 0.0912396694214876, 'fraction_numerical': 0.03603305785123967, 'mean_word_length': 3.7060653188180406, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5922012', 'n_tokens_mistral': 1118, 'n_tokens_neox': 1019, 'n_words': 314}