Skip to content

Commit 2701f62

Browse files
committed
Add CMSG macros for unix/bsd and unix/notbsd
1 parent 814ce1e commit 2701f62

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/unix/bsd/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,43 @@ pub const POLLRDBAND: ::c_short = 0x080;
332332
pub const POLLWRBAND: ::c_short = 0x100;
333333

334334
f! {
335+
pub fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
336+
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
337+
(*mhdr).msg_control as *mut cmsghdr
338+
} else {
339+
0 as *mut cmsghdr
340+
}
341+
}
342+
343+
pub fn CMSG_NXTHDR(mhdr: *const msghdr,
344+
cmsg: *const cmsghdr) -> *mut cmsghdr {
345+
if cmsg.is_null() {
346+
return CMSG_FIRSTHDR(mhdr);
347+
};
348+
let pad = mem::align_of::<cmsghdr>() - 1;
349+
let next = cmsg as usize + (*cmsg).cmsg_len as usize + pad & !pad;
350+
let max = (*mhdr).msg_control as usize
351+
+ (*mhdr).msg_controllen as usize;
352+
if next < max {
353+
next as *mut cmsghdr
354+
} else {
355+
0 as *mut cmsghdr
356+
}
357+
}
358+
359+
pub fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut ::c_uchar {
360+
cmsg.offset(1) as *mut ::c_uchar
361+
}
362+
363+
pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
364+
let pad = mem::align_of::<cmsghdr>() as ::c_uint - 1;
365+
mem::size_of::<cmsghdr>() as ::c_uint + ((length + pad) & !pad)
366+
}
367+
368+
pub fn CMSG_LEN(length: ::c_uint) -> ::c_uint {
369+
mem::size_of::<cmsghdr>() as ::c_uint + length
370+
}
371+
335372
pub fn FD_CLR(fd: ::c_int, set: *mut fd_set) -> () {
336373
let bits = mem::size_of_val(&(*set).fds_bits[0]) * 8;
337374
let fd = fd as usize;

src/unix/notbsd/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,43 @@ pub const ARPHRD_VOID: u16 = 0xFFFF;
977977
pub const ARPHRD_NONE: u16 = 0xFFFE;
978978

979979
f! {
980+
pub fn CMSG_FIRSTHDR(mhdr: *const msghdr) -> *mut cmsghdr {
981+
if (*mhdr).msg_controllen as usize >= mem::size_of::<cmsghdr>() {
982+
(*mhdr).msg_control as *mut cmsghdr
983+
} else {
984+
0 as *mut cmsghdr
985+
}
986+
}
987+
988+
pub fn CMSG_NXTHDR(mhdr: *const msghdr,
989+
cmsg: *const cmsghdr) -> *mut cmsghdr {
990+
if cmsg.is_null() {
991+
return CMSG_FIRSTHDR(mhdr);
992+
};
993+
let pad = mem::align_of::<cmsghdr>() - 1;
994+
let next = cmsg as usize + (*cmsg).cmsg_len as usize + pad & !pad;
995+
let max = (*mhdr).msg_control as usize
996+
+ (*mhdr).msg_controllen as usize;
997+
if next < max {
998+
next as *mut cmsghdr
999+
} else {
1000+
0 as *mut cmsghdr
1001+
}
1002+
}
1003+
1004+
pub fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut ::c_uchar {
1005+
cmsg.offset(1) as *mut ::c_uchar
1006+
}
1007+
1008+
pub fn CMSG_SPACE(length: ::c_uint) -> ::c_uint {
1009+
let pad = mem::align_of::<cmsghdr>() as ::c_uint - 1;
1010+
mem::size_of::<cmsghdr>() as ::c_uint + ((length + pad) & !pad)
1011+
}
1012+
1013+
pub fn CMSG_LEN(length: ::c_uint) -> ::c_uint {
1014+
mem::size_of::<cmsghdr>() as ::c_uint + length
1015+
}
1016+
9801017
pub fn FD_CLR(fd: ::c_int, set: *mut fd_set) -> () {
9811018
let fd = fd as usize;
9821019
let size = mem::size_of_val(&(*set).fds_bits[0]) * 8;

0 commit comments

Comments
 (0)