Skip to content

Use jschwinger233/elibpcap to compile and inject libpcap #543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jschwinger233
Copy link
Member

@jschwinger233 jschwinger233 commented Apr 15, 2025

The https://github.com/jschwinger233/elibpcap has been adopted by several popular projects such as https://github.com/gojue/ecapture, let's use it to simplify the codebase.

Historically, elibpcap was derived from pwru/internal/libpcap/ following #198. The two share nearly identical code and logic, so there should be no compatibility issues in transitioning pwru to use elibpcap.

Signed-off-by: gray [email protected]

This is because we are going to import elibpcap to compile with cgo,
we can't count on the pseudo #cgo directives from elibpcap package.

Signed-off-by: gray <[email protected]>
Signed-off-by: gray <[email protected]>
@jschwinger233 jschwinger233 marked this pull request as ready for review April 15, 2025 12:35
@jschwinger233 jschwinger233 requested a review from a team as a code owner April 15, 2025 12:35
@jschwinger233 jschwinger233 requested review from rgo3 and brb and removed request for a team April 15, 2025 12:35
Comment on lines +15 to +16
CGO_CFLAGS="-I/pwru/libpcap"
CGO_LDFLAGS="-L/pwru/libpcap -lpcap -static"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to compile locally, the path should be ./libpcap.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What command did you run locally? make release and make are both okay on my local ubuntu.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why this is supposed to work locally. The CI build with docker which set workdir to /pwru so there is no problem.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this shouldn't be an absolute path, otherwise e.g. make pwru will fail on hosts where /pwru/libpcap/ does not exist. We could also consider using -L$(CURDIR)/libpcap.

@brb
Copy link
Member

brb commented May 5, 2025

If I run make, I get the following error:

CGO_CFLAGS="-I/pwru/libpcap" CGO_LDFLAGS="-L/pwru/libpcap -lpcap -static" CC=cc GOARCH=amd64 CGO_ENABLED=1 go build  \
        -ldflags "-w -s \
        -X 'github.com/cilium/pwru/internal/pwru.Version=v1.0.10-pre-69-g3c948a7'"
# github.com/jschwinger233/elibpcap
vendor/github.com/jschwinger233/elibpcap/compile.go:15:10: fatal error: pcap.h: No such file or directory
   15 | #include <pcap.h>
      |          ^~~~~~~~
compilation terminated.
make: *** [Makefile:24: pwru] Error 1

The issue goes away if I apply the following diff:

diff --git a/vendor/github.com/jschwinger233/elibpcap/compile_static.go b/vendor/github.com/jschwinger233/elibpcap/compile_static.go
index 928555f..366d101 100644
--- a/vendor/github.com/jschwinger233/elibpcap/compile_static.go
+++ b/vendor/github.com/jschwinger233/elibpcap/compile_static.go
@@ -1,12 +1,12 @@
+//go:build static || !dynamic
 // +build static !dynamic

 package elibpcap

-
 /*
-#cgo LDFLAGS: -L/usr/local/lib -lpcap -static
+#cgo CFLAGS: -I${SRCDIR}/../../../../libpcap
+#cgo LDFLAGS: -L${SRCDIR}/../../../../libpcap -lpcap -static
 #include <stdlib.h>
 #include <pcap.h>
 */
 import "C"

@jschwinger233 One solution would be to ship the libcap in the jschwinger233/elibpcap?

@brb
Copy link
Member

brb commented May 26, 2025

One solution would be to ship the libcap in the jschwinger233/elibpcap?

@jschwinger233 👋 Would you accept such a contribution?

@jschwinger233
Copy link
Member Author

Thank you for suggestion! I believe shipping libpcap in elibpcap is the correct direction. I am learning from https://github.com/mattn/go-sqlite3 to see how to do it properly, then I'll upgrade the pr. 🙏

@jschwinger233 jschwinger233 marked this pull request as draft May 26, 2025 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants