2020#include <zephyr/net/socket_service.h>
2121#include <openthread/nat64.h>
2222#include "sockets_internal.h"
23+ #include <errno.h>
2324
2425#define MULTICAST_PORT 5353
2526#define MAX_SERVICES CONFIG_OPENTHREAD_ZEPHYR_BORDER_ROUTER_MAX_MDNS_SERVICES
@@ -104,13 +105,14 @@ static otError mdns_socket_init_v6(uint32_t ail_iface_idx)
104105 struct net_ipv6_mreq mreq_v6 = {0 };
105106 int mcast_hops = 255 ;
106107 int on = 1 ;
108+ int mcast_join_ret = 0 ;
107109
108110 struct net_sockaddr_in6 addr = {.sin6_family = NET_AF_INET6 ,
109- .sin6_port = net_htons (MULTICAST_PORT ),
110- .sin6_addr = {{{0 }}},
111- .sin6_scope_id = 0 };
111+ .sin6_port = net_htons (MULTICAST_PORT ),
112+ .sin6_addr = {{{0 }}},
113+ .sin6_scope_id = 0 };
112114
113- mdns_sock_v6 = zsock_socket (NET_AF_INET6 , SOCK_DGRAM , IPPROTO_UDP );
115+ mdns_sock_v6 = zsock_socket (NET_AF_INET6 , NET_SOCK_DGRAM , NET_IPPROTO_UDP );
114116 VerifyOrExit (mdns_sock_v6 >= 0 , error = OT_ERROR_FAILED );
115117 VerifyOrExit (net_if_get_name (net_if_get_by_index (ail_iface_idx ), name ,
116118 CONFIG_NET_INTERFACE_NAME_LEN ) > 0 ,
@@ -130,8 +132,9 @@ static otError mdns_socket_init_v6(uint32_t ail_iface_idx)
130132 error = OT_ERROR_FAILED );
131133 mreq_v6 .ipv6mr_ifindex = ail_iface_idx ;
132134 net_ipv6_addr_create (& mreq_v6 .ipv6mr_multiaddr , 0xff02 , 0 , 0 , 0 , 0 , 0 , 0 , 0x00fb );
133- VerifyOrExit (zsock_setsockopt (mdns_sock_v6 , NET_IPPROTO_IPV6 , ZSOCK_IPV6_ADD_MEMBERSHIP ,
134- & mreq_v6 , sizeof (mreq_v6 )) == 0 ,
135+ mcast_join_ret = zsock_setsockopt (mdns_sock_v6 , NET_IPPROTO_IPV6 , ZSOCK_IPV6_ADD_MEMBERSHIP ,
136+ & mreq_v6 , sizeof (mreq_v6 ));
137+ VerifyOrExit (mcast_join_ret == 0 || (mcast_join_ret == -1 && errno == EALREADY ),
135138 error = OT_ERROR_FAILED );
136139 VerifyOrExit (zsock_setsockopt (mdns_sock_v6 , NET_IPPROTO_IPV6 , ZSOCK_IPV6_MULTICAST_LOOP ,
137140 & on , sizeof (on )) == 0 ,
@@ -168,10 +171,11 @@ static otError mdns_socket_init_v4(uint32_t ail_iface_idx)
168171 struct net_ip_mreqn mreq_v4 = {0 };
169172 int mcast_hops = 255 ;
170173 int on = 1 ;
174+ int mcast_join_ret = 0 ;
171175
172176 struct net_sockaddr_in addr = {.sin_family = NET_AF_INET ,
173- .sin_port = net_htons (MULTICAST_PORT ),
174- .sin_addr = {{{0 }}}};
177+ .sin_port = net_htons (MULTICAST_PORT ),
178+ .sin_addr = {{{0 }}}};
175179
176180 mdns_sock_v4 = zsock_socket (NET_AF_INET , NET_SOCK_DGRAM , NET_IPPROTO_UDP );
177181 VerifyOrExit (mdns_sock_v4 >= 0 , error = OT_ERROR_FAILED );
@@ -190,8 +194,9 @@ static otError mdns_socket_init_v4(uint32_t ail_iface_idx)
190194 error = OT_ERROR_FAILED );
191195 mreq_v4 .imr_ifindex = ail_iface_idx ;
192196 mreq_v4 .imr_multiaddr .s_addr = net_htonl (0xE00000FB );
193- VerifyOrExit (zsock_setsockopt (mdns_sock_v4 , NET_IPPROTO_IP , ZSOCK_IP_ADD_MEMBERSHIP ,
194- & mreq_v4 , sizeof (mreq_v4 )) == 0 ,
197+ mcast_join_ret = zsock_setsockopt (mdns_sock_v4 , NET_IPPROTO_IP , ZSOCK_IP_ADD_MEMBERSHIP ,
198+ & mreq_v4 , sizeof (mreq_v4 ));
199+ VerifyOrExit (mcast_join_ret == 0 || (mcast_join_ret == -1 && errno == EALREADY ),
195200 error = OT_ERROR_FAILED );
196201 VerifyOrExit (zsock_setsockopt (mdns_sock_v4 , NET_IPPROTO_IP , ZSOCK_IP_MULTICAST_LOOP ,
197202 & on , sizeof (on )) == 0 ,
@@ -237,6 +242,10 @@ static otError mdns_socket_deinit(void)
237242 sockfd_udp [1 ].fd = -1 ;
238243 mdns_sock_v4 = -1 ;
239244#endif /* CONFIG_NET_IPV4 */
245+
246+ VerifyOrExit (net_socket_service_register (& mdns_udp_service , sockfd_udp ,
247+ ARRAY_SIZE (sockfd_udp ), NULL ) == 0 ,
248+ error = OT_ERROR_FAILED );
240249exit :
241250 return error ;
242251}
@@ -246,13 +255,13 @@ static void mdns_send_multicast(otMessage *message, uint32_t ail_iface_idx)
246255 uint16_t length = otMessageGetLength (message );
247256 struct otbr_msg_ctx * req = NULL ;
248257 struct net_sockaddr_in6 addr_v6 = {.sin6_family = NET_AF_INET6 ,
249- .sin6_port = net_htons (MULTICAST_PORT ),
250- .sin6_addr = NET_IN6ADDR_ANY_INIT ,
251- .sin6_scope_id = 0 };
258+ .sin6_port = net_htons (MULTICAST_PORT ),
259+ .sin6_addr = NET_IN6ADDR_ANY_INIT ,
260+ .sin6_scope_id = 0 };
252261#if defined(CONFIG_NET_IPV4 )
253262 struct net_sockaddr_in addr_v4 = {.sin_family = NET_AF_INET ,
254- .sin_port = net_htons (MULTICAST_PORT ),
255- .sin_addr = NET_INADDR_ANY_INIT };
263+ .sin_port = net_htons (MULTICAST_PORT ),
264+ .sin_addr = NET_INADDR_ANY_INIT };
256265#endif /* CONFIG_NET_IPV4*/
257266
258267 VerifyOrExit (length <= OTBR_MESSAGE_SIZE );
@@ -267,11 +276,11 @@ static void mdns_send_multicast(otMessage *message, uint32_t ail_iface_idx)
267276
268277 VerifyOrExit (zsock_sendto (mdns_sock_v6 , req -> buffer , length , 0 ,
269278 (struct net_sockaddr * )& addr_v6 ,
270- sizeof (addr_v6 )) > 0 );
279+ sizeof (addr_v6 )) > 0 );
271280#if defined(CONFIG_NET_IPV4 )
272281 VerifyOrExit (zsock_sendto (mdns_sock_v4 , req -> buffer , length , 0 ,
273282 (struct net_sockaddr * )& addr_v4 ,
274- sizeof (addr_v4 )) > 0 );
283+ sizeof (addr_v4 )) > 0 );
275284#endif /* CONFIG_NET_IPV4 */
276285exit :
277286 otMessageFree (message );
@@ -290,9 +299,9 @@ static void mdns_send_unicast(otMessage *message, const otPlatMdnsAddressInfo *a
290299 struct net_sockaddr_in addr_v4 = {0 };
291300#endif /* CONFIG_NET_IPV4*/
292301 struct net_sockaddr_in6 addr_v6 = {.sin6_family = NET_AF_INET6 ,
293- .sin6_port = net_htons (aAddress -> mPort ),
294- .sin6_addr = NET_IN6ADDR_ANY_INIT ,
295- .sin6_scope_id = 0 };
302+ .sin6_port = net_htons (aAddress -> mPort ),
303+ .sin6_addr = NET_IN6ADDR_ANY_INIT ,
304+ .sin6_scope_id = 0 };
296305
297306 VerifyOrExit (length <= OTBR_MESSAGE_SIZE );
298307 VerifyOrExit (openthread_border_router_allocate_message ((void * * )& req ) == OT_ERROR_NONE );
@@ -310,7 +319,7 @@ static void mdns_send_unicast(otMessage *message, const otPlatMdnsAddressInfo *a
310319
311320 VerifyOrExit (zsock_sendto (mdns_sock_v6 , req -> buffer , length , 0 ,
312321 (struct net_sockaddr * )& addr_v6 ,
313- sizeof (addr_v6 )) > 0 );
322+ sizeof (addr_v6 )) > 0 );
314323#if defined(CONFIG_NET_IPV4 )
315324 if (send_ipv4 ) {
316325 VerifyOrExit (zsock_sendto (mdns_sock_v4 , req -> buffer , length , 0 ,
@@ -345,7 +354,7 @@ static void mdns_receive_handler(struct net_socket_service_event *evt)
345354 if (family == NET_AF_INET ) {
346355 addrlen = sizeof (addr_v4 );
347356 len = zsock_recvfrom (mdns_sock_v4 , req -> buffer , sizeof (req -> buffer ), 0 ,
348- (struct net_sockaddr * )& addr_v4 , & addrlen );
357+ (struct net_sockaddr * )& addr_v4 , & addrlen );
349358 VerifyOrExit (len > 0 );
350359 otIp4ToIp4MappedIp6Address ((otIp4Address * )& addr_v4 .sin_addr .s_addr ,
351360 & req -> addr_info .mAddress );
@@ -354,7 +363,7 @@ static void mdns_receive_handler(struct net_socket_service_event *evt)
354363#endif /* CONFIG_NET_IPV4 */
355364 addrlen = sizeof (addr_v6 );
356365 len = zsock_recvfrom (mdns_sock_v6 , req -> buffer , sizeof (req -> buffer ), 0 ,
357- (struct net_sockaddr * )& addr_v6 , & addrlen );
366+ (struct net_sockaddr * )& addr_v6 , & addrlen );
358367 VerifyOrExit (len > 0 );
359368 memcpy (& req -> addr_info .mAddress , & addr_v6 .sin6_addr ,
360369 sizeof (req -> addr_info .mAddress ));
0 commit comments