forked from cjdelisle/cjdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFramingIface.c
174 lines (154 loc) · 5.88 KB
/
FramingIface.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
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "interface/FramingIface.h"
#include "interface/Iface.h"
#include "memory/Allocator.h"
#include "util/Identity.h"
#include "wire/Error.h"
struct MessageList;
struct MessageList {
struct Message* msg;
struct MessageList* next;
};
struct FramingIface_pvt {
struct FramingIface pub;
const uint32_t maxMessageSize;
struct Allocator* alloc;
uint32_t padding;
// fields specific to this frame.
uint32_t bytesRemaining;
struct Allocator* frameAlloc;
struct MessageList* frameParts;
union {
uint32_t length_be;
uint8_t bytes[4];
} header;
uint32_t headerIndex;
Identity
};
static struct Message* mergeMessage(struct FramingIface_pvt* fi, struct Message* last)
{
int length = last->length;
// The only accurate way to get the full length because this last might contain
// the beginning of the next frame.
struct MessageList* part = fi->frameParts;
for (part = fi->frameParts; part; part = part->next) {
length += part->msg->length;
}
struct Message* out = Message_new(0, length + fi->padding, fi->frameAlloc);
Message_push(out, last->bytes, last->length, NULL);
for (part = fi->frameParts; part; part = part->next) {
Message_push(out, part->msg->bytes, part->msg->length, NULL);
}
Assert_true(length <= out->length);
return out;
}
static Iface_DEFUN receiveMessage(struct Message* msg, struct Iface* streamIf)
{
struct FramingIface_pvt* fi =
Identity_containerOf(streamIf, struct FramingIface_pvt, pub.streamIf);
if (fi->bytesRemaining > fi->maxMessageSize) {
// Oversize message
return NULL;
}
if (fi->frameParts) {
if (fi->bytesRemaining <= (uint32_t)msg->length) {
struct Message* wholeMessage = mergeMessage(fi, msg);
fi->bytesRemaining = 0;
fi->frameParts = NULL;
Assert_true(fi->headerIndex == 0);
struct Allocator* frameAlloc = fi->frameAlloc;
fi->frameAlloc = NULL;
// Run the message through again since it's almost certainly not perfect size.
Iface_CALL(receiveMessage, wholeMessage, streamIf);
Allocator_free(frameAlloc);
return NULL;
}
fi->bytesRemaining -= msg->length;
Allocator_adopt(fi->frameAlloc, msg->alloc);
struct MessageList* parts = Allocator_malloc(fi->frameAlloc, sizeof(struct MessageList));
parts->msg = msg;
parts->next = fi->frameParts;
fi->frameParts = parts;
return NULL;
}
for (;;) {
while (fi->headerIndex < 4) {
if (!msg->length) {
return NULL;
}
fi->header.bytes[fi->headerIndex] = msg->bytes[0];
Message_shift(msg, -1, NULL);
fi->headerIndex++;
}
fi->headerIndex = 0;
fi->bytesRemaining = Endian_bigEndianToHost32(fi->header.length_be);
if (fi->bytesRemaining > fi->maxMessageSize) {
// oversize
return NULL;
}
if (fi->bytesRemaining == (uint32_t)msg->length) {
fi->bytesRemaining = 0;
return Iface_next(&fi->pub.messageIf, msg);
} else if (fi->bytesRemaining < (uint32_t)msg->length) {
struct Allocator* alloc = Allocator_child(msg->alloc);
struct Message* m = Message_new(fi->bytesRemaining, fi->padding, alloc);
Bits_memcpy(m->bytes, msg->bytes, fi->bytesRemaining);
Message_shift(msg, -fi->bytesRemaining, NULL);
fi->bytesRemaining = 0;
Iface_send(&fi->pub.messageIf, m);
Allocator_free(alloc);
continue;
} else {
fi->frameAlloc = Allocator_child(fi->alloc);
struct Message* m = Allocator_calloc(fi->frameAlloc, sizeof(struct Message), 1);
m->capacity = m->length = msg->length + 4;
m->bytes = Allocator_malloc(fi->frameAlloc, m->length);
m->alloc = fi->frameAlloc;
Message_shift(m, -m->length, NULL);
Message_push(m, msg->bytes, msg->length, NULL);
Message_push(m, fi->header.bytes, 4, NULL);
fi->bytesRemaining -= msg->length;
fi->frameParts = Allocator_malloc(fi->frameAlloc, sizeof(struct MessageList));
fi->frameParts->msg = m;
fi->frameParts->next = NULL;
}
return NULL;
}
}
static Iface_DEFUN sendMessage(struct Message* msg, struct Iface* messageIf)
{
struct FramingIface_pvt* fi =
Identity_containerOf(messageIf, struct FramingIface_pvt, pub.messageIf);
Message_push32(msg, msg->length, NULL);
return Iface_next(&fi->pub.streamIf, msg);
}
struct FramingIface* FramingIface_new(uint32_t maxMsgSize,
uint32_t padding,
struct Allocator* alloc)
{
struct FramingIface_pvt* context =
Allocator_clone(alloc, (&(struct FramingIface_pvt) {
.maxMessageSize = maxMsgSize,
.alloc = alloc,
.padding = padding,
.pub = {
.streamIf = { .send = receiveMessage },
.messageIf = { .send = sendMessage }
}
}));
Identity_set(context);
return &context->pub;
}