Remi NGUYEN VAN | fb70eba | 2022-04-04 20:26:16 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef LIBCONNECTIVITY_CONNECTIVITY_NATIVE_H_ |
| 18 | #define LIBCONNECTIVITY_CONNECTIVITY_NATIVE_H_ |
| 19 | |
| 20 | #include <sys/cdefs.h> |
| 21 | #include <netinet/in.h> |
| 22 | |
Remi NGUYEN VAN | fb70eba | 2022-04-04 20:26:16 +0900 | [diff] [blame] | 23 | __BEGIN_DECLS |
| 24 | |
| 25 | /** |
| 26 | * Blocks a port from being assigned during bind(). The caller is responsible for updating |
| 27 | * /proc/sys/net/ipv4/ip_local_port_range with the port being blocked so that calls to connect() |
| 28 | * will not automatically assign one of the blocked ports. |
| 29 | * Will return success even if port was already blocked. |
| 30 | * |
| 31 | * Returns 0 on success, or a POSIX error code (see errno.h) on failure: |
| 32 | * - EINVAL for invalid port number |
| 33 | * - EPERM if the UID of the client doesn't have network stack permission |
| 34 | * - Other errors as per https://man7.org/linux/man-pages/man2/bpf.2.html |
| 35 | * |
| 36 | * @param port Int corresponding to port number. |
| 37 | */ |
Maciej Żenczykowski | 6796ce3 | 2025-02-18 15:06:43 -0800 | [diff] [blame] | 38 | int AConnectivityNative_blockPortForBind(in_port_t port) __INTRODUCED_IN(34); |
Remi NGUYEN VAN | fb70eba | 2022-04-04 20:26:16 +0900 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Unblocks a port that has previously been blocked. |
| 42 | * Will return success even if port was already unblocked. |
| 43 | * |
| 44 | * Returns 0 on success, or a POSIX error code (see errno.h) on failure: |
| 45 | * - EINVAL for invalid port number |
| 46 | * - EPERM if the UID of the client doesn't have network stack permission |
| 47 | * - Other errors as per https://man7.org/linux/man-pages/man2/bpf.2.html |
| 48 | * |
| 49 | * @param port Int corresponding to port number. |
| 50 | */ |
Maciej Żenczykowski | 6796ce3 | 2025-02-18 15:06:43 -0800 | [diff] [blame] | 51 | int AConnectivityNative_unblockPortForBind(in_port_t port) __INTRODUCED_IN(34); |
Remi NGUYEN VAN | fb70eba | 2022-04-04 20:26:16 +0900 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Unblocks all ports that have previously been blocked. |
| 55 | * |
| 56 | * Returns 0 on success, or a POSIX error code (see errno.h) on failure: |
| 57 | * - EINVAL for invalid port number |
| 58 | * - EPERM if the UID of the client doesn't have network stack permission |
| 59 | * - Other errors as per https://man7.org/linux/man-pages/man2/bpf.2.html |
| 60 | */ |
Maciej Żenczykowski | 6796ce3 | 2025-02-18 15:06:43 -0800 | [diff] [blame] | 61 | int AConnectivityNative_unblockAllPortsForBind() __INTRODUCED_IN(34); |
Remi NGUYEN VAN | fb70eba | 2022-04-04 20:26:16 +0900 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Gets the list of ports that have been blocked. |
| 65 | * |
| 66 | * Returns 0 on success, or a POSIX error code (see errno.h) on failure: |
| 67 | * - EINVAL for invalid port number |
| 68 | * - EPERM if the UID of the client doesn't have network stack permission |
| 69 | * - Other errors as per https://man7.org/linux/man-pages/man2/bpf.2.html |
| 70 | * |
| 71 | * @param ports Array of ports that will be filled with the port numbers. |
| 72 | * @param count Pointer to the size of the ports array; the value will be set to the total number of |
| 73 | * blocked ports, which may be larger than the ports array that was filled. |
| 74 | */ |
Tyler Wear | 98401e1 | 2022-11-15 13:44:04 -0800 | [diff] [blame] | 75 | int AConnectivityNative_getPortsBlockedForBind(in_port_t* _Nonnull ports, size_t* _Nonnull count) |
Maciej Żenczykowski | 6796ce3 | 2025-02-18 15:06:43 -0800 | [diff] [blame] | 76 | __INTRODUCED_IN(34); |
Remi NGUYEN VAN | fb70eba | 2022-04-04 20:26:16 +0900 | [diff] [blame] | 77 | |
| 78 | __END_DECLS |
| 79 | |
| 80 | |
| 81 | #endif |