From 8f829b2495f3dbfe33ffada7f6f7aaa6289dd3ce Mon Sep 17 00:00:00 2001 From: Rafael Zalamena Date: Fri, 27 May 2022 17:15:46 -0300 Subject: [PATCH 1/2] pimd: remove temporary variables from MSDP peer Remove from MSDP peer data structure two temporary variables that should only be used when calling library functions. Signed-off-by: Rafael Zalamena --- pimd/pim_msdp.c | 12 ------------ pimd/pim_msdp.h | 3 --- pimd/pim_msdp_socket.c | 29 +++++++++++++++++++++-------- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/pimd/pim_msdp.c b/pimd/pim_msdp.c index 215cc3c5029c..bea74ff7d477 100644 --- a/pimd/pim_msdp.c +++ b/pimd/pim_msdp.c @@ -1012,16 +1012,6 @@ void pim_msdp_peer_pkt_txed(struct pim_msdp_peer *mp) } } -static void pim_msdp_addr2su(union sockunion *su, struct in_addr addr) -{ - sockunion_init(su); - su->sin.sin_addr = addr; - su->sin.sin_family = AF_INET; -#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN - su->sin.sin_len = sizeof(struct sockaddr_in); -#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ -} - /* 11.2.A1: create a new peer and transition state to listen or connecting */ struct pim_msdp_peer *pim_msdp_peer_add(struct pim_instance *pim, const struct in_addr *peer, @@ -1037,11 +1027,9 @@ struct pim_msdp_peer *pim_msdp_peer_add(struct pim_instance *pim, mp->pim = pim; mp->peer = *peer; pim_inet4_dump("", mp->peer, mp->key_str, sizeof(mp->key_str)); - pim_msdp_addr2su(&mp->su_peer, mp->peer); mp->local = *local; /* XXX: originator_id setting needs to move to the mesh group */ pim->msdp.originator_id = *local; - pim_msdp_addr2su(&mp->su_local, mp->local); if (mesh_group_name) mp->mesh_group_name = XSTRDUP(MTYPE_PIM_MSDP_MG_NAME, mesh_group_name); diff --git a/pimd/pim_msdp.h b/pimd/pim_msdp.h index f77b0e1a3afd..f01f575e0794 100644 --- a/pimd/pim_msdp.h +++ b/pimd/pim_msdp.h @@ -114,9 +114,6 @@ struct pim_msdp_peer { enum pim_msdp_peer_state state; enum pim_msdp_peer_flags flags; - /* TCP socket info */ - union sockunion su_local; - union sockunion su_peer; int fd; /* protocol timers */ diff --git a/pimd/pim_msdp_socket.c b/pimd/pim_msdp_socket.c index 2fb0bb87c789..b29993304db5 100644 --- a/pimd/pim_msdp_socket.c +++ b/pimd/pim_msdp_socket.c @@ -49,6 +49,16 @@ static void pim_msdp_update_sock_send_buffer_size(int fd) } } +static void pim_msdp_addr2su(union sockunion *su, struct in_addr addr) +{ + sockunion_init(su); + su->sin.sin_addr = addr; + su->sin.sin_family = AF_INET; +#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN + su->sin.sin_len = sizeof(struct sockaddr_in); +#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ +} + /** * Helper function to reduce code duplication. * @@ -64,7 +74,6 @@ static int _pim_msdp_sock_listen(const struct vrf *vrf, int rv; socklen_t socklen; struct sockaddr_in sin = {}; - union sockunion su_peer = {}; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) { @@ -117,7 +126,9 @@ static int _pim_msdp_sock_listen(const struct vrf *vrf, /* Set MD5 authentication. */ if (mp && mp->auth_key) { - su_peer = mp->su_peer; + union sockunion su_peer = {}; + + pim_msdp_addr2su(&su_peer, mp->peer); frr_with_privs (&pimd_privs) { sockopt_tcp_signature(sock, &su_peer, mp->auth_key); } @@ -349,6 +360,7 @@ int pim_msdp_sock_listen(struct pim_instance *pim) int pim_msdp_sock_connect(struct pim_msdp_peer *mp) { int rc; + union sockunion su_peer = {}, su_local = {}; if (PIM_DEBUG_MSDP_INTERNAL) { zlog_debug("MSDP peer %s attempt connect%s", mp->key_str, @@ -366,8 +378,11 @@ int pim_msdp_sock_connect(struct pim_msdp_peer *mp) pim_msdp_peer_stop_tcp_conn(mp, false /* chg_state */); } + pim_msdp_addr2su(&su_peer, mp->peer); + pim_msdp_addr2su(&su_local, mp->local); + /* Make socket for the peer. */ - mp->fd = sockunion_socket(&mp->su_peer); + mp->fd = sockunion_socket(&su_peer); if (mp->fd < 0) { flog_err_sys(EC_LIB_SOCKET, "pim_msdp_socket socket failure: %s", @@ -402,7 +417,7 @@ int pim_msdp_sock_connect(struct pim_msdp_peer *mp) sockopt_reuseport(mp->fd); /* source bind */ - rc = sockunion_bind(mp->fd, &mp->su_local, 0, &mp->su_local); + rc = sockunion_bind(mp->fd, &su_local, 0, &su_local); if (rc < 0) { flog_err_sys(EC_LIB_SOCKET, "pim_msdp_socket connect bind failure: %s", @@ -421,12 +436,10 @@ int pim_msdp_sock_connect(struct pim_msdp_peer *mp) /* Set authentication (if configured). */ if (mp->auth_key) { frr_with_privs (&pimd_privs) { - sockopt_tcp_signature(mp->fd, &mp->su_peer, - mp->auth_key); + sockopt_tcp_signature(mp->fd, &su_peer, mp->auth_key); } } /* Connect to the remote mp. */ - return (sockunion_connect(mp->fd, &mp->su_peer, - htons(PIM_MSDP_TCP_PORT), 0)); + return (sockunion_connect(mp->fd, &su_peer, htons(PIM_MSDP_TCP_PORT), 0)); } From 1e0629fd04b1d8ac7c64a70677cfd7d5fd38d473 Mon Sep 17 00:00:00 2001 From: Rafael Zalamena Date: Fri, 8 Jul 2022 16:09:10 -0300 Subject: [PATCH 2/2] pimd: constify peer lookup function Make parameter `const` to document that the variable will not change. Signed-off-by: Rafael Zalamena --- pimd/pim_msdp.c | 3 +-- pimd/pim_msdp.h | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/pimd/pim_msdp.c b/pimd/pim_msdp.c index bea74ff7d477..0c87b3f48548 100644 --- a/pimd/pim_msdp.c +++ b/pimd/pim_msdp.c @@ -1066,8 +1066,7 @@ struct pim_msdp_peer *pim_msdp_peer_add(struct pim_instance *pim, return mp; } -struct pim_msdp_peer *pim_msdp_peer_find(struct pim_instance *pim, - struct in_addr peer_addr) +struct pim_msdp_peer *pim_msdp_peer_find(const struct pim_instance *pim, struct in_addr peer_addr) { struct pim_msdp_peer lookup; diff --git a/pimd/pim_msdp.h b/pimd/pim_msdp.h index f01f575e0794..cd1f6647f69f 100644 --- a/pimd/pim_msdp.h +++ b/pimd/pim_msdp.h @@ -235,8 +235,7 @@ void pim_msdp_init(struct pim_instance *pim, struct event_loop *master); void pim_msdp_exit(struct pim_instance *pim); char *pim_msdp_state_dump(enum pim_msdp_peer_state state, char *buf, int buf_size); -struct pim_msdp_peer *pim_msdp_peer_find(struct pim_instance *pim, - struct in_addr peer_addr); +struct pim_msdp_peer *pim_msdp_peer_find(const struct pim_instance *pim, struct in_addr peer_addr); void pim_msdp_peer_established(struct pim_msdp_peer *mp); void pim_msdp_peer_pkt_rxed(struct pim_msdp_peer *mp); void pim_msdp_peer_stop_tcp_conn(struct pim_msdp_peer *mp, bool chg_state);