-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathWuSdp.cpp
152 lines (132 loc) · 3.97 KB
/
WuSdp.cpp
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
#include "WuSdp.h"
#include <stdio.h>
#include <string.h>
#include "WuArena.h"
#include "WuRng.h"
enum SdpParseState { kParseIgnore, kParseType, kParseEq, kParseField };
static bool ValidField(const IceField* field) { return field->length > 0; }
static bool BeginsWith(const char* s, size_t len, const char* prefix,
size_t plen) {
if (plen > len) return false;
for (size_t i = 0; i < plen; i++) {
char a = s[i];
char b = prefix[i];
if (a != b) return false;
}
return true;
}
static bool GetIceValue(const char* field, size_t len, const char* name,
IceField* o) {
if (BeginsWith(field, len, name, strlen(name))) {
for (size_t i = 0; i < len; i++) {
char c = field[i];
if (c == ':') {
size_t valueBegin = i + 1;
if (valueBegin < len) {
size_t valueLength = len - valueBegin;
o->value = field + valueBegin;
o->length = int32_t(valueLength);
return true;
}
break;
}
}
}
return false;
}
static void ParseSdpField(const char* field, size_t len, ICESdpFields* fields) {
GetIceValue(field, len, "ice-ufrag", &fields->ufrag);
GetIceValue(field, len, "ice-pwd", &fields->password);
GetIceValue(field, len, "mid", &fields->mid);
}
bool ParseSdp(const char* sdp, size_t len, ICESdpFields* fields) {
memset(fields, 0, sizeof(ICESdpFields));
SdpParseState state = kParseType;
size_t begin = 0;
size_t length = 0;
for (size_t i = 0; i < len; i++) {
char c = sdp[i];
switch (state) {
case kParseType: {
if (c == 'a') {
state = kParseEq;
} else {
state = kParseIgnore;
}
break;
}
case kParseEq: {
if (c == '=') {
state = kParseField;
begin = i + 1;
length = 0;
break;
} else {
return false;
}
}
case kParseField: {
switch (c) {
case '\n': {
ParseSdpField(sdp + begin, length, fields);
length = 0;
state = kParseType;
break;
}
case '\r': {
state = kParseIgnore;
ParseSdpField(sdp + begin, length, fields);
length = 0;
break;
};
default: { length++; }
}
}
default: {
if (c == '\n') state = kParseType;
}
}
}
return ValidField(&fields->ufrag) && ValidField(&fields->password) &&
ValidField(&fields->mid);
}
const char* GenerateSDP(WuArena* arena, const char* certFingerprint,
const char* serverIp, uint16_t serverPort,
const char* ufrag, int32_t ufragLen, const char* pass,
int32_t passLen, const ICESdpFields* remote,
int* outLength) {
const uint32_t port = uint32_t(serverPort);
char buf[4096];
int32_t length = snprintf(
buf, sizeof(buf),
"{\"answer\":{\"sdp\":\"v=0\\r\\n"
"o=- %u 1 IN IP4 %u\\r\\n"
"s=-\\r\\n"
"t=0 0\\r\\n"
"m=application %u UDP/DTLS/SCTP webrtc-datachannel\\r\\n"
"c=IN IP4 %s\\r\\n"
"a=ice-lite\\r\\n"
"a=ice-ufrag:%.*s\\r\\n"
"a=ice-pwd:%.*s\\r\\n"
"a=fingerprint:sha-256 %s\\r\\n"
"a=ice-options:trickle\\r\\n"
"a=setup:passive\\r\\n"
"a=mid:%.*s\\r\\n"
"a=sctp-port:%u\\r\\n\","
"\"type\":\"answer\"},\"candidate\":{\"sdpMLineIndex\":0,"
"\"sdpMid\":\"%.*s\",\"candidate\":\"candidate:1 1 UDP %u %s %u typ "
"host\"}}",
WuRandomU32(), port, port, serverIp, ufragLen, ufrag, passLen, pass,
certFingerprint, remote->mid.length, remote->mid.value, port,
remote->mid.length, remote->mid.value, WuRandomU32(), serverIp, port);
if (length <= 0 || length >= int32_t(sizeof(buf))) {
return NULL;
}
char* sdp = (char*)WuArenaAcquire(arena, length);
if (!sdp) {
return NULL;
}
memcpy(sdp, buf, length);
*outLength = length;
return sdp;
}