From fbccee1df040940570ab2d374573a8c43dc245cc Mon Sep 17 00:00:00 2001 From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org> Date: Tue, 16 Apr 2019 22:37:29 +0300 Subject: [PATCH] swim: move sockaddr_in checkers to swim_proto.h There are several places where it is necessary to check if a sockaddr_in is nullified, and to compare a couple of addresses. Some of them are in swim_proto.c, and more are coming in indirect SWIM messages patch. The patch moves the checkers into swim_proto.h so as to be usable from anywhere in SWIM. Also minor renames are made alongside. 'sockaddr_in' is too long to use in each related function's name, and is replaced with 'inaddr' by analogue with the standard library. Part of #3234 --- src/lib/swim/swim.c | 12 ++---------- src/lib/swim/swim_proto.c | 26 ++++++++++++++++---------- src/lib/swim/swim_proto.h | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/lib/swim/swim.c b/src/lib/swim/swim.c index 2dac6eedd0..40fa3fb21b 100644 --- a/src/lib/swim/swim.c +++ b/src/lib/swim/swim.c @@ -201,14 +201,6 @@ swim_uuid_str(const struct tt_uuid *uuid) return buf; } -/** Check if two AF_INET addresses are equal. */ -static bool -swim_sockaddr_in_eq(const struct sockaddr_in *a1, const struct sockaddr_in *a2) -{ - return a1->sin_port == a2->sin_port && - a1->sin_addr.s_addr == a2->sin_addr.s_addr; -} - /** * A cluster member description. This structure describes the * last known state of an instance. This state is updated @@ -950,7 +942,7 @@ swim_complete_step(struct swim_task *task, struct swim_member *m = rlist_first_entry(&swim->round_queue, struct swim_member, in_round_queue); - if (swim_sockaddr_in_eq(&m->addr, &task->dst)) { + if (swim_inaddr_eq(&m->addr, &task->dst)) { rlist_shift(&swim->round_queue); if (rc > 0) { /* @@ -1048,7 +1040,7 @@ static inline void swim_update_member_addr(struct swim *swim, struct swim_member *member, const struct sockaddr_in *addr, int incarnation_inc) { - if (! swim_sockaddr_in_eq(addr, &member->addr)) { + if (! swim_inaddr_eq(addr, &member->addr)) { member->incarnation += incarnation_inc; member->addr = *addr; swim_on_member_update(swim, member); diff --git a/src/lib/swim/swim_proto.c b/src/lib/swim/swim_proto.c index d84550663b..cd9dd195b6 100644 --- a/src/lib/swim/swim_proto.c +++ b/src/lib/swim/swim_proto.c @@ -154,6 +154,20 @@ swim_decode_uuid(struct tt_uuid *uuid, const char **pos, const char *end, return 0; } +/** + * Check if @a addr is not empty, i.e. not nullified. Set an error + * in the diagnostics area in case of emptiness. + */ +static inline int +swim_check_inaddr_not_empty(const struct sockaddr_in *addr, const char *prefix, + const char *addr_name) +{ + if (! swim_inaddr_is_empty(addr)) + return 0; + diag_set(SwimError, "%s %s address is mandatory", prefix, addr_name); + return -1; +} + void swim_member_def_create(struct swim_member_def *def) { @@ -236,15 +250,11 @@ swim_member_def_decode(struct swim_member_def *def, const char **pos, if (swim_decode_member_key(key, pos, end, prefix, def) != 0) return -1; } - if (def->addr.sin_port == 0 || def->addr.sin_addr.s_addr == 0) { - diag_set(SwimError, "%s member address is mandatory", prefix); - return -1; - } if (tt_uuid_is_nil(&def->uuid)) { diag_set(SwimError, "%s member uuid is mandatory", prefix); return -1; } - return 0; + return swim_check_inaddr_not_empty(&def->addr, prefix, "member"); } void @@ -429,11 +439,7 @@ swim_meta_def_decode(struct swim_meta_def *def, const char **pos, diag_set(SwimError, "%s version is mandatory", prefix); return -1; } - if (def->src.sin_port == 0 || def->src.sin_addr.s_addr == 0) { - diag_set(SwimError, "%s source address is mandatory", prefix); - return -1; - } - return 0; + return swim_check_inaddr_not_empty(&def->src, prefix, "source"); } void diff --git a/src/lib/swim/swim_proto.h b/src/lib/swim/swim_proto.h index ab40571850..3704696cf9 100644 --- a/src/lib/swim/swim_proto.h +++ b/src/lib/swim/swim_proto.h @@ -472,4 +472,26 @@ int swim_decode_uuid(struct tt_uuid *uuid, const char **pos, const char *end, const char *prefix, const char *param_name); +/** + * Check if @a addr is not empty, i.e. not nullified. Empty + * addresses are considered invalid and normally can not appear in + * packets just like any other errors can't. But since the SWIM + * protocol is public, there can be outlandish drivers and they + * can contain errors. Check for nullified address is a protection + * from malicious and invalid packets. + */ +static inline bool +swim_inaddr_is_empty(const struct sockaddr_in *addr) +{ + return addr->sin_port == 0 || addr->sin_addr.s_addr == 0; +} + +/** Check if two AF_INET addresses are equal. */ +static inline bool +swim_inaddr_eq(const struct sockaddr_in *a1, const struct sockaddr_in *a2) +{ + return a1->sin_port == a2->sin_port && + a1->sin_addr.s_addr == a2->sin_addr.s_addr; +} + #endif /* TARANTOOL_SWIM_PROTO_H_INCLUDED */ -- GitLab