@@ -226,11 +226,13 @@ impl Cmsg {
226
226
fn level_type_size ( & self ) -> ( libc:: c_int , libc:: c_int , libc:: c_uint ) {
227
227
match self {
228
228
#[ cfg( not( any( target_os = "solaris" , target_os = "illumos" ) ) ) ]
229
- Cmsg :: IpTos ( _) => (
230
- libc:: IPPROTO_IP ,
231
- libc:: IP_TOS ,
232
- std:: mem:: size_of :: < u8 > ( ) as libc:: c_uint ,
233
- ) ,
229
+ Cmsg :: IpTos ( _) => {
230
+ #[ cfg( not( target_os = "macos" ) ) ]
231
+ let len = std:: mem:: size_of :: < u8 > ( ) ;
232
+ #[ cfg( target_os = "macos" ) ]
233
+ let len = std:: mem:: size_of :: < i32 > ( ) ;
234
+ ( libc:: IPPROTO_IP , libc:: IP_TOS , len as libc:: c_uint )
235
+ }
234
236
#[ cfg( not( any( target_os = "fuchsia" , target_os = "solaris" , target_os = "illumos" ) ) ) ]
235
237
Cmsg :: Ipv6PktInfo { .. } => (
236
238
libc:: IPPROTO_IPV6 ,
@@ -248,7 +250,15 @@ impl Cmsg {
248
250
match self {
249
251
#[ cfg( not( any( target_os = "solaris" , target_os = "illumos" ) ) ) ]
250
252
Cmsg :: IpTos ( tos) => {
251
- buffer[ 0 ] = * tos;
253
+ #[ cfg( not( target_os = "macos" ) ) ]
254
+ {
255
+ buffer[ 0 ] = * tos;
256
+ }
257
+ #[ cfg( target_os = "macos" ) ]
258
+ {
259
+ let value = * tos as i32 ;
260
+ buffer. copy_from_slice ( & value. to_ne_bytes ( ) [ ..] )
261
+ }
252
262
}
253
263
#[ cfg( not( any( target_os = "fuchsia" , target_os = "solaris" , target_os = "illumos" ) ) ) ]
254
264
Cmsg :: Ipv6PktInfo { addr, ifindex } => {
@@ -283,8 +293,22 @@ impl Cmsg {
283
293
match ( cmsg_level, cmsg_type) {
284
294
#[ cfg( not( any( target_os = "solaris" , target_os = "illumos" ) ) ) ]
285
295
( libc:: IPPROTO_IP , libc:: IP_TOS ) => {
286
- assert_eq ! ( bytes. len( ) , std:: mem:: size_of:: <u8 >( ) , "{:?}" , bytes) ;
287
- Cmsg :: IpTos ( bytes[ 0 ] )
296
+ // Different systems encode received TOS as char or int.
297
+ match bytes {
298
+ [ b] => Cmsg :: IpTos ( * b) ,
299
+ [ a, b, c, d] => Cmsg :: IpTos ( i32:: from_ne_bytes ( [ * a, * b, * c, * d] ) as u8 ) ,
300
+ other => panic ! ( "unexpected length for IP_TOS: {:?}" , other) ,
301
+ }
302
+ }
303
+ #[ cfg( any(
304
+ target_os = "freebsd" ,
305
+ target_os = "openbsd" ,
306
+ target_os = "netbsd" ,
307
+ target_os = "macos"
308
+ ) ) ]
309
+ ( libc:: IPPROTO_IP , libc:: IP_RECVTOS ) => {
310
+ // BSD systems use IP_RECVTOS on the receive path.
311
+ Self :: from_raw ( libc:: IPPROTO_IP , libc:: IP_TOS , bytes)
288
312
}
289
313
#[ cfg( not( any( target_os = "fuchsia" , target_os = "solaris" , target_os = "illumos" ) ) ) ]
290
314
( libc:: IPPROTO_IPV6 , libc:: IPV6_PKTINFO ) => {
0 commit comments