CFLAGS=-D_GNU_SOURCE -std=gnu17 -Wall -O0 -static -I./deps/include
LDFLAGS = -Wl,--allow-multiple-definition
LIBS=deps/lib/libnetfilter_queue.a deps/lib/libnfnetlink.a deps/lib/libnftnl.a deps/lib/libmnl.a

exploit: exploit.c helpers.o helpers_nfqueue.o
	$(CC) $(CFLAGS) $(LDFLAGS) exploit.c -o exploit helpers.o helpers_nfqueue.o $(LIBS)

helpers.o: helpers.c helpers.h
	$(CC) -c helpers.c -I$(PWD)/deps/include

helpers_nfqueue.o: helpers_nfqueue.c helpers_nfqueue.h
	$(CC) -c helpers_nfqueue.c -I$(PWD)/deps/include

prerequisites:
	mkdir -p deps
	
	wget -O libmnl-1.0.5.tar.bz2 https://www.netfilter.org/pub/libmnl/libmnl-1.0.5.tar.bz2
	tar -xf libmnl-1.0.5.tar.bz2
	cd libmnl-1.0.5 && ./configure --prefix=$(PWD)/deps --enable-static=yes --enable-shared=no && make install
	
	wget -O libnftnl-1.2.8.tar.xz https://www.netfilter.org/pub/libnftnl/libnftnl-1.2.8.tar.xz
	tar -xf libnftnl-1.2.8.tar.xz
	cd libnftnl-1.2.8 && LIBMNL_CFLAGS=-I$(PWD)/deps/include LIBMNL_LIBS=$(PWD)/deps/lib/libmnl.a ./configure --prefix=$(PWD)/deps --enable-static=yes --enable-shared=no && make install

	wget -O libnfnetlink-1.0.2.tar.bz2 https://www.netfilter.org/pub/libnfnetlink/libnfnetlink-1.0.2.tar.bz2
	tar -xf libnfnetlink-1.0.2.tar.bz2
	cd libnfnetlink-1.0.2 && ./configure --prefix=$(PWD)/deps --enable-static=yes --enable-shared=no && make install

	wget -O libnetfilter_queue-1.0.5.tar.bz2 https://www.netfilter.org/pub/libnetfilter_queue/libnetfilter_queue-1.0.5.tar.bz2
	tar -xf libnetfilter_queue-1.0.5.tar.bz2
	cd libnetfilter_queue-1.0.5 && LIBNFNETLINK_CFLAGS=-I$(PWD)/deps/include LIBNFNETLINK_LIBS=$(PWD)/deps/lib/libnfnetlink.a LIBMNL_CFLAGS=-I$(PWD)/deps/include LIBMNL_LIBS=$(PWD)/deps/lib/libmnl.a ./configure --prefix=$(PWD)/deps --enable-static=yes --enable-shared=no && make install

	rm -rf libmnl-1.0.5.tar.bz2 libmnl-1.0.5
	rm -rf libnftnl-1.2.8.tar.xz libnftnl-1.2.8
	rm -rf libnfnetlink-1.0.2.tar.bz2 libnfnetlink-1.0.2
	rm -rf libnetfilter_queue-1.0.5.tar.bz2 libnetfilter_queue-1.0.5

run:
	./exploit

clean:
	rm -rf exploit helpers.o helpers_nfqueue.o