@@ -89,9 +89,10 @@ Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jobject this,
8989
9090#ifdef __solaris__
9191 rv = connect (fd , 0 , 0 );
92- #endif
93-
94- #if defined(__linux__ ) || defined(_ALLBSD_SOURCE ) || defined(_AIX )
92+ #elif defined(__APPLE__ )
93+ // On macOS systems we use disconnectx
94+ rv = disconnectx (fd , SAE_ASSOCID_ANY , SAE_CONNID_ANY );
95+ #elif defined(__linux__ ) || defined(_ALLBSD_SOURCE ) || defined(_AIX )
9596 {
9697 int len ;
9798 SOCKADDR sa ;
@@ -120,8 +121,16 @@ Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jobject this,
120121 }
121122
122123 rv = connect (fd , (struct sockaddr * )& sa , len );
124+ }
123125
124- #if defined(_ALLBSD_SOURCE )
126+ #endif
127+
128+ #if defined(_ALLBSD_SOURCE ) && !defined(__APPLE__ )
129+ // On _ALLBSD_SOURCE except __APPLE__ we consider EADDRNOTAVAIL
130+ // error to be OK and ignore it. __APPLE__ systems are excluded
131+ // in this check since for __APPLE__ systems, unlike other BSD systems,
132+ // we issue a "disconnectx" call (a few lines above),
133+ // which isn't expected to return this error code.
125134 if (rv < 0 && errno == EADDRNOTAVAIL )
126135 rv = errno = 0 ;
127136#endif
@@ -133,8 +142,6 @@ Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jobject this,
133142 if (rv < 0 && errno == EAFNOSUPPORT )
134143 rv = errno = 0 ;
135144#endif
136- }
137- #endif
138145
139146 if (rv < 0 )
140147 handleSocketError (env , errno );
0 commit comments