1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <slirp.h>
19
20#define MBUF_THRESH 30
21
22
23
24
25
26#define SLIRP_MSIZE (IF_MTU + IF_MAXLINKHDR + offsetof(struct mbuf, m_dat) + 6)
27
28void
29m_init(Slirp *slirp)
30{
31 slirp->m_freelist.m_next = slirp->m_freelist.m_prev = &slirp->m_freelist;
32 slirp->m_usedlist.m_next = slirp->m_usedlist.m_prev = &slirp->m_usedlist;
33}
34
35void m_cleanup(Slirp *slirp)
36{
37 struct mbuf *m, *next;
38
39 m = slirp->m_usedlist.m_next;
40 while (m != &slirp->m_usedlist) {
41 next = m->m_next;
42 if (m->m_flags & M_EXT) {
43 free(m->m_ext);
44 }
45 free(m);
46 m = next;
47 }
48 m = slirp->m_freelist.m_next;
49 while (m != &slirp->m_freelist) {
50 next = m->m_next;
51 free(m);
52 m = next;
53 }
54}
55
56
57
58
59
60
61
62
63
64struct mbuf *
65m_get(Slirp *slirp)
66{
67 register struct mbuf *m;
68 int flags = 0;
69
70 DEBUG_CALL("m_get");
71
72 if (slirp->m_freelist.m_next == &slirp->m_freelist) {
73 m = (struct mbuf *)malloc(SLIRP_MSIZE);
74 if (m == NULL) goto end_error;
75 slirp->mbuf_alloced++;
76 if (slirp->mbuf_alloced > MBUF_THRESH)
77 flags = M_DOFREE;
78 m->slirp = slirp;
79 } else {
80 m = slirp->m_freelist.m_next;
81 remque(m);
82 }
83
84
85 insque(m,&slirp->m_usedlist);
86 m->m_flags = (flags | M_USEDLIST);
87
88
89 m->m_size = SLIRP_MSIZE - offsetof(struct mbuf, m_dat);
90 m->m_data = m->m_dat;
91 m->m_len = 0;
92 m->m_nextpkt = NULL;
93 m->m_prevpkt = NULL;
94 m->arp_requested = false;
95 m->expiration_date = (uint64_t)-1;
96end_error:
97 DEBUG_ARG("m = %lx", (long )m);
98 return m;
99}
100
101void
102m_free(struct mbuf *m)
103{
104
105 DEBUG_CALL("m_free");
106 DEBUG_ARG("m = %lx", (long )m);
107
108 if(m) {
109
110 if (m->m_flags & M_USEDLIST)
111 remque(m);
112
113
114 if (m->m_flags & M_EXT)
115 free(m->m_ext);
116
117
118
119
120 if (m->m_flags & M_DOFREE) {
121 m->slirp->mbuf_alloced--;
122 free(m);
123 } else if ((m->m_flags & M_FREELIST) == 0) {
124 insque(m,&m->slirp->m_freelist);
125 m->m_flags = M_FREELIST;
126 }
127 }
128}
129
130
131
132
133
134
135void
136m_cat(struct mbuf *m, struct mbuf *n)
137{
138
139
140
141 if (M_FREEROOM(m) < n->m_len)
142 m_inc(m,m->m_size+MINCSIZE);
143
144 memcpy(m->m_data+m->m_len, n->m_data, n->m_len);
145 m->m_len += n->m_len;
146
147 m_free(n);
148}
149
150
151
152void
153m_inc(struct mbuf *m, int size)
154{
155 int datasize;
156
157
158 if(m->m_size>size) return;
159
160 if (m->m_flags & M_EXT) {
161 datasize = m->m_data - m->m_ext;
162 m->m_ext = (char *)realloc(m->m_ext,size);
163 m->m_data = m->m_ext + datasize;
164 } else {
165 char *dat;
166 datasize = m->m_data - m->m_dat;
167 dat = (char *)malloc(size);
168 memcpy(dat, m->m_dat, m->m_size);
169
170 m->m_ext = dat;
171 m->m_data = m->m_ext + datasize;
172 m->m_flags |= M_EXT;
173 }
174
175 m->m_size = size;
176
177}
178
179
180
181void
182m_adj(struct mbuf *m, int len)
183{
184 if (m == NULL)
185 return;
186 if (len >= 0) {
187
188 m->m_data += len;
189 m->m_len -= len;
190 } else {
191
192 len = -len;
193 m->m_len -= len;
194 }
195}
196
197
198
199
200
201int
202m_copy(struct mbuf *n, struct mbuf *m, int off, int len)
203{
204 if (len > M_FREEROOM(n))
205 return -1;
206
207 memcpy((n->m_data + n->m_len), (m->m_data + off), len);
208 n->m_len += len;
209 return 0;
210}
211
212
213
214
215
216
217
218struct mbuf *
219dtom(Slirp *slirp, void *dat)
220{
221 struct mbuf *m;
222
223 DEBUG_CALL("dtom");
224 DEBUG_ARG("dat = %lx", (long )dat);
225
226
227 for (m = slirp->m_usedlist.m_next; m != &slirp->m_usedlist;
228 m = m->m_next) {
229 if (m->m_flags & M_EXT) {
230 if( (char *)dat>=m->m_ext && (char *)dat<(m->m_ext + m->m_size) )
231 return m;
232 } else {
233 if( (char *)dat >= m->m_dat && (char *)dat<(m->m_dat + m->m_size) )
234 return m;
235 }
236 }
237
238 DEBUG_ERROR((dfd, "dtom failed"));
239
240 return (struct mbuf *)0;
241}
242