-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskbuff.c
161 lines (135 loc) · 3.04 KB
/
skbuff.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
/*
* File: skbuff.c
* Implements: sk_buff handling a la Linux kernel
*
* Copyright: Jens Låås, 2011
* Copyright license: According to GPL, see file COPYING in this directory.
*
*/
#include <stdlib.h>
#include <string.h>
#include "skbuff.h"
struct sk_buff *alloc_skb(unsigned int size)
{
struct sk_buff *skb;
void *data;
data = malloc(size);
if(!data) return NULL;
skb = malloc(sizeof(struct sk_buff));
if(!skb) {
free(data);
return NULL;
}
memset(skb, 0, sizeof(struct sk_buff));
skb->head = data;
skb->data = skb->tail = skb->head;
skb->end = skb->head + size;
return skb;
}
void skb_reset(struct sk_buff *skb)
{
skb->data = skb->head;
skb->tail = skb->head;
skb->len = 0;
}
void free_skb(struct sk_buff *skb)
{
free(skb->head);
memset(skb, 0, sizeof(struct sk_buff));
free(skb);
}
struct sk_buff *skb_clone(struct sk_buff *skb)
{
struct sk_buff *nskb;
nskb = malloc(sizeof(struct sk_buff));
if(!nskb) {
return NULL;
}
memcpy(nskb, skb, sizeof(struct sk_buff));
return nskb;
}
struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
int newheadroom, int newtailroom)
{
struct sk_buff *nskb;
void *ndata;
ndata = malloc(skb->len + newheadroom + newtailroom);
if(!ndata) return NULL;
nskb = malloc(sizeof(struct sk_buff));
if(!nskb) {
free(ndata);
return NULL;
}
memcpy(nskb, skb, sizeof(struct sk_buff));
memcpy(ndata + newheadroom, skb->data, skb->len);
nskb->head = ndata;
nskb->data = ndata + newheadroom;
nskb->end = ndata + skb->len + newheadroom + newtailroom;
nskb->tail = ndata + skb->len + newheadroom;
return nskb;
}
/*
* make copy of skb keep headroom and tailroom
*/
struct sk_buff *skb_copy(const struct sk_buff *skb)
{
struct sk_buff *nskb;
void *ndata;
ndata = malloc(skb->end - skb->head);
if(!ndata) return NULL;
nskb = malloc(sizeof(struct sk_buff));
if(!nskb) {
free(ndata);
return NULL;
}
memcpy(nskb, skb, sizeof(struct sk_buff));
memcpy(ndata, skb->head, skb->end - skb->head);
nskb->head = ndata;
nskb->data = ndata + skb_headroom(skb);
nskb->end = ndata + (skb->end - skb->head);
nskb->tail = nskb->data + skb->len;
return nskb;
}
/* create headroom */
void skb_reserve(struct sk_buff *skb, int len)
{
skb->data += len;
skb->tail += len;
}
/* query headroom */
unsigned int skb_headroom(const struct sk_buff *skb)
{
return skb->data - skb->head;
}
/* query tailroom */
int skb_tailroom(const struct sk_buff *skb)
{
return skb->end - skb->tail;
}
/* append data */
unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
{
unsigned char *tmp = skb->tail;
skb->tail += len;
skb->len += len;
return tmp;
}
/* prepend data */
unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
{
skb->data -= len;
skb->len += len;
return skb->data;
}
/* remove data from head */
unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
{
skb->len -= len;
return skb->data += len;
}
/* set absolute length. Can be used tio remove data from tail */
void skb_trim(struct sk_buff *skb, unsigned int len)
{
skb->len = len;
skb->tail = skb->data + len;
}