本文整理匯總了Golang中code/google/com/p/go/net/ipv4.NewPacketConn函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewPacketConn函數的具體用法?Golang NewPacketConn怎麽用?Golang NewPacketConn使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewPacketConn函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestUDPMultiplePacketConnWithMultipleGroupListeners
func TestUDPMultiplePacketConnWithMultipleGroupListeners(t *testing.T) {
switch runtime.GOOS {
case "plan9", "windows":
t.Skipf("not supported on %q", runtime.GOOS)
}
if testing.Short() {
t.Skip("to avoid external network")
}
for _, gaddr := range udpMultipleGroupListenerTests {
c1, err := net.ListenPacket("udp4", "224.0.0.0:1024") // wildcard address with reusable port
if err != nil {
t.Fatalf("net.ListenPacket failed: %v", err)
}
defer c1.Close()
c2, err := net.ListenPacket("udp4", "224.0.0.0:1024") // wildcard address with reusable port
if err != nil {
t.Fatalf("net.ListenPacket failed: %v", err)
}
defer c2.Close()
var ps [2]*ipv4.PacketConn
ps[0] = ipv4.NewPacketConn(c1)
ps[1] = ipv4.NewPacketConn(c2)
var mift []*net.Interface
ift, err := net.Interfaces()
if err != nil {
t.Fatalf("net.Interfaces failed: %v", err)
}
for i, ifi := range ift {
if _, ok := nettest.IsMulticastCapable("ip4", &ifi); !ok {
continue
}
for _, p := range ps {
if err := p.JoinGroup(&ifi, gaddr); err != nil {
t.Fatalf("ipv4.PacketConn.JoinGroup %v on %v failed: %v", gaddr, ifi, err)
}
}
mift = append(mift, &ift[i])
}
for _, ifi := range mift {
for _, p := range ps {
if err := p.LeaveGroup(ifi, gaddr); err != nil {
t.Fatalf("ipv4.PacketConn.LeaveGroup %v on %v failed: %v", gaddr, ifi, err)
}
}
}
}
}
示例2: TestUDPMultipleConnWithMultipleGroupListeners
func TestUDPMultipleConnWithMultipleGroupListeners(t *testing.T) {
if testing.Short() || !*testExternal {
t.Logf("skipping test to avoid external network")
return
}
for _, tt := range udpMultipleGroupListenerTests {
// listen to a group address, actually a wildcard address
// with reusable port
c1, err := net.ListenPacket("udp4", "224.0.0.0:1024") // see RFC 4727
if err != nil {
t.Fatalf("net.ListenPacket failed: %v", err)
}
defer c1.Close()
c2, err := net.ListenPacket("udp4", "224.0.0.0:1024") // see RFC 4727
if err != nil {
t.Fatalf("net.ListenPacket failed: %v", err)
}
defer c2.Close()
var ps [2]*ipv4.PacketConn
ps[0] = ipv4.NewPacketConn(c1)
ps[1] = ipv4.NewPacketConn(c2)
var mift []*net.Interface
ift, err := net.Interfaces()
if err != nil {
t.Fatalf("net.Interfaces failed: %v", err)
}
for i, ifi := range ift {
if _, ok := isGoodForMulticast(&ifi); !ok {
continue
}
for _, p := range ps {
if err := p.JoinGroup(&ifi, tt.gaddr); err != nil {
t.Fatalf("ipv4.PacketConn.JoinGroup %v on %v failed: %v", tt.gaddr, ifi, err)
}
}
mift = append(mift, &ift[i])
}
for _, ifi := range mift {
for _, p := range ps {
if err := p.LeaveGroup(ifi, tt.gaddr); err != nil {
t.Fatalf("ipv4.PacketConn.LeaveGroup %v on %v failed: %v", tt.gaddr, ifi, err)
}
}
}
}
}
示例3: TestReadWriteMulticastIPPayloadUDP
func TestReadWriteMulticastIPPayloadUDP(t *testing.T) {
if testing.Short() || !*testExternal {
t.Logf("skipping test to avoid external network")
return
}
c, err := net.ListenPacket("udp4", "224.0.0.0:1024") // see RFC 4727
if err != nil {
t.Fatalf("net.ListenPacket failed: %v", err)
}
defer c.Close()
ifi := loopbackInterface()
if ifi == nil {
t.Logf("skipping test; an appropriate interface not found")
return
}
dst, err := net.ResolveUDPAddr("udp4", "224.0.0.254:1024") // see RFC 4727
if err != nil {
t.Fatalf("net.ResolveUDPAddr failed: %v", err)
}
p := ipv4.NewPacketConn(c)
if err := p.JoinGroup(ifi, dst); err != nil {
t.Fatalf("ipv4.PacketConn.JoinGroup on %v failed: %v", ifi, err)
}
if err := p.SetMulticastInterface(ifi); err != nil {
t.Fatalf("ipv4.PacketConn.SetMulticastInterface failed: %v", err)
}
if err := p.SetMulticastLoopback(true); err != nil {
t.Fatalf("ipv4.PacketConn.SetMulticastLoopback failed: %v", err)
}
runPayloadTransponder(t, p, []byte("HELLO-R-U-THERE"), dst)
}
示例4: ServeIf
// ServeIf does the same job as Serve(), but listens and responds on the
// specified network interface (by index). It also doubles as an example of
// how to leverage the dhcp4.ServeConn interface.
//
// If your target only has one interface, use Serve(). ServeIf() requires an
// import outside the std library. Serving DHCP over multiple interfaces will
// require your own dhcp4.ServeConn, as listening to broadcasts utilises all
// interfaces (so you cannot have more than on listener).
func ServeIf(ifIndex int, conn net.PacketConn, handler Handler) error {
p := ipv4.NewPacketConn(conn)
if err := p.SetControlMessage(ipv4.FlagInterface, true); err != nil {
return err
}
return Serve(&serveIfConn{ifIndex: ifIndex, conn: p}, handler)
}
示例5: TestReadWriteMulticastIPPayloadICMP
func TestReadWriteMulticastIPPayloadICMP(t *testing.T) {
if testing.Short() || !*testExternal {
t.Logf("skipping test to avoid external network")
return
}
if os.Getuid() != 0 {
t.Logf("skipping test; must be root")
return
}
c, err := net.ListenPacket("ip4:icmp", "0.0.0.0")
if err != nil {
t.Fatalf("net.ListenPacket failed: %v", err)
}
defer c.Close()
ifi := loopbackInterface()
if ifi == nil {
t.Logf("skipping test; an appropriate interface not found")
return
}
dst, err := net.ResolveIPAddr("ip4", "224.0.0.254") // see RFC 4727
if err != nil {
t.Fatalf("net.ResolveIPAddr failed: %v", err)
}
p := ipv4.NewPacketConn(c)
if err := p.JoinGroup(ifi, dst); err != nil {
t.Fatalf("ipv4.PacketConn.JoinGroup on %v failed: %v", ifi, err)
}
if err := p.SetMulticastInterface(ifi); err != nil {
t.Fatalf("ipv4.PacketConn.SetMulticastInterface failed: %v", err)
}
cf := ipv4.FlagTTL | ipv4.FlagDst | ipv4.FlagInterface
for i, toggle := range []bool{true, false, true} {
wb, err := (&icmpMessage{
Type: icmpv4EchoRequest, Code: 0,
Body: &icmpEcho{
ID: os.Getpid() & 0xffff, Seq: i + 1,
Data: []byte("HELLO-R-U-THERE"),
},
}).Marshal()
if err != nil {
t.Fatalf("icmpMessage.Marshal failed: %v", err)
}
if err := p.SetControlMessage(cf, toggle); err != nil {
t.Fatalf("ipv4.PacketConn.SetControlMessage failed: %v", err)
}
rb := writeThenReadPayload(t, i, p, wb, dst)
m, err := parseICMPMessage(rb)
if err != nil {
t.Fatalf("parseICMPMessage failed: %v", err)
}
if m.Type != icmpv4EchoReply || m.Code != 0 {
t.Fatalf("got type=%v, code=%v; expected type=%v, code=%v", m.Type, m.Code, icmpv4EchoReply, 0)
}
}
}
示例6: setInterface
// setInterface is used to set the query interface, uses sytem
// default if not provided
func (c *client) setInterface(iface *net.Interface) error {
p := ipv4.NewPacketConn(c.ipv4UnicastConn)
if err := p.SetMulticastInterface(iface); err != nil {
return err
}
p2 := ipv6.NewPacketConn(c.ipv6UnicastConn)
if err := p2.SetMulticastInterface(iface); err != nil {
return err
}
p = ipv4.NewPacketConn(c.ipv4MulticastConn)
if err := p.SetMulticastInterface(iface); err != nil {
return err
}
p2 = ipv6.NewPacketConn(c.ipv6MulticastConn)
if err := p2.SetMulticastInterface(iface); err != nil {
return err
}
return nil
}
示例7: ListenUDPMcast
// args:
// - addr: multicast group/address to listen to
// - port: port number; addr:port builds the mcast socket
// - iface: name of network interface to listen to
// - d_dl_sock:
// - stopch:
func ListenUDPMcast(addr, port, iface string, d_dl_sock *zmq.Socket,
stopch chan bool) {
eth, err := net.InterfaceByName(iface)
if err != nil {
fmt.Println("Error interface:", err.Error())
os.Exit(1)
}
group := net.ParseIP(addr)
if group == nil {
fmt.Println("Error: invalid group address:", addr)
os.Exit(1)
}
// listen to all udp packets on mcast port
c, err := net.ListenPacket("udp4", "0.0.0.0:"+port)
if err != nil {
fmt.Println("Error listening for mcast:", err.Error())
os.Exit(1)
}
// close the listener when the application closes
defer c.Close()
// join mcast group
p := ipv4.NewPacketConn(c)
if err := p.JoinGroup(eth, &net.UDPAddr{IP: group}); err != nil {
fmt.Println("Error joining:", err.Error())
os.Exit(1)
}
fmt.Println("Listening on " + addr + ":" + port)
// enable transmissons of control message
if err := p.SetControlMessage(ipv4.FlagDst, true); err != nil {
fmt.Println("Error control message", err.Error())
}
c1 := devreader.MakeChannel(UMSocket{p, group})
LOOP:
for {
select {
case v1 := <-c1:
fmt.Println("received UDP multicast")
// forward to coord node
d_dl_sock.Send(string(v1), 0)
case <-stopch:
break LOOP
}
}
}
示例8: TestPacketConnReadWriteUnicastUDP
func TestPacketConnReadWriteUnicastUDP(t *testing.T) {
switch runtime.GOOS {
case "nacl", "plan9", "solaris", "windows":
t.Skipf("not supported on %q", runtime.GOOS)
}
ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
if ifi == nil {
t.Skipf("not available on %q", runtime.GOOS)
}
c, err := net.ListenPacket("udp4", "127.0.0.1:0")
if err != nil {
t.Fatalf("net.ListenPacket failed: %v", err)
}
defer c.Close()
dst, err := net.ResolveUDPAddr("udp4", c.LocalAddr().String())
if err != nil {
t.Fatalf("net.ResolveUDPAddr failed: %v", err)
}
p := ipv4.NewPacketConn(c)
defer p.Close()
cf := ipv4.FlagTTL | ipv4.FlagDst | ipv4.FlagInterface
for i, toggle := range []bool{true, false, true} {
if err := p.SetControlMessage(cf, toggle); err != nil {
if protocolNotSupported(err) {
t.Skipf("not supported on %q", runtime.GOOS)
}
t.Fatalf("ipv4.PacketConn.SetControlMessage failed: %v", err)
}
p.SetTTL(i + 1)
if err := p.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
t.Fatalf("ipv4.PacketConn.SetWriteDeadline failed: %v", err)
}
if _, err := p.WriteTo([]byte("HELLO-R-U-THERE"), nil, dst); err != nil {
t.Fatalf("ipv4.PacketConn.WriteTo failed: %v", err)
}
rb := make([]byte, 128)
if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil {
t.Fatalf("ipv4.PacketConn.SetReadDeadline failed: %v", err)
}
if _, cm, _, err := p.ReadFrom(rb); err != nil {
t.Fatalf("ipv4.PacketConn.ReadFrom failed: %v", err)
} else {
t.Logf("rcvd cmsg: %v", cm)
}
}
}
示例9: TestUDPUnicastSockopt
func TestUDPUnicastSockopt(t *testing.T) {
for _, tt := range unicastSockoptTests {
c, err := net.ListenPacket("udp4", "127.0.0.1:0")
if err != nil {
t.Errorf("net.ListenPacket failed: %v", err)
return
}
defer c.Close()
p := ipv4.NewPacketConn(c)
if err := testUnicastSockopt(t, tt, p); err != nil {
break
}
}
}
示例10: TestReadWriteUnicastIPPayloadUDP
func TestReadWriteUnicastIPPayloadUDP(t *testing.T) {
c, err := net.ListenPacket("udp4", "127.0.0.1:0")
if err != nil {
t.Fatalf("net.ListenPacket failed: %v", err)
}
defer c.Close()
dst, err := net.ResolveUDPAddr("udp4", c.LocalAddr().String())
if err != nil {
t.Fatalf("net.ResolveUDPAddr failed: %v", err)
}
p := ipv4.NewPacketConn(c)
runPayloadTransponder(t, p, []byte("HELLO-R-U-THERE"), dst)
}
示例11: TestUDPMulticastSockopt
func TestUDPMulticastSockopt(t *testing.T) {
if testing.Short() || !*testExternal {
t.Skip("to avoid external network")
}
for _, tt := range multicastSockoptTests {
c, err := net.ListenPacket("udp4", "0.0.0.0:0")
if err != nil {
t.Fatalf("net.ListenPacket failed: %v", err)
}
defer c.Close()
p := ipv4.NewPacketConn(c)
testMulticastSockopt(t, tt, p, &net.UDPAddr{IP: tt.gaddr})
}
}
示例12: createSocket
func (s *Ssdp) createSocket() error {
group := net.IPv4(239, 255, 255, 250)
interfaces, err := net.Interfaces()
if err != nil {
s.logger.Errorf("net.Interfaces error", err)
return err
}
con, err := net.ListenPacket("udp4", "0.0.0.0:1900")
if err != nil {
s.logger.Errorf("net.ListenPacket error: %v", err)
return err
}
p := ipv4.NewPacketConn(con)
p.SetMulticastLoopback(true)
didFindInterface := false
for i, v := range interfaces {
ef, err := v.Addrs()
if err != nil {
continue
}
hasRealAddress := false
for k := range ef {
asIp := net.ParseIP(ef[k].String())
if asIp.IsUnspecified() {
continue
}
hasRealAddress = true
break
}
if !hasRealAddress {
continue
}
err = p.JoinGroup(&v, &net.UDPAddr{IP: group})
if err != nil {
s.logger.Warnf("join group %d %v", i, err)
continue
}
didFindInterface = true
}
if !didFindInterface {
return errors.New("Unable to find a compatible network interface!")
}
s.socket.socket = p
s.socket.rawSocket = con
s.socket.readBytes = make([]byte, 2048)
return nil
}
示例13: TestUDPPerInterfaceSinglePacketConnWithSingleGroupListener
func TestUDPPerInterfaceSinglePacketConnWithSingleGroupListener(t *testing.T) {
switch runtime.GOOS {
case "plan9", "windows":
t.Skipf("not supported on %q", runtime.GOOS)
}
if testing.Short() {
t.Skip("to avoid external network")
}
gaddr := net.IPAddr{IP: net.IPv4(224, 0, 0, 254)} // see RFC 4727
type ml struct {
c *ipv4.PacketConn
ifi *net.Interface
}
var mlt []*ml
ift, err := net.Interfaces()
if err != nil {
t.Fatalf("net.Interfaces failed: %v", err)
}
for i, ifi := range ift {
ip, ok := nettest.IsMulticastCapable("ip4", &ifi)
if !ok {
continue
}
c, err := net.ListenPacket("udp4", ip.String()+":"+"1024") // unicast address with non-reusable port
if err != nil {
t.Fatalf("net.ListenPacket with %v failed: %v", ip, err)
}
defer c.Close()
p := ipv4.NewPacketConn(c)
if err := p.JoinGroup(&ifi, &gaddr); err != nil {
t.Fatalf("ipv4.PacketConn.JoinGroup on %v failed: %v", ifi, err)
}
mlt = append(mlt, &ml{p, &ift[i]})
}
for _, m := range mlt {
if err := m.c.LeaveGroup(m.ifi, &gaddr); err != nil {
t.Fatalf("ipv4.PacketConn.LeaveGroup on %v failed: %v", m.ifi, err)
}
}
}
示例14: TestReadWriteUnicastIPPayloadICMP
func TestReadWriteUnicastIPPayloadICMP(t *testing.T) {
if os.Getuid() != 0 {
t.Logf("skipping test; must be root")
return
}
c, err := net.ListenPacket("ip4:icmp", "0.0.0.0")
if err != nil {
t.Fatalf("net.ListenPacket failed: %v", err)
}
defer c.Close()
dst, err := net.ResolveIPAddr("ip4", "127.0.0.1")
if err != nil {
t.Fatalf("ResolveIPAddr failed: %v", err)
}
p := ipv4.NewPacketConn(c)
cf := ipv4.FlagTTL | ipv4.FlagDst | ipv4.FlagInterface
for i, toggle := range []bool{true, false, true} {
wb, err := (&icmpMessage{
Type: icmpv4EchoRequest, Code: 0,
Body: &icmpEcho{
ID: os.Getpid() & 0xffff, Seq: i + 1,
Data: []byte("HELLO-R-U-THERE"),
},
}).Marshal()
if err != nil {
t.Fatalf("icmpMessage.Marshal failed: %v", err)
}
if err := p.SetControlMessage(cf, toggle); err != nil {
t.Fatalf("ipv4.PacketConn.SetControlMessage failed: %v", err)
}
rb := writeThenReadPayload(t, i, p, wb, dst)
m, err := parseICMPMessage(rb)
if err != nil {
t.Fatalf("parseICMPMessage failed: %v", err)
}
if m.Type != icmpv4EchoReply || m.Code != 0 {
t.Fatalf("got type=%v, code=%v; expected type=%v, code=%v", m.Type, m.Code, icmpv4EchoReply, 0)
}
}
}
示例15: BenchmarkReadWriteIPv4UDP
func BenchmarkReadWriteIPv4UDP(b *testing.B) {
c, dst, err := benchmarkUDPListener()
if err != nil {
b.Fatalf("benchmarkUDPListener failed: %v", err)
}
defer c.Close()
p := ipv4.NewPacketConn(c)
cf := ipv4.FlagTTL | ipv4.FlagInterface
if err := p.SetControlMessage(cf, true); err != nil {
b.Fatalf("ipv4.PacketConn.SetControlMessage failed: %v", err)
}
ifi := loopbackInterface()
wb, rb := []byte("HELLO-R-U-THERE"), make([]byte, 128)
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchmarkReadWriteIPv4UDP(b, p, wb, rb, dst, ifi)
}
}