-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmessage.c
More file actions
175 lines (163 loc) · 5.28 KB
/
message.c
File metadata and controls
175 lines (163 loc) · 5.28 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
169
170
171
172
173
174
175
#include "message.h"
#include <unistd.h>
#include <math.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <regex.h>
#include <stdio.h>
#include <sys/socket.h>
#define HEADER_LEN 4 // TODO: Rename to ADBP_*
/*
* Convert digit string of `base` and `len` to base 10 num
* in base 10
*/
unsigned int to_base10(char *d_str, int len, int base)
{
if (len < 1) {
return 0;
}
char d = d_str[0];
// chars 0-9 = 48-57, chars a-f = 97-102
int val = (d > 57) ? d - ('a' - 10) : d - '0';
int result = val * pow(base, (len - 1));
d_str++; // increment pointer
return result + to_base10(d_str, len - 1, base);
}
int parse_content_len(char *payload, int payload_len, int *content_len)
{
// TODO: Have it detect null termination (and remove it from len)
int result = to_base10(payload, HEADER_LEN, 16);
if (result != payload_len - HEADER_LEN) {
// Unparsable packet (happens if message not intended
// for ADB server). Set -1 for invalid content length
content_len = NULL;
return 0;
}
if (content_len != NULL) {
*content_len = result;
}
return HEADER_LEN;
}
// TODO: Don't use regex, just prefix()
// then payload doesn't need null termination
int parse_host_prefix(char *payload_nt, enum adbp_transport *transport)
{
regex_t regex;
regmatch_t pm;
int result;
result = regcomp(®ex, "^reverse:", REG_EXTENDED);
if (result != 0) {
perror("Reverse regex did not compile");
exit(EXIT_FAILURE);
}
result = regexec(®ex, payload_nt, 1, &pm, 0);
if (result == 0) {
*transport = ADBP_ADBD_REVERSE_TRANSPORT;
return pm.rm_eo;
}
result = regcomp(®ex, "^host:|host-usb:|host-local:|host-serial:[A-Za-z0-9-]{1,20}:", REG_EXTENDED);
if (result != 0) {
perror("Host regex did not compile");
exit(EXIT_FAILURE);
}
result = regexec(®ex, payload_nt, 1, &pm, 0);
if (result == 0) {
*transport = ADBP_ADB_SERVER_TRANSPORT;
return pm.rm_eo;
}
// Assume packet is directly for adbp
*transport = ADBP_ADBD_TRANSPORT;
return 0;
}
int prefix(const char *pre, const char *str)
{
return strncmp(pre, str, strlen(pre)) == 0;
}
int parse_forward_type(char *payload_nt, enum adbp_request_type *req_type)
{
// TODO: Make the 'jumping' of these chars different? ':'
char forward_prefix[] = "forward";
char forward_kill_prefix[] = "killforward";
char forward_kill_all_prefix[] = "killforward-all";
if (prefix(forward_prefix, payload_nt)) {
*req_type = ADBP_FORWARD;
// Don't -1 in order to 'jump' over the ':'
return sizeof(forward_prefix);
} else if (prefix(forward_kill_all_prefix, payload_nt)) {
*req_type = ADBP_FORWARD_KILL_ALL;
return sizeof(forward_kill_all_prefix) - 1;
} else if (prefix(forward_kill_prefix, payload_nt)) {
*req_type = ADBP_FORWARD_KILL;
return sizeof(forward_kill_prefix);
} else {
*req_type = ADBP_UNDEFINED;
return 0;
}
}
int parse_next_port_num(char *payload_nt, int *port)
{
regex_t regex;
regmatch_t pm;
int result;
char prefix[4] = "tcp:"; // not null-terminated
// TODO: sprintf prefix into this string v
// TODO: Don't use regex (just prefix())
result = regcomp(®ex, "^tcp:[[:digit:]]{1,5}", REG_EXTENDED);
if (result != 0) {
perror("Regex did not compile");
exit(EXIT_FAILURE);
}
result = regexec(®ex, payload_nt, 1, &pm, 0);
if (result != 0) {
*port = 0;
return 0;
}
// match offset - chars in 'tcp:' prefix + null terminating char
char port_str[pm.rm_eo - sizeof(prefix) + 1];
memcpy(port_str, payload_nt + sizeof(prefix), sizeof(port_str));
port_str[sizeof(port_str) - 1] = '\0';
*port = atoi(port_str);
if (payload_nt[pm.rm_eo] == ';')
{
// Jump over ';', e.g., 'tcp:1000;'
return pm.rm_eo + 1;
} else {
return pm.rm_eo;
}
}
void parse_payload(adbp_forward_req *req, char *payload, int payload_len) {
int bytes_parsed = 0;
// Default req to invalid
req->valid = 0;
// Parse content len
bytes_parsed += parse_content_len(payload, payload_len, NULL);
if (bytes_parsed == 0) {
// not valid packet for adb server (probably meant for adbd)
return;
}
// Parse host prefix
bytes_parsed += parse_host_prefix(payload + bytes_parsed, &(req->req_transport));
if (req->req_transport == ADBP_ADBD_TRANSPORT) {
// packet meant for adbd
return;
}
// Parse forwarding type
bytes_parsed += parse_forward_type(payload + bytes_parsed, &(req->req_type));
if (req->req_type == ADBP_UNDEFINED) {
// host/reverse packet contains different request type
return;
}
// Parse ports
if (req->req_type == ADBP_FORWARD) {
// Parse out local port
bytes_parsed += parse_next_port_num(payload + bytes_parsed, &(req->local_port));
// Parse out device port
parse_next_port_num(payload + bytes_parsed, &(req->device_port));
} else if (req->req_type == ADBP_FORWARD_KILL) {
// Parse out local port
parse_next_port_num(payload + bytes_parsed, &(req->local_port));
}
// If finished parsing, req is valid
req->valid = 1;
}