This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit 4210b2226e546b61f80e592351fefc2d6d2aa253 Author: wenquan1 <[email protected]> AuthorDate: Thu Sep 17 09:23:24 2026 +0800 netutils/ptpd: move multicast join after interface address query The IGMP multicast join (ipmsfilter) was previously called before the interface address (interface_addr) was populated via SIOCGIFADDR. This meant the IGMP join had to locate the network device without a valid local address, which could fail or join on the wrong interface. Move the multicast group subscription to after the interface address is queried, and guard it with an AF_INET check since IGMP only applies to IPv4. This ensures the IGMP join can always locate the correct network device. Signed-off-by: wenquan1 <[email protected]> --- netutils/ptpd/ptpd.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/netutils/ptpd/ptpd.c b/netutils/ptpd/ptpd.c index d75acca97..8bb897a34 100644 --- a/netutils/ptpd/ptpd.c +++ b/netutils/ptpd/ptpd.c @@ -578,21 +578,10 @@ static int ptp_initialize_state(FAR struct ptp_state_s *state) goto errout; } - /* Subscribe to PTP multicast address */ + /* Bind socket for events to PTP multicast address */ bind_addr.sin_family = AF_INET; bind_addr.sin_addr.s_addr = HTONL(PTP_MULTICAST_ADDR); - - ret = ipmsfilter(&state->interface_addr.sin_addr, - &bind_addr.sin_addr, MCAST_INCLUDE); - if (ret < 0) - { - ptperr("Failed to bind multicast address: %d\n", errno); - goto errout; - } - - /* Bind socket for events */ - bind_addr.sin_port = HTONS(PTP_UDP_PORT_EVENT); ret = bind(state->event_socket, (FAR struct sockaddr *)&bind_addr, sizeof(bind_addr)); @@ -655,6 +644,25 @@ static int ptp_initialize_state(FAR struct ptp_state_s *state) state->interface_addr = *(FAR struct sockaddr_in *)&req.ifr_ifru.ifru_addr; + /* Subscribe to PTP multicast address (AF_INET only). + * Must be done after interface_addr is populated so the IGMP join + * can locate the correct network device. + */ + + if (state->config->af == AF_INET) + { + struct in_addr mcast_addr; + + mcast_addr.s_addr = HTONL(PTP_MULTICAST_ADDR); + ret = ipmsfilter(&state->interface_addr.sin_addr, + &mcast_addr, MCAST_INCLUDE); + if (ret < 0) + { + ptperr("Failed to join multicast group: %d\n", errno); + goto errout; + } + } + /* Get hardware address to initialize the identity field in header. * Clock identity is EUI-64, which we make from EUI-48. */
