// writeThenReadDatagram transmits ICMP for IPv4 datagrams to the // loopback address or interface and captures the response datagrams // from the protocol stack within the kernel. func writeThenReadDatagram(t *testing.T, i int, c *ipv4.RawConn, wb []byte, src, dst net.Addr) []byte { rb := make([]byte, ipv4.HeaderLen+len(wb)) wh := &ipv4.Header{ Version: ipv4.Version, Len: ipv4.HeaderLen, TOS: i + 1, TotalLen: ipv4.HeaderLen + len(wb), TTL: i + 1, Protocol: 1, } if src != nil { wh.Src = src.(*net.IPAddr).IP } if dst != nil { wh.Dst = dst.(*net.IPAddr).IP } c.SetDeadline(time.Now().Add(100 * time.Millisecond)) if err := c.WriteTo(wh, wb, nil); err != nil { t.Fatalf("ipv4.RawConn.WriteTo failed: %v", err) } rh, b, cm, err := c.ReadFrom(rb) if err != nil { t.Fatalf("ipv4.RawConn.ReadFrom failed: %v", err) } t.Logf("rcvd cmsg: %v", cm.String()) t.Logf("rcvd hdr: %v", rh.String()) return b }
// runDatagramTransponder transmits ICMP for IPv4 datagrams to the // loopback address or interface and captures the response datagrams // from the protocol stack within the kernel. func runDatagramTransponder(t *testing.T, c *ipv4.RawConn, wb []byte, src, dst net.Addr) { cf := ipv4.FlagTTL | ipv4.FlagDst | ipv4.FlagInterface rb := make([]byte, ipv4.HeaderLen+len(wb)) for i, toggle := range []bool{true, false, true} { if err := c.SetControlMessage(cf, toggle); err != nil { t.Fatalf("ipv4.RawConn.SetControlMessage failed: %v", err) } wh := &ipv4.Header{} wh.Version = ipv4.Version wh.Len = ipv4.HeaderLen wh.TOS = i + 1 wh.TotalLen = ipv4.HeaderLen + len(wb) wh.TTL = i + 1 wh.Protocol = 1 if src != nil { wh.Src = src.(*net.IPAddr).IP } if dst != nil { wh.Dst = dst.(*net.IPAddr).IP } c.SetDeadline(time.Now().Add(100 * time.Millisecond)) if err := c.WriteTo(wh, wb, nil); err != nil { t.Fatalf("ipv4.RawConn.WriteTo failed: %v", err) } rh, _, cm, err := c.ReadFrom(rb) if err != nil { t.Fatalf("ipv4.RawConn.ReadFrom failed: %v", err) } t.Logf("rcvd cmsg: %v", cm.String()) t.Logf("rcvd hdr: %v", rh.String()) } }