idevice: Add usbmux device id (handle/mux id) to internal data structure
diff --git a/include/libimobiledevice/libimobiledevice.h b/include/libimobiledevice/libimobiledevice.h
index 68eb511..5ec1a6d 100644
--- a/include/libimobiledevice/libimobiledevice.h
+++ b/include/libimobiledevice/libimobiledevice.h
@@ -253,7 +253,7 @@
 /* misc */
 
 /**
- * Gets the handle of the device. Depends on the connection type.
+ * Gets the handle or (usbmux device id) of the device.
  */
 idevice_error_t idevice_get_handle(idevice_t device, uint32_t *handle);
 
diff --git a/src/idevice.c b/src/idevice.c
index ead9b86..cb9bb5c 100644
--- a/src/idevice.c
+++ b/src/idevice.c
@@ -254,8 +254,9 @@
 	if (res > 0) {
 		idevice_t dev = (idevice_t) malloc(sizeof(struct idevice_private));
 		dev->udid = strdup(muxdev.udid);
+		dev->mux_id = muxdev.handle;
 		dev->conn_type = CONNECTION_USBMUXD;
-		dev->conn_data = (void*)(long)muxdev.handle;
+		dev->conn_data = NULL;
 		dev->version = 0;
 		*device = dev;
 		return IDEVICE_E_SUCCESS;
@@ -275,9 +276,6 @@
 
 	free(device->udid);
 
-	if (device->conn_type == CONNECTION_USBMUXD) {
-		device->conn_data = 0;
-	}
 	if (device->conn_data) {
 		free(device->conn_data);
 	}
@@ -292,7 +290,7 @@
 	}
 
 	if (device->conn_type == CONNECTION_USBMUXD) {
-		int sfd = usbmuxd_connect((uint32_t)(long)device->conn_data, port);
+		int sfd = usbmuxd_connect(device->mux_id, port);
 		if (sfd < 0) {
 			debug_info("ERROR: Connecting to usbmuxd failed: %d (%s)", sfd, strerror(-sfd));
 			return IDEVICE_E_UNKNOWN_ERROR;
@@ -502,16 +500,11 @@
 
 LIBIMOBILEDEVICE_API idevice_error_t idevice_get_handle(idevice_t device, uint32_t *handle)
 {
-	if (!device)
+	if (!device || !handle)
 		return IDEVICE_E_INVALID_ARG;
 
-	if (device->conn_type == CONNECTION_USBMUXD) {
-		*handle = (uint32_t)(long)device->conn_data;
-		return IDEVICE_E_SUCCESS;
-	} else {
-		debug_info("Unknown connection type %d", device->conn_type);
-	}
-	return IDEVICE_E_UNKNOWN_ERROR;
+	*handle = device->mux_id;
+	return IDEVICE_E_SUCCESS;
 }
 
 LIBIMOBILEDEVICE_API idevice_error_t idevice_get_udid(idevice_t device, char **udid)
diff --git a/src/idevice.h b/src/idevice.h
index e46a7e5..94e828b 100644
--- a/src/idevice.h
+++ b/src/idevice.h
@@ -74,6 +74,7 @@
 
 struct idevice_private {
 	char *udid;
+	uint32_t mux_id;
 	enum connection_type conn_type;
 	void *conn_data;
 	int version;