本文整理匯總了Golang中syscall.Bind函數的典型用法代碼示例。如果您正苦於以下問題:Golang Bind函數的具體用法?Golang Bind怎麽用?Golang Bind使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Bind函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NlConnect
// NlConnect is same with libnl nl_connect. nl_close is required for releaseing internal fd.
func NlConnect(sk *NlSock, protocol int) error {
if sk.Fd != -1 {
return NLE_BAD_SOCK
}
if fd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW|syscall.SOCK_CLOEXEC, protocol); err != nil {
return err
} else {
sk.Fd = fd
}
if sk.Flags&NL_SOCK_BUFSIZE_SET != 0 {
if err := NlSocketSetBufferSize(sk, 0, 0); err != nil {
return err
}
}
if sk.Local.Pid == 0 { // _nl_socket_is_local_port_unspecified
pid := syscall.Getpid()
var local *syscall.SockaddrNetlink
for high := 1023; high > 0; high-- {
if next := func() bool {
pidLock.Lock()
defer pidLock.Unlock()
if _, exists := pidUsed[high]; !exists {
pidUsed[high] = true
return false
}
return true
}(); next {
continue
}
local = &syscall.SockaddrNetlink{
Pid: uint32((high << 22) | (pid & 0x3FFFFF)),
}
if err := syscall.Bind(sk.Fd, local); err == nil {
break
} else if err != syscall.EADDRINUSE {
return err
} else {
local = nil
}
}
if local == nil {
return NLE_EXIST
}
sk.Local = *local
sk.Flags &^= NL_OWN_PORT
} else {
if err := syscall.Bind(sk.Fd, &(sk.Local)); err != nil {
return err
}
}
return nil
}
示例2: Bind
// Create a tcp socket, setting the TCP_FASTOPEN socket option.
func (s *TFOServer) Bind() (err error) {
s.fd, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
if err != nil {
if err == syscall.ENOPROTOOPT {
err = errors.New("TCP Fast Open server support is unavailable (unsupported kernel).")
}
return
}
err = syscall.SetsockoptInt(s.fd, syscall.SOL_TCP, TCP_FASTOPEN, 1)
if err != nil {
err = errors.New(fmt.Sprintf("Failed to set necessary TCP_FASTOPEN socket option: %s", err))
return
}
sa := &syscall.SockaddrInet4{Addr: s.ServerAddr, Port: s.ServerPort}
err = syscall.Bind(s.fd, sa)
if err != nil {
err = errors.New(fmt.Sprintf("Failed to bind to Addr: %v, Port: %d, Reason: %s", s.ServerAddr, s.ServerPort, err))
return
}
log.Printf("Server: Bound to addr: %v, port: %d\n", s.ServerAddr, s.ServerPort)
err = syscall.Listen(s.fd, LISTEN_BACKLOG)
if err != nil {
err = errors.New(fmt.Sprintf("Failed to listen: %s", err))
return
}
return
}
示例3: main
func main() {
fd, err := syscall.Socket(
syscall.AF_NETLINK, syscall.SOCK_RAW,
NETLINK_KOBJECT_UEVENT,
)
if err != nil {
log.Println(err)
return
}
nl := syscall.SockaddrNetlink{
Family: syscall.AF_NETLINK,
Pid: uint32(os.Getpid()),
Groups: 1,
}
err = syscall.Bind(fd, &nl)
if err != nil {
log.Println(err)
return
}
b := make([]byte, UEVENT_BUFFER_SIZE*2)
for {
n, err2 := syscall.Read(fd, b)
log.Println(n, err2)
log.Println(b)
act, dev, subsys := parseUBuffer(b)
log.Println(act, dev, subsys)
}
}
示例4: advert
func advert(_ net.PacketConn, src, dst net.IP, p *net.IPNet) error {
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
if err != nil {
return err
}
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1)
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
pdst := dst
if *useLimited && runtime.GOOS == "freebsd" {
dst = directed(p)
syscall.SetsockoptInt(s, syscall.IPPROTO_IP, 0x17, 1) // IP_ONESBCAST
}
sa := &syscall.SockaddrInet4{Port: *port}
copy(sa.Addr[:], src.To4())
if err := syscall.Bind(s, sa); err != nil {
syscall.Close(s)
return err
}
f := os.NewFile(uintptr(s), fmt.Sprintf("udp:%v->", src))
c, err := net.FilePacketConn(f)
f.Close()
if err != nil {
return err
}
defer c.Close()
// If you are lucky, you can see that on some platform the
// kernel sometimes transmits a wrong frame addressed to IPv4
// limited broadcast address with some nexthop's link-layer
// address on a broadcast-capable link.
// In general, using BPF for transmitting IPv4 limited
// broadcast addresses is a reasonable choice.
_, err = c.WriteTo([]byte(fmt.Sprintf("%s-%v", runtime.GOOS, pdst)), &net.UDPAddr{IP: dst, Port: *port})
return err
}
示例5: mcastOpen
func mcastOpen(bindAddr net.IP, port int, ifname string) (*ipv4.PacketConn, *net.UDPConn, error) {
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
if err != nil {
log.Fatal(err)
}
if err := syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
log.Fatal(err)
}
//syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEPORT, 1)
if err := syscall.SetsockoptString(s, syscall.SOL_SOCKET, syscall.SO_BINDTODEVICE, ifname); err != nil {
log.Fatal(err)
}
lsa := syscall.SockaddrInet4{Port: port}
copy(lsa.Addr[:], bindAddr.To4())
if err := syscall.Bind(s, &lsa); err != nil {
syscall.Close(s)
log.Fatal(err)
}
f := os.NewFile(uintptr(s), "")
c, err := net.FilePacketConn(f)
f.Close()
if err != nil {
log.Fatal(err)
}
u := c.(*net.UDPConn)
p := ipv4.NewPacketConn(c)
return p, u, nil
}
示例6: probeIPv6Stack
func probeIPv6Stack() (supportsIPv6, supportsIPv4map bool) {
var probes = []struct {
laddr net.TCPAddr
value int
ok bool
}{
// IPv6 communication capability
{laddr: net.TCPAddr{IP: net.ParseIP("::1")}, value: 1},
// IPv6 IPv4-mapped address communication capability
{laddr: net.TCPAddr{IP: net.IPv4(127, 0, 0, 1)}, value: 0},
}
for i := range probes {
s, err := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
if err != nil {
continue
}
defer closesocket(s)
syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, probes[i].value)
sa, err := sockaddrTCP(&probes[i].laddr, syscall.AF_INET6)
if err != nil {
continue
}
if err := syscall.Bind(s, sa); err != nil {
continue
}
probes[i].ok = true
}
return probes[0].ok, probes[1].ok
}
示例7: Init
func (nlSock *NLSocket) Init() error {
//16 - NETLINK_GENERIC
sd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_DGRAM, NETLINK_GENERIC)
if err != nil {
return fmt.Errorf("cant create netlink socket %v\n", err)
}
pid := uint32(syscall.Getpid())
sa := &syscall.SockaddrNetlink{
Pid: pid,
Groups: 0,
Family: syscall.AF_NETLINK}
if err = syscall.Bind(sd, sa); err != nil {
return fmt.Errorf("cant bind to netlink socket: %v\n", err)
}
nlSock.Lock = new(sync.Mutex)
nlSock.Sd = sd
nlSock.Seq = 0
nlSock.PortID = pid
for k, v := range LookupOnStartup {
familyId, err := nlSock.ResolveFamily(NulStringType(k))
if err != nil {
return err
}
CreateMsgType(v, uint16(*familyId))
MT2Family[k] = uint16(*familyId)
}
return nil
}
示例8: main
func main() {
acceptingFd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
check(err)
addr := &syscall.SockaddrInet4{Port: 3000, Addr: [4]byte{0, 0, 0, 0}}
err = syscall.Bind(acceptingFd, addr)
check(err)
err = syscall.Listen(acceptingFd, 100)
check(err)
for {
connectionFd, _, err := syscall.Accept(acceptingFd)
check(err)
fmt.Println("Accepted new connectrion")
data := make([]byte, 1024)
_, err = syscall.Read(connectionFd, data)
check(err)
fmt.Printf("Received: %s\n", string(data))
_, err = syscall.Write(connectionFd, data)
check(err)
err = syscall.Close(connectionFd)
check(err)
}
}
示例9: NewReusablePortPacketConn
// NewReusablePortPacketConn returns net.FileListener that created from a file discriptor for a socket with SO_REUSEPORT option.
func NewReusablePortPacketConn(proto, addr string) (l net.PacketConn, err error) {
var (
soType, fd int
file *os.File
sockaddr syscall.Sockaddr
)
if sockaddr, soType, err = getSockaddr(proto, addr); err != nil {
return nil, err
}
if fd, err = syscall.Socket(soType, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP); err != nil {
return nil, err
}
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, reusePort, 1); err != nil {
return nil, err
}
if err = syscall.Bind(fd, sockaddr); err != nil {
return nil, err
}
// File Name get be nil
file = os.NewFile(uintptr(fd), filePrefix+strconv.Itoa(os.Getpid()))
if l, err = net.FilePacketConn(file); err != nil {
return nil, err
}
if err = file.Close(); err != nil {
return nil, err
}
return l, err
}
示例10: dial
func (fd *netFD) dial(laddr, raddr sockaddr, deadline time.Time, toAddr func(syscall.Sockaddr) Addr) error {
var err error
var lsa syscall.Sockaddr
if laddr != nil {
if lsa, err = laddr.sockaddr(fd.family); err != nil {
return err
} else if lsa != nil {
if err := syscall.Bind(fd.sysfd, lsa); err != nil {
return os.NewSyscallError("bind", err)
}
}
}
var rsa syscall.Sockaddr
if raddr != nil {
if rsa, err = raddr.sockaddr(fd.family); err != nil {
return err
}
if err := fd.connect(lsa, rsa, deadline); err != nil {
return err
}
fd.isConnected = true
} else {
if err := fd.init(); err != nil {
return err
}
}
lsa, _ = syscall.Getsockname(fd.sysfd)
if rsa, _ = syscall.Getpeername(fd.sysfd); rsa != nil {
fd.setAddr(toAddr(lsa), toAddr(rsa))
} else {
fd.setAddr(toAddr(lsa), raddr)
}
return nil
}
示例11: dial
func (fd *netFD) dial(ctx context.Context, laddr, raddr sockaddr) error {
var err error
var lsa syscall.Sockaddr
if laddr != nil {
if lsa, err = laddr.sockaddr(fd.family); err != nil {
return err
} else if lsa != nil {
if err := syscall.Bind(fd.sysfd, lsa); err != nil {
return os.NewSyscallError("bind", err)
}
}
}
var rsa syscall.Sockaddr
if raddr != nil {
if rsa, err = raddr.sockaddr(fd.family); err != nil {
return err
}
if err := fd.connect(ctx, lsa, rsa); err != nil {
return err
}
fd.isConnected = true
} else {
if err := fd.init(); err != nil {
return err
}
}
lsa, _ = syscall.Getsockname(fd.sysfd)
if rsa, _ = syscall.Getpeername(fd.sysfd); rsa != nil {
fd.setAddr(fd.addrFunc()(lsa), fd.addrFunc()(rsa))
} else {
fd.setAddr(fd.addrFunc()(lsa), raddr)
}
return nil
}
示例12: bind
// Bind our netlink socket and
// send a listen control message to the connector driver.
func (listener *netlinkListener) bind() error {
sock, err := syscall.Socket(
syscall.AF_NETLINK,
syscall.SOCK_DGRAM,
syscall.NETLINK_CONNECTOR)
if err != nil {
return err
}
listener.sock = sock
listener.addr = &syscall.SockaddrNetlink{
Family: syscall.AF_NETLINK,
Groups: _CN_IDX_PROC,
Pid: uint32(os.Getpid()),
}
err = syscall.Bind(listener.sock, listener.addr)
if err != nil {
return err
}
return listener.send(_PROC_CN_MCAST_LISTEN)
}
示例13: connect
func (fd *netFD) connect(la, ra syscall.Sockaddr) error {
// Do not need to call fd.writeLock here,
// because fd is not yet accessible to user,
// so no concurrent operations are possible.
if !canUseConnectEx(fd.net) {
return syscall.Connect(fd.sysfd, ra)
}
// ConnectEx windows API requires an unconnected, previously bound socket.
if la == nil {
switch ra.(type) {
case *syscall.SockaddrInet4:
la = &syscall.SockaddrInet4{}
case *syscall.SockaddrInet6:
la = &syscall.SockaddrInet6{}
default:
panic("unexpected type in connect")
}
if err := syscall.Bind(fd.sysfd, la); err != nil {
return err
}
}
// Call ConnectEx API.
o := &fd.wop
o.sa = ra
_, err := wsrv.ExecIO(o, "ConnectEx", func(o *operation) error {
return syscall.ConnectEx(o.fd.sysfd, o.sa, nil, 0, nil, &o.o)
})
if err != nil {
return err
}
// Refresh socket properties.
return syscall.Setsockopt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_UPDATE_CONNECT_CONTEXT, (*byte)(unsafe.Pointer(&fd.sysfd)), int32(unsafe.Sizeof(fd.sysfd)))
}
示例14: ProxyNetListen
func (io *NetIOManager) ProxyNetListen(sa syscall.Sockaddr) error {
serverfd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM,
syscall.IPPROTO_TCP)
if err != nil {
goto Error
}
err = syscall.Bind(serverfd, sa)
if err != nil {
goto Cleanup
}
err = syscall.Listen(serverfd, io.max_backlog)
if err != nil {
goto Cleanup
}
err = syscall.EpollCtl(io.epoll_fd, syscall.EPOLL_CTL_ADD, serverfd,
&syscall.EpollEvent{Events: syscall.EPOLLIN, Fd: int32(serverfd)})
if err != nil {
goto Cleanup
}
io.proxy_server_fd = serverfd
return nil
Cleanup:
syscall.Close(serverfd)
Error:
return err
}
示例15: connect
func (fd *netFD) connect(ra syscall.Sockaddr) error {
if !canUseConnectEx(fd.net) {
return syscall.Connect(fd.sysfd, ra)
}
// ConnectEx windows API requires an unconnected, previously bound socket.
var la syscall.Sockaddr
switch ra.(type) {
case *syscall.SockaddrInet4:
la = &syscall.SockaddrInet4{}
case *syscall.SockaddrInet6:
la = &syscall.SockaddrInet6{}
default:
panic("unexpected type in connect")
}
if err := syscall.Bind(fd.sysfd, la); err != nil {
return err
}
// Call ConnectEx API.
var o connectOp
o.Init(fd, 'w')
o.ra = ra
_, err := iosrv.ExecIO(&o, fd.wdeadline.value())
if err != nil {
return err
}
// Refresh socket properties.
return syscall.Setsockopt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_UPDATE_CONNECT_CONTEXT, (*byte)(unsafe.Pointer(&fd.sysfd)), int32(unsafe.Sizeof(fd.sysfd)))
}