go resolve tcp address

import (
    "encoding/binary"
    "fmt"
    "net"
    "strconv"
    "unsafe"
)

func htons(w uint16) uint16 {
    return w>>8 | w<<8
}

func resolveaddr(address string) (af int, sa interface{}, size uintptr, err error) {
    var addr *net.TCPAddr
    addr, err = net.ResolveTCPAddr("tcp", address)
    if err != nil {
        return
    }
    ip := addr.IP
    if len(ip) == 0 {
        ip = net.IPv4zero
    }
    if len(ip) == net.IPv4len {
        af = AF_INET
        sa = &sockaddr_in{
            sin_family: AF_INET,
            sin_port:   htons(uint16(addr.Port)),
            sin_addr:   binary.BigEndian.Uint32(ip),
        }
        size = unsafe.Sizeof(sockaddr_in{})
    } else if len(ip) == net.IPv6len {
        af = AF_INET6
        scopeid := uint32(0)
        nif, err := net.InterfaceByName(addr.Zone)
        if err == nil {
            scopeid = uint32(nif.Index)
        } else {
            n, _ := strconv.Atoi(addr.Zone)
            scopeid = uint32(n)
        }
        sin6 := &sockaddr_in6{
            sin6_family:   AF_INET6,
            sin6_port:     htons(uint16(addr.Port)),
            sin6_scope_id: scopeid,
        }
        copy(sin6.sin6_addr[:], ip)
        sa = sin6
        size = unsafe.Sizeof(sockaddr_in6{})
    } else {
        err = fmt.Errorf("unrecognized address: %s", address)
    }
    return
}

标签: none

添加新评论