]>
Commit | Line | Data |
---|---|---|
84814e2a | 1 | /* Helper functions for translating between O_* and SOCK_* flags. |
04277e02 | 2 | Copyright (C) 2008-2019 Free Software Foundation, Inc. |
84814e2a TS |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
17 | <https://www.gnu.org/licenses/>. */ | |
18 | ||
19 | ||
20 | #include <fcntl.h> | |
21 | #include <sys/socket.h> | |
22 | ||
23 | /* Do some compile-time checks for the SOCK_* constants, which we rely on. */ | |
24 | _Static_assert (SOCK_CLOEXEC == O_CLOEXEC, | |
25 | "SOCK_CLOEXEC is assumed to be the same as O_CLOEXEC"); | |
26 | _Static_assert (((SOCK_MAX - 1) | SOCK_TYPE_MASK) == SOCK_TYPE_MASK, | |
27 | "SOCK_TYPE_MASK must contain SOCK_MAX - 1"); | |
28 | _Static_assert ((SOCK_CLOEXEC & SOCK_TYPE_MASK) == 0, | |
29 | "SOCK_TYPE_MASK must not contain SOCK_CLOEXEC"); | |
30 | _Static_assert ((SOCK_NONBLOCK & SOCK_TYPE_MASK) == 0, | |
31 | "SOCK_TYPE_MASK must not contain SOCK_NONBLOCK"); | |
32 | ||
33 | ||
34 | /* Convert from SOCK_* flags to O_* flags. */ | |
35 | __extern_always_inline | |
36 | int | |
37 | sock_to_o_flags (int in) | |
38 | { | |
39 | int out = 0; | |
40 | ||
41 | if (in & SOCK_NONBLOCK) | |
42 | out |= O_NONBLOCK; | |
43 | /* Others are passed through unfiltered. */ | |
44 | out |= in & ~(SOCK_NONBLOCK); | |
45 | ||
46 | return out; | |
47 | } | |
48 | ||
49 | /* Convert from O_* flags to SOCK_* flags. */ | |
50 | __extern_always_inline | |
51 | int | |
52 | o_to_sock_flags (int in) | |
53 | { | |
54 | int out = 0; | |
55 | ||
56 | if (in & O_NONBLOCK) | |
57 | out |= SOCK_NONBLOCK; | |
58 | /* Others are passed through unfiltered. */ | |
59 | out |= in & ~(O_NONBLOCK); | |
60 | ||
61 | return out; | |
62 | } |