Skip to content

Commit b05aa00

Browse files
Frantisek Tobiasvcunat
authored andcommitted
daemon/network: Avoid binding multiple transport protocols to one addr and port combination
1 parent 00978d9 commit b05aa00

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

daemon/network.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,7 @@ static int open_endpoint(const char *addr_str,
466466
return kr_error(ret);
467467
}
468468

469-
/** @internal Fetch a pointer to endpoint of given parameters (or NULL).
470-
* Beware that there might be multiple matches, though that's not common.
471-
* The matching isn't really precise in the sense that it might not find
472-
* and endpoint that would *collide* the passed one. */
469+
/** Fetch a pointer to endpoint of given parameters or a colliding one (or NULL). */
473470
static struct endpoint * endpoint_get(const char *addr_str,
474471
const struct sockaddr *sa,
475472
endpoint_flags_t flags)
@@ -483,12 +480,23 @@ static struct endpoint * endpoint_get(const char *addr_str,
483480
return NULL;
484481
endpoint_array_t *ep_array = *val;
485482

486-
uint16_t port = kr_inaddr_port(sa);
487483
for (int i = 0; i < ep_array->len; ++i) {
488484
struct endpoint *ep = &ep_array->at[i];
489-
if ((flags.xdp || ep->port == port) && endpoint_flags_eq(ep->flags, flags)) {
485+
if (flags.xdp) {
486+
/* `key` matched by interface name */
487+
kr_assert(ep_array->len == 1);
490488
return ep;
491489
}
490+
if (kr_straddr_family(addr_str) == AF_UNIX) {
491+
/* `key` matched by path */
492+
kr_assert(ep_array->len == 1);
493+
return ep;
494+
}
495+
/* else `key` already matched IP address + port,
496+
* so the only remaining distinction is UDP vs. TCP */
497+
kr_assert(ep_array->len <= 2);
498+
if (ep->flags.sock_type == flags.sock_type)
499+
return ep;
492500
}
493501
return NULL;
494502
}

daemon/network.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ typedef struct {
3232

3333
struct endpoint_key;
3434

35-
static inline bool endpoint_flags_eq(endpoint_flags_t f1, endpoint_flags_t f2)
36-
{
37-
if (f1.sock_type != f2.sock_type)
38-
return false;
39-
if (f1.kind && f2.kind)
40-
return strcasecmp(f1.kind, f2.kind);
41-
else
42-
return f1.tls == f2.tls && f1.kind == f2.kind;
43-
}
44-
4535
/** Wrapper for a single socket to listen on.
4636
* There are two types: normal have handle, special have flags.kind (and never both).
4737
*
@@ -142,7 +132,7 @@ void network_unregister(void);
142132
void network_deinit(void);
143133

144134
/** Start listening on addr#port with flags.
145-
* \note if we did listen on that combination already,
135+
* \note if we did listen on a colliding combination already,
146136
* nothing is done and kr_error(EADDRINUSE) is returned.
147137
* \note there's no short-hand to listen both on UDP and TCP.
148138
* \note ownership of flags.* is taken on success. TODO: non-success?

0 commit comments

Comments
 (0)