예제 #1
0
파일: network.go 프로젝트: kelixin/go.pkt
// Pack packets into their binary form and inject them in the given capture
// handle.. This will stack the packets before encoding them and also calculate
// the checksums.
func Send(c capture.Handle, pkts ...packet.Packet) error {
	if pkts[0].GetType() != c.LinkType() {
		return fmt.Errorf("Expected packet type %s, got %s",
			pkts[0].GetType(), c.LinkType())
	}

	buf, err := layers.Pack(pkts...)
	if err != nil {
		return fmt.Errorf("Could not pack: %s", err)
	}

	err = c.Inject(buf)
	if err != nil {
		return fmt.Errorf("Could not inject: %s", err)
	}

	return nil
}
예제 #2
0
파일: main.go 프로젝트: kelixin/go.pkt
func main() {
	log.SetFlags(0)

	usage := `Usage: dump [options] [<expression>]

Dump the traffic on the network (like tcpdump).

Options:
  -c <count>  Exit after receiving count packets.
  -i <iface>  Listen on interface.
  -r <file>   Read packets from file.
  -w <file>   Write the raw packets to file.`

	args, err := docopt.Parse(usage, nil, true, "", false)
	if err != nil {
		log.Fatalf("Invalid arguments: %s", err)
	}

	var count uint64

	if args["-c"] != nil {
		count, err = strconv.ParseUint(args["-c"].(string), 10, 64)
		if err != nil {
			log.Fatalf("Error parsing count: %s", err)
		}
	}

	var src capture.Handle

	if args["-i"] != nil {
		src, err = pcap.Open(args["-i"].(string))
		if err != nil {
			log.Fatalf("Error opening iface: %s", err)
		}
	} else if args["-r"] != nil {
		src, err = file.Open(args["-r"].(string))
		if err != nil {
			log.Fatalf("Error opening file: %s", err)
		}
	} else {
		log.Fatalf("Must select a source (either -i or -r)")
	}
	defer src.Close()

	var dst capture.Handle

	if args["-w"] != nil {
		dst, err = file.Open(args["-w"].(string))
		if err != nil {
			log.Fatalf("Error opening file: %s", err)
		}
		defer dst.Close()
	}

	err = src.Activate()
	if err != nil {
		log.Fatalf("Error activating source: %s", err)
	}

	if args["<expression>"] != nil {
		expr := args["<expression>"].(string)

		flt, err := filter.Compile(expr, src.LinkType(), false)
		if err != nil {
			log.Fatalf("Error parsing filter: %s", err)
		}
		defer flt.Cleanup()

		err = src.ApplyFilter(flt)
		if err != nil {
			log.Fatalf("Error appying filter: %s", err)
		}
	}

	var i uint64

	for {
		buf, err := src.Capture()
		if err != nil {
			log.Fatalf("Error: %s", err)
			break
		}

		if buf == nil {
			break
		}

		i++

		if dst == nil {
			rcv_pkt, err := layers.UnpackAll(buf, src.LinkType())
			if err != nil {
				log.Printf("Error: %s\n", err)
			}

			log.Println(rcv_pkt)
		} else {
			dst.Inject(buf)
		}

		if count > 0 && i >= count {
			break
		}
	}
}