本文整理汇总了Golang中net.UDPConn.File方法的典型用法代码示例。如果您正苦于以下问题:Golang UDPConn.File方法的具体用法?Golang UDPConn.File怎么用?Golang UDPConn.File使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.UDPConn
的用法示例。
在下文中一共展示了UDPConn.File方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getFile
func getFile(conn *net.UDPConn) *os.File {
file, err := conn.File()
if err != nil {
log.Fatal(err)
}
return file
}
示例2: getUDPSocketName
func getUDPSocketName(conn *net.UDPConn) (syscall.Sockaddr, error) {
file, err := conn.File()
if err != nil {
return nil, err
}
return syscall.Getsockname(int(file.Fd()))
}
示例3: setUDPSocketOptions6
// setUDPSocketOptions6 prepares the v6 socket for sessions.
func setUDPSocketOptions6(conn *net.UDPConn) error {
file, err := conn.File()
if err != nil {
return err
}
if err := syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IPV6, syscall.IPV6_RECVPKTINFO, 1); err != nil {
return err
}
return nil
}
示例4: getUDPSocketOptions6Only
// getUDPSocketOption6Only return true if the socket is v6 only and false when it is v4/v6 combined
// (dualstack).
func getUDPSocketOptions6Only(conn *net.UDPConn) (bool, error) {
file, err := conn.File()
if err != nil {
return false, err
}
// dual stack. See http://stackoverflow.com/questions/1618240/how-to-support-both-ipv4-and-ipv6-connections
v6only, err := syscall.GetsockoptInt(int(file.Fd()), syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY)
if err != nil {
return false, err
}
return v6only == 1, nil
}
示例5: SetMulticastLoopback
// SetMulticastLoopback turns on or off multicast loopbacks on the interface the connection is on.
func SetMulticastLoopback(conn *net.UDPConn, ipversion int, v bool) error {
file, err := conn.File()
if err != nil {
return err
}
fd := int(file.Fd())
switch ipversion {
default:
return setIPv4MulticastLoopback(fd, v)
case 6:
return setIPv6MulticastLoopback(fd, v)
}
}
示例6: setUDPSocketOptions4
// setUDPSocketOptions4 prepares the v4 socket for sessions.
func setUDPSocketOptions4(conn *net.UDPConn) error {
file, err := conn.File()
if err != nil {
return err
}
if err := syscall.SetsockoptInt(int(file.Fd()), syscall.IPPROTO_IP, syscall.IP_PKTINFO, 1); err != nil {
return err
}
// Calling File() above results in the connection becoming blocking, we must fix that.
// See https://github.com/miekg/dns/issues/279
err = syscall.SetNonblock(int(file.Fd()), true)
if err != nil {
return err
}
return nil
}
示例7: runCProxy
func runCProxy(tun *os.File, conn *net.UDPConn, ctl *os.File, tunIP ip.IP4, tunMTU int) {
var log_errors int
if log.V(1) {
log_errors = 1
}
c, err := conn.File()
if err != nil {
log.Error("Converting UDPConn to File failed: ", err)
return
}
defer c.Close()
C.run_proxy(
C.int(tun.Fd()),
C.int(c.Fd()),
C.int(ctl.Fd()),
C.in_addr_t(tunIP.NetworkOrder()),
C.size_t(tunMTU),
C.int(log_errors),
)
}
示例8: SetMulticastTTL
// SetMulticastTTL sets the TTL on packets from this connection.
func SetMulticastTTL(conn *net.UDPConn, ipversion int, v int) error {
var proto, opt int
switch ipversion {
default:
proto = syscall.IPPROTO_IP
opt = syscall.IP_MULTICAST_TTL
case 6:
proto = syscall.IPPROTO_IPV6
opt = syscall.IPV6_MULTICAST_HOPS
}
if file, err := conn.File(); err == nil {
fd := int(file.Fd())
err := syscall.SetsockoptInt(fd, proto, opt, v)
if err != nil {
return os.NewSyscallError("setsockopt", err)
}
file.Close()
} else {
return err
}
return nil
}
示例9: handleUDP
func handleUDP(udpConn *net.UDPConn, addr string) {
log.Printf("serving SIP on UDP %s", addr)
if _, err := udpConn.File(); err != nil {
log.Printf("handleUDP: unable to set UDP connection to blocking: %s", err)
}
for {
/*
rfc3261 18.1.1 Sending Requests
However implementations MUST be able to handle messages up to the maximum
datagram packet size. For UDP, this size is 65,535 bytes, including
IP and UDP headers.
*/
buf := make([]byte, 66536)
n, addr, err := udpConn.ReadFromUDP(buf)
if err != nil {
log.Printf("ReadFromUDP: %s", err)
}
log.Printf("ReadFromUDP: %d bytes from %s", n, addr)
parseSIP(string(buf[:n]))
}
}
示例10: fastProxy
func fastProxy(sm *subnet.SubnetManager, tun *os.File, conn *net.UDPConn, tunIP ip.IP4, tunMTU uint, port int) {
log.Info("Running fast proxy loop")
c, err := conn.File()
if err != nil {
log.Error("Converting UDPConn to File failed: ", err)
return
}
defer c.Close()
ctl, peerCtl, err := newCtlSockets()
if err != nil {
log.Error("Failed to create control socket: ", err)
return
}
defer ctl.Close()
defer peerCtl.Close()
go runCProxy(tun, c, peerCtl, tunIP, tunMTU)
log.Info("Watching for new subnet leases")
evts := make(chan subnet.EventBatch)
sm.Start(evts)
for evtBatch := range evts {
for _, evt := range evtBatch {
if evt.Type == subnet.SubnetAdded {
log.Info("Subnet added: ", evt.Lease.Network)
var attrs subnet.BaseAttrs
if err := json.Unmarshal([]byte(evt.Lease.Data), &attrs); err != nil {
log.Error("Error decoding subnet lease JSON: ", err)
continue
}
cmd := C.command{
cmd: C.CMD_SET_ROUTE,
dest_net: C.in_addr_t(evt.Lease.Network.IP.NetworkOrder()),
dest_net_len: C.int(evt.Lease.Network.PrefixLen),
next_hop_ip: C.in_addr_t(attrs.PublicIP.NetworkOrder()),
next_hop_port: C.short(port),
}
writeCommand(ctl, &cmd)
} else if evt.Type == subnet.SubnetRemoved {
log.Info("Subnet removed: ", evt.Lease.Network)
cmd := C.command{
cmd: C.CMD_DEL_ROUTE,
dest_net: C.in_addr_t(evt.Lease.Network.IP.NetworkOrder()),
dest_net_len: C.int(evt.Lease.Network.PrefixLen),
}
writeCommand(ctl, &cmd)
} else {
log.Error("Internal error: unknown event type: ", int(evt.Type))
}
}
}
}