mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-10 01:48:00 +00:00
windows/network.c: refactor switch in sk_newlistener.
The code that diverges based on the address family is now in the form of a switch statement, rather than an unwieldy series of chained ifs. And the final call to bind() has all its arguments worked out in the previous switch, rather than computing them at the last minute with an equally unwieldy set of ?: operators that repeat the previous test. This will make it easier to add more cases, and also, to keep each case under its own ifdef without losing too much legibility.
This commit is contained in:
parent
f08879a556
commit
5b5904b7cc
@ -1131,6 +1131,8 @@ Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
|
|||||||
SOCKADDR_IN6 a6;
|
SOCKADDR_IN6 a6;
|
||||||
#endif
|
#endif
|
||||||
SOCKADDR_IN a;
|
SOCKADDR_IN a;
|
||||||
|
struct sockaddr *bindaddr;
|
||||||
|
unsigned bindsize;
|
||||||
|
|
||||||
DWORD err;
|
DWORD err;
|
||||||
const char *errstr;
|
const char *errstr;
|
||||||
@ -1198,8 +1200,9 @@ Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
|
|||||||
(const char *)&on, sizeof(on));
|
(const char *)&on, sizeof(on));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch (address_family) {
|
||||||
#ifndef NO_IPV6
|
#ifndef NO_IPV6
|
||||||
if (address_family == AF_INET6) {
|
case AF_INET6: {
|
||||||
memset(&a6, 0, sizeof(a6));
|
memset(&a6, 0, sizeof(a6));
|
||||||
a6.sin6_family = AF_INET6;
|
a6.sin6_family = AF_INET6;
|
||||||
if (local_host_only)
|
if (local_host_only)
|
||||||
@ -1226,9 +1229,12 @@ Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
a6.sin6_port = p_htons(port);
|
a6.sin6_port = p_htons(port);
|
||||||
} else
|
bindaddr = (struct sockaddr *)&a6;
|
||||||
|
bindsize = sizeof(a6);
|
||||||
|
break;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
{
|
case AF_INET: {
|
||||||
bool got_addr = false;
|
bool got_addr = false;
|
||||||
a.sin_family = AF_INET;
|
a.sin_family = AF_INET;
|
||||||
|
|
||||||
@ -1256,16 +1262,15 @@ Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
|
|||||||
}
|
}
|
||||||
|
|
||||||
a.sin_port = p_htons((short)port);
|
a.sin_port = p_htons((short)port);
|
||||||
|
bindaddr = (struct sockaddr *)&a;
|
||||||
|
bindsize = sizeof(a);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
unreachable("bad address family in sk_newlistener_internal");
|
||||||
}
|
}
|
||||||
#ifndef NO_IPV6
|
|
||||||
retcode = p_bind(s, (address_family == AF_INET6 ?
|
retcode = p_bind(s, bindaddr, bindsize);
|
||||||
(struct sockaddr *) &a6 :
|
|
||||||
(struct sockaddr *) &a),
|
|
||||||
(address_family ==
|
|
||||||
AF_INET6 ? sizeof(a6) : sizeof(a)));
|
|
||||||
#else
|
|
||||||
retcode = p_bind(s, (struct sockaddr *) &a, sizeof(a));
|
|
||||||
#endif
|
|
||||||
if (retcode != SOCKET_ERROR) {
|
if (retcode != SOCKET_ERROR) {
|
||||||
err = 0;
|
err = 0;
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user