[Android] libpcap with ndk-build

โ˜๐Ÿป libpcap ๋นŒ๋“œ

cpp๋กœ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๋•Œ pcap library๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.
์ด๋ฒˆ post์—์„œ๋Š” Android ๋Œ€์ƒ pcap ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•๊นŒ์ง€ ๋‹ค๋ค„๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.
ํŠนํžˆ libpcap์„ shared_library๋กœ ๋นŒ๋“œํ•˜๋Š” ๊ณผ์ •์— ๋Œ€ํ•œ ์„ค๋ช…์€ ๋”ฐ๋กœ ์ฐพ์•„๋ด๋„ ์—†๊ธฐ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ๋‚จ๊ฒจ๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

โ˜๐Ÿป how to ?

libpcap์„ ๋นŒ๋“œํ•˜๊ธฐ ์œ„ํ•ด์„œ cmake, configure ์Šคํฌ๋ฆฝํŠธ, ๋“ฑ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ,
์ €๋ฒˆ Post์— ์ด์–ด์„œ ndk-build๋ฅผ ์ด์šฉํ•œ ๋นŒ๋“œ๋ฅผ ์ง„ํ–‰ํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.

๋นŒ๋“œ ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1) libpcap ์†Œ์Šค ๋ฐ›๊ธฐ
2) Android.mk, Application.mk ์ž‘์„ฑ
3) ndk-build ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰

libpcap ์†Œ์Šค ๋ฐ›๊ธฐ

git clone https://android.googlesource.com/platform/external/libpcap

๋งํฌ

Android.mk, Application.mk ์ž‘์„ฑ

Android.mk, Application.mk๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์ „์— ๋จผ์ € ndk-build๋ฅผ ์ด์šฉํ•ด๋ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์„ ํ•˜๊ฒŒ ๋œ ๊ณ„๊ธฐ๋ฅผ ์„ค๋ช…ํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.
libpcap์„ ๋นŒ๋“œํ•˜๊ธฐ ์œ„ํ•ด cmake, configure ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด๋ดค๋‹ค.
๊ฐ๊ฐ์ด ์žฅ๋‹จ์ ์ด ์žˆ์—ˆ๋‹ค.

cmake์˜ ๊ฒฝ์šฐ์—๋Š” ์‰ฝ๊ณ  ํŽธํ•˜์ง€๋งŒ ๋‚ด๊ฐ€ ์ž˜ ๋ชฐ๋ž๊ณ ,
configure์˜ ๊ฒฝ์šฐ์—๋Š” ๋งค๋ฒˆ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์„ ํƒํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ ๊ทธ ๊ณผ์ •์ด ๋„ˆ๋ฌด ๋ฒˆ๊ฑฐ๋กญ๋‹ค๊ณ  ๋Š๊ปด์กŒ๋‹ค.

ํŠนํžˆ ์•„ํ‚คํ…์ฒ˜๋ฅผ ๋‹ฌ๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ๋งค๋ฒˆ ๋‹ฌ๋ฆฌ ๋นŒ๋“œ๋ฅผ ์ง„ํ–‰ํ•ด์•ผํ•œ๋‹ค๋Š” ์ ์—์„œ,
ํ•œ๋ฒˆ์— ์ด ์ž‘์—…์„ ํ•  ์ˆ˜๋Š” ์—†์„๊นŒ ? ์— ๋Œ€ํ•œ ๊ณ ๋ฏผ์„ ํ–ˆ๋‹ค.

ํŠนํžˆ libpcap ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋‹ค๊ฐ€ Android.bp ํŒŒ์ผ์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์–ด ์ง„ํ–‰์„ ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.libpcap์„ ndk-build๋ฅผ ์ด์šฉํ•ด์„œ ๋นŒ๋“œํ•˜๊ธฐ ๋ณด๋‹ค๋Š” ์ตœ๊ทผ์—๋Š” configure๋‚˜ cmake๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด ndk-build๋ฅผ ์ด์šฉํ•  ํ•„์š”๋Š” ์—†์„ ๊ฒƒ ๊ฐ™๋‹ค.

