diff options
Diffstat (limited to 'include/libimobiledevice/reverse_proxy.h')
-rw-r--r-- | include/libimobiledevice/reverse_proxy.h | 213 |
1 files changed, 213 insertions, 0 deletions
diff --git a/include/libimobiledevice/reverse_proxy.h b/include/libimobiledevice/reverse_proxy.h new file mode 100644 index 0000000..5e2f54b --- /dev/null +++ b/include/libimobiledevice/reverse_proxy.h | |||
@@ -0,0 +1,213 @@ | |||
1 | /** | ||
2 | * @file libimobiledevice/reverse_proxy.h | ||
3 | * @brief Provide a reverse proxy to allow the device to communicate through, | ||
4 | * which is used during firmware restore. | ||
5 | * \internal | ||
6 | * | ||
7 | * Copyright (c) 2021 Nikias Bassen, All Rights Reserved. | ||
8 | * | ||
9 | * This library is free software; you can redistribute it and/or | ||
10 | * modify it under the terms of the GNU Lesser General Public | ||
11 | * License as published by the Free Software Foundation; either | ||
12 | * version 2.1 of the License, or (at your option) any later version. | ||
13 | * | ||
14 | * This library is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * Lesser General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU Lesser General Public | ||
20 | * License along with this library; if not, write to the Free Software | ||
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
22 | */ | ||
23 | |||
24 | #ifndef IREVERSE_PROXY_H | ||
25 | #define IREVERSE_PROXY_H | ||
26 | |||
27 | #ifdef __cplusplus | ||
28 | extern "C" { | ||
29 | #endif | ||
30 | |||
31 | #include <libimobiledevice/libimobiledevice.h> | ||
32 | |||
33 | #define REVERSE_PROXY_DEFAULT_PORT 1082 /**< default port the reverse proxy is listening on */ | ||
34 | |||
35 | /** Error Codes */ | ||
36 | typedef enum { | ||
37 | REVERSE_PROXY_E_SUCCESS = 0, | ||
38 | REVERSE_PROXY_E_INVALID_ARG = -1, | ||
39 | REVERSE_PROXY_E_PLIST_ERROR = -2, | ||
40 | REVERSE_PROXY_E_MUX_ERROR = -3, | ||
41 | REVERSE_PROXY_E_SSL_ERROR = -4, | ||
42 | REVERSE_PROXY_E_NOT_ENOUGH_DATA = -5, | ||
43 | REVERSE_PROXY_E_TIMEOUT = -6, | ||
44 | REVERSE_PROXY_E_UNKNOWN_ERROR = -256 | ||
45 | } reverse_proxy_error_t; | ||
46 | |||
47 | typedef struct reverse_proxy_client_private reverse_proxy_client_private; /**< \private */ | ||
48 | typedef reverse_proxy_client_private *reverse_proxy_client_t; /**< The client handle. */ | ||
49 | |||
50 | /** reverse proxy client type */ | ||
51 | typedef enum { | ||
52 | RP_TYPE_CTRL = 1, /**< control connection */ | ||
53 | RP_TYPE_CONN /**< proxy connection */ | ||
54 | } reverse_proxy_client_type_t; | ||
55 | |||
56 | /** reverse proxy status for reverse_proxy_status_cb_t callback */ | ||
57 | typedef enum { | ||
58 | RP_STATUS_READY = 1, /**< proxy is ready */ | ||
59 | RP_STATUS_TERMINATE, /**< proxy terminated */ | ||
60 | RP_STATUS_CONNECT_REQ, /**< connection request received (only RP_TYPE_CTRL) */ | ||
61 | RP_STATUS_SHUTDOWN_REQ, /**< shutdown request received (only RP_TYPE_CTRL) */ | ||
62 | RP_STATUS_CONNECTED, /**< connection established (only RP_TYPE_CONN) */ | ||
63 | RP_STATUS_DISCONNECTED, /**< connection closed (only RP_TYPE_CONN) */ | ||
64 | } reverse_proxy_status_t; | ||
65 | |||
66 | /** reverse proxy data direction passed to reverse_proxy_data_cb_t callback */ | ||
67 | typedef enum { | ||
68 | RP_DATA_DIRECTION_OUT = 1, /**< data going out to remote host */ | ||
69 | RP_DATA_DIRECTION_IN /**< data coming in from remote host */ | ||
70 | } reverse_proxy_data_direction_t; | ||
71 | |||
72 | /** | ||
73 | * Log callback function prototype. | ||
74 | * | ||
75 | * @param client The client that called the callback function | ||
76 | * @param log_msg The log message | ||
77 | * @param user_data The user_data pointer that was set when registering the callback | ||
78 | */ | ||
79 | typedef void (*reverse_proxy_log_cb_t) (reverse_proxy_client_t client, const char* log_msg, void* user_data); | ||
80 | |||
81 | /** | ||
82 | * Data callback function prototype. | ||
83 | * | ||
84 | * @param client The client that called the callback function | ||
85 | * @param direction The direction of the data, either RP_DATA_DIRECTION_OUT or RP_DATA_DIRECTION_IN | ||
86 | * @param buffer The data buffer | ||
87 | * @param length The length of the data buffer | ||
88 | * @param user_data The user_data pointer that was set when registering the callback | ||
89 | */ | ||
90 | typedef void (*reverse_proxy_data_cb_t) (reverse_proxy_client_t client, reverse_proxy_data_direction_t direction, const char* buffer, uint32_t length, void* user_data); | ||
91 | |||
92 | /** | ||
93 | * Status callback function prototype. | ||
94 | * | ||
95 | * @param client The client that called the callback function | ||
96 | * @param status The status the client is reporting | ||
97 | * @param status_msg A status message the client reports along with the status | ||
98 | * @param user_data The user_data pointer that was set when registering the callback | ||
99 | */ | ||
100 | typedef void (*reverse_proxy_status_cb_t) (reverse_proxy_client_t client, reverse_proxy_status_t status, const char* status_msg, void* user_data); | ||
101 | |||
102 | /** | ||
103 | * Create a reverse proxy client using com.apple.PurpleReverseProxy.Ctrl and | ||
104 | * com.apple.PurpleReverseProxy.Conn lockdown services. This will open a port | ||
105 | * 1083 on the device that iOS apps could connect to; \b however that is | ||
106 | * only allowed if an app has the com.apple.private.PurpleReverseProxy.allowed | ||
107 | * entitlement, which currently only \c /usr/libexec/fdrhelper holds. | ||
108 | * | ||
109 | * @note This function only creates and initializes the reverse proxy; | ||
110 | * to make it operational, call reverse_proxy_client_start_proxy(). | ||
111 | * | ||
112 | * @param device The device to connect to. | ||
113 | * @param client Pointer that will be set to a newly allocated #reverse_proxy_client_t | ||
114 | * upon successful return. | ||
115 | * @param label A label to pass to lockdownd when creating the service | ||
116 | * connections, usually the program name. | ||
117 | * | ||
118 | * @return REVERSE_PROXY_E_SUCCESS on success, | ||
119 | * or a REVERSE_PROXY_E_* error code otherwise. | ||
120 | */ | ||
121 | LIBIMOBILEDEVICE_API reverse_proxy_error_t reverse_proxy_client_create_with_service(idevice_t device, reverse_proxy_client_t* client, const char* label); | ||
122 | |||
123 | /** | ||
124 | * Create a reverse proxy client using an open port on the device. This is | ||
125 | * used during firmware restores with the default port REVERSE_PROXY_DEFAULT_PORT (1082). | ||
126 | * | ||
127 | * @note This function only creates and initializes the reverse proxy; | ||
128 | * to make it operational, call reverse_proxy_client_start_proxy(). | ||
129 | * | ||
130 | * @param device The device to connect to. | ||
131 | * @param client Pointer that will be set to a newly allocated reverse_proxy_client_t | ||
132 | * upon successful return. | ||
133 | * @param device_port An open port on the device. Unless it's being used for | ||
134 | * a custom implementation, pass REVERSE_PROXY_DEFAULT_PORT here. | ||
135 | * | ||
136 | * @return REVERSE_PROXY_E_SUCCESS on success, | ||
137 | * or a REVERSE_PROXY_E_* error code otherwise. | ||
138 | */ | ||
139 | LIBIMOBILEDEVICE_API reverse_proxy_error_t reverse_proxy_client_create_with_port(idevice_t device, reverse_proxy_client_t* client, uint16_t device_port); | ||
140 | |||
141 | /** | ||
142 | * Disconnects a reverse proxy client and frees up the client data. | ||
143 | * | ||
144 | * @param client The reverse proxy client to disconnect and free. | ||
145 | */ | ||
146 | LIBIMOBILEDEVICE_API reverse_proxy_error_t reverse_proxy_client_free(reverse_proxy_client_t client); | ||
147 | |||
148 | /** | ||
149 | * Make an initialized reverse proxy client operational, i.e. start the actual proxy. | ||
150 | * | ||
151 | * @param client The reverse proxy client to start. | ||
152 | * @param control_protocol_version The control protocol version to use. | ||
153 | * This is either 1 or 2. Recent devices use 2. | ||
154 | * | ||
155 | * @return REVERSE_PROXY_E_SUCCESS on success, | ||
156 | * or a REVERSE_PROXY_E_* error code otherwise. | ||
157 | */ | ||
158 | LIBIMOBILEDEVICE_API reverse_proxy_error_t reverse_proxy_client_start_proxy(reverse_proxy_client_t client, int control_protocol_version); | ||
159 | |||
160 | /** | ||
161 | * Set a status callback function. This allows to report the status of the | ||
162 | * reverse proxy, like Ready, Connect Request, Connected, etc. | ||
163 | * | ||
164 | * @note Set the callback before calling reverse_proxy_client_start_proxy(). | ||
165 | * | ||
166 | * @param client The reverse proxy client | ||
167 | * @param callback The status callback function that will be called | ||
168 | * when the status of the reverse proxy changes. | ||
169 | * @param user_data A pointer that will be passed to the callback function. | ||
170 | */ | ||
171 | LIBIMOBILEDEVICE_API void reverse_proxy_client_set_status_callback(reverse_proxy_client_t client, reverse_proxy_status_cb_t callback, void* user_data); | ||
172 | |||
173 | /** | ||
174 | * Set a log callback function. Useful for debugging or verbosity. | ||
175 | * | ||
176 | * @note Set the callback before calling reverse_proxy_client_start_proxy(). | ||
177 | * | ||
178 | * @param client The reverse proxy client | ||
179 | * @param callback The log callback function that will be called | ||
180 | * when the reverse proxy logs something. | ||
181 | * @param user_data A pointer that will be passed to the callback function. | ||
182 | */ | ||
183 | LIBIMOBILEDEVICE_API void reverse_proxy_client_set_log_callback(reverse_proxy_client_t client, reverse_proxy_log_cb_t callback, void* user_data); | ||
184 | |||
185 | /** | ||
186 | * Set a data callback function. Useful for debugging or extra verbosity. | ||
187 | * | ||
188 | * @note Set the callback before calling reverse_proxy_client_start_proxy(). | ||
189 | * | ||
190 | * @param client The reverse proxy client | ||
191 | * @param callback The status callback function that will be called | ||
192 | * when the status of the reverse proxy changes. | ||
193 | * @param user_data A pointer that will be passed to the callback function. | ||
194 | */ | ||
195 | |||
196 | LIBIMOBILEDEVICE_API void reverse_proxy_client_set_data_callback(reverse_proxy_client_t client, reverse_proxy_data_cb_t callback, void* user_data); | ||
197 | |||
198 | /** | ||
199 | * Helper function to return the type of a given reverse proxy client, which | ||
200 | * is either RP_TYPE_CTRL or RP_TYPE_CONN. Useful for callback functions. | ||
201 | * @see reverse_proxy_client_type_t | ||
202 | * | ||
203 | * @param client The reverse proxy client | ||
204 | * | ||
205 | * @return The type of the rerverse proxy client | ||
206 | */ | ||
207 | LIBIMOBILEDEVICE_API reverse_proxy_client_type_t reverse_proxy_get_type(reverse_proxy_client_t client); | ||
208 | |||
209 | #ifdef __cplusplus | ||
210 | } | ||
211 | #endif | ||
212 | |||
213 | #endif | ||