Exemplo n.º 1
0
func (s *AfpacketSniffer) Open(config *Config) error {
	// Capture settings
	const (
		// MMap buffer size
		buffer_mb int = 24
		// Max packet length
		snaplen int = 65536
		// Set the interface in promiscuous mode
		promisc bool = true
	)

	frame_size, block_size, num_blocks, err := afpacketComputeSize(
		buffer_mb,
		snaplen,
		os.Getpagesize())
	if err != nil {
		return fmt.Errorf("Error calculating afpacket size: %s", err)
	}

	// Configure the afpacket ring and bind it to the interface
	var tPacket *afpacket.TPacket
	tPacket, err = afpacket.NewTPacket(
		afpacket.OptInterface(*iface),
		afpacket.OptFrameSize(frame_size),
		afpacket.OptBlockSize(block_size),
		afpacket.OptNumBlocks(num_blocks))
	if err != nil {
		fmt.Errorf("Error opening afpacket interface: %s", err)
	}
	s.handle = tPacket

	return nil
}
Exemplo n.º 2
0
func NewAFPacketHandle(ifName string, snaplen int32) (*AFPacketHandle, error) {
	tpacket, err := afpacket.NewTPacket(
		afpacket.OptInterface(ifName),
		afpacket.OptFrameSize(snaplen),
	)

	if err != nil {
		return nil, err
	}

	return &AFPacketHandle{tpacket: tpacket}, err
}