-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathutil.c
executable file
·213 lines (173 loc) · 4.71 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
// util.c
// TODO: change this to c++/STL
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define __USE_GNU
#include <sys/socket.h>
#include <netinet/ip.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <time.h>
#include <math.h>
#include <fcntl.h>
#include <pthread.h>
#include <openssl/ssl.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/md5.h>
/*
#include "boolhack.h"
#include "prototype.h"
#include "rtp.h"
#include "crc32.h"
#include "srtp_priv.h"
#include "srtp_key_len.h"
#include "srtp.h"
*/
#include "debug.h"
#include "u32helper.h"
#include "memdebughack.h"
#include "iplookup_hack.h"
char g_malloc_debug_buffers[N_DEBUG_BUFFERS][DEBUG_STR_SIZE];
volatile dbg_int_t g_malloc_debug_buffers_next;
volatile dbg_int_t g_malloc_debug_buffers_free_last;
const static char *sdp_read_fail = "";
char sdp_read_resultbuf[1024];
int
str_read(const char* src, char* dest, char *endChars, unsigned int maxlen)
{
char* pDest = dest;
while (*src != '\0' && pDest - dest < maxlen-1) {
char* pDelim = endChars;
while(*pDelim) { if (*pDelim == *src) {pDelim = NULL; break;} else pDelim++; }
if(!pDelim) break;
*pDest = *src;
src++;
pDest++;
}
*pDest = '\0';
return pDest - dest;
}
int
str_read_from_key(char* key, char* buf, char* dest, char* endchars, unsigned int maxlen, int index)
{
char *p = buf;
while(index >= 0)
{
p = strstr(p, key);
if(!p) return 0;
p += strlen(key);
index--;
}
return str_read(p, dest, endchars, maxlen);
}
char str_read_key_buf[4096];
char* str_read_unsafe(char* buf, char* key, int index)
{
char* delim = ":\r\n";
memset(str_read_key_buf, 0, sizeof(str_read_key_buf));
/* hack: */
int result =
str_read_from_key(key, buf, str_read_key_buf, delim, sizeof(str_read_key_buf), index);
return str_read_key_buf;
}
char* str_read_unsafe_delim(char* buf, char* key, int index, char* delim)
{
memset(str_read_key_buf, 0, sizeof(str_read_key_buf));
/* hack: */
str_read_from_key(key, buf, str_read_key_buf, delim, sizeof(str_read_key_buf), index);
return str_read_key_buf;
}
char* str_read_unsafe_allowedchars(char* buf, char* key, int index, const char* allowedchars)
{
char *offset = buf;
char *p = buf;
memset(str_read_key_buf, 0, sizeof(str_read_key_buf));
while(1)
{
p = strstr(offset, key);
if(!p) return str_read_key_buf;
offset = p + strlen(key);
if(index == 0) break;
index -= 1;
}
if(index == 0 && offset != NULL)
{
p = offset;
while(1)
{
int allowed = 0;
for(int i = 0; i < strlen(allowedchars); i++)
{
if(*p == allowedchars[i]) { allowed = 1; break; }
}
if(!allowed) break;
p += 1;
}
strncpy(str_read_key_buf, offset, p-offset);
}
return str_read_key_buf;
}
void
hex_print(char* dest, unsigned char *buf, int buf_len)
{
dest[0] = '\0';
int k = 0;
while(k < buf_len && buf) {
char tmp[64];
sprintf(tmp, "%02x", (unsigned char) buf[k]);
strcat(dest, tmp);
k++;
}
}
const char*
sdp_read(const char* sdp, const char* key)
{
int l = 0;
char *p = strstr(sdp, key);
if(p) {
p += strlen(key);
while(*p != '\r' && *p != '\n' && *p != '\0' && l < sizeof(sdp_read_resultbuf)-1) {
sdp_read_resultbuf[l] = *p;
p++;
l++;
}
sdp_read_resultbuf[l] = '\0';
return sdp_read_resultbuf;
}
return sdp_read_fail;
}
const char* websocket_header_upgrade_token = "Sec-WebSocket-Key: ";
char* websocket_accept_header(const char* headers_buf, char storage[256]) {
char buf[512], result[512];
const char* header_token = websocket_header_upgrade_token;
const char* ws_const = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
char* key = strstr(headers_buf, header_token);
memset(storage, 0, 256);
if(!key) {
return storage;
}
key += strlen(header_token);
int l = 0;
while(key[l] != '\r' && key[l] != '\n' && key[l] != '\0') l++;
strncpy(buf, key, l);
buf[l] = '\0';
strcat(buf, ws_const);
#if !DTLS_BUILD_WITH_BORINGSSL
sha1(buf, strlen(buf), result);
#else
#warning "websocket_accept_header not implemented with boringssl"
#endif
EVP_ENCODE_CTX ctx;
int b64_len = 0;
EVP_EncodeInit(&ctx);
EVP_EncodeUpdate(&ctx, storage, &b64_len, result, strlen(result));
EVP_EncodeFinal(&ctx, storage, &b64_len);
return storage;
}