-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtbuf.c
66 lines (56 loc) · 1.21 KB
/
tbuf.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
#include <stdlib.h>
#include <memory.h>
#include "ip.h"
#include "tbuf.h"
int tbuf_free(struct tbuf *buf)
{
buf->payload = buf->start;
if (buf != NULL) {
if (buf->payload != NULL) {
free(buf->payload);
}
free(buf);
return TBUF_OK;
}
return TBUF_FAIL;
}
struct tbuf *tbuf_malloc(__u16 len)
{
struct tbuf *buf = NULL;
buf = malloc(sizeof(struct tbuf));
if (buf == NULL) {
return buf;
}
buf->payload = malloc(MTU_LEN * sizeof(__u8));
buf->start = buf->payload;
memset(buf->payload, 0, len);
buf->len = len;
if (buf->payload == NULL) {
tbuf_free(buf);
buf = NULL;
return buf;
}
return buf;
}
int tbuf_header(struct tbuf *buf, __u16 offset)
{
__u8 *p;
p = buf->start + offset;
if (p < buf->start ||
p > (buf->start + buf->len)) {
return 1;
}
buf->payload = p;
return 0;
}
int tbuf_copy(struct tbuf *dst, struct tbuf *src)
{
if(dst != NULL) {
tbuf_free(dst);
}
dst = tbuf_malloc(src->len);
dst->len = src->len;
dst->payload = dst->start + (src->payload - src->start);
memcpy(dst->start, src->start, src->len);
return 0;
}