[BUG] backend: risk of picking a wrong port when mapping is used with crossed families
A similar issue as the previous one causes port mapping to fail in some
combinations of client and server address families. Using the macros fixes
the issue.
diff --git a/src/backend.c b/src/backend.c
index 31e29d3..d850ebf 100644
--- a/src/backend.c
+++ b/src/backend.c
@@ -700,22 +700,11 @@
get_frt_addr(s);
/* First, retrieve the port from the incoming connection */
- if (s->req->prod->addr.c.to.ss_family == AF_INET)
- base_port = ntohs(((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_port);
- else if (s->req->prod->addr.c.to.ss_family == AF_INET6)
- base_port = ntohs(((struct sockaddr_in6 *)&s->req->prod->addr.c.to)->sin6_port);
- else
- base_port = 0;
+ base_port = get_host_port(&s->req->prod->addr.c.to);
/* Second, assign the outgoing connection's port */
- if (s->req->cons->addr.c.to.ss_family == AF_INET) {
- ((struct sockaddr_in *)&s->req->cons->addr.s.to)->sin_port =
- htons(base_port + ntohs(((struct sockaddr_in *)&s->req->cons->addr.s.to)->sin_port));
- }
- else if (s->req->prod->addr.c.to.ss_family == AF_INET6) {
- ((struct sockaddr_in6 *)&s->req->cons->addr.s.to)->sin6_port =
- htons(base_port + ntohs(((struct sockaddr_in6 *)&s->req->cons->addr.s.to)->sin6_port));
- }
+ base_port += get_host_port(&s->req->prod->addr.s.to);
+ set_host_port(&s->req->cons->addr.s.to, base_port);
}
}
else if (s->be->options & PR_O_DISPATCH) {