| 405 | } |
| 406 | |
| 407 | func (pc *PingConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { |
| 408 | var na netip.Addr |
| 409 | switch v := addr.(type) { |
| 410 | case *PingAddr: |
| 411 | na = v.addr |
| 412 | case *net.IPAddr: |
| 413 | na, _ = netip.AddrFromSlice(v.IP) |
| 414 | default: |
| 415 | return 0, fmt.Errorf("ping write: wrong net.Addr type") |
| 416 | } |
| 417 | if !((na.Is4() && pc.laddr.addr.Is4()) || (na.Is6() && pc.laddr.addr.Is6())) { |
| 418 | return 0, fmt.Errorf("ping write: mismatched protocols") |
| 419 | } |
| 420 | |
| 421 | buf := bytes.NewReader(p) |
| 422 | rfa, _ := convertToFullAddr(netip.AddrPortFrom(na, 0)) |
| 423 | // won't block, no deadlines |
| 424 | n64, tcpipErr := pc.ep.Write(buf, tcpip.WriteOptions{ |
| 425 | To: &rfa, |
| 426 | }) |
| 427 | if tcpipErr != nil { |
| 428 | return int(n64), fmt.Errorf("ping write: %s", tcpipErr) |
| 429 | } |
| 430 | |
| 431 | return int(n64), nil |
| 432 | } |
| 433 | |
| 434 | func (pc *PingConn) Write(p []byte) (n int, err error) { |
| 435 | return pc.WriteTo(p, &pc.raddr) |