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