Skip to content

Commit

Permalink
hping3: add new package
Browse files Browse the repository at this point in the history
The new package would help measuring one-way delays using ICMP type 13
packets. This is important for various scripts that automatically adjust
CAKE shaper bandwidth based on the observed bufferbloat. They need to
understand whether the delay is on the way up or on the way down, so
that they can adjust the bandwidth of the proper part of the shaper.

https://forum.openwrt.org/t/cake-w-adaptive-bandwidth-historic/108848
https://forum.openwrt.org/t/cake-w-adaptive-bandwidth/135379

V2: refreshed patches

Signed-off-by: Alexander E. Patrakov <patrakov@gmail.com>
  • Loading branch information
patrakov authored and neheb committed Sep 22, 2022
1 parent f1bacdc commit 688a541
Show file tree
Hide file tree
Showing 14 changed files with 747 additions and 0 deletions.
55 changes: 55 additions & 0 deletions net/hping3/Makefile
@@ -0,0 +1,55 @@
#
# Copyright (C) 2022 Alexander E. Patrakov
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
# Special thanks to the Debian and Fedora projects for their hping3
# packaging, elements of which (patches, list of dependencies, etc.) are
# reused here.

include $(TOPDIR)/rules.mk

PKG_NAME:=hping3
PKG_SOURCE_DATE:=2014-12-26
PKG_SOURCE_VERSION:=3547c7691742c6eaa31f8402e0ccbb81387c1b99
PKG_RELEASE:=1

PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/antirez/hping
PKG_MIRROR_HASH:=0d65eaa8e55ab10025fb2d615eb9567255288b3b9eb66465f54d0e1718bb80ad

PKG_MAINTAINER:=Alexander E. Patrakov <patrakov@gmail.com>
PKG_LICENSE:=GPL-2.0-only

PKG_INSTALL:=1
PKG_BUILD_PARALLEL:=1

include $(INCLUDE_DIR)/package.mk

define Package/hping3
SECTION:=net
CATEGORY:=Network
TITLE:=Active Network Smashing Tool
URL:=https://github.com/antirez/hping
DEPENDS:= +libpcap
endef

define Package/hping3/description
hping3 is a network tool able to send custom ICMP/UDP/TCP packets
and to display target replies like ping does with ICMP replies. It
handles fragmentation and arbitrary packet body and size, and can
be used to transfer files under supported protocols. Using hping3,
you can test firewall rules, perform (spoofed) port scanning, test
network performance using different protocols, do path MTU
discovery, perform traceroute-like actions under different
protocols, fingerprint remote operating systems, audit TCP/IP
stacks, etc. Tcl support is not included.
endef

define Package/hping3/install
$(INSTALL_DIR) $(1)/usr/sbin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/hping3 $(1)/usr/sbin/
endef

$(eval $(call BuildPackage,hping3))
96 changes: 96 additions & 0 deletions net/hping3/patches/011_buildsystem.patch
@@ -0,0 +1,96 @@
--- a/Makefile.in
+++ b/Makefile.in
@@ -9,8 +9,8 @@
CC= gcc
AR=/usr/bin/ar
RANLIB=/usr/bin/ranlib
-CCOPT= -O2 -Wall @PCAP_INCLUDE@ @TCL_INC@ @USE_TCL@
-DEBUG= -g
+CCOPT= $(CFLAGS) @PCAP_INCLUDE@ @TCL_INC@ @USE_TCL@
+DEBUG=
#uncomment the following if you need libpcap based build under linux
#(not raccomanded)
COMPILE_TIME=
@@ -50,17 +50,13 @@ libars.a: $(ARSOBJ)
$(RANLIB) $@

