-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
236 lines (193 loc) · 6.18 KB
/
main.cpp
File metadata and controls
236 lines (193 loc) · 6.18 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
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
#include "main.h"
#include "time.h"
#include "utils.h"
#include "media.h"
static const std::string USAGE = "Usage: bubble_client server_ip server_port username password";
#define BUF_SIZE (2 * 1024)
static char buffer[BUF_SIZE];
static MediaSession *p_media_session = NULL;
int main(int argc, char *argv[])
{
if (argc != 5)
{
std::printf("%s\n", USAGE.c_str());
return 1;
}
const std::string server_ip(argv[1]);
const int server_port = atoi(argv[2]);
const std::string username(argv[3]);
const std::string password(argv[4]);
int status;
Session session(server_ip, server_port);
status = init_bubble_session(session);
if (status != 0)
{
return 1;
}
std::printf("[INFO] Bubble session initialized\n");
if (!verify_user(session, username, password))
{
LOG_WARN("Username or password is invalid");
return 1;
}
std::printf("[INFO] User is logged in\n");
status = open_stream(session, 0, 1);
if (status != 0)
{
LOG_ERR("Failed to open stream");
return 1;
}
std::printf("[INFO] Stream is opened\n");
start_media_session(session);
return 0;
}
void start_media_session(Session& session)
{
MediaSession media_session(&session);
signal(SIGINT, signal_handler);
p_media_session = &media_session;
media_session.start();
p_media_session = NULL;
}
void signal_handler(int)
{
std::printf("[INFO] Received interrupt!\n");
p_media_session->isRunning = false;
}
PackHead* write_packhead(uint data_size, char cPackType, char *buffer)
{
PackHead *packhead = (PackHead *)buffer;
struct timespec timetic;
packhead->cHeadChar = PACKHEAD_MAGIC;
packhead->cPackType = cPackType;
clock_gettime(CLOCK_MONOTONIC, &timetic);
packhead->uiTicket = htonl(timetic.tv_sec + timetic.tv_nsec/1000);
packhead->uiLength = htonl(STRUCT_MEMBER_POS(PackHead, pData)
- STRUCT_MEMBER_POS(PackHead, cPackType)
+ data_size);
return packhead;
}
bool check_packet_len(uint32_t uiPackLength, size_t expected_data_size)
{
return uiPackLength == expected_data_size +
(STRUCT_MEMBER_POS(PackHead, pData) - STRUCT_MEMBER_POS(PackHead, cPackType));
}
int init_bubble_session(Session& session)
{
const char init_session_req[] = "GET /bubble/live?ch=0&stream=0 HTTP/1.1\r\n\r\n";
const size_t init_resp_size = 1142;
int nbytes;
nbytes = session.send(init_session_req, sizeof(init_session_req) - 1);
if (nbytes < 0 || (size_t)nbytes != sizeof(init_session_req) - 1)
{
LOG_ERR("Failed to send session init request");
return -1;
}
nbytes = session.receive_til_full(buffer, init_resp_size);
if (nbytes <= 0)
{
LOG_ERR("Failed to initialize bubble session");
return -1;
}
return 0;
}
int open_stream(Session& session, uint channel, uint stream_id)
{
PackHead *packhead;
BubbleOpenStream openStreamPack;
size_t packsize;
int nbytes;
std::printf("[INFO] Opening stream: channel=%u stream_id=%u\n", channel, stream_id);
packsize = GET_PACKSIZE(sizeof(BubbleOpenStream));
assert(packsize <= BUF_SIZE);
packhead = write_packhead(sizeof(BubbleOpenStream), PT_OPENSTREAM, buffer);
memset(&openStreamPack, 0, sizeof(BubbleOpenStream));
openStreamPack.uiChannel = channel;
openStreamPack.uiStreamId = stream_id;
openStreamPack.uiOpened = 1;
memcpy(packhead->pData, &openStreamPack, sizeof(BubbleOpenStream));
nbytes = session.send(buffer, packsize);
if (nbytes < 0 || (size_t)nbytes != packsize)
{
LOG_ERR("Failed to send open stream packet");
return -1;
}
return 0;
}
bool verify_user(Session& session, const std::string& username, const std::string& password)
{
if (username.length() > MAX_USERNAME_LEN || password.length() > MAX_PASSWORD_LEN)
{
LOG_WARN("Username or password is too long");
}
if (!send_user_creds(session, username, password))
{
return false;
}
return recv_verify_user_result(session);
}
bool recv_verify_user_result(Session& session)
{
char *packet = session.receive_packet();
PackHead *packhead;
uint32_t uiPackLength;
MsgPackData *msgpack;
UserVrfB *vrfResult;
if (!packet)
{
LOG_ERR("Failed to receive user verification result");
return false;
}
packhead = (PackHead*) packet;
if (packhead->cPackType != 0x00)
{
std::fprintf(stderr, "[ERROR] Packet type %02x is invalid\n", packhead->cPackType);
goto on_error;
}
uiPackLength = ntohl(packhead->uiLength);
if (!check_packet_len(uiPackLength, sizeof(UserVrfB) + STRUCT_MEMBER_POS(MsgPackData, pMsg)))
{
LOG_ERR("Packet size is invalid");
goto on_error;
}
msgpack = (MsgPackData *)packhead->pData;
if (msgpack->cMsgType[0] != MSGT_USERVRF_B)
{
std::fprintf(stderr, "[ERROR] Message type %02x is invalid\n", msgpack->cMsgType[0]);
goto on_error;
}
vrfResult = (UserVrfB *)msgpack->pMsg;
if (vrfResult->bVerify != 1)
{
LOG_ERR("Username or password is invalid");
goto on_error;
}
delete[] packet;
return true;
on_error:
delete[] packet;
return false;
}
bool send_user_creds(Session& session, const std::string& username, const std::string& password)
{
int packsize = GET_PACKSIZE(STRUCT_MEMBER_POS(MsgPackData, pMsg) + sizeof(UserVrf));
assert(packsize <= BUF_SIZE);
PackHead* packhead;
MsgPackData msg;
UserVrf usr_creds;
packhead = write_packhead(STRUCT_MEMBER_POS(MsgPackData, pMsg) + sizeof(UserVrf),
PT_MSGPACK, buffer);
memset(&msg, 0, sizeof(MsgPackData));
msg.cMsgType[0] = MSGT_USERVRF;
msg.uiLength = htonl(sizeof(msg.cMsgType) + sizeof(UserVrf));
strncpy((char*)usr_creds.sUserName, username.c_str(), MAX_USERNAME_LEN);
strncpy((char*)usr_creds.sPassWord, password.c_str(), MAX_PASSWORD_LEN);
memcpy(packhead->pData, &msg, STRUCT_MEMBER_POS(MsgPackData, pMsg));
memcpy(packhead->pData + STRUCT_MEMBER_POS(MsgPackData, pMsg), &usr_creds, sizeof(UserVrf));
int nbytes = session.send(buffer, packsize);
if (nbytes != packsize) {
LOG_ERR("Failed to send user credentials");
return false;
}
return true;
}