如何将 uintptr 转换为 go struct

How to convert uintptr to go struct

我试图通过 golang 包中的系统调用调用 WindowsAPI(尽量不要使用 cgo),但我面临 uintptr 我不知道如何访问 uintptr 具有其地址的数据

这是我正在做的代码

package soq

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

const (
    retErrInsufficientBuffer = 122
)

var (
    kernel32, _  = syscall.LoadLibrary("kernel32")
    heapAlloc, _ = syscall.GetProcAddress(kernel32, "HeapAlloc")
    heapFree, _  = syscall.GetProcAddress(kernel32, "HeapFree")

    getProcessHeap, _ = syscall.GetProcAddress(kernel32, "GetProcessHeap")
    processHeap, _, _ = syscall.Syscall6(getProcessHeap, 0, 0, 0, 0, 0, 0, 0)

    libIphlpapi, _  = syscall.LoadLibrary("iphlpapi")
    pGetUDPTable, _ = syscall.GetProcAddress(libIphlpapi, "GetUdpTable")

    pUDPTable uintptr
)

// UDPTableRow .
type UDPTableRow struct {
    LocalAddr net.IP
    LocalPort uint32
}

// UDPTable .
type UDPTable struct {
    NumEntries uint32
    Table      []UDPTableRow
}

// HeapAlloc .
func HeapAlloc(size uint32) uintptr {
    pMem, _, _ := syscall.Syscall(
        heapAlloc, 3,
        processHeap,
        0,
        uintptr(size),
    )
    return pMem
}

// HeapFree .
func HeapFree(pMem uintptr) {
    syscall.Syscall(
        heapFree,
        3,
        processHeap,
        0,
        pMem,
    )
}

// GetUDPTable .
func GetUDPTable() (*UDPTable, error) {
    var dwSize uint32

    if r1, _, _ := syscall.Syscall(
        pGetUDPTable,
        3,
        pUDPTable,
        uintptr(unsafe.Pointer(&dwSize)),
        0,
    ); r1 == retErrInsufficientBuffer {
        HeapFree(pUDPTable)
        pUDPTable = HeapAlloc(dwSize)
    }

    if r1, _, _ := syscall.Syscall(
        pGetUDPTable,
        3,
        pUDPTable,
        uintptr(unsafe.Pointer(&dwSize)),
        0,
    ); r1 != 0 {
        fmt.Println("GetUdpTable() failed with return value", r1)
    }

    /*
        How to retrive data from "pUDPTable"
        It's structure is

        https://msdn.microsoft.com/en-us/library/windows/desktop/aa366930(v=vs.85).aspx

        typedef struct _MIB_UDPTABLE {
            DWORD      dwNumEntries;
            MIB_UDPROW table[ANY_SIZE];
        } MIB_UDPTABLE, *PMIB_UDPTABLE;

        https://msdn.microsoft.com/en-us/library/windows/desktop/aa366926(v=vs.85).aspx

        typedef struct _MIB_UDPROW {
          DWORD dwLocalAddr;
          DWORD dwLocalPort;
        } MIB_UDPROW, *PMIB_UDPROW;
    */

    fmt.Printf("%#v", pUDPTable)

    return nil, nil
}

例如,从WindowsAPIGetUdpTable函数到return一个table的UDP地址:

package main 

import (
    "encoding/binary"
    "fmt"
    "net"
    "os"
    "syscall"
    "unsafe"
)

var (
    kernel32, _     = syscall.LoadLibrary("kernel32")
    libIphlpapi, _  = syscall.LoadLibrary("iphlpapi")
    pGetUDPTable, _ = syscall.GetProcAddress(libIphlpapi, "GetUdpTable")
)

func GetUdpTable() ([]net.UDPAddr, error) {
    var dwSize uint32
    r1, _, e1 := syscall.Syscall(
        pGetUDPTable,
        3,
        uintptr(0),
        uintptr(unsafe.Pointer(&dwSize)),
        0,
    )
    const retErrInsufficientBuffer = 122
    if r1 != retErrInsufficientBuffer {
        err := fmt.Errorf("GetUdpTable() failed with return values", r1, e1)
        return nil, err
    }
    buf := make([]byte, dwSize)
    r1, _, e1 = syscall.Syscall(
        pGetUDPTable,
        3,
        uintptr(unsafe.Pointer(&buf[0])),
        uintptr(unsafe.Pointer(&dwSize)),
        0,
    )
    if r1 != 0 {
        err := fmt.Errorf("GetUdpTable() failed with return values", r1, e1)
        return nil, err
    }

    // MIB_UDPROW structure
    // https://docs.microsoft.com/en-us/windows/win32/api/udpmib/ns-udpmib-mib_udprow
    // https://msdn.microsoft.com/en-us/library/windows/desktop/aa366926.aspx
    type MIB_UDPROW struct {
        dwLocalAddr uint32
        dwLocalPort uint32 // network byte order
    }

    // MIB_UDPTABLE structure
    // https://docs.microsoft.com/en-us/windows/win32/api/udpmib/ns-udpmib-mib_udptable
    // https://msdn.microsoft.com/en-us/library/windows/desktop/aa366930.aspx
    const ANY_SIZE = (1 << 30) / unsafe.Sizeof(MIB_UDPROW{})
    type MIB_UDPTABLE struct {
        dwNumEntries uint32
        table        [ANY_SIZE]MIB_UDPROW
    }

    p_MIB_UDPTABLE := (*MIB_UDPTABLE)(unsafe.Pointer(&buf[0]))
    table := p_MIB_UDPTABLE.table[:p_MIB_UDPTABLE.dwNumEntries]
    udps := make([]net.UDPAddr, len(table))
    for i, row := range table {
        var udp net.UDPAddr
        udp.IP = make([]byte, 4)
        binary.LittleEndian.PutUint32(udp.IP, row.dwLocalAddr)
        udp.Port = int(uint16(row.dwLocalPort>>8 | row.dwLocalPort<<8))
        udps[i] = udp
    }
    return udps, nil
}

func main() {
    udpTable, err := GetUdpTable()
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        return
    }
    fmt.Println(len(udpTable))
    for _, udpAddr := range udpTable {
        fmt.Println(udpAddr.IP, udpAddr.Port)
    }
}

输出:

29
192.168.1.2 137
192.168.56.1 137
192.168.1.2 138
192.168.56.1 138
192.168.1.2 443
0.0.0.0 500
127.0.0.1 1900
192.168.1.2 1900
192.168.56.1 1900
0.0.0.0 3702
0.0.0.0 3702
0.0.0.0 3702
0.0.0.0 3702
0.0.0.0 4500
0.0.0.0 5004
0.0.0.0 5005
0.0.0.0 5355
0.0.0.0 17500
192.168.1.2 21986
0.0.0.0 53521
0.0.0.0 54363
0.0.0.0 54364
192.168.1.2 55808
127.0.0.1 55809
0.0.0.0 59285
0.0.0.0 59287
0.0.0.0 59289
127.0.0.1 63042
0.0.0.0 58180