diff options
Diffstat (limited to 'src/service.c')
| -rw-r--r-- | src/service.c | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/src/service.c b/src/service.c new file mode 100644 index 0000000..9474021 --- /dev/null +++ b/src/service.c | |||
| @@ -0,0 +1,207 @@ | |||
| 1 | /* | ||
| 2 | * service.c | ||
| 3 | * generic service implementation. | ||
| 4 | * | ||
| 5 | * Copyright (c) 2013 Nikias Bassen. All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | #ifdef HAVE_CONFIG_H | ||
| 22 | #include <config.h> | ||
| 23 | #endif | ||
| 24 | #include <stdlib.h> | ||
| 25 | #include <string.h> | ||
| 26 | |||
| 27 | #include "service.h" | ||
| 28 | #include "idevice.h" | ||
| 29 | #include "common/debug.h" | ||
| 30 | |||
| 31 | /** | ||
| 32 | * Convert an idevice_error_t value to an service_error_t value. | ||
| 33 | * Used internally to get correct error codes. | ||
| 34 | * | ||
| 35 | * @param err An idevice_error_t error code | ||
| 36 | * | ||
| 37 | * @return A matching service_error_t error code, | ||
| 38 | * SERVICE_E_UNKNOWN_ERROR otherwise. | ||
| 39 | */ | ||
| 40 | static service_error_t idevice_to_service_error(idevice_error_t err) | ||
| 41 | { | ||
| 42 | switch (err) { | ||
| 43 | case IDEVICE_E_SUCCESS: | ||
| 44 | return SERVICE_E_SUCCESS; | ||
| 45 | case IDEVICE_E_INVALID_ARG: | ||
| 46 | return SERVICE_E_INVALID_ARG; | ||
| 47 | case IDEVICE_E_SSL_ERROR: | ||
| 48 | return SERVICE_E_SSL_ERROR; | ||
| 49 | case IDEVICE_E_NOT_ENOUGH_DATA: | ||
| 50 | return SERVICE_E_NOT_ENOUGH_DATA; | ||
| 51 | case IDEVICE_E_TIMEOUT: | ||
| 52 | return SERVICE_E_TIMEOUT; | ||
| 53 | default: | ||
| 54 | break; | ||
| 55 | } | ||
| 56 | return SERVICE_E_UNKNOWN_ERROR; | ||
| 57 | } | ||
| 58 | |||
| 59 | service_error_t service_client_new(idevice_t device, lockdownd_service_descriptor_t service, service_client_t *client) | ||
| 60 | { | ||
| 61 | if (!device || !service || service->port == 0 || !client || *client) | ||
| 62 | return SERVICE_E_INVALID_ARG; | ||
| 63 | |||
| 64 | /* Attempt connection */ | ||
| 65 | idevice_connection_t connection = NULL; | ||
| 66 | if (idevice_connect(device, service->port, &connection) != IDEVICE_E_SUCCESS) { | ||
| 67 | return SERVICE_E_MUX_ERROR; | ||
| 68 | } | ||
| 69 | |||
| 70 | /* create client object */ | ||
| 71 | service_client_t client_loc = (service_client_t)malloc(sizeof(struct service_client_private)); | ||
| 72 | client_loc->connection = connection; | ||
| 73 | |||
| 74 | /* enable SSL if requested */ | ||
| 75 | if (service->ssl_enabled == 1) | ||
| 76 | service_enable_ssl(client_loc); | ||
| 77 | |||
| 78 | /* all done, return success */ | ||
| 79 | *client = client_loc; | ||
| 80 | return SERVICE_E_SUCCESS; | ||
| 81 | } | ||
| 82 | |||
| 83 | service_error_t service_client_factory_start_service(idevice_t device, const char* service_name, void **client, const char* label, int32_t (*constructor_func)(idevice_t, lockdownd_service_descriptor_t, void**), int32_t *error_code) | ||
| 84 | { | ||
| 85 | *client = NULL; | ||
| 86 | |||
| 87 | lockdownd_client_t lckd = NULL; | ||
| 88 | if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &lckd, label)) { | ||
| 89 | debug_info("Could not create a lockdown client."); | ||
| 90 | return SERVICE_E_START_SERVICE_ERROR; | ||
| 91 | } | ||
| 92 | |||
| 93 | lockdownd_service_descriptor_t service = NULL; | ||
| 94 | lockdownd_error_t lerr = lockdownd_start_service(lckd, service_name, &service); | ||
| 95 | lockdownd_client_free(lckd); | ||
| 96 | |||
| 97 | if (lerr != LOCKDOWN_E_SUCCESS) { | ||
| 98 | debug_info("Could not start service %s: %s", service_name, lockdownd_strerror(lerr)); | ||
| 99 | return SERVICE_E_START_SERVICE_ERROR; | ||
| 100 | } | ||
| 101 | |||
| 102 | int32_t ec; | ||
| 103 | if (constructor_func) { | ||
| 104 | ec = (int32_t)constructor_func(device, service, client); | ||
| 105 | } else { | ||
| 106 | ec = service_client_new(device, service, (service_client_t*)client); | ||
| 107 | } | ||
| 108 | if (error_code) { | ||
| 109 | *error_code = ec; | ||
| 110 | } | ||
| 111 | |||
| 112 | if (ec != SERVICE_E_SUCCESS) { | ||
| 113 | debug_info("Could not connect to service %s! Port: %i, error: %i", service_name, service->port, ec); | ||
| 114 | } | ||
| 115 | |||
| 116 | lockdownd_service_descriptor_free(service); | ||
| 117 | service = NULL; | ||
| 118 | |||
| 119 | return (ec == SERVICE_E_SUCCESS) ? SERVICE_E_SUCCESS : SERVICE_E_START_SERVICE_ERROR; | ||
| 120 | } | ||
| 121 | |||
| 122 | service_error_t service_client_free(service_client_t client) | ||
| 123 | { | ||
| 124 | if (!client) | ||
| 125 | return SERVICE_E_INVALID_ARG; | ||
| 126 | |||
| 127 | service_error_t err = idevice_to_service_error(idevice_disconnect(client->connection)); | ||
| 128 | |||
| 129 | free(client); | ||
| 130 | client = NULL; | ||
| 131 | |||
| 132 | return err; | ||
| 133 | } | ||
| 134 | |||
| 135 | service_error_t service_send(service_client_t client, const char* data, uint32_t size, uint32_t *sent) | ||
| 136 | { | ||
| 137 | service_error_t res = SERVICE_E_UNKNOWN_ERROR; | ||
| 138 | uint32_t bytes = 0; | ||
| 139 | |||
| 140 | if (!client || (client && !client->connection) || !data || (size == 0)) { | ||
| 141 | return SERVICE_E_INVALID_ARG; | ||
| 142 | } | ||
| 143 | |||
| 144 | debug_info("sending %d bytes", size); | ||
| 145 | res = idevice_to_service_error(idevice_connection_send(client->connection, data, size, &bytes)); | ||
| 146 | if (res != SERVICE_E_SUCCESS) { | ||
| 147 | debug_info("ERROR: sending to device failed."); | ||
| 148 | } | ||
| 149 | if (sent) { | ||
| 150 | *sent = bytes; | ||
| 151 | } | ||
| 152 | |||
| 153 | return res; | ||
| 154 | } | ||
| 155 | |||
| 156 | service_error_t service_receive_with_timeout(service_client_t client, char* data, uint32_t size, uint32_t *received, unsigned int timeout) | ||
| 157 | { | ||
| 158 | service_error_t res = SERVICE_E_UNKNOWN_ERROR; | ||
| 159 | uint32_t bytes = 0; | ||
| 160 | |||
| 161 | if (!client || (client && !client->connection) || !data || (size == 0)) { | ||
| 162 | return SERVICE_E_INVALID_ARG; | ||
| 163 | } | ||
| 164 | |||
| 165 | res = idevice_to_service_error(idevice_connection_receive_timeout(client->connection, data, size, &bytes, timeout)); | ||
| 166 | if (res != SERVICE_E_SUCCESS && res != SERVICE_E_TIMEOUT) { | ||
| 167 | debug_info("could not read data"); | ||
| 168 | return res; | ||
| 169 | } | ||
| 170 | if (received) { | ||
| 171 | *received = bytes; | ||
| 172 | } | ||
| 173 | |||
| 174 | return res; | ||
| 175 | } | ||
| 176 | |||
| 177 | service_error_t service_receive(service_client_t client, char* data, uint32_t size, uint32_t *received) | ||
| 178 | { | ||
| 179 | return service_receive_with_timeout(client, data, size, received, 30000); | ||
| 180 | } | ||
| 181 | |||
| 182 | service_error_t service_enable_ssl(service_client_t client) | ||
| 183 | { | ||
| 184 | if (!client || !client->connection) | ||
| 185 | return SERVICE_E_INVALID_ARG; | ||
| 186 | return idevice_to_service_error(idevice_connection_enable_ssl(client->connection)); | ||
| 187 | } | ||
| 188 | |||
| 189 | service_error_t service_disable_ssl(service_client_t client) | ||
| 190 | { | ||
| 191 | return service_disable_bypass_ssl(client, 0); | ||
| 192 | } | ||
| 193 | |||
| 194 | service_error_t service_disable_bypass_ssl(service_client_t client, uint8_t sslBypass) | ||
| 195 | { | ||
| 196 | if (!client || !client->connection) | ||
| 197 | return SERVICE_E_INVALID_ARG; | ||
| 198 | return idevice_to_service_error(idevice_connection_disable_bypass_ssl(client->connection, sslBypass)); | ||
| 199 | } | ||
| 200 | |||
| 201 | service_error_t service_get_connection(service_client_t client, idevice_connection_t *connection) | ||
| 202 | { | ||
| 203 | if (!client || !client->connection || !connection) | ||
| 204 | return SERVICE_E_INVALID_ARG; | ||
| 205 | *connection = client->connection; | ||
| 206 | return SERVICE_E_SUCCESS; | ||
| 207 | } | ||
