Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
L
libplist
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pwn
libplist
Commits
ad65cefa
Commit
ad65cefa
authored
Aug 19, 2008
by
Jonathan Beck
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
migrate usbmux.c.
parent
21d5d4ac
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
105 additions
and
102 deletions
+105
-102
libiphone.h
include/libiphone/libiphone.h
+9
-8
iphone.c
src/iphone.c
+1
-2
lockdown.c
src/lockdown.c
+3
-3
lockdown.h
src/lockdown.h
+1
-1
usbmux.c
src/usbmux.c
+89
-81
usbmux.h
src/usbmux.h
+2
-7
No files found.
include/libiphone/libiphone.h
View file @
ad65cefa
...
@@ -32,18 +32,19 @@ extern "C" {
...
@@ -32,18 +32,19 @@ extern "C" {
//general errors
//general errors
#define IPHONE_E_SUCCESS 0
#define IPHONE_E_SUCCESS 0
#define IPHONE_E_INVALID_ARG -1
#define IPHONE_E_INVALID_ARG -1
#define IPHONE_E_NO_DEVICE -2
#define IPHONE_E_UNKNOWN_ERROR -2
#define IPHONE_E_TIMEOUT -3
#define IPHONE_E_NO_DEVICE -3
#define IPHONE_E_NOT_ENOUGH_DATA -4
#define IPHONE_E_TIMEOUT -4
#define IPHONE_E_BAD_HEADER -5
#define IPHONE_E_NOT_ENOUGH_DATA -5
#define IPHONE_E_BAD_HEADER -6
//lockdownd specific error
//lockdownd specific error
#define IPHONE_E_INVALID_CONF -
6
#define IPHONE_E_INVALID_CONF -
7
#define IPHONE_E_PAIRING_FAILED -
7
#define IPHONE_E_PAIRING_FAILED -
8
#define IPHONE_E_SSL_ERROR -
8
#define IPHONE_E_SSL_ERROR -
9
//afc specific error
//afc specific error
#define IPHONE_E_NO_SUCH_FILE -
9
#define IPHONE_E_NO_SUCH_FILE -
10
struct
iphone_device_int
;
struct
iphone_device_int
;
typedef
struct
iphone_device_int
*
iphone_device_t
;
typedef
struct
iphone_device_int
*
iphone_device_t
;
...
...
src/iphone.c
View file @
ad65cefa
...
@@ -42,7 +42,6 @@ int iphone_get_device ( iphone_device_t *device ){
...
@@ -42,7 +42,6 @@ int iphone_get_device ( iphone_device_t *device ){
struct
usb_bus
*
bus
,
*
busses
;
struct
usb_bus
*
bus
,
*
busses
;
struct
usb_device
*
dev
;
struct
usb_device
*
dev
;
iphone_device_t
phone
=
(
iphone_device_t
)
malloc
(
sizeof
(
struct
iphone_device_int
));
iphone_device_t
phone
=
(
iphone_device_t
)
malloc
(
sizeof
(
struct
iphone_device_int
));
usbmux_version_header
*
version
=
version_header
();
// Initialize the struct
// Initialize the struct
phone
->
device
=
NULL
;
phone
->
device
=
NULL
;
...
@@ -126,7 +125,7 @@ int iphone_get_device ( iphone_device_t *device ){
...
@@ -126,7 +125,7 @@ int iphone_get_device ( iphone_device_t *device ){
if
(
debug
)
fprintf
(
stderr
,
"get_iPhone(): Unknown error.
\n
"
);
if
(
debug
)
fprintf
(
stderr
,
"get_iPhone(): Unknown error.
\n
"
);
free_iPhone
(
phone
);
free_iPhone
(
phone
);
free
(
version
);
free
(
version
);
return
IPHONE_E_
NO_DEVICE
;
// if it got to this point it's gotta be bad
return
IPHONE_E_
UNKNOWN_ERROR
;
// if it got to this point it's gotta be bad
}
}
/** Cleans up an iPhone structure, then frees the structure itself.
/** Cleans up an iPhone structure, then frees the structure itself.
...
...
src/lockdown.c
View file @
ad65cefa
...
@@ -77,8 +77,8 @@ char *lockdownd_generate_hostid() {
...
@@ -77,8 +77,8 @@ char *lockdownd_generate_hostid() {
iphone_lckd_client_t
new_lockdownd_client
(
iphone_device_t
phone
)
{
iphone_lckd_client_t
new_lockdownd_client
(
iphone_device_t
phone
)
{
if
(
!
phone
)
return
NULL
;
if
(
!
phone
)
return
NULL
;
iphone_lckd_client_t
control
=
(
iphone_lckd_client_t
)
malloc
(
sizeof
(
struct
iphone_lckd_client_int
));
iphone_lckd_client_t
control
=
(
iphone_lckd_client_t
)
malloc
(
sizeof
(
struct
iphone_lckd_client_int
));
control
->
connection
=
mux_connect
(
phone
,
0x0a00
,
0xf27e
);
if
(
!
control
->
connection
)
{
if
(
IPHONE_E_SUCCESS
!=
iphone_mux_new_client
(
phone
,
0x0a00
,
0xf27e
,
&
control
->
connection
)
)
{
free
(
control
);
free
(
control
);
return
NULL
;
return
NULL
;
}
}
...
@@ -100,7 +100,7 @@ void iphone_lckd_free_client( iphone_lckd_client_t client ) {
...
@@ -100,7 +100,7 @@ void iphone_lckd_free_client( iphone_lckd_client_t client ) {
mux_close_connection
(
client
->
connection
);
mux_close_connection
(
client
->
connection
);
}
}
if
(
client
->
ssl_session
)
gnutls_deinit
(
*
c
ontrol
->
ssl_session
);
if
(
client
->
ssl_session
)
gnutls_deinit
(
*
c
lient
->
ssl_session
);
free
(
client
->
ssl_session
);
free
(
client
->
ssl_session
);
free
(
client
);
free
(
client
);
}
}
...
...
src/lockdown.h
View file @
ad65cefa
...
@@ -33,7 +33,7 @@
...
@@ -33,7 +33,7 @@
struct
iphone_lckd_client_int
{
struct
iphone_lckd_client_int
{
usbmux_connection
*
connection
;
iphone_umux_client_t
connection
;
gnutls_session_t
*
ssl_session
;
gnutls_session_t
*
ssl_session
;
int
in_SSL
;
int
in_SSL
;
char
*
gtls_buffer_hack
;
char
*
gtls_buffer_hack
;
...
...
src/usbmux.c
View file @
ad65cefa
...
@@ -29,8 +29,8 @@
...
@@ -29,8 +29,8 @@
extern
int
debug
;
extern
int
debug
;
static
usbmux_connection
*
*
connlist
=
NULL
;
static
iphone_umux_client_t
*
connlist
=
NULL
;
static
int
c
onnection
s
=
0
;
static
int
c
lient
s
=
0
;
/** Creates a USBMux packet for the given set of ports.
/** Creates a USBMux packet for the given set of ports.
*
*
...
@@ -76,10 +76,10 @@ usbmux_version_header *version_header() {
...
@@ -76,10 +76,10 @@ usbmux_version_header *version_header() {
*
*
* @param connection The connection to delete from the tracking list.
* @param connection The connection to delete from the tracking list.
*/
*/
void
delete_connection
(
usbmux_connection
*
connection
)
{
void
delete_connection
(
iphone_umux_client_t
connection
)
{
usbmux_connection
**
newlist
=
(
usbmux_connection
**
)
malloc
(
sizeof
(
usbmux_connection
*
)
*
(
connection
s
-
1
));
iphone_umux_client_t
*
newlist
=
(
iphone_umux_client_t
*
)
malloc
(
sizeof
(
iphone_umux_client_t
)
*
(
client
s
-
1
));
int
i
=
0
,
j
=
0
;
int
i
=
0
,
j
=
0
;
for
(
i
=
0
;
i
<
c
onnection
s
;
i
++
)
{
for
(
i
=
0
;
i
<
c
lient
s
;
i
++
)
{
if
(
connlist
[
i
]
==
connection
)
continue
;
if
(
connlist
[
i
]
==
connection
)
continue
;
else
{
else
{
newlist
[
j
]
=
connlist
[
i
];
newlist
[
j
]
=
connlist
[
i
];
...
@@ -88,7 +88,7 @@ void delete_connection(usbmux_connection *connection) {
...
@@ -88,7 +88,7 @@ void delete_connection(usbmux_connection *connection) {
}
}
free
(
connlist
);
free
(
connlist
);
connlist
=
newlist
;
connlist
=
newlist
;
c
onnection
s
--
;
c
lient
s
--
;
if
(
connection
->
recv_buffer
)
free
(
connection
->
recv_buffer
);
if
(
connection
->
recv_buffer
)
free
(
connection
->
recv_buffer
);
if
(
connection
->
header
)
free
(
connection
->
header
);
if
(
connection
->
header
)
free
(
connection
->
header
);
connection
->
r_len
=
0
;
connection
->
r_len
=
0
;
...
@@ -101,59 +101,64 @@ void delete_connection(usbmux_connection *connection) {
...
@@ -101,59 +101,64 @@ void delete_connection(usbmux_connection *connection) {
* @param connection The connection to add to the global list of connections.
* @param connection The connection to add to the global list of connections.
*/
*/
void
add_connection
(
usbmux_connection
*
connection
)
{
void
add_connection
(
iphone_umux_client_t
connection
)
{
usbmux_connection
**
newlist
=
(
usbmux_connection
**
)
realloc
(
connlist
,
sizeof
(
usbmux_connection
*
)
*
(
connection
s
+
1
));
iphone_umux_client_t
*
newlist
=
(
iphone_umux_client_t
*
)
realloc
(
connlist
,
sizeof
(
iphone_umux_client_t
)
*
(
client
s
+
1
));
newlist
[
c
onnection
s
]
=
connection
;
newlist
[
c
lient
s
]
=
connection
;
connlist
=
newlist
;
connlist
=
newlist
;
c
onnection
s
++
;
c
lient
s
++
;
}
}
/** Initializes a connection on phone, with source port s_port and destination port d_port
/** Initializes a connection on phone, with source port s_port and destination port d_port
*
*
* @param
phon
e The iPhone to initialize a connection on.
* @param
devic
e The iPhone to initialize a connection on.
* @param s_port The source port
* @param s
rc
_port The source port
* @param d_port The destination port -- 0xf27e for lockdownd.
* @param d
st
_port The destination port -- 0xf27e for lockdownd.
*
*
@param client A mux TCP header for the connection which is used for tracking and data transfer.
* @return
A mux TCP header for the connection which is used for tracking and data transfer
.
* @return
IPHONE_E_SUCCESS on success, an error code otherwise
.
*/
*/
usbmux_connection
*
mux_connect
(
iphone_device_t
phone
,
uint16
s_port
,
uint16
d_port
)
{
int
iphone_mux_new_client
(
iphone_device_t
device
,
uint16_t
src_port
,
uint16_t
dst_port
,
iphone_umux_client_t
*
client
){
if
(
!
phone
||
!
s_port
||
!
d_port
)
return
NULL
;
if
(
!
device
||
!
src_port
||
!
dst_port
)
return
IPHONE_E_INVALID_ARG
;
int
bytes
=
0
;
int
bytes
=
0
;
// Initialize connection stuff
// Initialize connection stuff
usbmux_connection
*
new_connection
=
(
usbmux_connection
*
)
malloc
(
sizeof
(
usbmux_connection
));
iphone_umux_client_t
new_connection
=
(
iphone_umux_client_t
)
malloc
(
sizeof
(
struct
iphone_umux_client_int
));
new_connection
->
header
=
new_mux_packet
(
s_port
,
d_port
);
new_connection
->
header
=
new_mux_packet
(
src_port
,
dst_port
);
// blargg
// blargg
if
(
new_connection
&&
new_connection
->
header
)
{
if
(
new_connection
&&
new_connection
->
header
)
{
new_connection
->
header
->
tcp_flags
=
0x02
;
new_connection
->
header
->
tcp_flags
=
0x02
;
new_connection
->
header
->
length
=
htonl
(
new_connection
->
header
->
length
);
new_connection
->
header
->
length
=
htonl
(
new_connection
->
header
->
length
);
new_connection
->
header
->
length16
=
htons
(
new_connection
->
header
->
length16
);
new_connection
->
header
->
length16
=
htons
(
new_connection
->
header
->
length16
);
if
(
send_to_phone
(
phon
e
,
(
char
*
)
new_connection
->
header
,
sizeof
(
usbmux_tcp_header
))
>=
0
)
{
if
(
send_to_phone
(
devic
e
,
(
char
*
)
new_connection
->
header
,
sizeof
(
usbmux_tcp_header
))
>=
0
)
{
usbmux_tcp_header
*
response
;
usbmux_tcp_header
*
response
;
response
=
(
usbmux_tcp_header
*
)
malloc
(
sizeof
(
usbmux_tcp_header
));
response
=
(
usbmux_tcp_header
*
)
malloc
(
sizeof
(
usbmux_tcp_header
));
bytes
=
recv_from_phone
(
phon
e
,
(
char
*
)
response
,
sizeof
(
*
response
));
bytes
=
recv_from_phone
(
devic
e
,
(
char
*
)
response
,
sizeof
(
*
response
));
if
(
response
->
tcp_flags
!=
0x12
)
{
if
(
response
->
tcp_flags
!=
0x12
)
{
free
(
response
);
free
(
response
);
return
NULL
;
return
IPHONE_E_UNKNOWN_ERROR
;
}
else
{
}
else
{
free
(
response
);
free
(
response
);
if
(
debug
)
printf
(
"mux_connect: connection success
\n
"
);
if
(
debug
)
printf
(
"mux_connect: connection success
\n
"
);
new_connection
->
header
->
tcp_flags
=
0x10
;
new_connection
->
header
->
tcp_flags
=
0x10
;
new_connection
->
header
->
scnt
=
1
;
new_connection
->
header
->
scnt
=
1
;
new_connection
->
header
->
ocnt
=
1
;
new_connection
->
header
->
ocnt
=
1
;
new_connection
->
phone
=
phon
e
;
new_connection
->
phone
=
devic
e
;
new_connection
->
recv_buffer
=
NULL
;
new_connection
->
recv_buffer
=
NULL
;
new_connection
->
r_len
=
0
;
new_connection
->
r_len
=
0
;
add_connection
(
new_connection
);
add_connection
(
new_connection
);
return
new_connection
;
*
client
=
new_connection
;
return
IPHONE_E_SUCCESS
;
}
}
}
else
{
}
else
{
return
NULL
;
return
IPHONE_E_NOT_ENOUGH_DATA
;
}
}
}
}
// if we get to this point it's probably bad
// if we get to this point it's probably bad
return
NULL
;
return
IPHONE_E_UNKNOWN_ERROR
;
}
}
/** Cleans up the given USBMux connection.
/** Cleans up the given USBMux connection.
...
@@ -161,56 +166,59 @@ usbmux_connection *mux_connect(iphone_device_t phone, uint16 s_port, uint16 d_po
...
@@ -161,56 +166,59 @@ usbmux_connection *mux_connect(iphone_device_t phone, uint16 s_port, uint16 d_po
*
*
* @param connection The connection to close.
* @param connection The connection to close.
*/
*/
void
mux_close_connection
(
usbmux_connection
*
connection
)
{
void
iphone_mux_free_client
(
iphone_umux_client_t
client
)
{
if
(
!
c
onnection
||
!
connection
->
phone
)
return
;
if
(
!
c
lient
||
!
client
->
phone
)
return
;
c
onnection
->
header
->
tcp_flags
=
0x04
;
c
lient
->
header
->
tcp_flags
=
0x04
;
c
onnection
->
header
->
scnt
=
htonl
(
connection
->
header
->
scnt
);
c
lient
->
header
->
scnt
=
htonl
(
client
->
header
->
scnt
);
c
onnection
->
header
->
ocnt
=
htonl
(
connection
->
header
->
ocnt
);
c
lient
->
header
->
ocnt
=
htonl
(
client
->
header
->
ocnt
);
int
bytes
=
0
;
int
bytes
=
0
;
bytes
=
usb_bulk_write
(
c
onnection
->
phone
->
device
,
BULKOUT
,
(
char
*
)
connection
->
header
,
sizeof
(
usbmux_tcp_header
),
800
);
bytes
=
usb_bulk_write
(
c
lient
->
phone
->
device
,
BULKOUT
,
(
char
*
)
client
->
header
,
sizeof
(
usbmux_tcp_header
),
800
);
if
(
debug
&&
bytes
<
0
)
if
(
debug
&&
bytes
<
0
)
printf
(
"mux_close_connection(): when writing, libusb gave me the error: %s
\n
"
,
usb_strerror
());
printf
(
"mux_close_connection(): when writing, libusb gave me the error: %s
\n
"
,
usb_strerror
());
bytes
=
usb_bulk_read
(
c
onnection
->
phone
->
device
,
BULKIN
,
(
char
*
)
connection
->
header
,
sizeof
(
usbmux_tcp_header
),
800
);
bytes
=
usb_bulk_read
(
c
lient
->
phone
->
device
,
BULKIN
,
(
char
*
)
client
->
header
,
sizeof
(
usbmux_tcp_header
),
800
);
if
(
debug
&&
bytes
<
0
)
if
(
debug
&&
bytes
<
0
)
printf
(
"get_iPhone(): when reading, libusb gave me the error: %s
\n
"
,
usb_strerror
());
printf
(
"get_iPhone(): when reading, libusb gave me the error: %s
\n
"
,
usb_strerror
());
delete_connection
(
c
onnection
);
delete_connection
(
c
lient
);
}
}
/** Sends the given data over the selected connection.
/** Sends the given data over the selected connection.
*
*
* @param connection The connection we're sending data on.
* @param phone The iPhone to send to.
* @param client The client we're sending data on.
* @param data A pointer to the data to send.
* @param data A pointer to the data to send.
* @param datalen How much data we're sending.
* @param datalen How much data we're sending.
*
*
* @return The number of bytes sent, minus the header (28), or -1 on error.
* @return The number of bytes sent, minus the header (28), or -1 on error.
*/
*/
int
mux_send
(
usbmux_connection
*
connection
,
const
char
*
data
,
uint32
datalen
)
{
if
(
!
connection
->
phone
||
!
connection
||
!
data
||
datalen
==
0
)
return
-
1
;
int
iphone_mux_send
(
iphone_umux_client_t
client
,
const
char
*
data
,
uint32_t
datalen
)
{
// connection->scnt and connection->ocnt should already be in host notation...
if
(
!
client
->
phone
||
!
client
||
!
data
||
datalen
==
0
)
return
-
1
;
// client->scnt and client->ocnt should already be in host notation...
// we don't need to change them juuuust yet.
// we don't need to change them juuuust yet.
int
bytes
=
0
;
int
bytes
=
0
;
if
(
debug
)
printf
(
"mux_send(): client wants to send %i bytes
\n
"
,
datalen
);
if
(
debug
)
printf
(
"mux_send(): client wants to send %i bytes
\n
"
,
datalen
);
char
*
buffer
=
(
char
*
)
malloc
(
sizeof
(
usbmux_tcp_header
)
+
datalen
+
2
);
// allow 2 bytes of safety padding
char
*
buffer
=
(
char
*
)
malloc
(
sizeof
(
usbmux_tcp_header
)
+
datalen
+
2
);
// allow 2 bytes of safety padding
// Set the length and pre-emptively htonl/htons it
// Set the length and pre-emptively htonl/htons it
c
onnection
->
header
->
length
=
htonl
(
sizeof
(
usbmux_tcp_header
)
+
datalen
);
c
lient
->
header
->
length
=
htonl
(
sizeof
(
usbmux_tcp_header
)
+
datalen
);
c
onnection
->
header
->
length16
=
htons
(
sizeof
(
usbmux_tcp_header
)
+
datalen
);
c
lient
->
header
->
length16
=
htons
(
sizeof
(
usbmux_tcp_header
)
+
datalen
);
// Put scnt and ocnt into big-endian notation
// Put scnt and ocnt into big-endian notation
c
onnection
->
header
->
scnt
=
htonl
(
connection
->
header
->
scnt
);
c
lient
->
header
->
scnt
=
htonl
(
client
->
header
->
scnt
);
c
onnection
->
header
->
ocnt
=
htonl
(
connection
->
header
->
ocnt
);
c
lient
->
header
->
ocnt
=
htonl
(
client
->
header
->
ocnt
);
// Concatenation of stuff in the buffer.
// Concatenation of stuff in the buffer.
memcpy
(
buffer
,
c
onnection
->
header
,
sizeof
(
usbmux_tcp_header
));
memcpy
(
buffer
,
c
lient
->
header
,
sizeof
(
usbmux_tcp_header
));
memcpy
(
buffer
+
sizeof
(
usbmux_tcp_header
),
data
,
datalen
);
memcpy
(
buffer
+
sizeof
(
usbmux_tcp_header
),
data
,
datalen
);
// We have a buffer full of data, we should now send it to the phone.
// We have a buffer full of data, we should now send it to the phone.
if
(
debug
)
printf
(
"actually sending %zi bytes of data at %p
\n
"
,
sizeof
(
usbmux_tcp_header
)
+
datalen
,
buffer
);
if
(
debug
)
printf
(
"actually sending %zi bytes of data at %p
\n
"
,
sizeof
(
usbmux_tcp_header
)
+
datalen
,
buffer
);
bytes
=
send_to_phone
(
c
onnection
->
phone
,
buffer
,
sizeof
(
usbmux_tcp_header
)
+
datalen
);
bytes
=
send_to_phone
(
c
lient
->
phone
,
buffer
,
sizeof
(
usbmux_tcp_header
)
+
datalen
);
if
(
debug
)
printf
(
"mux_send: sent %i bytes!
\n
"
,
bytes
);
if
(
debug
)
printf
(
"mux_send: sent %i bytes!
\n
"
,
bytes
);
// Now that we've sent it off, we can clean up after our sloppy selves.
// Now that we've sent it off, we can clean up after our sloppy selves.
if
(
debug
)
{
if
(
debug
)
{
...
@@ -222,12 +230,12 @@ int mux_send(usbmux_connection *connection, const char *data, uint32 datalen) {
...
@@ -222,12 +230,12 @@ int mux_send(usbmux_connection *connection, const char *data, uint32 datalen) {
if
(
buffer
)
free
(
buffer
);
if
(
buffer
)
free
(
buffer
);
// Re-calculate scnt and ocnt
// Re-calculate scnt and ocnt
c
onnection
->
header
->
scnt
=
ntohl
(
connection
->
header
->
scnt
)
+
datalen
;
c
lient
->
header
->
scnt
=
ntohl
(
client
->
header
->
scnt
)
+
datalen
;
c
onnection
->
header
->
ocnt
=
ntohl
(
connection
->
header
->
ocnt
);
c
lient
->
header
->
ocnt
=
ntohl
(
client
->
header
->
ocnt
);
// Revert lengths
// Revert lengths
c
onnection
->
header
->
length
=
ntohl
(
connection
->
header
->
length
);
c
lient
->
header
->
length
=
ntohl
(
client
->
header
->
length
);
c
onnection
->
header
->
length16
=
ntohs
(
connection
->
header
->
length16
);
c
lient
->
header
->
length16
=
ntohs
(
client
->
header
->
length16
);
// Now return the bytes.
// Now return the bytes.
if
(
bytes
<
sizeof
(
usbmux_tcp_header
)
+
datalen
)
{
if
(
bytes
<
sizeof
(
usbmux_tcp_header
)
+
datalen
)
{
...
@@ -247,52 +255,52 @@ int mux_send(usbmux_connection *connection, const char *data, uint32 datalen) {
...
@@ -247,52 +255,52 @@ int mux_send(usbmux_connection *connection, const char *data, uint32 datalen) {
*
*
* @return How many bytes were read, or -1 if something bad happens.
* @return How many bytes were read, or -1 if something bad happens.
*/
*/
int
mux_recv
(
usbmux_connection
*
connection
,
char
*
data
,
uint32
datalen
)
{
int
iphone_mux_recv
(
iphone_umux_client_t
client
,
char
*
data
,
uint32_t
datalen
)
{
/*
/*
* Order of operation:
* Order of operation:
* 1.) Check if the c
onnection
has a pre-received buffer.
* 1.) Check if the c
lient
has a pre-received buffer.
* 2.) If so, fill data with the buffer, as much as needed.
* 2.) If so, fill data with the buffer, as much as needed.
* a.) Return quickly if the buffer has enough
* a.) Return quickly if the buffer has enough
* b.) If the buffer is only part of the datalen, get the rest of datalen (and if we can't, just return)
* b.) If the buffer is only part of the datalen, get the rest of datalen (and if we can't, just return)
* 3.) If not, receive directly from the phone.
* 3.) If not, receive directly from the phone.
* a.) Check incoming packet's ports. If proper, follow proper buffering and receiving operation.
* a.) Check incoming packet's ports. If proper, follow proper buffering and receiving operation.
* b.) If not, find the c
onnection the ports belong to and fill that connection
's buffer, then return mux_recv with the same args to try again.
* b.) If not, find the c
lient the ports belong to and fill that client
's buffer, then return mux_recv with the same args to try again.
*/
*/
if
(
debug
)
printf
(
"mux_recv: datalen == %i
\n
"
,
datalen
);
if
(
debug
)
printf
(
"mux_recv: datalen == %i
\n
"
,
datalen
);
int
bytes
=
0
,
i
=
0
,
complex
=
0
,
offset
=
0
;
int
bytes
=
0
,
i
=
0
,
complex
=
0
,
offset
=
0
;
char
*
buffer
=
NULL
;
char
*
buffer
=
NULL
;
usbmux_tcp_header
*
header
=
NULL
;
usbmux_tcp_header
*
header
=
NULL
;
if
(
c
onnection
->
recv_buffer
)
{
if
(
c
lient
->
recv_buffer
)
{
if
(
c
onnection
->
r_len
>=
datalen
)
{
if
(
c
lient
->
r_len
>=
datalen
)
{
memcpy
(
data
,
c
onnection
->
recv_buffer
,
datalen
);
memcpy
(
data
,
c
lient
->
recv_buffer
,
datalen
);
if
(
c
onnection
->
r_len
==
datalen
)
{
if
(
c
lient
->
r_len
==
datalen
)
{
// reset everything
// reset everything
free
(
c
onnection
->
recv_buffer
);
free
(
c
lient
->
recv_buffer
);
c
onnection
->
r_len
=
0
;
c
lient
->
r_len
=
0
;
c
onnection
->
recv_buffer
=
NULL
;
c
lient
->
recv_buffer
=
NULL
;
}
else
{
}
else
{
buffer
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
(
c
onnection
->
r_len
-
datalen
));
buffer
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
(
c
lient
->
r_len
-
datalen
));
memcpy
(
buffer
,
c
onnection
->
recv_buffer
+
datalen
,
(
connection
->
r_len
-
datalen
));
memcpy
(
buffer
,
c
lient
->
recv_buffer
+
datalen
,
(
client
->
r_len
-
datalen
));
c
onnection
->
r_len
-=
datalen
;
c
lient
->
r_len
-=
datalen
;
free
(
c
onnection
->
recv_buffer
);
free
(
c
lient
->
recv_buffer
);
c
onnection
->
recv_buffer
=
buffer
;
c
lient
->
recv_buffer
=
buffer
;
}
}
// Since we were able to fill the data straight from our buffer, we can just return datalen. See 2a above.
// Since we were able to fill the data straight from our buffer, we can just return datalen. See 2a above.
return
datalen
;
return
datalen
;
}
else
{
}
else
{
memcpy
(
data
,
c
onnection
->
recv_buffer
,
connection
->
r_len
);
memcpy
(
data
,
c
lient
->
recv_buffer
,
client
->
r_len
);
free
(
c
onnection
->
recv_buffer
);
// don't need to deal with anymore, but...
free
(
c
lient
->
recv_buffer
);
// don't need to deal with anymore, but...
offset
=
c
onnection
->
r_len
;
// see #2b, above
offset
=
c
lient
->
r_len
;
// see #2b, above
c
onnection
->
r_len
=
0
;
c
lient
->
r_len
=
0
;
}
}
}
// End of what to do if we have a pre-buffer. See #1 and #2 above.
}
// End of what to do if we have a pre-buffer. See #1 and #2 above.
buffer
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
131072
);
// make sure we get enough ;)
buffer
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
131072
);
// make sure we get enough ;)
// See #3.
// See #3.
bytes
=
recv_from_phone
(
c
onnection
->
phone
,
buffer
,
131072
);
bytes
=
recv_from_phone
(
c
lient
->
phone
,
buffer
,
131072
);
if
(
bytes
<
28
)
{
if
(
bytes
<
28
)
{
free
(
buffer
);
free
(
buffer
);
if
(
debug
)
printf
(
"mux_recv: Did not even get the header.
\n
"
);
if
(
debug
)
printf
(
"mux_recv: Did not even get the header.
\n
"
);
...
@@ -300,10 +308,10 @@ int mux_recv(usbmux_connection *connection, char *data, uint32 datalen) {
...
@@ -300,10 +308,10 @@ int mux_recv(usbmux_connection *connection, char *data, uint32 datalen) {
}
}
header
=
(
usbmux_tcp_header
*
)
buffer
;
header
=
(
usbmux_tcp_header
*
)
buffer
;
if
(
header
->
sport
!=
c
onnection
->
header
->
dport
||
header
->
dport
!=
connection
->
header
->
sport
)
{
if
(
header
->
sport
!=
c
lient
->
header
->
dport
||
header
->
dport
!=
client
->
header
->
sport
)
{
// Ooooops -- we got someone else's packet.
// Ooooops -- we got someone else's packet.
// We gotta stick it in their buffer. (Take that any old way you want ;) )
// We gotta stick it in their buffer. (Take that any old way you want ;) )
for
(
i
=
0
;
i
<
c
onnection
s
;
i
++
)
{
for
(
i
=
0
;
i
<
c
lient
s
;
i
++
)
{
if
(
connlist
[
i
]
->
header
->
sport
==
header
->
dport
&&
connlist
[
i
]
->
header
->
dport
==
header
->
sport
)
{
if
(
connlist
[
i
]
->
header
->
sport
==
header
->
dport
&&
connlist
[
i
]
->
header
->
dport
==
header
->
sport
)
{
// we have a winner.
// we have a winner.
char
*
nfb
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
(
connlist
[
i
]
->
r_len
+
(
bytes
-
28
)));
char
*
nfb
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
(
connlist
[
i
]
->
r_len
+
(
bytes
-
28
)));
...
@@ -312,7 +320,7 @@ int mux_recv(usbmux_connection *connection, char *data, uint32 datalen) {
...
@@ -312,7 +320,7 @@ int mux_recv(usbmux_connection *connection, char *data, uint32 datalen) {
free
(
connlist
[
i
]
->
recv_buffer
);
free
(
connlist
[
i
]
->
recv_buffer
);
}
}
connlist
[
i
]
->
r_len
+=
bytes
-
28
;
connlist
[
i
]
->
r_len
+=
bytes
-
28
;
//connlist[i]->recv_buffer = (char*)realloc(connlist[i]->recv_buffer, sizeof(char) * c
onnection
->r_len); // grow their buffer
//connlist[i]->recv_buffer = (char*)realloc(connlist[i]->recv_buffer, sizeof(char) * c
lient
->r_len); // grow their buffer
connlist
[
i
]
->
recv_buffer
=
nfb
;
connlist
[
i
]
->
recv_buffer
=
nfb
;
nfb
=
NULL
;
// A cookie for you if you can guess what "nfb" means.
nfb
=
NULL
;
// A cookie for you if you can guess what "nfb" means.
complex
=
connlist
[
i
]
->
r_len
-
(
bytes
-
28
);
complex
=
connlist
[
i
]
->
r_len
-
(
bytes
-
28
);
...
@@ -324,7 +332,7 @@ int mux_recv(usbmux_connection *connection, char *data, uint32 datalen) {
...
@@ -324,7 +332,7 @@ int mux_recv(usbmux_connection *connection, char *data, uint32 datalen) {
// Free our buffer and continue.
// Free our buffer and continue.
free
(
buffer
);
free
(
buffer
);
buffer
=
NULL
;
buffer
=
NULL
;
return
mux_recv
(
c
onnection
,
data
,
datalen
);
// recurse back in to try again
return
mux_recv
(
c
lient
,
data
,
datalen
);
// recurse back in to try again
}
}
// The packet was absolutely meant for us if it hits this point.
// The packet was absolutely meant for us if it hits this point.
...
@@ -333,18 +341,18 @@ int mux_recv(usbmux_connection *connection, char *data, uint32 datalen) {
...
@@ -333,18 +341,18 @@ int mux_recv(usbmux_connection *connection, char *data, uint32 datalen) {
if
((
bytes
-
28
)
>
datalen
)
{
if
((
bytes
-
28
)
>
datalen
)
{
// Copy what we need into the data, buffer the rest because we can.
// Copy what we need into the data, buffer the rest because we can.
memcpy
(
data
+
offset
,
buffer
+
28
,
datalen
);
// data+offset: see #2b, above
memcpy
(
data
+
offset
,
buffer
+
28
,
datalen
);
// data+offset: see #2b, above
complex
=
c
onnection
->
r_len
+
(
bytes
-
28
)
-
datalen
;
complex
=
c
lient
->
r_len
+
(
bytes
-
28
)
-
datalen
;
c
onnection
->
recv_buffer
=
(
char
*
)
realloc
(
connection
->
recv_buffer
,
(
sizeof
(
char
)
*
complex
));
c
lient
->
recv_buffer
=
(
char
*
)
realloc
(
client
->
recv_buffer
,
(
sizeof
(
char
)
*
complex
));
c
onnection
->
r_len
=
complex
;
c
lient
->
r_len
=
complex
;
complex
=
c
onnection
->
r_len
-
(
bytes
-
28
)
-
datalen
;
complex
=
c
lient
->
r_len
-
(
bytes
-
28
)
-
datalen
;
memcpy
(
c
onnection
->
recv_buffer
+
complex
,
buffer
+
28
+
datalen
,
(
bytes
-
28
)
-
datalen
);
memcpy
(
c
lient
->
recv_buffer
+
complex
,
buffer
+
28
+
datalen
,
(
bytes
-
28
)
-
datalen
);
free
(
buffer
);
free
(
buffer
);
c
onnection
->
header
->
ocnt
+=
bytes
-
28
;
c
lient
->
header
->
ocnt
+=
bytes
-
28
;
return
datalen
;
return
datalen
;
}
else
{
}
else
{
// Fill the data with what we have, and just return.
// Fill the data with what we have, and just return.
memcpy
(
data
+
offset
,
buffer
+
28
,
bytes
-
28
);
// data+offset: see #2b, above
memcpy
(
data
+
offset
,
buffer
+
28
,
bytes
-
28
);
// data+offset: see #2b, above
c
onnection
->
header
->
ocnt
+=
bytes
-
28
;
c
lient
->
header
->
ocnt
+=
bytes
-
28
;
free
(
buffer
);
free
(
buffer
);
return
(
bytes
-
28
);
return
(
bytes
-
28
);
}
}
...
...
src/usbmux.h
View file @
ad65cefa
...
@@ -43,12 +43,12 @@ typedef struct {
...
@@ -43,12 +43,12 @@ typedef struct {
uint16
window
,
nullnull
,
length16
;
uint16
window
,
nullnull
,
length16
;
}
usbmux_tcp_header
;
}
usbmux_tcp_header
;
typedef
struc
t
{
struct
iphone_umux_client_in
t
{
usbmux_tcp_header
*
header
;
usbmux_tcp_header
*
header
;
iphone_device_t
phone
;
iphone_device_t
phone
;
char
*
recv_buffer
;
char
*
recv_buffer
;
int
r_len
;
int
r_len
;
}
usbmux_connection
;
};
usbmux_tcp_header
*
new_mux_packet
(
uint16
s_port
,
uint16
d_port
);
usbmux_tcp_header
*
new_mux_packet
(
uint16
s_port
,
uint16
d_port
);
...
@@ -58,10 +58,5 @@ typedef struct {
...
@@ -58,10 +58,5 @@ typedef struct {
usbmux_version_header
*
version_header
();
usbmux_version_header
*
version_header
();
usbmux_connection
*
mux_connect
(
iphone_device_t
phone
,
uint16
s_port
,
uint16
d_port
);
void
mux_close_connection
(
usbmux_connection
*
connection
);
int
mux_send
(
usbmux_connection
*
connection
,
const
char
*
data
,
uint32
datalen
);
int
mux_recv
(
usbmux_connection
*
connection
,
char
*
data
,
uint32
datalen
);
#endif
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment