ping img.onload 原理 ping相关协议

 

 ping(网络诊断工具)_百度百科 https://baike.baidu.com/item/ping/6235?fr=aladdin

在物理链路连通和路由设置正确的情况下,使用Ping命令仍然屏不通,可能有以下几个问题:
(1)网线刚插到交换机上就Ping通网关,忽略了生成树的收敛时间。当然,较新的交换机都支持快速生成树,或者有的管理员干脆把用户端口(accessport)的生成树协议关掉,问题就解决了 [5]  
(2)不管中间经过了多少个节点,只要有节点(包括端节点)对ICMP信息包进行了过滤,Ping不通是正常的。最常见的就是防火墙的行为 [5]  
(3)某些路由器端口是不允许用户Ping的 [5]  
(4)网络因设备间的超时,造成ICMP报文无法在缺省时间(2秒)内收到。超时的原因有:主机没有足够的时间和资源来响应;路径太长,没到达目的地时TTL的值为0,最后一个路由器将发回ICMP超时信息;使用扩展Ping,增加应答等待时间间隔等 [5]  
(5)引入NAT的场合会造成单向Ping通。NAT可以起到隐蔽内部地址的作用,当由内Ping外时,可以Ping通是因为NAT表的映射关系存在,当由外发起Ping内网主机时,就无从查找边界路由器的NAT访问列表了 [5]  

 

ping  img.onload  原理  JavaScript实现 ping相关协议

 

<div onclick="P('183.232.231.174')">
    P
</div>

<script>
    // 183.232.231.174  baidu
    function P(ip) {
        console.log(ip);
        var img = new Image();
        const start = new Date().getTime();
        img.src = "http://" + ip + "/" + start;
    }
</script>

  

 

 

 

 

 

ICMP

Internet Control Message Protocol

互发 

6162636465666768696a6b6c6d6e6f7071727374757677616263646566676869

abcdefghijklmnopqrstuvwabcdefghi

 

ICMP
Type 0 Echo ping request
Type 0 Echo ping reply

 

// Copyright 2009 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// taken from http://golang.org/src/pkg/net/ipraw_test.go

const (
    icmpv4EchoRequest = 8
    icmpv4EchoReply   = 0
    icmpv6EchoRequest = 128
    icmpv6EchoReply   = 129
)

type icmpMessage struct {
    Type     int             // type
    Code     int             // code
    Checksum int             // checksum
    Body     icmpMessageBody // body
}

type icmpMessageBody interface {
    Len() int
    Marshal() ([]byte, error)
}

// Marshal returns the binary enconding of the ICMP echo request or
// reply message m.
func (m *icmpMessage) Marshal() ([]byte, error) {
    b := []byte{byte(m.Type), byte(m.Code), 0, 0}
    if m.Body != nil && m.Body.Len() != 0 {
        mb, err := m.Body.Marshal()
        if err != nil {
            return nil, err
        }
        b = append(b, mb...)
    }
    switch m.Type {
    case icmpv6EchoRequest, icmpv6EchoReply:
        return b, nil
    }
    csumcv := len(b) - 1 // checksum coverage
    s := uint32(0)
    for i := 0; i < csumcv; i += 2 {
        s += uint32(b[i+1])<<8 | uint32(b[i])
    }
    if csumcv&1 == 0 {
        s += uint32(b[csumcv])
    }
    s = s>>16 + s&0xffff
    s = s + s>>16
    // Place checksum back in header; using ^= avoids the
    // assumption the checksum bytes are zero.
    b[2] ^= byte(^s & 0xff)
    b[3] ^= byte(^s >> 8)
    return b, nil
}

// parseICMPMessage parses b as an ICMP message.
func parseICMPMessage(b []byte) (*icmpMessage, error) {
    msglen := len(b)
    if msglen < 4 {
        return nil, errors.New("message too short")
    }
    m := &icmpMessage{Type: int(b[0]), Code: int(b[1]), Checksum: int(b[2])<<8 | int(b[3])}
    if msglen > 4 {
        var err error
        switch m.Type {
        case icmpv4EchoRequest, icmpv4EchoReply, icmpv6EchoRequest, icmpv6EchoReply:
            m.Body, err = parseICMPEcho(b[4:])
            if err != nil {
                return nil, err
            }
        }
    }
    return m, nil
}

// imcpEcho represenets an ICMP echo request or reply message body.
type icmpEcho struct {
    ID   int    // identifier
    Seq  int    // sequence number
    Data []byte // data
}

func (p *icmpEcho) Len() int {
    if p == nil {
        return 0
    }
    return 4 + len(p.Data)
}

// Marshal returns the binary enconding of the ICMP echo request or
// reply message body p.
func (p *icmpEcho) Marshal() ([]byte, error) {
    b := make([]byte, 4+len(p.Data))
    b[0], b[1] = byte(p.ID>>8), byte(p.ID&0xff)
    b[2], b[3] = byte(p.Seq>>8), byte(p.Seq&0xff)
    copy(b[4:], p.Data)
    return b, nil
}

// parseICMPEcho parses b as an ICMP echo request or reply message body.
func parseICMPEcho(b []byte) (*icmpEcho, error) {
    bodylen := len(b)
    p := &icmpEcho{ID: int(b[0])<<8 | int(b[1]), Seq: int(b[2])<<8 | int(b[3])}
    if bodylen > 4 {
        p.Data = make([]byte, bodylen-4)
        copy(p.Data, b[4:])
    }
    return p, nil
}

