func (p *Ping) PacketSize(s int) {
	p.pSize = s
}
PacketSize set packet size
package icmp
import (
	"errors"
	"fmt"
	"golang.org/x/net/icmp"
	"golang.org/x/net/ipv4"
	"golang.org/x/net/ipv6"
	"math/rand"
	"net"
	"os"
	"os/signal"
	"regexp"
	"strings"
	"sync"
	"syscall"
	"time"
	"github.com/mehrdadrad/mylg/cli"
)
type packet struct {
	bytes []byte
	addr  net.Addr
	err   error
}
type Response struct {
	RTT      float64
	Size     int
	Sequence int
	Addr     string
	Timeout  bool
	Error    error
}
func NewPing(args string, cfg cli.Config) (*Ping, error) {
	var (
		err error
	)
	target, flag := cli.Flag(args)
	if _, ok := flag["help"]; ok || len(target) < 3 {
		help(cfg)
		return nil, nil
	}
	p := Ping{
		id:        rand.Intn(0xffff),
		seq:       -1,
		pSize:     64,
		target:    target,
		isV4Avail: false,
		isV6Avail: false,
		isCIDR:    isCIDR(target),
		count:     cli.SetFlag(flag, "c", cfg.Ping.Count).(int),
		forceV4:   cli.SetFlag(flag, "4", false).(bool),
		forceV6:   cli.SetFlag(flag, "6", false).(bool),
		network:   "ip",
		source:    "",
		MaxRTT:    time.Second,
	}
	if !p.isCIDR {
		ips, err := net.LookupIP(target)
		if err != nil {
			return nil, err
		}
		p.addrs = ips
		if err := p.SetIP(ips); err != nil {
			return nil, err
		}
	}
	timeoutStr := cli.SetFlag(flag, "t", cfg.Ping.Timeout).(string)
	timeoutStr = NormalizeDuration(timeoutStr)
	if p.timeout, err = time.ParseDuration(timeoutStr); err != nil {
		return nil, fmt.Errorf("timeout options is not valid")
	}
	intervalStr := cli.SetFlag(flag, "i", cfg.Ping.Interval).(string)
	intervalStr = NormalizeDuration(intervalStr)
	if p.interval, err = time.ParseDuration(intervalStr); err != nil {
		return nil, fmt.Errorf("interval options is not valid")
	}
	return &p, nil
}
func (p *Ping) Run() chan Response {
	var r = make(chan Response, 1)
	go func() {
		for n := 0; n < p.count; n++ {
			p.Ping(r)
			if n != p.count-1 {
				time.Sleep(p.interval)
			}
		}
		close(r)
	}()
	return r
}
func (p *Ping) MRun() chan Response {
	var (
		r           = make(chan Response, 1000)
		sigCh       = make(chan os.Signal, 1)
		_, ipNet, _ = net.ParseCIDR(p.target)
		ifs, _      = net.Interfaces()
		skipIPs     = map[string]struct{}{}
	)
	for _, i := range ifs {
		addrs, _ := i.Addrs()
		for _, addr := range addrs {
			IP, net, _ := net.ParseCIDR(addr.String())
			if IsIPv4(IP) {
				skipIPs[broadcast(IP, net)] = struct{}{}
			}
			skipIPs[strings.Split(net.String(), "/")[0]] = struct{}{}
		}
	}
	if len(ipNet.IP) == 4 {
		go func() {
			signal.Notify(sigCh, os.Interrupt)
			defer signal.Stop(sigCh)
		LOOP:
			for ip := range walkIPv4(p.target) {
				if _, ok := skipIPs[ip]; ok {
					continue
				}
				pp := p
				pp.isV4Avail = true
				pp.addr = &net.IPAddr{IP: net.ParseIP(ip)}
				pp.Ping(r)
				select {
				case <-sigCh:
					break LOOP
				default:
					continue
				}
			}
			close(r)
		}()
	} else {
		println("IPv6 doesn't support")
		close(r)
	}
	return r
}
func broadcast(ipo net.IP, ipnet *net.IPNet) string {
	ip := ipo.To4()
	broadcast := make(net.IP, net.IPv4len)
	copy(broadcast, ip)
	for i, v := range ip {
		broadcast[i] = v | ^ipnet.Mask[i]
	}
	return broadcast.String()
}
func IsIPv4(ip net.IP) bool {
	return len(ip.To4()) == net.IPv4len
}
func IsIPv6(ip net.IP) bool {
	if r := strings.Index(ip.String(), ":"); r != -1 {
		return true
	}
	return false
}
func (p *Ping) parseMessage(m *packet) (*ipv4.Header, *icmp.Message, error) {
	var proto = ProtocolIPv4ICMP
	if p.isV6Avail {
		proto = ProtocolIPv6ICMP
	}
	msg, err := icmp.ParseMessage(proto, m.bytes)
	if err != nil {
		return nil, nil, err
	}
	bytes, _ := msg.Body.Marshal(msg.Type.Protocol())
	h, err := icmp.ParseIPv4Header(bytes)
	return h, msg, err
}
func (p *Ping) SetIP(ips []net.IP) error {
	for _, ip := range ips {
		if IsIPv4(ip) && !p.forceV6 {
			p.addr = &net.IPAddr{IP: ip}
			p.isV4Avail = true
			return nil
		} else if IsIPv6(ip) && !p.forceV4 {
			p.addr = &net.IPAddr{IP: ip}
			p.isV6Avail = true
			return nil
		}
	}
	return fmt.Errorf("there is not  A or AAAA record")
}
func (p *Ping) CIDRHeader() {
	fmt.Printf("PING %s : %d data bytes\n", p.target, p.pSize-8)
}