-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.c
135 lines (116 loc) · 3.78 KB
/
device.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
#include <dirent.h>
#include <endian.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <linux/usb/cdc-wdm.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include "mbim.h"
void *buffer(int fd) {
uint16_t size = 512;
struct header *header;
ioctl(fd, IOCTL_WDM_MAX_COMMAND, &size);
header = calloc(size, 1);
if (header == NULL)
err(EXIT_FAILURE, "calloc");
header->length = htole32(size);
header->transaction_id = getpid();
return header;
}
int device(const char *path) {
int fd = open(path, O_RDWR);
if (fd < 0)
err(EXIT_FAILURE, "open");
if (flock(fd, LOCK_EX) < 0)
err(EXIT_FAILURE, "flock");
return fd;
}
void interface(int fd) {
struct stat status;
char *path = NULL;
if (fstat(fd, &status) < 0)
err(EXIT_FAILURE, "fstat");
if (!S_ISCHR(status.st_mode))
return;
if (asprintf(&path, "/sys/dev/char/%u:%u/device/net",
major(status.st_rdev), minor(status.st_rdev)) >= 0) {
struct dirent **list = NULL;
ssize_t count = scandir(path, &list, NULL, alphasort);
for (ssize_t i = 0; i < count; i++) {
if (list[i]->d_name[0] != '.')
printf("interface %s\n", list[i]->d_name);
free(list[i]);
}
free(list);
free(path);
}
if (asprintf(&path, "/sys/dev/char/%u:%u/device/device/net",
major(status.st_rdev), minor(status.st_rdev)) >= 0) {
struct dirent **list = NULL;
ssize_t count = scandir(path, &list, NULL, alphasort);
for (ssize_t i = 0; i < count; i++) {
if (list[i]->d_name[0] != '.')
printf("interface %s\n", list[i]->d_name);
free(list[i]);
}
free(list);
free(path);
}
}
void *receive(int fd, uint32_t type, uint32_t cid) {
struct header *header = buffer(fd);
ssize_t count, size = le32toh(header->length);
uint32_t transaction = header->transaction_id;
do {
count = read(fd, header, size);
if (count < 0) {
if (errno != EAGAIN && errno != EINTR)
err(EXIT_FAILURE, "read");
continue;
}
if (count < (ssize_t) sizeof(struct header))
errx(EXIT_FAILURE, "Response is truncated");
if (count < le32toh(header->length))
errx(EXIT_FAILURE, "Response is truncated");
} while (header->transaction_id != transaction);
if (le32toh(header->type) == MESSAGE_TYPE_FUNCTION_ERROR) {
struct error_message *msg = (void *) header;
if (count < (ssize_t) sizeof(struct error_message))
errx(EXIT_FAILURE, "Error response is truncated");
for (struct map *e = function_error_messages; e->value; e++)
if (e->key == le32toh(msg->error_status_code))
errx(EXIT_FAILURE, "%s", e->value);
errx(EXIT_FAILURE, "Request failed with status %u",
le32toh(msg->error_status_code));
}
if (le32toh(header->type) != type)
errx(EXIT_FAILURE, "Response has unexpected type");
if (le32toh(header->type) == MESSAGE_TYPE_COMMAND_DONE) {
struct command_done_message *msg = (void *) header;
count -= sizeof(struct command_done_message);
if (count < 0 || count < le32toh(msg->buffer_length))
errx(EXIT_FAILURE, "Response is truncated");
if (msg->command_id != cid)
errx(EXIT_FAILURE, "Response has unexpected command id");
if (msg->status_code != STATUS_SUCCESS) {
for (struct map *e = command_error_messages; e->value; e++)
if (e->key == le32toh(msg->status_code))
errx(EXIT_FAILURE, "%s", e->value);
errx(EXIT_FAILURE, "Command failed with status %u",
le32toh(msg->status_code));
}
}
return header;
}
void transmit(int fd, const void *msg) {
const struct header *header = msg;
while (write(fd, header, le32toh(header->length)) < 0)
if (errno != EAGAIN && errno != EINTR)
err(EXIT_FAILURE, "write");
}