func Pinger(address string, timeout int) (err error) {
    c, err := net.Dial("ip4:icmp", address)
    if err != nil {
        return
    }
    c.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
    defer c.Close()

    typ := icmpv4EchoRequest
    xid, xseq := os.Getpid()&0xffff, 1
    wb, err := (&icmpMessage{
        Type: typ, Code: 0,
        Body: &icmpEcho{
            ID: xid, Seq: xseq,
            Data: bytes.Repeat([]byte("Go Go Gadget Ping!!!"), 3),
        },
    }).Marshal()
    if err != nil {
        return
    }
    if _, err = c.Write(wb); err != nil {
        return
    }
    var m *icmpMessage
    rb := make([]byte, 20+len(wb))
    for {
        if _, err = c.Read(rb); err != nil {
            return
        }
        rb = ipv4Payload(rb)
        if m, err = parseICMPMessage(rb); err != nil {
            return
        }
        switch m.Type {
        case icmpv4EchoRequest, icmpv6EchoRequest:
            continue
        }
        break
    }
    return
}

func ipv4Payload(b []byte) []byte {
    if len(b) < 20 {
        return b
    }
    hdrlen := int(b[0]&0x0f) << 2
    return b[hdrlen:]
}

 

 

golang实现ping命令 - 云+社区 - 腾讯云 https://cloud.tencent.com/developer/article/1071047

 

 


C:\Users\sas>ping

用法: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]
[-r count] [-s count] [[-j host-list] | [-k host-list]]
[-w timeout] [-R] [-S srcaddr] [-c compartment] [-p]
[-4] [-6] target_name

选项:
-t Ping 指定的主机,直到停止。
若要查看统计信息并继续操作,请键入 Ctrl+Break;
若要停止,请键入 Ctrl+C。
-a 将地址解析为主机名。
-n count 要发送的回显请求数。
-l size 发送缓冲区大小。
-f 在数据包中设置“不分段”标记(仅适用于 IPv4)。
-i TTL 生存时间。
-v TOS 服务类型(仅适用于 IPv4。该设置已被弃用,
对 IP 标头中的服务类型字段没有任何
影响)。
-r count 记录计数跃点的路由(仅适用于 IPv4)。
-s count 计数跃点的时间戳(仅适用于 IPv4)。
-j host-list 与主机列表一起使用的松散源路由(仅适用于 IPv4)。
-k host-list 与主机列表一起使用的严格源路由(仅适用于 IPv4)。
-w timeout 等待每次回复的超时时间(毫秒)。
-R 同样使用路由标头测试反向路由(仅适用于 IPv6)。
根据 RFC 5095,已弃用此路由标头。
如果使用此标头,某些系统可能丢弃
回显请求。
-S srcaddr 要使用的源地址。
-c compartment 路由隔离舱标识符。
-p Ping Hyper-V 网络虚拟化提供程序地址。
-4 强制使用 IPv4。
-6 强制使用 IPv6。


 


C:\>ping baidu.com

正在 Ping baidu.com [39.156.66.10] 具有 32 字节的数据:
来自 39.156.66.10 的回复: 字节=32 时间=282ms TTL=50
来自 39.156.66.10 的回复: 字节=32 时间=260ms TTL=50
来自 39.156.66.10 的回复: 字节=32 时间=157ms TTL=50
来自 39.156.66.10 的回复: 字节=32 时间=45ms TTL=50

39.156.66.10 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 45ms,最长 = 282ms,平均 = 186ms

C:\>

 

 

 

 

 

 

 

 

https://www.lifewire.com/definition-of-ping-817864

 

Ping is the name of a standard software utility used to test network connections. It can be used to determine if a remote device—such as a website or game server—can be reached across the network and if so, the connection's latency.

Ping tools are part of Windows, macOS, Linux, and some routers and game consoles. You can download other ping tools from third-party developers and use the tools on phones and tablets.

Note: Computer enthusiasts also use the term "ping" colloquially when initiating contact with another person via email, instant message, or other online tools. In that context, though, the word "ping" just means to notify, usually briefly.

Ping Tools

Most ping utilities and tools use Internet Control Message Protocol (ICMP). They send request messages to a target network address at periodic intervals and measure the time it takes for a response message to arrive.

These tools typically support options such as:

  • How many times to send requests.
  • How large of a request message to send.
  • How long to wait for each reply.

The output of ping varies depending on the tool. Standard results include:

  • IP address of the responding computer.
  • Length of time in milliseconds between sending the request and receiving the response.
  • An indication of the number of network hops between the requesting and responding computers.
 
  • Error messages if the target computer did not respond.

Where to Find Ping Tools

When using ping on a computer, there are ping commands that work with Command Prompt in Windows.

One tool called Ping works on iOS to ping any URL or IP address. It gives the total packets sent, received, and lost, as well as the minimum, maximum, and average time that it took to receive a response.

A different app named Ping, but for Android, can perform similar functions.

What Is the Ping of Death?

In late 1996 and early 1997, a flaw in the implementation of networking in some operating systems became well-known and popularized by hackers as a way to remotely crash computers. The "Ping of Death" attack was relatively easy to carry out and dangerous due to its high probability of success.

Technically speaking, the Ping of Death attack involved sending IP packets of a size greater than 65,535 bytes to the target computer. IP packets of this size are illegal, but a programmer can build applications capable of creating them.

Carefully programmed operating systems could detect and safely handle illegal IP packets, but some failed to do so. ICMP ping utilities often included large-packet capability and became the namesake of the problem, although UDP and other IP-based protocols also could transport the Ping of Death.

Operating system vendors quickly devised patches to avoid the Ping of Death, which no longer poses a threat to today's computer networks. Still, many websites have kept the convention of blocking ICMP ping messages at their firewalls to avoid similar denial of service attacks.

 

posted @ 2016-12-28 15:42  papering  阅读(367)  评论(0编辑  收藏  举报