Skip to content

Commit 33ff832

Browse files
committed
Auto merge of #492 - APTy:add-socket-peek-flag, r=alexcrichton
Add MSG_PEEK socket flag #### MSG_PEEK This PR exposes a cross-platform `MSG_PEEK` flag that a user may pass into the `flags` parameter for `recv()`, `recvfrom()`, or `recvmsg()` calls. ``` MSG_PEEK This flag causes the receive operation to return data from the beginning of the receive queue without removing that data from the queue. Thus, a subsequent receive call will return the same data. ``` In short, users may call `recv()` to "peek" at new data (or a message) without consuming it. Recall that in the normal case, calls to `recv()` consume the data from the socket's receive queue, making it unavailable to future calls. #### Motivation This change enables `libc` users, such as the rust standard library, to potentially add peeking functionality to TCP and UDP implementations, like `TcpStream` and `UdpSocket`, without being concerned about the highly platform-dependent nature of the flags. (In this case, the flag's value `0x2` is very consistent, but that is not the case for many of the `MSG_*` flags. It makes sense to keep these differences confined to `libc`) #### Reference [bsd/apple: socket.h](https://opensource.apple.com/source/xnu/xnu-2050.7.9/bsd/sys/socket.h) [bsd/freebsdlike/freebsd: socket.h](https://github.com/freebsd/freebsd/blob/master/sys/sys/socket.h#L418) [bsd/freebsdlike/dragonfly: socket.h](https://github.com/DragonFlyBSD/DragonFlyBSD/blob/1f249c981c4e89e7cde1836a75b61cac36dc7ac5/sys/sys/socket.h#L367) [bsd/netbsdlike: socket.h](https://github.com/IIJ-NetBSD/netbsd-src/blob/af5d253140491f2d1816c59ecb8a4d8a8d927688/sys/sys/socket.h#L517) [unix/haiku: socket.h](https://github.com/haiku/haiku/blob/b65adbdfbc322bb7d86d74049389c688e9962f15/headers/posix/sys/socket.h#L114) [unix/notbsd/linux: socket.h](https://github.com/torvalds/linux/blob/5924bbecd0267d87c24110cbe2041b5075173a25/include/linux/socket.h#L264) [unix/notbsd/android: socket.h](https://android.googlesource.com/platform/development/+/73a5a3baaa5089f1ab2049e5934fa5d8a3f3e76a/ndk/platforms/android-20/include/sys/socket.h#229)
2 parents 95d5534 + b619df8 commit 33ff832

File tree

6 files changed

+7
-0
lines changed

6 files changed

+7
-0
lines changed

src/unix/bsd/apple/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,8 @@ pub const SO_RCVTIMEO: ::c_int = 0x1006;
860860
pub const SO_ERROR: ::c_int = 0x1007;
861861
pub const SO_TYPE: ::c_int = 0x1008;
862862

863+
pub const MSG_PEEK: ::c_int = 0x2;
864+
863865
pub const IFF_LOOPBACK: ::c_int = 0x8;
864866

865867
pub const SHUT_RD: ::c_int = 0;

src/unix/bsd/freebsdlike/dragonfly/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ pub const NOTE_TRACK: ::uint32_t = 0x00000001;
344344
pub const NOTE_TRACKERR: ::uint32_t = 0x00000002;
345345
pub const NOTE_CHILD: ::uint32_t = 0x00000004;
346346

347+
pub const MSG_PEEK: ::c_int = 0x2;
347348
pub const MSG_NOSIGNAL: ::c_int = 0x400;
348349

349350
pub const EMPTY: ::c_short = 0;

src/unix/bsd/freebsdlike/freebsd/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ pub const USER_MAXID: ::c_int = 21;
331331
#[doc(hidden)]
332332
pub const CTL_P1003_1B_MAXID: ::c_int = 26;
333333

334+
pub const MSG_PEEK: ::c_int = 0x2;
334335
pub const MSG_NOSIGNAL: ::c_int = 0x20000;
335336

336337
pub const EMPTY: ::c_short = 0;

src/unix/bsd/netbsdlike/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ pub const SO_RCVLOWAT: ::c_int = 0x1004;
392392
pub const SO_ERROR: ::c_int = 0x1007;
393393
pub const SO_TYPE: ::c_int = 0x1008;
394394

395+
pub const MSG_PEEK: ::c_int = 0x2;
395396
pub const MSG_NOSIGNAL: ::c_int = 0x400;
396397

397398
pub const IFF_LOOPBACK: ::c_int = 0x8;

src/unix/haiku/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ pub const IPV6_V6ONLY: ::c_int = 30;
556556

557557
pub const SO_DEBUG: ::c_int = 0x00000004;
558558

559+
pub const MSG_PEEK: ::c_int = 0x2;
559560
pub const MSG_NOSIGNAL: ::c_int = 0x0800;
560561

561562
pub const SHUT_RD: ::c_int = 0;

src/unix/notbsd/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ pub const IPV6_V6ONLY: ::c_int = 26;
468468

469469
pub const SO_DEBUG: ::c_int = 1;
470470

471+
pub const MSG_PEEK: ::c_int = 0x2;
471472
pub const MSG_NOSIGNAL: ::c_int = 0x4000;
472473

473474
pub const SHUT_RD: ::c_int = 0;

0 commit comments

Comments
 (0)