Compare commits

...

No commits in common. "c8" and "cf4314ba1f497a31ab9c872bba9b7531345acc8b" have entirely different histories.

13 changed files with 688 additions and 69 deletions

2
.gitignore vendored
View File

@ -1 +1 @@
SOURCES/libnet-1.1.6.tar.gz
/libnet-*.tar.gz

View File

@ -1 +0,0 @@
dffff71c325584fdcf99b80567b60f8ad985e34c SOURCES/libnet-1.1.6.tar.gz

205
120.patch Normal file
View File

@ -0,0 +1,205 @@
From fdf4c18b1f2c17eddc871d4a593240a59dd0682f Mon Sep 17 00:00:00 2001
From: Adrian Reber <areber@redhat.com>
Date: Wed, 14 Apr 2021 09:21:50 +0200
Subject: [PATCH 1/3] libnet_port_list.c: fix gcc -fanalyzer warning
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This fixes:
libnet_port_list.c:99:8: warning: leak of _8 [CWE-401] [-Wanalyzer-malloc-leak]
99 | if (!all_lists)
| ^
libnet_plist_chain_new: events 1-3
|
| 48 | if (l == NULL)
| | ^
| | |
| | (1) following false branch (when l_70(D) is non-NULL)...
|......
| 53 | if (token_list == NULL)
| | ~~ ~
| | | |
| | | (3) following false branch (when token_list_71(D) is non-NULL)...
| | (2) ...to here
|
libnet_plist_chain_new: event 4
|
|cc1:
| (4): ...to here
|
libnet_plist_chain_new: events 5-9
|
| 83 | *plist = malloc(sizeof (libnet_plist_t));
| | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| | (5) allocated here
| 84 |
| 85 | if (!(*plist))
| | ~
| | |
| | (6) assuming *plist_74(D) is non-NULL
| | (7) following false branch...
|......
| 93 | tmp = *plist;
| | ~~~
| | |
| | (8) ...to here
|......
| 99 | if (!all_lists)
| | ~
| | |
| | (9) _8 leaks here; was allocated at (5)
|
Signed-off-by: Adrian Reber <areber@redhat.com>
---
src/libnet_port_list.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/libnet_port_list.c b/src/libnet_port_list.c
index 942a856..a99867c 100644
--- a/src/libnet_port_list.c
+++ b/src/libnet_port_list.c
@@ -101,6 +101,7 @@ libnet_plist_chain_new(libnet_t *l, libnet_plist_t **plist, char *token_list)
all_lists = all_lists_tmp;
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"libnet_build_plist_chain: realloc %s", strerror(errno));
+ free(tmp);
*plist = NULL;
return(-1);
}
From 60d71f03f26d349b4bf069983f625128ddcfdba8 Mon Sep 17 00:00:00 2001
From: Adrian Reber <areber@redhat.com>
Date: Wed, 14 Apr 2021 09:41:27 +0200
Subject: [PATCH 2/3] =?UTF-8?q?libnet=5Fpblock.c:=20fix=20warning=20"unuse?=
=?UTF-8?q?d=20variable=20=E2=80=98c=E2=80=99"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Adrian Reber <areber@redhat.com>
---
src/libnet_pblock.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/src/libnet_pblock.c b/src/libnet_pblock.c
index fa46d35..e3eb184 100644
--- a/src/libnet_pblock.c
+++ b/src/libnet_pblock.c
@@ -489,7 +489,6 @@ libnet_pblock_coalesce(libnet_t *l, uint8_t **packet, uint32_t *size)
{
if (q->flags & LIBNET_PBLOCK_DO_CHECKSUM)
{
- uint32_t c;
uint8_t* end = *packet + l->aligner + l->total_size;
uint8_t* beg = *packet + n;
int ip_offset = calculate_ip_offset(l, q);
From 5085aebbe93b81eb93bfec5e33c04286f779ceda Mon Sep 17 00:00:00 2001
From: Adrian Reber <areber@redhat.com>
Date: Wed, 14 Apr 2021 10:13:23 +0200
Subject: [PATCH 3/3] libnet_cq.c: fix 'dereference of possibly-NULL'
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
libnet_cq.c:139:18: warning: dereference of possibly-NULL new_18 [CWE-690] [-Wanalyzer-possible-null-dereference]
139 | new->context = l;
| ~~~~~~~~~~~~~^~~
libnet_cq_add.part.0: events 1-6
|
| 71 | libnet_cq_add(libnet_t *l, char *label)
| | ^~~~~~~~~~~~~
| | |
| | (1) entry to libnet_cq_add.part.0
|......
| 89 | if (label == NULL)
| | ~
| | |
| | (2) following false branch (when label_1(D) is non-NULL)...
|......
| 97 | if (l_cq == NULL)
| | ~~ ~
| | | |
| | | (4) following false branch...
| | (3) ...to here
|......
| 124 | if (libnet_cq_dup_check(l, label))
| | ~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | | |
| | | (6) calling libnet_cq_dup_check from libnet_cq_add.part.0
| | (5) ...to here
|
+--> libnet_cq_dup_check: events 7-13
|
| 269 | libnet_cq_dup_check(libnet_t *l, char *label)
| | ^~~~~~~~~~~~~~~~~~~
| | |
| | (7) entry to libnet_cq_dup_check
|......
| 273 | for (p = l_cq; p; p = p->next)
| | ~ ~~~~~~~~~~~
| | | |
| | | (13) ...to here
| | (8) following true branch (when p_6 is non-NULL)...
| 274 | {
| 275 | if (p->context == l)
| | ~~ ~
| | | |
| | | (10) following false branch...
| | (9) ...to here
|......
| 281 | if (strncmp(p->context->label, label, LIBNET_LABEL_SIZE) == 0)
| | ~~ ~
| | | |
| | | (12) following false branch...
| | (11) ...to here
|
<------+
|
libnet_cq_add.part.0: events 14-20
|
| 124 | if (libnet_cq_dup_check(l, label))
| | ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | ||
| | |(14) returning to libnet_cq_add.part.0 from libnet_cq_dup_check
| | (15) following false branch...
|......
| 130 | new = (libnet_cq_t *)malloc(sizeof (libnet_cq_t));
| | ~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | | |
| | | (17) this call could return NULL
| | (16) ...to here
| 131 | if (l_cq == NULL)
| | ~
| | |
| | (18) following false branch...
|......
| 139 | new->context = l;
| | ~~~~~~~~~~~~~~~~
| | | |
| | | (20) new_18 could be NULL: unchecked value from (17)
| | (19) ...to here
|
Signed-off-by: Adrian Reber <areber@redhat.com>
---
src/libnet_cq.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/libnet_cq.c b/src/libnet_cq.c
index 2234cbc..b3c5677 100644
--- a/src/libnet_cq.c
+++ b/src/libnet_cq.c
@@ -128,7 +128,7 @@ libnet_cq_add(libnet_t *l, char *label)
}
new = (libnet_cq_t *)malloc(sizeof (libnet_cq_t));
- if (l_cq == NULL)
+ if (new == NULL)
{
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
"%s(): can't malloc new context queue: %s",

6
gating.yaml Normal file
View File

@ -0,0 +1,6 @@
--- !Policy
product_versions:
- rhel-9
decision_context: osci_compose_gate
rules:
- !PassingTestCaseRule {test_case_name: osci.brew-build.tier0.functional}

16
libnet-config.patch Normal file
View File

@ -0,0 +1,16 @@
--- libnet-1.2/libnet-config.in.orig 2021-04-08 14:13:20.095564421 +0200
+++ libnet-1.2/libnet-config.in 2021-04-08 14:13:42.038730961 +0200
@@ -12,12 +12,11 @@
prefix=@prefix@
exec_prefix=@exec_prefix@
-libdir=@libdir@
includedir=@includedir@
libnet_defines="@PKG_CONFIG_DEFINES@"
libnet_cflags="-I${includedir} @PKG_CONFIG_CFLAGS@"
-libnet_libs="-L${libdir} @PKG_CONFIG_LIBS@ -lnet"
+libnet_libs="@PKG_CONFIG_LIBS@ -lnet"
usage()
{

View File

@ -1,15 +1,21 @@
Summary: C library for portable packet creation and injection
Name: libnet
Version: 1.1.6
Release: 15%{?dist}
License: BSD
Group: System Environment/Libraries
URL: http://www.sourceforge.net/projects/libnet-dev/
Source: http://downloads.sourceforge.net/libnet-dev/%{name}-%{version}.tar.gz
%if 0%{?fedora} >= 17 || 0%{?rhel} >= 7
BuildRequires: autoconf, automake, libtool
%endif
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
Summary: C library for portable packet creation and injection
Name: libnet
Version: 1.2
Release: 6%{?dist}
License: BSD
URL: https://github.com/libnet/libnet
# This used to be https://github.com/libnet/libnet/releases/download/v%%{version}/%%{name}-%%{version}.tar.gz
# But because of licensing a repacked tarball is used
# The repacked tarball has the directory win32/wpdpack removed
# which was never used. Upstream also removed that directory:
# https://github.com/libnet/libnet/commit/eba016f9506d9041e1bc8786c10ca94ebe626057
Source0: libnet-1.2-repack.tar.gz
Patch0: libnet-config.patch
Patch1: libnet_pblock.patch
Patch2: https://patch-diff.githubusercontent.com/raw/libnet/libnet/pull/120.patch
BuildRequires: gcc
BuildRequires: make
BuildRequires: %{_bindir}/pod2man
%description
Libnet is an API to help with the construction and handling of network
@ -20,88 +26,119 @@ layer and at the link layer as well as a host of supplementary and
complementary functionality.
%package devel
Summary: Development files for the libnet library
Group: Development/Libraries
Requires: %{name}%{?_isa} = %{version}-%{release}
Summary: Development files for the libnet library
Requires: %{name}%{?_isa} = %{version}-%{release}
Requires: pkgconfig
%description devel
The libnet-devel package includes header files and libraries necessary
for developing programs which use the libnet library. Libnet is very handy
with which to write network tools and network test code. See the manpage
and sample test code for more detailed information.
for developing programs which use the libnet library. Libnet is very
handy with which to write network tools and network test code. See the
man page and sample test code for more detailed information.
%if 0%{!?_without_doc:1}
%package doc
Summary: Documentation files for the libnet library
BuildArch: noarch
BuildRequires: doxygen
BuildRequires: graphviz
%description doc
Libnet is an API to help with the construction and handling of network
packets. It provides a portable framework for low-level network packet
writing and handling. This package contains the API documentation for
developing applications that use libnet.
%endif
%prep
%setup -q
%if 0%{?fedora} >= 17 || 0%{?rhel} >= 7
autoreconf --force --install
%endif
# Keep the sample directory untouched by make
rm -rf __dist_sample
mkdir __dist_sample
cp -a sample __dist_sample
%patch0 -p1
%patch1 -p1
%patch2 -p1
# Avoid library soname bump (https://github.com/libnet/libnet/issues/115)
sed -e 's/-version-info 9:0:0/-version-info 9:0:8/' -i src/Makefile.{am,in}
%build
%if 0%{?fedora} < 17 && 0%{?rhel} < 7
%configure --libdir=/%{_lib}
%else
%configure
%endif
make %{?_smp_mflags}
%make_build
%install
rm -rf $RPM_BUILD_ROOT
make DESTDIR=$RPM_BUILD_ROOT INSTALL='install -p' install
%make_install INSTALL='install -p'
%if 0%{?fedora} < 17 && 0%{?rhel} < 7
# Move %{name}.so to %{_libdir}, remove static .a and libtool .la files
rm -f $RPM_BUILD_ROOT/%{_lib}/%{name}.{a,la,so}
pushd $RPM_BUILD_ROOT/%{_lib}
mkdir -p $RPM_BUILD_ROOT%{_libdir}
ln -sf ../../%{_lib}/$(ls %{name}.so.?.?.?) $RPM_BUILD_ROOT%{_libdir}/%{name}.so
popd
%else
# Don't install any libtool .la files
rm -f $RPM_BUILD_ROOT%{_libdir}/%{name}.{a,la}
%endif
# Prepare samples directory and perform some fixes
rm -rf __dist_sample/sample/win32
rm -f __dist_sample/sample/Makefile.{am,in}
sed -e 's@#include "../include/libnet.h"@#include <libnet.h>@' \
__dist_sample/sample/libnet_test.h > __dist_sample/sample/libnet_test.h.new
touch -c -r __dist_sample/sample/libnet_test.h{,.new}
mv -f __dist_sample/sample/libnet_test.h{.new,}
# Clean up for later usage in documentation
rm -rf $RPM_BUILD_ROOT%{_defaultdocdir}
# Remove makefile relics from documentation
rm -f doc/html/Makefile*
# Prepare samples for usage in documentation
rm -rf sample/{Makefile*,win32}
for file in sample/*.[hc]; do
sed \
-e 's@#include "../include/libnet.h"@#include <libnet.h>@' \
-e 's@#include "../include/config.h"@#include <config.h>@' \
$file > $file.new
touch -c -r $file{,.new}
mv -f $file{.new,}
done
%clean
rm -rf $RPM_BUILD_ROOT
%post -p /sbin/ldconfig
%postun -p /sbin/ldconfig
%ldconfig_scriptlets
%files
%defattr(-,root,root,-)
%doc README doc/CHANGELOG doc/CONTRIB doc/COPYING
%if 0%{?fedora} < 17 && 0%{?rhel} < 7
/%{_lib}/%{name}.so.*
%else
%license LICENSE
%doc README.md ChangeLog.md
%{_libdir}/%{name}.so.*
%endif
%files devel
%defattr(-,root,root,-)
%doc doc/CHANGELOG doc/CONTRIB doc/COPYING doc/DESIGN_NOTES doc/MIGRATION doc/PACKET_BUILDING
%doc doc/RAWSOCKET_NON_SEQUITUR doc/TODO doc/html/ __dist_sample/sample/
%doc doc/MIGRATION.md doc/RAWSOCKET.md sample/
%{_bindir}/%{name}-config
%{_libdir}/%{name}.so
%{_includedir}/libnet.h
%{_libdir}/pkgconfig/%{name}.pc
%{_includedir}/%{name}.h
%{_includedir}/%{name}/
%{_mandir}/man3/%{name}*.3*
%if 0%{!?_without_doc:1}
%files doc
%doc doc/html/
%endif
%changelog
* Mon Aug 09 2021 Mohan Boddu <mboddu@redhat.com> - 1.2-6
- Rebuilt for IMA sigs, glibc 2.34, aarch64 flags
Related: rhbz#1991688
* Fri Apr 23 2021 Adrian Reber <areber@redhat.com> - 1.2-5
- Repack tarball to remove problematic license from unused code
- Apply fixes for warnings found by gcc -fanalyzer
* Fri Apr 16 2021 Mohan Boddu <mboddu@redhat.com> - 1.2-4
- Rebuilt for RHEL 9 BETA on Apr 15th 2021. Related: rhbz#1947937
* Thu Apr 08 2021 Adrian Reber <adrian@lisas.de> - 1.2-3
- Fix file conflicts with libnet-devel
* Tue Jan 26 2021 Fedora Release Engineering <releng@fedoraproject.org> - 1.2-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_34_Mass_Rebuild
* Sat Jan 02 2021 Robert Scheck <robert@fedoraproject.org> 1.2-1
- Upgrade to 1.2 (#1912031)
* Tue Jul 28 2020 Fedora Release Engineering <releng@fedoraproject.org> - 1.1.6-20
- Rebuilt for https://fedoraproject.org/wiki/Fedora_33_Mass_Rebuild
* Wed Jan 29 2020 Fedora Release Engineering <releng@fedoraproject.org> - 1.1.6-19
- Rebuilt for https://fedoraproject.org/wiki/Fedora_32_Mass_Rebuild
* Thu Jul 25 2019 Fedora Release Engineering <releng@fedoraproject.org> - 1.1.6-18
- Rebuilt for https://fedoraproject.org/wiki/Fedora_31_Mass_Rebuild
* Fri Feb 01 2019 Fedora Release Engineering <releng@fedoraproject.org> - 1.1.6-17
- Rebuilt for https://fedoraproject.org/wiki/Fedora_30_Mass_Rebuild
* Fri Jul 13 2018 Fedora Release Engineering <releng@fedoraproject.org> - 1.1.6-16
- Rebuilt for https://fedoraproject.org/wiki/Fedora_29_Mass_Rebuild
* Wed Feb 07 2018 Fedora Release Engineering <releng@fedoraproject.org> - 1.1.6-15
- Rebuilt for https://fedoraproject.org/wiki/Fedora_28_Mass_Rebuild

18
libnet_pblock.patch Normal file
View File

@ -0,0 +1,18 @@
diff --git a/src/libnet_pblock.c b/src/libnet_pblock.c
index 9fd015f..fa46d35 100644
--- a/src/libnet_pblock.c
+++ b/src/libnet_pblock.c
@@ -500,10 +500,9 @@ libnet_pblock_coalesce(libnet_t *l, uint8_t **packet, uint32_t *size)
q->ptag, libnet_diag_dump_pblock_type(q->type),
ip_offset);
#endif
- c = libnet_inet_checksum(l, iph,
- libnet_pblock_p2p(q->type), q->h_len,
- beg, end);
- if (c == -1)
+ if (libnet_inet_checksum(l, iph,
+ libnet_pblock_p2p(q->type), q->h_len,
+ beg, end) == -1)
{
/* err msg set in libnet_do_checksum() */
goto err;

1
sources Normal file
View File

@ -0,0 +1 @@
SHA512 (libnet-1.2-repack.tar.gz) = a65a19516bfc2088d21d54479c8f4a1ff7ef5f2e554cb071403f8d7e5283fdfe2572c6dcaf149764f668a11a479f70e4e5a0aef9cf33aadac2f7e6dd4f480d99

View File

@ -0,0 +1,47 @@
# SPDX-License-Identifier: LGPL-2.1+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Makefile of /CoreOS/libnet
# Description: Sanity
# Author: Susant Sahani<susant@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
export TEST=/CoreOS/libnet
export TESTVERSION=1.0
INCLUDES =
OBJS = test-libnet.c
CFLAG = -Wall -g3
CC = gcc
LIBS = -lnet -lcmocka -lpcap -lpthread
test-libnet:${OBJ}
${CC} ${CFLAGS} ${INCLUDES} -o $@ ${OBJS} ${LIBS}
run: test-libnet
./runtest.sh
clean:
-rm -f *~ test-libnet
.c.o:
${CC} ${CFLAGS} ${INCLUDES} -c $<
CC = gcc
include /usr/share/rhts/lib/rhts-make.include
$(METADATA): Makefile
@echo "Owner: Susant Sahani<susant@redhat.com>" > $(METADATA)
@echo "Name: $(TEST)" >> $(METADATA)
@echo "TestVersion: $(TESTVERSION)" >> $(METADATA)
@echo "Path: $(TEST_DIR)" >> $(METADATA)
@echo "Description: Test libnet sanity" >> $(METADATA)
@echo "Type: Sanity" >> $(METADATA)
@echo "TestTime: 5m" >> $(METADATA)
@echo "RunFor: libnet" >> $(METADATA)
@echo "Requires: libnet libnet-devel" >> $(METADATA)
@echo "Priority: Normal" >> $(METADATA)
@echo "License: GPLv2" >> $(METADATA)
@echo "Confidential: no" >> $(METADATA)
@echo "Destructive: no" >> $(METADATA)
@echo "Releases: -Fedora 29" >> $(METADATA)
rhts-lint $(METADATA)

44
tests/sanity-tests/runtest.sh Executable file
View File

@ -0,0 +1,44 @@
#!/bin/bash
# SPDX-License-Identifier: LGPL-2.1+
# ~~~
# runtest.sh of libnet
# Description: Tests for libnet
#
# Author: Susant Sahani <susant@redhat.com>
# Copyright (c) 2018 Red Hat, Inc.
# ~~~
# Include Beaker environment
. /usr/share/beakerlib/beakerlib.sh || exit 1
PACKAGE="libnet"
rlJournalStart
rlPhaseStartSetup
rlAssertRpm $PACKAGE
rlLog "Setting up veth Interface "
rlRun "ip link add veth-test type veth peer name veth-peer"
rlRun "ip addr add 192.168.50.5 dev veth-test"
rlRun "ip addr add 192.168.50.6 dev veth-peer"
rlRun "ip link set dev veth-test up"
rlRun "ip link set dev veth-peer up"
rlRun "cp test-libnet /usr/bin/"
rlRun "systemctl daemon-reload"
rlPhaseEnd
rlPhaseStartTest
rlLog "Starting libnet tests ..."
rlRun "/usr/bin/test-libnet"
rlPhaseEnd
rlPhaseStartCleanup
rlRun "rm /usr/bin/test-libnet"
rlRun "ip link del veth-test"
rlLog "libnet tests done"
rlPhaseEnd
rlJournalPrintText
rlJournalEnd
rlGetTestState

BIN
tests/sanity-tests/test-libnet Executable file

Binary file not shown.

View File

@ -0,0 +1,229 @@
/*
# SPDX-License-Identifier: LGPL-2.1+
# ~~~
# Description: libnet tests
#
# Author: Susant Sahani <susant@redhat.com>
# Copyright (c) 2018 Red Hat, Inc.
# ~~~
*/
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <netinet/in.h>
#include <setjmp.h>
#include <inttypes.h>
#include <cmocka.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include <libnet.h>
#include <netinet/if_ether.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include <pcap.h>
#include <pthread.h>
#include <sys/time.h>
#define PCAP_FILE "/var/run/libnet-test.pcap"
pcap_t *handle;
struct UDP_hdr {
u_short uh_sport; /* source port */
u_short uh_dport; /* destination port */
u_short uh_ulen; /* datagram length */
u_short uh_sum; /* datagram checksum */
};
static void assert_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) {
unsigned int ipl;
uint32_t src, dst;
struct ip *ip;
struct ether_header *eth_header;
eth_header = (struct ether_header *) packet;
if (ntohs(eth_header->ether_type) != ETHERTYPE_IP)
return;
packet += sizeof(struct ether_header);
ip = (struct ip *) packet;
ipl = ip->ip_hl * 4;
src = inet_addr("192.168.50.6");
dst = inet_addr("192.168.50.5");
assert_memory_equal(&dst, &ip->ip_dst, sizeof(uint32_t));
assert_memory_equal(&src, &ip->ip_src, sizeof(uint32_t));
if (ip->ip_p == IPPROTO_UDP) {
struct UDP_hdr *udp;
packet += ipl;
udp = (struct UDP_hdr *) packet;
printf("UDP src_port=%d dst_port=%d\n", ntohs(udp->uh_sport), ntohs(udp->uh_dport));
assert_int_equal(2425, ntohs(udp->uh_sport));
assert_int_equal(2426, ntohs(udp->uh_dport));
} else if (ip->ip_p == IPPROTO_TCP) {
struct tcphdr *tcp;
packet += ipl;
tcp = (struct tcphdr *) packet;
printf("TCP src_port=%d dst_port=%d\n", ntohs(tcp->th_sport), ntohs(tcp->th_dport));
assert_int_equal(2425, ntohs(tcp->th_sport));
assert_int_equal(2426, ntohs(tcp->th_dport));
}
}
void *capture_packet_live(void *ptr) {
char dev[] = "veth-test";
char error_buffer[PCAP_ERRBUF_SIZE];
struct bpf_program filter;
bpf_u_int32 subnet_mask, ip;
int total_packet_count;
int r;
r = pcap_lookupnet(dev, &ip, &subnet_mask, error_buffer);
assert_true(r >=0);
handle = pcap_open_live(dev, BUFSIZ, 1, 1000, error_buffer);
assert_non_null(handle);
r = pcap_compile(handle, &filter, ptr, 0, ip);
assert_true(r >= 0);
r = pcap_setfilter(handle, &filter);
assert_true(r >= 0);
pcap_loop(handle, total_packet_count, assert_packet, NULL);
return 0;
}
void terminate_thread(int signum) {
pcap_breakloop(handle);
pcap_close(handle);
handle = NULL;
pthread_exit(NULL);
}
static void test_udp_packet_ipv4(void **state) {
char dst[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
char src[6] = {0x12,0x34,0x56,0x78,0x9a,0xab};
char err_buf[LIBNET_ERRBUF_SIZE] = {};
char buf[1024] = {};
libnet_t* l;
int r, i;
uint32_t len;
pthread_t pcap_thread;
struct itimerval tval;
timerclear(& tval.it_interval); /* zero interval means no reset of timer */
timerclear(& tval.it_value);
tval.it_value.tv_sec = 10; /* 10 second timeout */
(void) signal(SIGALRM, terminate_thread);
(void) setitimer(ITIMER_REAL, & tval, NULL);
r = pthread_create(&pcap_thread, NULL, capture_packet_live, "udp port 2425");
assert_true(r >=0);
l = libnet_init(LIBNET_LINK_ADV, "veth-peer", err_buf);
assert_non_null(l);
len = sprintf(buf, "1:1:1111111111111:32:hello world%d", 1);
for (i = 0; i < 5; i++) {
r = libnet_build_udp(2425, 2426, len + 8, 0, buf, len, l, 0);
assert_true(r >= 0);
r = libnet_build_ipv4(20 + 8 + len, 0, 0, 0, 128, 17, 0, inet_addr("192.168.50.6"), inet_addr("192.168.50.5"), NULL, 0, l, 0);
assert_true(r >= 0);
r = libnet_build_ethernet(dst, src, 0x0800, NULL, 0, l, 0);
assert_true(r >= 0);
r = libnet_write(l);
assert_true(r >= 0);
sleep(1);
}
libnet_destroy(l);
}
static void test_tcp_packet_ipv4(void **state) {
char dst[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
char src[6] = {0x12,0x34,0x56,0x78,0x9a,0xab};
char errbuf[LIBNET_ERRBUF_SIZE];
uint8_t *payload, payload_s;
libnet_ptag_t tcp, ip, eth;
int c, i, j, seqn, ack;
char buf[1024] = {};
pthread_t pcap_thread;
struct itimerval tval;
uint32_t len;
libnet_t *l;
int r;
timerclear(& tval.it_interval);
timerclear(& tval.it_value);
tval.it_value.tv_sec = 10;
(void) signal(SIGALRM, terminate_thread);
(void) setitimer(ITIMER_REAL, & tval, NULL);
r = pthread_create(&pcap_thread, NULL, capture_packet_live, "tcp port 2425");
assert_true(r >=0);
payload_s = 10;
payload = malloc(payload_s*sizeof(uint8_t));
assert_non_null(payload);
memset(payload,0,payload_s);
l = libnet_init(LIBNET_LINK, "veth-peer", errbuf);
assert_non_null(l);
tcp = ip = eth = LIBNET_PTAG_INITIALIZER;
for (i=0; i<5; i++){
seqn=i * (LIBNET_TCP_H+payload_s + 1);
r = libnet_build_tcp(2425, 2426, seqn, seqn + LIBNET_TCP_H + payload_s + 1,
TH_SYN, 32767, 0, 10, LIBNET_TCP_H + payload_s,
payload, payload_s, l, tcp);
assert_true(r >=0);
r = libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_TCP_H + payload_s,0,242,0,64,IPPROTO_TCP,0,
inet_addr("192.168.50.6"), inet_addr("192.168.50.5"),
NULL,0,l,ip);
assert_true(r >=0);
r = libnet_build_ethernet(dst, src, ETHERTYPE_IP, NULL, 0, l, eth);
assert_true(r >=0);
r = libnet_write(l);
assert_true(r >=0);
sleep(1);
}
}
int main(int argc, char *argv[]) {
const struct CMUnitTest libnet_tests[] = {
cmocka_unit_test(test_udp_packet_ipv4),
cmocka_unit_test(test_tcp_packet_ipv4),
};
return cmocka_run_group_tests(libnet_tests, NULL, NULL);
}

17
tests/tests.yml Normal file
View File

@ -0,0 +1,17 @@
- hosts: localhost
roles:
- role: standard-test-beakerlib
tags:
- classic
tests:
- sanity-tests
required_packages:
- libnet
- libnet-devel
- libpcap
- libpcap-devel
- systemd
- wireshark-cli
- libcmocka
- libcmocka-devel
- gcc