-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse.c
432 lines (360 loc) · 14.6 KB
/
response.c
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <endian.h>
#include <err.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <uchar.h>
#include <arpa/inet.h>
#include "mbim.h"
static void enum32(const char *key, uint32_t value, struct map *values) {
if (values == NULL) {
printf("%s %u\n", key, le32toh(value));
return;
}
for (fputs(key, stdout); values->value; values++)
if (values->key == le32toh(value))
printf(" %s", values->value);
putc('\n', stdout);
}
static void mask32(const char *key, uint32_t value, struct map *values) {
if (values == NULL) {
printf("%s %u\n", key, le32toh(value));
return;
}
for (fputs(key, stdout); values->value; values++)
if (values->key & le32toh(value))
printf(" %s", values->value);
putc('\n', stdout);
}
static void string(const char *key, const void *buffer, size_t size,
const struct reference *value) {
uint16_t *in = (uint16_t *) ((char *) buffer + le32toh(value->offset));
ssize_t count, length = le32toh(value->length) >> 1;
char out[MB_LEN_MAX];
if (size < le32toh(value->offset) + le32toh(value->length))
err(EXIT_FAILURE, "Response is truncated");
if (le32toh(value->offset) == 0)
return;
printf("%s ", key);
for (mbstate_t state = { 0 }; length > 0; in++, length--)
if ((count = c16rtomb(out, le16toh(*in), &state)) > 0)
for (ssize_t i = 0; i < count; i++)
putc(out[i] & 224 && out[i] != 127 ? out[i] : 32, stdout);
putc('\n', stdout);
}
static void ipv4(const char *key, const void *buffer, size_t size,
uint32_t offset, size_t index, int mask) {
char *in = (char *) buffer + le32toh(offset), out[INET_ADDRSTRLEN];
if (size < le32toh(offset) + (index + 1) * (mask ? 8 : 4))
err(EXIT_FAILURE, "Response is truncated");
if (inet_ntop(AF_INET, in + (mask ? 4 + 8 * index : 4 * index),
out, sizeof(out)) == NULL)
printf("%s invalid\n", key);
else if (mask)
printf("%s %s/%u\n", key, out, le32toh(((uint32_t *) in)[2 * index]));
else
printf("%s %s\n", key, out);
}
static void ipv6(const char *key, const void *buffer, size_t size,
uint32_t offset, size_t index, int mask) {
char *in = (char *) buffer + le32toh(offset), out[INET6_ADDRSTRLEN];
if (size < le32toh(offset) + (index + 1) * (mask ? 20 : 16))
err(EXIT_FAILURE, "Response is truncated");
if (inet_ntop(AF_INET6, in + (mask ? 4 + 20 * index : 16 * index),
out, sizeof(out)) == NULL)
printf("%s invalid\n", key);
else if (mask)
printf("%s %s/%u\n", key, out, le32toh(((uint32_t *) in)[5 * index]));
else
printf("%s %s\n", key, out);
}
static int response_open(int fd) {
struct open_done_message *msg = receive(fd, MESSAGE_TYPE_OPEN_DONE, -1);
if (msg->status_code == STATUS_SUCCESS)
return EXIT_SUCCESS;
return EXIT_FAILURE;
}
static int response_close(int fd) {
struct close_done_message *msg = receive(fd, MESSAGE_TYPE_CLOSE_DONE, -1);
if (msg->status_code == STATUS_SUCCESS)
return EXIT_SUCCESS;
return EXIT_FAILURE;
}
static int response_caps(int fd) {
struct command_done_message *msg = receive(fd, MESSAGE_TYPE_COMMAND_DONE,
BASIC_CONNECT_DEVICE_CAPS);
struct basic_connect_device_caps_r *reply = (void *) msg->buffer;
size_t size = le32toh(msg->buffer_length);
if (size < sizeof(*reply))
err(EXIT_FAILURE, "Response is truncated");
enum32("device-type", reply->device_type, device_type_values);
mask32("cellular-class", reply->cellular_class, cellular_class_values);
enum32("voice-class", reply->voice_class, voice_class_values);
mask32("sim-class", reply->sim_class, sim_class_values);
mask32("data-class", reply->data_class, data_class_values);
mask32("sms-caps", reply->sms_caps, sms_caps_values);
mask32("control-caps", reply->control_caps, ctrl_caps_values);
enum32("max-sessions", reply->max_sessions, NULL);
string("device-id", reply, size, &reply->device_id);
string("firmware-info", reply, size, &reply->firmware_info);
string("hardware-info", reply, size, &reply->hardware_info);
free(msg);
return EXIT_SUCCESS;
}
static int response_subscriber(int fd) {
struct command_done_message *msg = receive(fd, MESSAGE_TYPE_COMMAND_DONE,
BASIC_CONNECT_SUBSCRIBER_READY_STATUS);
struct basic_connect_subscriber_ready_status_r *reply
= (void *) msg->buffer;
size_t size = le32toh(msg->buffer_length);
uint32_t state;
if (size < sizeof(*reply))
err(EXIT_FAILURE, "Response is truncated");
state = le32toh(reply->ready_state);
enum32("ready-state", reply->ready_state,
subscriber_ready_state_values);
string("sim-icc-id", reply, size, &reply->sim_icc_id);
string("subscriber-id", reply, size, &reply->subscriber_id);
if (le32toh(reply->ready_info) & READY_INFO_FLAG_PROTECT_UNIQUE_ID)
printf("protect-unique-id 1\n");
for (size_t i = 0; i < le32toh(reply->telephone_numbers_count); i++)
string("telephone-number", reply, size, reply->telephone_numbers + i);
free(msg);
if (state == SUBSCRIBER_READY_STATE_INITIALIZED)
return EXIT_SUCCESS;
return EXIT_FAILURE;
}
static int response_radio(int fd) {
struct command_done_message *msg = receive(fd, MESSAGE_TYPE_COMMAND_DONE,
BASIC_CONNECT_RADIO_STATE);
struct basic_connect_radio_state_r *reply = (void *) msg->buffer;
if (le32toh(msg->buffer_length) < sizeof(*reply))
err(EXIT_FAILURE, "Response is truncated");
enum32("hw-radio", reply->hw_radio_state, radio_switch_state_values);
enum32("sw-radio", reply->sw_radio_state, radio_switch_state_values);
free(msg);
return EXIT_SUCCESS;
}
static int response_pin(int fd) {
struct command_done_message *msg = receive(fd, MESSAGE_TYPE_COMMAND_DONE,
BASIC_CONNECT_PIN);
struct basic_connect_pin_r *reply = (void *) msg->buffer;
uint32_t state;
if (le32toh(msg->buffer_length) < sizeof(*reply))
err(EXIT_FAILURE, "Response is truncated");
state = le32toh(reply->pin_state);
enum32("pin-state", reply->pin_state, pin_state_values);
if (le32toh(reply->pin_state) != PIN_STATE_UNLOCKED) {
enum32("pin-type", reply->pin_type, pin_type_values);
enum32("remaining-attempts", reply->remaining_attempts, NULL);
}
free(msg);
if (state == PIN_STATE_UNLOCKED)
return EXIT_SUCCESS;
return EXIT_FAILURE;
}
static int response_home(int fd) {
struct command_done_message *msg = receive(fd, MESSAGE_TYPE_COMMAND_DONE,
BASIC_CONNECT_HOME_PROVIDER);
struct basic_connect_home_provider_r *reply = (void *) msg->buffer;
size_t size = le32toh(msg->buffer_length);
if (size < sizeof(*reply))
err(EXIT_FAILURE, "Response is truncated");
string("provider-id", reply, size, &reply->provider.provider_id);
string("provider-name", reply, size, &reply->provider.provider_name);
mask32("provider-state", reply->provider.provider_state,
provider_state_values);
mask32("cellular-class", reply->provider.cellular_class,
cellular_class_values);
if (le32toh(reply->provider.rssi) < 32)
printf("rssi %d\n", 2 * le32toh(reply->provider.rssi) - 113);
if (le32toh(reply->provider.error_rate) < 8)
printf("error-rate %d\n", reply->provider.error_rate);
free(msg);
return EXIT_SUCCESS;
}
static int response_visible(int fd) {
struct command_done_message *msg = receive(fd, MESSAGE_TYPE_COMMAND_DONE,
BASIC_CONNECT_VISIBLE_PROVIDERS);
struct basic_connect_visible_providers_r *reply = (void *) msg->buffer;
size_t size = le32toh(msg->buffer_length);
if (size < sizeof(*reply) || size < sizeof(*reply)
+ le32toh(reply->providers_count) * sizeof(struct reference))
err(EXIT_FAILURE, "Response is truncated");
for (size_t i = 0; i < le32toh(reply->providers_count); i++) {
size_t offset = le32toh(reply->providers[i].offset);
size_t length = le32toh(reply->providers[i].length);
struct provider *provider = (void *) ((char *) reply + offset);
if (size < offset + length)
err(EXIT_FAILURE, "Response is truncated");
if (i > 0)
putc('\n', stdout);
string("provider-id", provider, length, &provider->provider_id);
string("provider-name", provider, length, &provider->provider_name);
mask32("provider-state", provider->provider_state, provider_state_values);
mask32("cellular-class", provider->cellular_class, cellular_class_values);
if (le32toh(provider->rssi) < 32)
printf("rssi %d\n", 2 * le32toh(provider->rssi) - 113);
if (le32toh(provider->error_rate) < 8)
printf("error-rate %d\n", provider->error_rate);
}
free(msg);
return EXIT_SUCCESS;
}
static int response_register(int fd) {
struct command_done_message *msg = receive(fd, MESSAGE_TYPE_COMMAND_DONE,
BASIC_CONNECT_REGISTER_STATE);
struct basic_connect_register_state_r *reply = (void *) msg->buffer;
size_t size = le32toh(msg->buffer_length);
uint32_t state;
if (size < sizeof(*reply))
err(EXIT_FAILURE, "Response is truncated");
state = le32toh(reply->register_state);
if (reply->nw_error)
enum32("network-error", reply->nw_error, nw_error_values);
enum32("register-state", reply->register_state, register_state_values);
enum32("register-mode", reply->register_mode, register_mode_values);
mask32("available-data-classes", reply->available_data_classes,
data_class_values);
mask32("current-cellular-class", reply->current_cellular_class,
cellular_class_values);
string("provider-id", reply, size, &reply->provider_id);
string("provider-name", reply, size, &reply->provider_name);
string("roaming-text", reply, size, &reply->roaming_text);
free(msg);
switch (state) {
case REGISTER_STATE_HOME:
case REGISTER_STATE_ROAMING:
case REGISTER_STATE_PARTNER:
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
static uint32_t handle_attach(int fd) {
struct command_done_message *msg = receive(fd, MESSAGE_TYPE_COMMAND_DONE,
BASIC_CONNECT_PACKET_SERVICE);
struct basic_connect_packet_service_r *reply = (void *) msg->buffer;
uint32_t state;
if (le32toh(msg->buffer_length) < sizeof(*reply))
err(EXIT_FAILURE, "Response is truncated");
state = le32toh(reply->packet_service_state);
if (reply->nw_error)
enum32("network-error", reply->nw_error, nw_error_values);
enum32("packet-service-state", reply->packet_service_state,
packet_service_state_values);
printf("uplink-speed %ju\n", (intmax_t) le64toh(reply->uplink_speed));
printf("downlink-speed %ju\n", (intmax_t) le64toh(reply->downlink_speed));
free(msg);
return state;
}
static int response_attach(int fd) {
if (handle_attach(fd) == PACKET_SERVICE_STATE_ATTACHED)
return EXIT_SUCCESS;
return EXIT_FAILURE;
}
static int response_detach(int fd) {
if (handle_attach(fd) == PACKET_SERVICE_STATE_DETACHED)
return EXIT_SUCCESS;
return EXIT_FAILURE;
}
static int response_signal(int fd) {
struct command_done_message *msg = receive(fd, MESSAGE_TYPE_COMMAND_DONE,
BASIC_CONNECT_SIGNAL_STATE);
struct basic_connect_signal_state_r *reply = (void *) msg->buffer;
if (le32toh(msg->buffer_length) < sizeof(*reply))
err(EXIT_FAILURE, "Response is truncated");
if (le32toh(reply->rssi) < 32)
printf("rssi %d\n", 2 * le32toh(reply->rssi) - 113);
if (le32toh(reply->error_rate) < 8)
printf("error-rate %d\n", reply->error_rate);
free(msg);
return EXIT_SUCCESS;
}
static uint32_t handle_connect(int fd) {
struct command_done_message *msg = receive(fd, MESSAGE_TYPE_COMMAND_DONE,
BASIC_CONNECT_CONNECT);
struct basic_connect_connect_r *reply = (void *) msg->buffer;
uint32_t state;
if (le32toh(msg->buffer_length) < sizeof(*reply))
err(EXIT_FAILURE, "Response is truncated");
state = le32toh(reply->activation_state);
if (reply->nw_error)
enum32("network-error", reply->nw_error, nw_error_values);
enum32("session-id", reply->session_id, NULL);
enum32("activation-state", reply->activation_state,
activation_state_values);
if (reply->voice_call_state)
enum32("voice-call-state", reply->voice_call_state,
voice_call_state_values);
enum32("ip-type", reply->ip_type, context_ip_type_values);
free(msg);
return state;
}
static int response_connect(int fd) {
if (handle_connect(fd) == ACTIVATION_STATE_ACTIVATED)
return EXIT_SUCCESS;
return EXIT_FAILURE;
}
static int response_disconnect(int fd) {
if (handle_connect(fd) == ACTIVATION_STATE_DEACTIVATED)
return EXIT_SUCCESS;
return EXIT_FAILURE;
}
static int response_config(int fd) {
struct command_done_message *msg = receive(fd, MESSAGE_TYPE_COMMAND_DONE,
BASIC_CONNECT_IP_CONFIGURATION);
struct basic_connect_ip_configuration_r *reply = (void *) msg->buffer;
size_t size = le32toh(msg->buffer_length);
uint32_t mtu = -1;
if (size < sizeof(*reply))
err(EXIT_FAILURE, "Response is truncated");
interface(fd);
if (le32toh(reply->ipv4_configuration_available) & IP_CONFIG_FLAG_ADDRESS)
for (size_t i = 0; i < le32toh(reply->ipv4_address_count); i++)
ipv4("address", reply, size, reply->ipv4_address, i, 1);
if (le32toh(reply->ipv6_configuration_available) & IP_CONFIG_FLAG_ADDRESS)
for (size_t i = 0; i < le32toh(reply->ipv6_address_count); i++)
ipv6("address", reply, size, reply->ipv6_address, i, 1);
if (le32toh(reply->ipv4_configuration_available) & IP_CONFIG_FLAG_DNS)
ipv4("gateway", reply, size, reply->ipv4_gateway, 0, 0);
if (le32toh(reply->ipv6_configuration_available) & IP_CONFIG_FLAG_DNS)
ipv6("gateway", reply, size, reply->ipv6_gateway, 0, 0);
if (le32toh(reply->ipv4_configuration_available) & IP_CONFIG_FLAG_DNS)
for (size_t i = 0; i < le32toh(reply->ipv4_dns_server_count); i++)
ipv4("dns", reply, size, reply->ipv4_dns_server, i, 0);
if (le32toh(reply->ipv6_configuration_available) & IP_CONFIG_FLAG_DNS)
for (size_t i = 0; i < le32toh(reply->ipv6_dns_server_count); i++)
ipv6("dns", reply, size, reply->ipv6_dns_server, i, 0);
if (le32toh(reply->ipv4_configuration_available) & IP_CONFIG_FLAG_MTU)
if (le32toh(reply->ipv4_mtu) < mtu)
mtu = le32toh(reply->ipv4_mtu);
if (le32toh(reply->ipv6_configuration_available) & IP_CONFIG_FLAG_MTU)
if (le32toh(reply->ipv6_mtu) < mtu)
mtu = le32toh(reply->ipv6_mtu);
if (mtu + 1 != 0)
printf("mtu %u\n", mtu);
free(msg);
return EXIT_SUCCESS;
}
static int response_interface(int fd) {
return interface(fd);
}
struct response_handler response_handlers[] = {
{ "open", response_open },
{ "close", response_close },
{ "caps", response_caps },
{ "subscriber", response_subscriber },
{ "radio", response_radio },
{ "pin", response_pin },
{ "home", response_home },
{ "visible", response_visible },
{ "register", response_register },
{ "attach", response_attach },
{ "detach", response_detach },
{ "signal", response_signal },
{ "connect", response_connect },
{ "disconnect", response_disconnect },
{ "config", response_config },
{ "interface", response_interface },
{ NULL, NULL }
};