hping3: byteorder.h $(OBJ)
- $(CC) -o hping3 $(CCOPT) $(DEBUG) $(OBJ) -L/usr/local/lib $(PCAP) @SOLARISLIB@ @TCL_LIB@
+ $(CC) -o hping3 $(CCOPT) $(DEBUG) $(OBJ) $(PCAP) @SOLARISLIB@ @TCL_LIB@
@echo
- ./hping3 -v
@echo "use \`make strip' to strip hping3 binary"
@echo "use \`make install' to install hping3"

hping3-static: byteorder.h $(OBJ)
- $(CC) -static -o hping3-static $(CCOPT) $(DEBUG) $(OBJ) -L/usr/local/lib $(PCAP) @SOLARISLIB@ @TCL_LIB@ -ldl
-
-byteorder.h:
- ./configure
+ $(CC) -static -o hping3-static $(CCOPT) $(DEBUG) $(OBJ) $(PCAP) @SOLARISLIB@ @TCL_LIB@ -ldl

.c.o:
$(CC) -c $(CCOPT) $(DEBUG) $(COMPILE_TIME) $<
@@ -72,6 +68,8 @@ distclean:
rm -rf hping3 *.o byteorder byteorder.h systype.h Makefile libars.a .depend

install: hping3
+
+dont-install:
cp -f hping3 /usr/sbin/
chmod 755 /usr/sbin/hping3
ln -s /usr/sbin/hping3 /usr/sbin/hping
--- /dev/null
+++ b/byteorder.h
@@ -0,0 +1,15 @@
+
+#ifndef __BYTEORDER_H
+#define __BYTEORDER_H
+
+#if defined(__BYTE_ORDER__)
+ #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+ #define __BIG_ENDIAN_BITFIELD
+ #else
+ #define __LITTLE_ENDIAN_BITFIELD
+ #endif
+#else
+ #error Byte order not set
+#endif
+
+#endif /* __BYTEORDER_H */
--- a/configure
+++ b/configure
@@ -1,5 +1,24 @@
#!/bin/sh

+# This fake configure script applies hard-coded settings for OpenWRT
+
+echo creating Makefile...
+sed -e "s^@PCAP@^PCAP=-lpcap^g" \
+ -e "s^@PCAP_INCLUDE@^^g" \
+ -e "s^@MANPATH@^/usr/share/man^g" \
+ -e "s^@SOLARISLIB@^^g" \
+ -e "s^@USE_TCL@^^g" \
+ -e "s^@TCL_INC@^^g" \
+ -e "s^@TCL_VER@^^g" \
+ -e "s^@TCL_LIB@^^g" \
+ <Makefile.in > Makefile
+
+echo now you can try \`make\'
+exit 0
+
+# Original file follows, to make the patch smaller
+#!/bin/sh
+
show_help()
{
echo configure help:
--- /dev/null
+++ b/systype.h
@@ -0,0 +1,6 @@
+#ifndef __SYSTYPE_H
+#define __SYSTYPE_H
+
+#define OSTYPE_LINUX
+
+#endif /* SYSTYPE_H */
22 changes: 22 additions & 0 deletions net/hping3/patches/020_pcap_bpf.patch
@@ -0,0 +1,22 @@
--- a/libpcap_stuff.c
+++ b/libpcap_stuff.c
@@ -16,7 +16,7 @@
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
-#include <net/bpf.h>
+#include <pcap-bpf.h>
#include <pcap.h>

#include "globals.h"
--- a/script.c
+++ b/script.c
@@ -23,7 +23,7 @@
#include <sched.h>

#include <sys/ioctl.h>
-#include <net/bpf.h>
+#include <pcap-bpf.h>
#include <pcap.h>

#include "release.h"
37 changes: 37 additions & 0 deletions net/hping3/patches/040_spelling.patch
@@ -0,0 +1,37 @@
Subject: Various spelling fixes.
Author: Romain Francoise <rfrancoise@debian.org>
Last-Updated: 2020-09-06

--- a/antigetopt.c
+++ b/antigetopt.c
@@ -142,7 +142,7 @@ chain_start:
#define UNK_LONG_ERRSTRING "unrecognized option `--%s'\n"
#define ARG_SHORT_ERRSTRING "option requires an argument -- %c\n"
#define ARG_LONG_ERRSTRING "option `--%s' requires an argument\n"
-#define AMB_ERRSTRING "option `--%s' is ambiguos\n"
+#define AMB_ERRSTRING "option `--%s' is ambiguous\n"
#define IERR_ERRSTRING "internal error. ago_gnu_error() called with " \
"a bad error code (%d)\n"
void ago_gnu_error(char *pname, int error)
--- a/statistics.c
+++ b/statistics.c
@@ -30,7 +30,7 @@ void print_statistics(int signal_id)
lossrate = 100;

