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
|
#include "cmumble.h"
#define PREAMBLE_SIZE 6
GStaticMutex write_mutex = G_STATIC_MUTEX_INIT;
static const struct {
const ProtobufCMessageDescriptor *descriptor;
const char *name;
} messages[] = {
#define MUMBLE_MSG(a,b) { &mumble_proto__##b##__descriptor, #a },
MUMBLE_MSGS
#undef MUMBLE_MSG
};
static void
add_preamble(uint8_t *buffer, uint16_t type, uint32_t len)
{
buffer[1] = (type) & 0xff;
buffer[0] = (type >> 8) & 0xff;
buffer[5] = (len) & 0xff;
buffer[4] = (len >> 8) & 0xff;
buffer[3] = (len >> 16) & 0xff;
buffer[2] = (len >> 24) & 0xff;
}
static void
get_preamble(uint8_t *buffer, int *type, int *len)
{
uint16_t msgType;
uint32_t msgLen;
msgType = buffer[1] | (buffer[0] << 8);
msgLen = buffer[5] | (buffer[4] << 8) | (buffer[3] << 16) | (buffer[2] << 24);
*type = (int)msgType;
*len = (int)msgLen;
}
void
send_msg(struct context *ctx, ProtobufCMessage *msg)
{
uint8_t pad[128];
uint8_t preamble[PREAMBLE_SIZE];
int type = -1;
int i;
ProtobufCBufferSimple buffer = PROTOBUF_C_BUFFER_SIMPLE_INIT(pad);
for (i = 0; i < G_N_ELEMENTS(messages); ++i)
if (messages[i].descriptor == msg->descriptor)
type = i;
assert(type >= 0);
if (type == UDPTunnel) {
MumbleProto__UDPTunnel *tunnel = (MumbleProto__UDPTunnel *) msg;
buffer.data = tunnel->packet.data;
buffer.len = tunnel->packet.len;
buffer.must_free_data = 0;
} else {
protobuf_c_message_pack_to_buffer(msg, &buffer.base);
}
add_preamble(preamble, type, buffer.len);
g_static_mutex_lock(&write_mutex);
g_output_stream_write(ctx->output, preamble, PREAMBLE_SIZE, NULL, NULL);
g_output_stream_write(ctx->output, buffer.data, buffer.len, NULL, NULL);
g_static_mutex_unlock(&write_mutex);
PROTOBUF_C_BUFFER_SIMPLE_CLEAR(&buffer);
}
int
recv_msg(struct context *ctx, const struct mumble_callbacks *cbs)
{
uint8_t preamble[PREAMBLE_SIZE];
ProtobufCMessage *msg;
gchar *data;
int type, len;
gssize ret;
const callback_t *callbacks = (const callback_t *) cbs;
GError *error = NULL;
ret = g_pollable_input_stream_read_nonblocking(ctx->input,
preamble, PREAMBLE_SIZE,
NULL, &error);
if (ret <= 0) {
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
return 0;
g_printerr("read failed: %ld\n", ret);
return 0;
}
get_preamble(preamble, &type, &len);
if (!(type >= 0 && type < G_N_ELEMENTS(messages))) {
printf("unknown message type: %d\n", type);
return;
}
if (len <= 0) {
g_printerr("length 0\n");
return;
}
data = g_malloc(len);
if (data == NULL) {
g_printerr("out of mem\n");
g_main_loop_quit (ctx->loop);
}
ret = g_input_stream_read(G_INPUT_STREAM(ctx->input), data, len, NULL, NULL);
/* tunneled udp data - not a regular protobuf message
* create dummy ProtobufCMessage */
if (type == UDPTunnel) {
MumbleProto__UDPTunnel udptunnel;
mumble_proto__udptunnel__init(&udptunnel);
udptunnel.packet.len = len;
udptunnel.packet.data = data;
if (callbacks[UDPTunnel])
callbacks[UDPTunnel](&udptunnel.base, ctx);
g_free(data);
return;
}
msg = protobuf_c_message_unpack(messages[type].descriptor, NULL,
len, data);
if (msg == NULL) {
g_printerr("message unpack failure\n");
return;
}
g_print("debug: received message: %s type:%d, len:%d\n", messages[type].name, type, len);
if (callbacks[type])
callbacks[type](msg, ctx);
protobuf_c_message_free_unpacked(msg, NULL);
g_free(data);
return 1;
}
|