分享
  1. 首页
  2. 文章

[go]ipv6 ping测试

laputa73 · · 9543 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

其实很简单.就是对照go源码的ipraw_test.go和之前的ping.go

改造一个ping6.go.变化很小

// 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
//20131204,尝试改造支持ipv6
package ping6
import (
	"bytes"
	"errors"
	"net"
	"os"
	"time"
	"fmt"
)
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 Ping(address string, timeout int) (alive bool,err error,timedelay int64) {
 t1:=time.Now().UnixNano()
	err = Pinger(address, timeout)
	t2:=time.Now().UnixNano()
	alive = err == nil
	return alive,err,t2-t1
}
func Pinger(address string, timeout int) (err error) {
	//c, err := net.Dial("ip4:icmp", address)
	c, err := net.Dial("ip6:ipv6-icmp", address)
	if err != nil {
		fmt.Println("error ",err)
		return
	}
	
		
 
	c.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Millisecond)) //时延ms单位
	defer c.Close()
	//typ := icmpv4EchoRequest
	typ := icmpv6EchoRequest
	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
		}
		
			//if net == "ip4" { //only for ipv4
			//	rb = ipv4Payload(rb)
			//}
	
		
		if m, err = parseICMPMessage(rb); err != nil {
			return
		}
		switch m.Type {
		case icmpv4EchoRequest, icmpv6EchoRequest:
		 //fmt.Println("type ",m.Type)
			continue
		}
		break
	}
	return
}
func ipv4Payload(b []byte) []byte {
	if len(b) < 20 {
		return b
	}
	hdrlen := int(b[0]&0x0f) << 2
	fmt.Println("hdrlen ",hdrlen) //ipv4的时候为20
	return b[hdrlen:]
}


测试程序

/*
测试一下icmpv6
*/
package main
import (
"ping6"
"fmt"
"time"
"os"
)
func main() {
	//1.输入参数处理.这里使用os而非flag
	var host string
	if len(os.Args) != 2 {
		//fmt.Println("Usage: ", os.Args[0], "host")
		host="::1"
		//os.Exit(1)
	}else{
	 host = os.Args[1] //目标域名
 }
 
 t1:=time.Now().UnixNano()
 
 alive,err,timedelay:=ping6.Ping(host,1000)
 fmt.Println("result ",alive,err,timedelay)
 t2:=time.Now().UnixNano()
 fmt.Println(t2-t1);
 
}



有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:laputa73

查看原文:[go]ipv6 ping测试

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
9543 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