[MINOR] make use of set_host_port() and get_host_port() to get rid of family mismatches
This also simplifies the code and makes it more auditable.
diff --git a/src/cfgparse.c b/src/cfgparse.c
index 1e3a92c..48d2617 100644
--- a/src/cfgparse.c
+++ b/src/cfgparse.c
@@ -1230,15 +1230,7 @@
goto out;
}
newpeer->addr = *sk;
-
- switch (newpeer->addr.ss_family) {
- case AF_INET:
- ((struct sockaddr_in *)&newpeer->addr)->sin_port = htons(realport);
- break;
- case AF_INET6:
- ((struct sockaddr_in6 *)&newpeer->addr)->sin6_port = htons(realport);
- break;
- }
+ set_host_port(&newpeer->addr, realport);
if (strcmp(newpeer->id, localpeer) == 0) {
/* Current is local peer, it define a frontend */
@@ -3935,15 +3927,7 @@
goto out;
}
newsrv->addr = *sk;
-
- switch (newsrv->addr.ss_family) {
- case AF_INET:
- ((struct sockaddr_in *)&newsrv->addr)->sin_port = htons(realport);
- break;
- case AF_INET6:
- ((struct sockaddr_in6 *)&newsrv->addr)->sin6_port = htons(realport);
- break;
- }
+ set_host_port(&newsrv->addr, realport);
newsrv->check_port = curproxy->defsrv.check_port;
newsrv->inter = curproxy->defsrv.inter;
@@ -4446,16 +4430,9 @@
goto out;
}
- switch (newsrv->check_addr.ss_family) {
- case AF_INET:
- if (!newsrv->check_port && ((struct sockaddr_in *)&newsrv->check_addr)->sin_port)
- newsrv->check_port = ntohs(((struct sockaddr_in *)&newsrv->check_addr)->sin_port);
- break;
- case AF_INET6:
- if (!newsrv->check_port && ((struct sockaddr_in6 *)&newsrv->check_addr)->sin6_port)
- newsrv->check_port = ntohs(((struct sockaddr_in6 *)&newsrv->check_addr)->sin6_port);
- break;
- }
+ /* try to get the port from check_addr if check_port not set */
+ if (!newsrv->check_port)
+ newsrv->check_port = get_host_port(&newsrv->check_addr);
if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS))
newsrv->check_port = realport; /* by default */
@@ -4464,15 +4441,9 @@
* the server either. We'll check if we have
* a known port on the first listener.
*/
- struct listener *l;
- l = curproxy->listen;
- if (l) {
- int port;
- port = (l->addr.ss_family == AF_INET6)
- ? ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port)
- : ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
- newsrv->check_port = port;
- }
+ struct listener *l = curproxy->listen;
+ while (l && !(newsrv->check_port = get_host_port(&l->addr)))
+ l = l->next;
}
if (!newsrv->check_port) {
Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n",
diff --git a/src/checks.c b/src/checks.c
index b98c49b..6a102df 100644
--- a/src/checks.c
+++ b/src/checks.c
@@ -1329,15 +1329,7 @@
break;
fdinfo[fd].port_range = s->sport_range;
-
- switch (src.ss_family) {
- case AF_INET:
- ((struct sockaddr_in *)&src)->sin_port = htons(fdinfo[fd].local_port);
- break;
- case AF_INET6:
- ((struct sockaddr_in6 *)&src)->sin6_port = htons(fdinfo[fd].local_port);
- break;
- }
+ set_host_port(&src, fdinfo[fd].local_port);
ret = tcp_bind_socket(fd, flags, &src, remote);
} while (ret != 0); /* binding NOK */
diff --git a/src/log.c b/src/log.c
index 02b6b20..16ace2f 100644
--- a/src/log.c
+++ b/src/log.c
@@ -356,9 +356,7 @@
" %s %s/%s %ld/%ld/%s%ld %s%lld"
" %c%c %d/%d/%d/%d/%s%u %ld/%ld\n",
s->si[0].addr.c.from.ss_family == AF_UNIX ? "unix" : pn,
- s->si[0].addr.c.from.ss_family == AF_UNIX ? s->listener->luid : (ntohs((s->si[0].addr.c.from.ss_family == AF_INET) ?
- ((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_port :
- ((struct sockaddr_in6 *)&s->si[0].addr.c.from)->sin6_port)),
+ s->si[0].addr.c.from.ss_family == AF_UNIX ? s->listener->luid : get_host_port(&s->si[0].addr.c.from),
tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.tv_accept.tv_usec/1000,
fe->id, be->id, svid,
diff --git a/src/proto_http.c b/src/proto_http.c
index acf2ea3..32f9f26 100644
--- a/src/proto_http.c
+++ b/src/proto_http.c
@@ -915,9 +915,7 @@
w = snprintf(h, sizeof(tmpline) - (h - tmpline),
" %d %03d",
s->req->prod->addr.c.from.ss_family == AF_UNIX ? s->listener->luid :
- ntohs((s->req->prod->addr.c.from.ss_family == AF_INET) ?
- ((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_port :
- ((struct sockaddr_in6 *)&s->req->prod->addr.c.from)->sin6_port),
+ get_host_port(&s->req->prod->addr.c.from),
(int)s->logs.accept_date.tv_usec/1000);
if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
goto trunc;
@@ -1192,9 +1190,7 @@
" %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
(s->req->prod->addr.c.from.ss_family == AF_UNIX) ? "unix" : pn,
(s->req->prod->addr.c.from.ss_family == AF_UNIX) ? s->listener->luid :
- ntohs((s->req->prod->addr.c.from.ss_family == AF_INET) ?
- ((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_port :
- ((struct sockaddr_in6 *)&s->req->prod->addr.c.from)->sin6_port),
+ get_host_port(&s->req->prod->addr.c.from),
tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
fe->id, be->id, svid,
diff --git a/src/proto_tcp.c b/src/proto_tcp.c
index 3b6d39d..8a04f16 100644
--- a/src/proto_tcp.c
+++ b/src/proto_tcp.c
@@ -316,14 +316,7 @@
break;
fdinfo[fd].port_range = srv->sport_range;
- switch (src.ss_family) {
- case AF_INET:
- ((struct sockaddr_in *)&src)->sin_port = htons(fdinfo[fd].local_port);
- break;
- case AF_INET6:
- ((struct sockaddr_in6 *)&src)->sin6_port = htons(fdinfo[fd].local_port);
- break;
- }
+ set_host_port(&src, fdinfo[fd].local_port);
ret = tcp_bind_socket(fd, flags, &src, &si->addr.s.from);
} while (ret != 0); /* binding NOK */
@@ -1305,11 +1298,7 @@
acl_fetch_sport(struct proxy *px, struct session *l4, void *l7, int dir,
struct acl_expr *expr, struct acl_test *test)
{
- if (l4->si[0].addr.c.from.ss_family == AF_INET)
- test->i = ntohs(((struct sockaddr_in *)&l4->si[0].addr.c.from)->sin_port);
- else if (l4->si[0].addr.c.from.ss_family == AF_INET6)
- test->i = ntohs(((struct sockaddr_in6 *)(&l4->si[0].addr.c.from))->sin6_port);
- else
+ if (!(test->i = get_host_port(&l4->si[0].addr.c.from)))
return 0;
test->flags = 0;
@@ -1376,11 +1365,7 @@
if (!(l4->flags & SN_FRT_ADDR_SET))
get_frt_addr(l4);
- if (l4->si[0].addr.c.to.ss_family == AF_INET)
- test->i = ntohs(((struct sockaddr_in *)&l4->si[0].addr.c.to)->sin_port);
- else if (l4->si[0].addr.c.to.ss_family == AF_INET6)
- test->i = ntohs(((struct sockaddr_in6 *)(&l4->si[0].addr.c.to))->sin6_port);
- else
+ if (!(test->i = get_host_port(&l4->si[0].addr.c.to)))
return 0;
test->flags = 0;
@@ -1394,11 +1379,7 @@
if (!(l4->flags & SN_FRT_ADDR_SET))
get_frt_addr(l4);
- if (l4->si[0].addr.c.to.ss_family == AF_INET)
- data->integer = ntohs(((struct sockaddr_in *)&l4->si[0].addr.c.to)->sin_port);
- else if (l4->si[0].addr.c.to.ss_family == AF_INET6)
- data->integer = ntohs(((struct sockaddr_in6 *)&l4->si[0].addr.c.to)->sin6_port);
- else
+ if (!(data->integer = get_host_port(&l4->si[0].addr.c.to)))
return 0;
return 1;
diff --git a/src/proxy.c b/src/proxy.c
index 6df460e..9409d55 100644
--- a/src/proxy.c
+++ b/src/proxy.c
@@ -733,15 +733,8 @@
if (!resume_listener(l)) {
int port;
- if (l->addr.ss_family == AF_INET6) {
- port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port);
- Warning("Port %d busy while trying to enable %s %s.\n",
- port, proxy_cap_str(p->cap), p->id);
- send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n",
- port, proxy_cap_str(p->cap), p->id);
- }
- else if (l->addr.ss_family == AF_INET) {
- port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port);
+ port = get_host_port(&l->addr);
+ if (port) {
Warning("Port %d busy while trying to enable %s %s.\n",
port, proxy_cap_str(p->cap), p->id);
send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n",
diff --git a/src/standard.c b/src/standard.c
index 5c27bdb..e371717 100644
--- a/src/standard.c
+++ b/src/standard.c
@@ -327,14 +327,7 @@
if (!ret)
goto out;
- switch (ret->ss_family) {
- case AF_INET:
- ((struct sockaddr_in *)ret)->sin_port = htons(port);
- break;
- case AF_INET6:
- ((struct sockaddr_in6 *)ret)->sin6_port = htons(port);
- break;
- }
+ set_host_port(ret, port);
out:
free(str2);
return ret;
@@ -383,14 +376,7 @@
if (!ret)
goto out;
- switch (ret->ss_family) {
- case AF_INET:
- ((struct sockaddr_in *)ret)->sin_port = htons(portl);
- break;
- case AF_INET6:
- ((struct sockaddr_in6 *)ret)->sin6_port = htons(portl);
- break;
- }
+ set_host_port(ret, portl);
*low = portl;
*high = porth;