๋ณธ๋ก ์œผ๋กœ ๋Œ์•„์™€์„œ Android.mk์™€ Application.mk์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ–ˆ๋‹ค.

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := libpcap
# LOCAL_CFLAGS := -D_BSD_SOURCE
LOCAL_CFLAGS := -DHAVE_CONFIG_H
LOCAL_CFLAGS += -Dlint
LOCAL_CFLAGS += -D_U_="__attribute__((__unused__))"
LOCAL_CFLAGS += -Wall
# LOCAL_CFLAGS += -Werror
LOCAL_CFLAGS += -Wno-macro-redefined
LOCAL_CFLAGS += -Wno-pointer-arith
LOCAL_CFLAGS += -Wno-sign-compare
LOCAL_CFLAGS += -Wno-unused-parameter
LOCAL_CFLAGS += -Wno-unused-result
LOCAL_CFLAGS += -Wno-tautological-compare


LOCAL_SRC_FILES := \
    libpcap/bpf_dump.c \
    libpcap/bpf_filter.c \
    libpcap/bpf_image.c \
    libpcap/etherent.c \
    libpcap/fad-gifc.c \
    libpcap/fmtutils.c \
    libpcap/gencode.c \
    libpcap/grammar.c \
    libpcap/nametoaddr.c \
    libpcap/optimize.c \
    libpcap/pcap-common.c \
    libpcap/pcap-linux.c \
    libpcap/pcap-netfilter-linux-android.c \
    libpcap/pcap-usb-linux-common.c \
    libpcap/pcap-usb-linux.c \
    libpcap/pcap-util.c \
    libpcap/pcap.c \
    libpcap/savefile.c \
    libpcap/scanner.c \
    libpcap/sf-pcap.c \
    libpcap/sf-pcapng.c


LOCAL_SRC_FILES_linux += libpcap/missing/strlcpy.c

LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)

include $(BUILD_SHARED_LIBRARY)

# build 2
include $(CLEAR_VARS)
LOCAL_MODULE    := interface-list
LOCAL_SRC_FILES := interface-list.c 
LOCAL_STATIC_LIBRARIES := libpcap
LOCAL_C_INCLUDES := $(LOCAL_PATH) $(LOCAL_PATH)/libpcap
include $(BUILD_EXECUTABLE)

Application.mk

APP_PLATFORM := android-23
# APP_ABI := armeabi-v7a arm64-v8a
APP_ALLOW_MISSING_DEPS := true
APP_STL := c++_shared

ndk-build ์‹คํ–‰

์ž‘์„ฑ์ด ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด ์ด์ œ ndk-build๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค. jni์˜ ์ƒ์œ„ํด๋”์—์„œ ndk-build ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰์‹œ์ผœ์ฃผ์ž.

ndk-build or /path/to/ndk-build

โ˜๐Ÿป ๊ฒฐ๋ก 

Android.mk์™€ Application.mk๊ฐ€ Make์˜ ๋Š๋ฆฐ ๋นŒ๋“œ์†๋„๋กœ Soong ํ”„๋กœ์ ํŠธ๋กœ ๋Œ€์ฒด๋˜๋ฉด์„œ, ๋งŽ์ด ์‚ฌ์šฉ๋˜์ง„ ์•Š๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค. ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, Cmake๋‚˜ configure์ฒ˜๋Ÿผ ๋Œ€์•ˆ๋„ ๋งŽ์€ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค.

ํ˜น์—ฌ๋‚˜ ํ•„์š”ํ•˜๋‹ค๋ฉด ์œ„๋ฅผ ์ฐธ๊ณ ํ•˜๊ธธ ๋ฐ”๋ž€๋‹ค.

Categories:

Updated:

Leave a comment