forked from rileytestut/AltServer-Windows
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathWiredConnection.cpp
More file actions
executable file
·168 lines (143 loc) · 3.9 KB
/
WiredConnection.cpp
File metadata and controls
executable file
·168 lines (143 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "WiredConnection.h"
#include "ServerError.hpp"
#include <libimobiledevice/common/socket.h>
// Must come after other #include's
#include "AltInclude.h"
// Copied from imobiledevice/common/socket.c
static int verbose = 0;
int socket_check_fd(int fd, fd_mode fdm, unsigned int timeout)
{
fd_set fds;
int sret;
int eagain;
struct timeval to;
struct timeval* pto;
if (fd < 0) {
if (verbose >= 2)
fprintf(stderr, "ERROR: invalid fd in check_fd %d\n", fd);
return -1;
}
FD_ZERO(&fds);
FD_SET(fd, &fds);
sret = -1;
do {
if (timeout > 0) {
to.tv_sec = (time_t)(timeout / 1000);
to.tv_usec = (time_t)((timeout - (to.tv_sec * 1000)) * 1000);
pto = &to;
}
else {
pto = NULL;
}
eagain = 0;
switch (fdm) {
case FDM_READ:
sret = select(fd + 1, &fds, NULL, NULL, pto);
break;
case FDM_WRITE:
sret = select(fd + 1, NULL, &fds, NULL, pto);
break;
case FDM_EXCEPT:
sret = select(fd + 1, NULL, NULL, &fds, pto);
break;
default:
return -1;
}
if (sret < 0) {
switch (errno) {
case EINTR:
// interrupt signal in select
if (verbose >= 2)
fprintf(stderr, "%s: EINTR\n", __func__);
eagain = 1;
break;
case EAGAIN:
if (verbose >= 2)
fprintf(stderr, "%s: EAGAIN\n", __func__);
break;
default:
if (verbose >= 2)
fprintf(stderr, "%s: select failed: %s\n", __func__,
strerror(errno));
return -1;
}
}
else if (sret == 0) {
return -ETIMEDOUT;
}
} while (eagain);
return sret;
}
WiredConnection::WiredConnection(std::shared_ptr<Device> device, idevice_connection_t connection) : _device(device), _connection(connection)
{
}
WiredConnection::~WiredConnection()
{
}
void WiredConnection::Disconnect()
{
if (this->connection() == NULL)
{
return;
}
idevice_disconnect(this->connection());
_connection = NULL;
}
pplx::task<void> WiredConnection::SendData(std::vector<unsigned char>& data)
{
return pplx::create_task([data, this]() mutable {
int fd = 0;
idevice_error_t result = idevice_connection_get_fd(this->connection(), &fd);
if (result != IDEVICE_E_SUCCESS || fd <= 0)
{
altlog("Error getting WiredConnection file descriptor. " << result);
throw ServerError(ServerErrorCode::LostConnection);
}
int socket_result = socket_check_fd(fd, FDM_READ, 1);
if (socket_result != 0 && socket_result != -ETIMEDOUT)
{
altlog("Error checking WiredConnection file descriptor " << fd << ". " << socket_result);
throw ServerError(ServerErrorCode::LostConnection);
}
while (data.size() > 0)
{
uint32_t sentBytes = 0;
idevice_error_t result = idevice_connection_send(this->connection(), (const char*)data.data(), (int32_t)data.size(), &sentBytes);
if (result != IDEVICE_E_SUCCESS || sentBytes == 0)
{
altlog("Error sending data over WiredConnection. " << result);
throw ServerError(ServerErrorCode::LostConnection);
}
data.erase(data.begin(), data.begin() + sentBytes);
}
});
}
pplx::task<std::vector<unsigned char>> WiredConnection::ReceiveData(int expectedSize)
{
return pplx::create_task([=]() -> std::vector<unsigned char> {
char bytes[4096];
std::vector<unsigned char> receivedData;
receivedData.reserve(expectedSize);
while (receivedData.size() < expectedSize)
{
uint32_t size = min((uint32_t)4096, (uint32_t)expectedSize - (uint32_t)receivedData.size());
uint32_t receivedBytes = 0;
idevice_error_t result = idevice_connection_receive_timeout(this->connection(), bytes, size, &receivedBytes, 10000);
if (result != IDEVICE_E_SUCCESS || receivedBytes == 0)
{
altlog("Error receiving data over WiredConnection. " << result);
throw ServerError(ServerErrorCode::LostConnection);
}
receivedData.insert(receivedData.end(), bytes, bytes + receivedBytes);
}
return receivedData;
});
}
std::shared_ptr<Device> WiredConnection::device() const
{
return _device;
}
idevice_connection_t WiredConnection::connection() const
{
return _connection;
}