示例#1
0
文件: afpacket.go 项目: songgao/ether
func (d *afpacket) Read(to *ethernet.Frame) (err error) {
	to.Resize(d.ifce.MTU)
	for {
		*to = (*to)[:cap(*to)]
		var n int
		n, _, err = unix.Recvfrom(d.fd, []byte(*to), 0)
		if err != nil {
			return
		}
		*to = (*to)[:n]
		if !equalMAC(to.Source(), d.ifce.HardwareAddr) && (d.filter == nil || d.filter(*to)) {
			return
		}
	}
}
示例#2
0
文件: bpf.go 项目: songgao/ether
func (d *bpfDev) Read(to *ethernet.Frame) (err error) {
	to.Resize(d.ifce.MTU)
	for {
		for d.p < d.n {
			hdr := (*bpfHdr)(unsafe.Pointer(&d.readBuf[d.p]))
			frameStart := d.p + int(hdr.hdrlen)
			n := int(hdr.caplen)
			*to = (*to)[:n]
			copy(*to, d.readBuf[frameStart:frameStart+n])
			d.p += bpfWordalign(int(hdr.hdrlen) + int(hdr.caplen))
			if !equalMAC(to.Source(), d.ifce.HardwareAddr) && (d.filter == nil || d.filter(*to)) {
				return
			}
		}

		d.n, err = d.fd.Read(d.readBuf)
		if err != nil {
			return
		}
		d.p = 0
	}
}