forked from gjedeer/tuntox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
256 lines (218 loc) · 5.3 KB
/
util.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
#include "log.h"
#include "util.h"
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <string.h>
#include <tox/tox.h>
#include <stdio.h>
#include <stdlib.h>
void writechecksum(uint8_t *address)
{
uint8_t *checksum = address + 36;
uint32_t i;
for (i = 0; i < 36; ++i)
checksum[i % 2] ^= address[i];
}
/* From utox/util.c */
void to_hex(char_t *a, const char_t *p, int size)
{
char_t b, c;
const char_t *end = p + size;
while(p != end) {
b = *p++;
c = (b & 0xF);
b = (b >> 4);
if(b < 10) {
*a++ = b + '0';
} else {
*a++ = b - 10 + 'A';
}
if(c < 10) {
*a++ = c + '0';
} else {
*a++ = c - 10 + 'A';
}
}
*a = '\0';
}
/* From utox/util.c */
void id_to_string(char_t *dest, const char_t *src)
{
to_hex(dest, src, TOX_ADDRESS_SIZE);
}
/* From utox/util.c */
int string_to_id(char_t *w, char_t *a)
{
char_t *end = w + TOX_ADDRESS_SIZE;
while(w != end) {
char_t c, v;
c = *a++;
if(c >= '0' && c <= '9') {
v = (c - '0') << 4;
} else if(c >= 'A' && c <= 'F') {
v = (c - 'A' + 10) << 4;
} else if(c >= 'a' && c <= 'f') {
v = (c - 'a' + 10) << 4;
} else {
return 0;
}
c = *a++;
if(c >= '0' && c <= '9') {
v |= (c - '0');
} else if(c >= 'A' && c <= 'F') {
v |= (c - 'A' + 10);
} else if(c >= 'a' && c <= 'f') {
v |= (c - 'a' + 10);
} else {
return 0;
}
*w++ = v;
}
return 1;
}
/* Parse the -L parameter */
/* 0 = success */
int parse_local_port_forward(char *string, int *local_port, char **hostname, int *remote_port)
{
char *lport;
char *host;
char *rport;
/* First replace all @ with :, ':' is forbidden in some environments */
char *p = string;
while(*p)
{
if(*p == '@') *p = ':';
p++;
}
lport = strtok(string, ":");
host = strtok(NULL, ":");
rport = strtok(NULL, ":");
if(!lport || !host || !rport)
{
return -1;
}
*local_port = atoi(lport);
*hostname = host;
*remote_port = atoi(rport);
return 0;
}
/* Parse the -W parameter */
/* 0 = success */
int parse_pipe_port_forward(char *string, char **hostname, int *remote_port)
{
char *host;
char *rport;
/* First replace all @ with :, ':' is forbidden in some environments */
char *p = string;
while(*p)
{
if(*p == '@') *p = ':';
p++;
}
host = strtok(string, ":");
rport = strtok(NULL, ":");
if(!host || !rport)
{
return -1;
}
*hostname = host;
*remote_port = atoi(rport);
return 0;
}
void* file_raw(char *path, uint32_t *size)
{
FILE *file;
char *data;
int len;
file = fopen(path, "rb");
if(!file) {
log_printf(L_WARNING, "File not found (%s)\n", path);
return NULL;
}
fseek(file, 0, SEEK_END);
len = ftell(file);
if(len <= 0)
{
fclose(file);
return NULL;
}
data = malloc(len);
if(!data) {
fclose(file);
return NULL;
}
fseek(file, 0, SEEK_SET);
if(fread(data, len, 1, file) != 1) {
log_printf(L_WARNING, "Read error (%s)\n", path);
fclose(file);
free(data);
return NULL;
}
fclose(file);
log_printf(L_DEBUG, "Read %u bytes (%s)\n", len, path);
if(size) {
*size = len;
}
return data;
}
const char *readable_connection_status(TOX_CONNECTION status)
{
switch(status)
{
case TOX_CONNECTION_NONE:
return "There is no connection";
case TOX_CONNECTION_TCP:
return "A TCP connection has been established (via TCP relay)";
case TOX_CONNECTION_UDP:
return "An UDP connection has been established";
default:
log_printf(L_WARNING, "Received unknown connection status %d\n", (int)status);
return "Unknown connection status";
}
}
/* From https://github.com/TokTok/c-toxcore/blob/master/other/fun/cracker.c */
size_t hex_string_to_bin(const char *hex_string, size_t hex_len, uint8_t *bytes)
{
size_t i;
const char *pos = hex_string;
// make even
for (i = 0; i < hex_len / 2; ++i, pos += 2) {
uint8_t val;
if (sscanf(pos, "%02hhx", &val) != 1) {
return 0;
}
bytes[i] = val;
}
if (i * 2 < hex_len) {
uint8_t val;
if (sscanf(pos, "%hhx", &val) != 1) {
return 0;
}
bytes[i] = (uint8_t)(val << 4);
++i;
}
return i;
}
/* Very stupid test to filter out hostnames */
bool is_valid_ipv4(const char *ip_address)
{
unsigned int a,b,c,d;
return sscanf(ip_address,"%u.%u.%u.%u", &a, &b, &c, &d) == 4;
}
bool is_valid_ipv6(const char *ip_address)
{
struct in6_addr result;
return (inet_pton(AF_INET6, ip_address, &result) == 1);
}
void save_printable_tox_id(const unsigned char *tox_printable_id, const char *path)
{
FILE *f = fopen(path, "w");
if(!f)
{
log_printf(L_ERROR, "Could not write to %s: %d %s", path, errno, strerror(errno));
}
log_printf(L_DEBUG, "Writing Tox ID to %s", path);
fputs((char*)tox_printable_id, f);
fclose(f);
}