From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top
Subject: [PATCH 6/8] Treat port numbers as unsigned
Date: Fri, 23 Sep 2022 14:58:04 +1000 [thread overview]
Message-ID: <20220923045806.956143-7-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20220923045806.956143-1-david@gibson.dropbear.id.au>
[-- Attachment #1: Type: text/plain, Size: 2828 bytes --]
Port numbers are unsigned values, but we're storing them in (signed) int
variables in some places. This isn't actually harmful, because int is
large enough to hold the entire range of ports. However in places we don't
want to use an in_port_t (usually to avoid overflow on the last iteration
of a loop) it makes more conceptual sense to use an unsigned int. This will
also avoid some problems with later cleanups.
Signed-off-by: David Gibson <david(a)gibson.dropbear.id.au>
---
conf.c | 11 ++++++-----
tcp.c | 4 ++--
udp.c | 2 +-
3 files changed, 9 insertions(+), 8 deletions(-)
diff --git a/conf.c b/conf.c
index 2cbc519..503d894 100644
--- a/conf.c
+++ b/conf.c
@@ -118,11 +118,12 @@ static int get_bound_ports_ns(void *arg)
static int conf_ports(const struct ctx *c, char optname, const char *optarg,
struct port_fwd *fwd)
{
- int start_src, end_src, start_dst, end_dst, exclude_only = 1, i, port;
char addr_buf[sizeof(struct in6_addr)] = { 0 }, *addr = addr_buf;
+ int start_src, end_src, start_dst, end_dst, exclude_only = 1, i;
char buf[BUFSIZ], *sep, *spec, *p;
port_fwd_map exclude = { 0 };
sa_family_t af = AF_UNSPEC;
+ unsigned port;
if (!strcmp(optarg, "none")) {
if (fwd->mode)
@@ -204,11 +205,11 @@ static int conf_ports(const struct ctx *c, char optname, const char *optarg,
p++;
errno = 0;
- port = strtol(p, &sep, 10);
+ port = strtoul(p, &sep, 10);
if (sep == p)
break;
- if (port < 0 || port > USHRT_MAX || errno)
+ if (port > USHRT_MAX || errno)
goto bad;
switch (*sep) {
@@ -271,11 +272,11 @@ static int conf_ports(const struct ctx *c, char optname, const char *optarg,
break;
errno = 0;
- port = strtol(p, &sep, 10);
+ port = strtoul(p, &sep, 10);
if (sep == p)
break;
- if (port < 0 || port > USHRT_MAX || errno)
+ if (port > USHRT_MAX || errno)
goto bad;
/* -p 22
diff --git a/tcp.c b/tcp.c
index 509a0b3..d96232c 100644
--- a/tcp.c
+++ b/tcp.c
@@ -3182,7 +3182,7 @@ void tcp_sock_init(const struct ctx *c, int ns, sa_family_t af,
static int tcp_sock_init_ns(void *arg)
{
struct ctx *c = (struct ctx *)arg;
- int port;
+ unsigned port;
ns_enter(c);
@@ -3381,7 +3381,7 @@ struct tcp_port_rebind_arg {
static int tcp_port_rebind(void *arg)
{
struct tcp_port_rebind_arg *a = (struct tcp_port_rebind_arg *)arg;
- int port;
+ unsigned port;
if (a->bind_in_ns) {
ns_enter(a->c);
diff --git a/udp.c b/udp.c
index d17b3b4..27c3aa3 100644
--- a/udp.c
+++ b/udp.c
@@ -1193,7 +1193,7 @@ void udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
int udp_sock_init_ns(void *arg)
{
struct ctx *c = (struct ctx *)arg;
- int dst;
+ unsigned dst;
if (ns_enter(c))
return 0;
--
@@ -1193,7 +1193,7 @@ void udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
int udp_sock_init_ns(void *arg)
{
struct ctx *c = (struct ctx *)arg;
- int dst;
+ unsigned dst;
if (ns_enter(c))
return 0;
--
2.37.3
next prev parent reply other threads:[~2022-09-23 4:58 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-23 4:57 [PATCH 0/8] Clean up and fix bugs in port forwarding data structures David Gibson
2022-09-23 4:57 ` [PATCH 1/8] Improve types and names for port forwarding configuration David Gibson
2022-09-23 22:52 ` Stefano Brivio
2022-09-24 2:57 ` David Gibson
2022-09-24 6:28 ` Stefano Brivio
2022-09-24 9:06 ` David Gibson
2022-09-23 4:58 ` [PATCH 2/8] Consolidate port forwarding configuration into a common structure David Gibson
2022-09-23 4:58 ` [PATCH 3/8] udp: Delay initialization of UDP reversed port mapping table David Gibson
2022-09-23 4:58 ` [PATCH 4/8] Don't use indirect remap functions for conf_ports() David Gibson
2022-09-23 4:58 ` [PATCH 5/8] Pass entire port forwarding configuration substructure to conf_ports() David Gibson
2022-09-23 4:58 ` David Gibson [this message]
2022-09-23 4:58 ` [PATCH 7/8] Fix widespread off-by-one error dealing with port numbers David Gibson
2022-09-23 4:58 ` [PATCH 8/8] icmp: Correct off by one errors dealing with number of echo request ids David Gibson
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=20220923045806.956143-7-david@gibson.dropbear.id.au \
--to=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
/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.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
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 IMAP folder(s).