fprintf(stderr, "\n--- %s hping statistic ---\n", targetname);
- fprintf(stderr, "%d packets tramitted, %d packets received, "
+ fprintf(stderr, "%d packets transmitted, %d packets received, "
"%d%% packet loss\n", sent_pkt, recv_pkt, lossrate);
if (out_of_sequence_pkt)
fprintf(stderr, "%d out of sequence packets received\n",
--- a/ars.c
+++ b/ars.c
@@ -361,7 +361,7 @@ void *ars_add_data(struct ars_packet *pk
static void *ptr = "zzappt"; /* we can't return NULL for size == 0 */

if (size < 0) {
- ars_set_error(pkt, "Tryed to add a DATA layer with size < 0");
+ ars_set_error(pkt, "Tried to add a DATA layer with size < 0");
return NULL;
}
retval = ars_add_generic(pkt, size, ARS_TYPE_DATA);
69 changes: 69 additions & 0 deletions net/hping3/patches/080_ip_id_field.patch
@@ -0,0 +1,69 @@
Subject: Fix "-N" command line argument
Author: Guillaume Delacour <gui@iroqwa.org>
Bug-Debian: http://bugs.debian.org/482844
Last-Update: 2009-05-13

--- a/docs/hping2.8
+++ b/docs/hping2.8
@@ -343,7 +343,7 @@ options. If in doubt try
.I -N --id
Set ip->id field. Default id is random but if fragmentation is turned on
and id isn't specified it will be
-.BR "getpid() & 0xFF" ,
+.BR "getpid() & 0xFFFF" ,
to implement a better solution is in TODO list.
.TP
.I -H --ipproto
@@ -714,4 +714,4 @@ On solaris hping does not work on the lo
a solaris problem, as stated in the tcpdump-workers mailing list,
so the libpcap can't do nothing to handle it properly.
.SH SEE ALSO
-ping(8), traceroute(8), ifconfig(8), nmap(1)
\ No newline at end of file
+ping(8), traceroute(8), ifconfig(8), nmap(1)
--- a/docs/hping3.8
+++ b/docs/hping3.8
@@ -352,7 +352,7 @@ options. If in doubt try
.I -N --id
Set ip->id field. Default id is random but if fragmentation is turned on
and id isn't specified it will be
-.BR "getpid() & 0xFF" ,
+.BR "getpid() & 0xFFFF" ,
to implement a better solution is in TODO list.
.TP
.I -H --ipproto
--- a/hping2.h
+++ b/hping2.h
@@ -121,7 +121,7 @@
#define DEFAULT_ICMP_IP_IHL (IPHDR_SIZE >> 2)
#define DEFAULT_ICMP_IP_TOS 0
#define DEFAULT_ICMP_IP_TOT_LEN 0 /* computed by send_icmp_*() */
-#define DEFAULT_ICMP_IP_ID 0 /* rand */
+#define DEFAULT_ICMP_IP_ID -1 /* rand */
#define DEFAULT_ICMP_CKSUM -1 /* -1 means compute the cksum */
#define DEFAULT_ICMP_IP_PROTOCOL 6 /* TCP */
#define DEFAULT_RAW_IP_PROTOCOL 6 /* TCP */
--- a/parseoptions.c
+++ b/parseoptions.c
@@ -468,6 +468,10 @@ int parse_options(int argc, char **argv)
break;
case OPT_ICMP_IPID:
icmp_ip_id = strtol(ago_optarg, NULL, 0);
+ if (icmp_ip_id < 0 || icmp_ip_id > 0xffff) {
+ fprintf(stderr, "Bad ICMP IP ID, resetting to random.\n");
+ icmp_ip_id = DEFAULT_ICMP_IP_ID;
+ }
break;
case OPT_ICMP_IPPROTO:
icmp_ip_protocol = strtol(ago_optarg, NULL, 0);
--- a/sendicmp.c
+++ b/sendicmp.c
@@ -83,7 +83,7 @@ void send_icmp_echo(void)
icmp->type = opt_icmptype; /* echo replay or echo request */
icmp->code = opt_icmpcode; /* should be indifferent */
icmp->checksum = 0;
- icmp->un.echo.id = getpid() & 0xffff;
+ icmp->un.echo.id = icmp_ip_id == DEFAULT_ICMP_IP_ID ? getpid() & 0xffff : icmp_ip_id;
icmp->un.echo.sequence = _icmp_seq;

/* data */
16 changes: 16 additions & 0 deletions net/hping3/patches/110_dontfrag_offbyone.patch
@@ -0,0 +1,16 @@
Subject: Off-by-one error with --dontfrag
Author: Christian Mock <cm@coretec.at>
Bugs-Debian: http://bugs.debian.org/537704
Last-Update: 2009-09-04

--- a/sendip_handler.c
+++ b/sendip_handler.c
@@ -19,7 +19,7 @@ void send_ip_handler(char *packet, unsig
{
ip_optlen = ip_opt_build(ip_opt);

- if (!opt_fragment && (size+ip_optlen+20 >= h_if_mtu))
+ if (!opt_fragment && (size+ip_optlen+20 > h_if_mtu))
{
/* auto-activate fragmentation */
virtual_mtu = h_if_mtu-20;
34 changes: 34 additions & 0 deletions net/hping3/patches/120_rtt_icmp_unreachable.patch
@@ -0,0 +1,34 @@
Subject: Add RTT handling for ICMP destination unreachable packets.
Author: Baruch Even <baruch@debian.org>
Bug-Debian: http://bugs.debian.org/248273
Last-Update: 2009-09-04

--- a/waitpacket.c
+++ b/waitpacket.c
@@ -229,6 +229,7 @@ int recv_icmp(void *packet, size_t size)
{
struct myicmphdr icmp;
struct myiphdr quoted_ip;
+ struct myudphdr quoted_udp;

/* Check if the packet can contain the ICMP header */
if (size < ICMPHDR_SIZE) {
@@ -286,6 +287,18 @@ int recv_icmp(void *packet, size_t size)
case 3:
if (!opt_quiet)
log_icmp_unreach(inet_ntoa(src), icmp.code);
+ if (quoted_ip.protocol == IPPROTO_UDP)
+ {
+ int sequence = 0, status;
+ float ms_delay = 0.0;
+ unsigned short port;
+
+ /* Get RTT for UDP packet */
+ memcpy(&quoted_udp, packet+ICMPHDR_SIZE+(quoted_ip.ihl<<2), sizeof(quoted_udp));
+ port = ntohs(quoted_udp.uh_sport);
+ status = rtt(&sequence, port, &ms_delay);
+ printf("status=%d port=%d seq=%d\n", status, port, sequence);
+ }
return 1;
case 11:
if (opt_traceroute)

2 comments on commit 688a541

@lynxthecat
Copy link

Choose a reason for hiding this comment

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

Please can this get pulled into 22.03 branch?

@neheb
Copy link
Contributor

@neheb neheb commented on 688a541 Sep 24, 2022

Choose a reason for hiding this comment

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

Done.

Please sign in to comment.