netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeremy Sowden <jeremy@azazel.net>
To: Jan Engelhardt <jengelh@inai.de>
Cc: Netfilter Devel <netfilter-devel@vger.kernel.org>, kaskada@email.cz
Subject: [xtables-addons 3/4] xt_ipp2p: move result printing code into separate functions
Date: Mon, 13 Sep 2021 10:20:50 +0100	[thread overview]
Message-ID: <20210913092051.79743-4-jeremy@azazel.net> (raw)
In-Reply-To: <20210913092051.79743-1-jeremy@azazel.net>

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
---
 extensions/xt_ipp2p.c | 75 +++++++++++++++++++++++++++++++++++--------
 1 file changed, 61 insertions(+), 14 deletions(-)

diff --git a/extensions/xt_ipp2p.c b/extensions/xt_ipp2p.c
index 298950514569..56fcbe497718 100644
--- a/extensions/xt_ipp2p.c
+++ b/extensions/xt_ipp2p.c
@@ -19,6 +19,27 @@ MODULE_AUTHOR("Eicke Friedrich/Klaus Degner <ipp2p@ipp2p.org>");
 MODULE_DESCRIPTION("An extension to iptables to identify P2P traffic.");
 MODULE_LICENSE("GPL");
 
+union ipp2p_addr {
+	__be32 ip;
+};
+
+struct ipp2p_result_printer {
+	const union ipp2p_addr *saddr, *daddr;
+	short sport, dport;
+	void (*print) (const union ipp2p_addr *, short,
+		       const union ipp2p_addr *, short,
+		       bool, unsigned int);
+};
+
+static void
+print_result (const struct ipp2p_result_printer *rp, bool result,
+	      unsigned int hlen)
+{
+	rp->print(rp->saddr, rp->sport,
+		  rp->daddr, rp->dport,
+		  result, hlen);
+}
+
 /* Search for UDP eDonkey/eMule/Kad commands */
 static unsigned int
 udp_search_edk(const unsigned char *t, const unsigned int packet_len)
@@ -807,10 +828,19 @@ static const struct {
 	{0},
 };
 
