You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.1 KiB

  1. //+build linux
  2. package nfqueue_test
  3. import (
  4. "context"
  5. "fmt"
  6. "time"
  7. nfqueue "github.com/florianl/go-nfqueue"
  8. )
  9. func ExampleNfqueue_Register() {
  10. // Send outgoing pings to nfqueue queue 100
  11. // # sudo iptables -I OUTPUT -p icmp -j NFQUEUE --queue-num 100
  12. // Set configuration options for nfqueue
  13. config := nfqueue.Config{
  14. NfQueue: 100,
  15. MaxPacketLen: 0xFFFF,
  16. MaxQueueLen: 0xFF,
  17. Copymode: nfqueue.NfQnlCopyPacket,
  18. ReadTimeout: 10 * time.Millisecond,
  19. WriteTimeout: 15 * time.Millisecond,
  20. }
  21. nf, err := nfqueue.Open(&config)
  22. if err != nil {
  23. fmt.Println("could not open nfqueue socket:", err)
  24. return
  25. }
  26. defer nf.Close()
  27. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  28. defer cancel()
  29. fn := func(a nfqueue.Attribute) int {
  30. id := *a.PacketID
  31. // Just print out the id and payload of the nfqueue packet
  32. fmt.Printf("[%d]\t%v\n", id, *a.Payload)
  33. nf.SetVerdict(id, nfqueue.NfAccept)
  34. return 0
  35. }
  36. // Register your function to listen on nflqueue queue 100
  37. err = nf.Register(ctx, fn)
  38. if err != nil {
  39. fmt.Println(err)
  40. return
  41. }
  42. // Block till the context expires
  43. <-ctx.Done()
  44. }