@@ -147,52 +147,10 @@ pub const fn family_changed(a: SocketAddr, b: SocketAddr) -> bool {
147147/// connecting to an invalid address (NULL or AF_UNSPEC). The error
148148/// EAFNOSUPPORT may be harmlessly returned; consider it success.
149149#[ cfg( unix) ]
150- pub fn udp_disconnect ( sock : & Socket ) -> io:: Result < ( ) > {
150+ pub fn disconnect_socket ( sock : & Socket ) -> io:: Result < ( ) > {
151151 let fd = sock. as_raw_fd ( ) ;
152152
153- // Interpret connect() rc correctly per platform.
154- #[ inline]
155- #[ cfg( any(
156- target_os = "macos" ,
157- target_os = "ios" ,
158- target_os = "freebsd" ,
159- target_os = "openbsd" ,
160- target_os = "netbsd" ,
161- target_os = "dragonfly" ,
162- ) ) ]
163- fn ok_or_eafnosupport ( rc : i32 ) -> io:: Result < ( ) > {
164- if rc == 0 {
165- Ok ( ( ) )
166- } else {
167- let err = io:: Error :: last_os_error ( ) ;
168- if err. raw_os_error ( ) == Some ( libc:: EAFNOSUPPORT ) {
169- // macOS/*BSD man page: harmless when disconnecting UDP
170- Ok ( ( ) )
171- } else {
172- Err ( err)
173- }
174- }
175- }
176-
177- // On non-BSD Unix (Linux/Android), do NOT ignore EAFNOSUPPORT.
178- #[ inline]
179- #[ cfg( not( any(
180- target_os = "macos" ,
181- target_os = "ios" ,
182- target_os = "freebsd" ,
183- target_os = "openbsd" ,
184- target_os = "netbsd" ,
185- target_os = "dragonfly" ,
186- ) ) ) ]
187- fn ok_or_eafnosupport ( rc : i32 ) -> io:: Result < ( ) > {
188- if rc == 0 {
189- Ok ( ( ) )
190- } else {
191- Err ( io:: Error :: last_os_error ( ) )
192- }
193- }
194-
195- // --- macOS / iOS / *BSD: try AF_UNSPEC first, then NULL ---
153+ // --- macOS / iOS / *BSD: AF_UNSPEC is sufficient. ---
196154 #[ cfg( any(
197155 target_os = "macos" ,
198156 target_os = "ios" ,
@@ -215,13 +173,15 @@ pub fn udp_disconnect(sock: &Socket) -> io::Result<()> {
215173 addr. sa_len as libc:: socklen_t ,
216174 )
217175 } ;
218- if ok_or_eafnosupport ( rc ) . is_ok ( ) {
176+ if rc == 0 {
219177 return Ok ( ( ) ) ;
220178 }
221-
222- // Fallback: connect(fd, NULL, 0)
223- let rc2 = unsafe { libc:: connect ( fd, std:: ptr:: null ( ) , 0 ) } ;
224- return ok_or_eafnosupport ( rc2) ;
179+ let err = io:: Error :: last_os_error ( ) ;
180+ if err. raw_os_error ( ) == Some ( libc:: EAFNOSUPPORT ) {
181+ // macOS/*BSD man page: harmless when disconnecting UDP
182+ return Ok ( ( ) ) ;
183+ }
184+ return Err ( err) ;
225185 }
226186
227187 // --- Linux/Android: AF_UNSPEC is the standard way; no sa_len field. ---
@@ -245,13 +205,16 @@ pub fn udp_disconnect(sock: &Socket) -> io::Result<()> {
245205 std:: mem:: size_of :: < libc:: sockaddr > ( ) as libc:: socklen_t ,
246206 )
247207 } ;
248- return ok_or_eafnosupport ( rc) ;
208+ if rc == 0 {
209+ return Ok ( ( ) ) ;
210+ }
211+ return Err ( io:: Error :: last_os_error ( ) ) ;
249212 }
250213}
251214
252215/// Windows: disconnect a UDP socket by connecting to INADDR_ANY/IN6ADDR_ANY and port 0.
253216#[ cfg( windows) ]
254- pub fn udp_disconnect ( sock : & Socket ) -> io:: Result < ( ) > {
217+ pub fn disconnect_socket ( sock : & Socket ) -> io:: Result < ( ) > {
255218 let local = sock. local_addr ( ) ?;
256219 let any_std = match local. as_socket ( ) {
257220 Some ( SocketAddr :: V6 ( _) ) => SocketAddr :: new ( IpAddr :: V6 ( Ipv6Addr :: UNSPECIFIED ) , 0 ) ,
@@ -264,9 +227,9 @@ pub fn udp_disconnect(sock: &Socket) -> io::Result<()> {
264227
265228/// Fallback: not supported on this platform.
266229#[ cfg( all( not( unix) , not( windows) ) ) ]
267- pub fn udp_disconnect ( _sock : & Socket ) -> io:: Result < ( ) > {
230+ pub fn disconnect_socket ( _sock : & Socket ) -> io:: Result < ( ) > {
268231 Err ( io:: Error :: new (
269232 io:: ErrorKind :: Other ,
270- "Function udp_disconnect is not supported on this OS" ,
233+ "Function disconnect_socket is not supported on this OS" ,
271234 ) )
272235}
0 commit comments