+static void
+ipp2p_print_result_tcp(const union ipp2p_addr *saddr, short sport,
+		       const union ipp2p_addr *daddr, short dport,
+		       bool p2p_result, unsigned int hlen)
+{
+	printk("IPP2P.debug:TCP-match: %d from: %pI4:%hu to: %pI4:%hu Length: %u\n",
+	       p2p_result, &saddr->ip, sport, &daddr->ip, dport, hlen);
+}
+
 static bool
 ipp2p_mt_tcp(const struct ipt_p2p_info *info, const struct tcphdr *tcph,
 	     const unsigned char *haystack, unsigned int hlen,
-	     const struct iphdr *ip)
+	     const struct ipp2p_result_printer *rp)
 {
 	size_t tcph_len = tcph->doff * 4;
 	bool p2p_result = false;
@@ -838,11 +868,7 @@ ipp2p_mt_tcp(const struct ipt_p2p_info *info, const struct tcphdr *tcph,
 			p2p_result = matchlist[i].function_name(haystack, hlen);
 			if (p2p_result)	{
 				if (info->debug)
-					printk("IPP2P.debug:TCP-match: %d from: %pI4:%hu to: %pI4:%hu Length: %d\n",
-					       p2p_result, &ip->saddr,
-					       ntohs(tcph->source),
-					       &ip->daddr,
-					       ntohs(tcph->dest), hlen);
+					print_result(rp, p2p_result, hlen);
 				return p2p_result;
 			}
 		}
@@ -851,10 +877,19 @@ ipp2p_mt_tcp(const struct ipt_p2p_info *info, const struct tcphdr *tcph,
 	return p2p_result;
 }
 
+static void
+ipp2p_print_result_udp(const union ipp2p_addr *saddr, short sport,
+		       const union ipp2p_addr *daddr, short dport,
+		       bool p2p_result, unsigned int hlen)
+{
+	printk("IPP2P.debug:UDP-match: %d from: %pI4:%hu to: %pI4:%hu Length: %u\n",
+	       p2p_result, &saddr->ip, sport, &daddr->ip, dport, hlen);
+}
+
 static bool
 ipp2p_mt_udp(const struct ipt_p2p_info *info, const struct udphdr *udph,
 	     const unsigned char *haystack, unsigned int hlen,
-	     const struct iphdr *ip)
+	     const struct ipp2p_result_printer *rp)
 {
 	size_t udph_len = sizeof(*udph);
 	bool p2p_result = false;
@@ -878,11 +913,7 @@ ipp2p_mt_udp(const struct ipt_p2p_info *info, const struct udphdr *udph,
 			p2p_result = udp_list[i].function_name(haystack, hlen);
 			if (p2p_result) {
 				if (info->debug)
-					printk("IPP2P.debug:UDP-match: %d from: %pI4:%hu to: %pI4:%hu Length: %d\n",
-					       p2p_result, &ip->saddr,
-					       ntohs(udph->source),
-					       &ip->daddr,
-					       ntohs(udph->dest), hlen);
+					print_result(rp, p2p_result, hlen);
 				return p2p_result;
 			}
 		}
@@ -896,6 +927,8 @@ ipp2p_mt(const struct sk_buff *skb, struct xt_action_param *par)
 {
 	const struct ipt_p2p_info *info = par->matchinfo;
 	const struct iphdr *ip = ip_hdr(skb);
+	struct ipp2p_result_printer printer;
+	union ipp2p_addr saddr, daddr;
 	const unsigned char *haystack;  /* packet-data */
 	unsigned int hlen;              /* packet-data length */
 
@@ -916,19 +949,33 @@ ipp2p_mt(const struct sk_buff *skb, struct xt_action_param *par)
 	haystack = skb_transport_header(skb);
 	hlen     = ntohs(ip->tot_len) - skb_transport_offset(skb);
 
+	saddr.ip = ip->saddr;
+	daddr.ip = ip->daddr;
+
+	printer.saddr = &saddr;
+	printer.daddr = &daddr;
+
 	switch (ip->protocol) {
 	case IPPROTO_TCP:	/* what to do with a TCP packet */
 	{
 		const struct tcphdr *tcph = tcp_hdr(skb);
 
-		return ipp2p_mt_tcp(info, tcph, haystack, hlen, ip);
+		printer.sport = ntohs(tcph->source);
+		printer.dport = ntohs(tcph->dest);
+		printer.print = ipp2p_print_result_tcp;
+
+		return ipp2p_mt_tcp(info, tcph, haystack, hlen, &printer);
 	}
 	case IPPROTO_UDP:	/* what to do with a UDP packet */
 	case IPPROTO_UDPLITE:
 	{
 		const struct udphdr *udph = udp_hdr(skb);
 
-		return ipp2p_mt_udp(info, udph, haystack, hlen, ip);
+		printer.sport = ntohs(udph->source);
+		printer.dport = ntohs(udph->dest);
+		printer.print = ipp2p_print_result_udp;
+
+		return ipp2p_mt_udp(info, udph, haystack, hlen, &printer);
 	}
 	default:
 		return 0;
-- 
2.33.0


  parent reply	other threads:[~2021-09-13  9:24 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-13  9:20 [xtables-addons 0/4] IPv6 support for xt_ipp2p Jeremy Sowden
2021-09-13  9:20 ` [xtables-addons 1/4] xt_ipp2p: don't search haystack if it's empty Jeremy Sowden
2021-09-13  9:20 ` [xtables-addons 2/4] xt_ipp2p: move the protocol-specific code out into separate functions Jeremy Sowden
2021-09-13  9:20 ` Jeremy Sowden [this message]
2021-09-13  9:20 ` [xtables-addons 4/4] xt_ipp2p: add ipv6 support Jeremy Sowden
     [not found] ` <1wg.aVMb.5l0xziYPqYA.1XFsCY@seznam.cz>
2021-09-13 14:55   ` [xtables-addons 0/4] IPv6 support for xt_ipp2p Jeremy Sowden
2021-09-13 17:19 ` Jan Engelhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210913092051.79743-4-jeremy@azazel.net \
    --to=jeremy@azazel.net \
    --cc=jengelh@inai.de \
    --cc=kaskada@email.cz \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).