1 | /*
|
---|
2 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
3 | *
|
---|
4 | * Please read the file COPYRIGHT for the
|
---|
5 | * terms and conditions of the copyright.
|
---|
6 | */
|
---|
7 |
|
---|
8 | #define WANT_SYS_IOCTL_H
|
---|
9 | #include <slirp.h>
|
---|
10 |
|
---|
11 | #ifndef HAVE_INET_ATON
|
---|
12 | int
|
---|
13 | inet_aton(const char *cp, struct in_addr *ia)
|
---|
14 | {
|
---|
15 | u_int32_t addr = inet_addr(cp);
|
---|
16 | if (addr == 0xffffffff)
|
---|
17 | return 0;
|
---|
18 | ia->s_addr = addr;
|
---|
19 | return 1;
|
---|
20 | }
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * Get our IP address and put it in our_addr
|
---|
25 | */
|
---|
26 | void
|
---|
27 | getouraddr(PNATState pData)
|
---|
28 | {
|
---|
29 | our_addr.s_addr = loopback_addr.s_addr;
|
---|
30 | }
|
---|
31 |
|
---|
32 | struct quehead
|
---|
33 | {
|
---|
34 | struct quehead *qh_link;
|
---|
35 | struct quehead *qh_rlink;
|
---|
36 | };
|
---|
37 |
|
---|
38 | void
|
---|
39 | insque(PNATState pData, void *a, void *b)
|
---|
40 | {
|
---|
41 | register struct quehead *element = (struct quehead *) a;
|
---|
42 | register struct quehead *head = (struct quehead *) b;
|
---|
43 | element->qh_link = head->qh_link;
|
---|
44 | head->qh_link = (struct quehead *)element;
|
---|
45 | element->qh_rlink = (struct quehead *)head;
|
---|
46 | ((struct quehead *)(element->qh_link))->qh_rlink = (struct quehead *)element;
|
---|
47 | }
|
---|
48 |
|
---|
49 | void
|
---|
50 | remque(PNATState pData, void *a)
|
---|
51 | {
|
---|
52 | register struct quehead *element = (struct quehead *) a;
|
---|
53 | ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
|
---|
54 | ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
|
---|
55 | element->qh_rlink = NULL;
|
---|
56 | /* element->qh_link = NULL; TCP FIN1 crashes if you do this. Why ? */
|
---|
57 | }
|
---|
58 |
|
---|
59 | int
|
---|
60 | add_exec(struct ex_list **ex_ptr, int do_pty, char *exec, int addr, int port)
|
---|
61 | {
|
---|
62 | struct ex_list *tmp_ptr;
|
---|
63 |
|
---|
64 | /* First, check if the port is "bound" */
|
---|
65 | for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next)
|
---|
66 | {
|
---|
67 | if (port == tmp_ptr->ex_fport && addr == tmp_ptr->ex_addr)
|
---|
68 | return -1;
|
---|
69 | }
|
---|
70 |
|
---|
71 | tmp_ptr = *ex_ptr;
|
---|
72 | *ex_ptr = (struct ex_list *)RTMemAlloc(sizeof(struct ex_list));
|
---|
73 | (*ex_ptr)->ex_fport = port;
|
---|
74 | (*ex_ptr)->ex_addr = addr;
|
---|
75 | (*ex_ptr)->ex_pty = do_pty;
|
---|
76 | (*ex_ptr)->ex_exec = RTStrDup(exec);
|
---|
77 | (*ex_ptr)->ex_next = tmp_ptr;
|
---|
78 | return 0;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Set fd blocking and non-blocking
|
---|
84 | */
|
---|
85 | void
|
---|
86 | fd_nonblock(int fd)
|
---|
87 | {
|
---|
88 | #ifdef FIONBIO
|
---|
89 | int opt = 1;
|
---|
90 |
|
---|
91 | ioctlsocket(fd, FIONBIO, &opt);
|
---|
92 | #else
|
---|
93 | int opt;
|
---|
94 |
|
---|
95 | opt = fcntl(fd, F_GETFL, 0);
|
---|
96 | opt |= O_NONBLOCK;
|
---|
97 | fcntl(fd, F_SETFL, opt);
|
---|
98 | #endif
|
---|
99 | }
|
---|
100 |
|
---|
101 | void
|
---|
102 | fd_block(int fd)
|
---|
103 | {
|
---|
104 | #ifdef FIONBIO
|
---|
105 | int opt = 0;
|
---|
106 |
|
---|
107 | ioctlsocket(fd, FIONBIO, &opt);
|
---|
108 | #else
|
---|
109 | int opt;
|
---|
110 |
|
---|
111 | opt = fcntl(fd, F_GETFL, 0);
|
---|
112 | opt &= ~O_NONBLOCK;
|
---|
113 | fcntl(fd, F_SETFL, opt);
|
---|
114 | #endif
|
---|
115 | }
|
---|
116 |
|
---|
117 | #ifdef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
118 | #define ITEM_MAGIC 0xdead0001
|
---|
119 | struct item
|
---|
120 | {
|
---|
121 | uint32_t magic;
|
---|
122 | uma_zone_t zone;
|
---|
123 | uint32_t ref_count;
|
---|
124 | LIST_ENTRY(item) list;
|
---|
125 | };
|
---|
126 |
|
---|
127 | #define ZONE_MAGIC 0xdead0002
|
---|
128 | struct uma_zone
|
---|
129 | {
|
---|
130 | uint32_t magic;
|
---|
131 | PNATState pData; /* to minimize changes in the rest of UMA emulation code */
|
---|
132 | const char *name;
|
---|
133 | size_t size; /* item size */
|
---|
134 | ctor_t pfCtor;
|
---|
135 | dtor_t pfDtor;
|
---|
136 | zinit_t pfInit;
|
---|
137 | zfini_t pfFini;
|
---|
138 | uma_alloc_t pfAlloc;
|
---|
139 | uma_free_t pfFree;
|
---|
140 | int max_items;
|
---|
141 | int cur_items;
|
---|
142 | LIST_HEAD(RT_NOTHING, item) used_items;
|
---|
143 | LIST_HEAD(RT_NOTHING, item) free_items;
|
---|
144 | uma_zone_t master_zone;
|
---|
145 | };
|
---|
146 |
|
---|
147 |
|
---|
148 | static void *slirp_uma_alloc(uma_zone_t zone,
|
---|
149 | int size, uint8_t *pflags, int wait)
|
---|
150 | {
|
---|
151 | struct item *it;
|
---|
152 | if ( (zone->max_items != 0 && zone->cur_items >= zone->max_items)
|
---|
153 | || (zone->max_items == 0 && !LIST_EMPTY(&zone->free_items))
|
---|
154 | )
|
---|
155 | {
|
---|
156 | /*
|
---|
157 | * @todo (r=vvl) here should be some
|
---|
158 | * accounting of extra items in case
|
---|
159 | * breakthrough barrier
|
---|
160 | */
|
---|
161 | if (LIST_EMPTY(&zone->free_items))
|
---|
162 | return NULL;
|
---|
163 | it = LIST_FIRST(&zone->free_items);
|
---|
164 | LIST_REMOVE(it, list);
|
---|
165 | LIST_INSERT_HEAD(&zone->used_items, it, list);
|
---|
166 | goto allocated;
|
---|
167 | }
|
---|
168 | /*@todo 'Z' should be depend on flag */
|
---|
169 | it = RTMemAllocZ(sizeof(struct item) + zone->size);
|
---|
170 | if (it == NULL)
|
---|
171 | {
|
---|
172 | Log(("NAT: uma no memory"));
|
---|
173 | return NULL;
|
---|
174 | }
|
---|
175 | it->magic = ITEM_MAGIC;
|
---|
176 | LIST_INSERT_HEAD(&zone->used_items, it, list);
|
---|
177 | zone->cur_items++;
|
---|
178 | it->zone = zone;
|
---|
179 |
|
---|
180 | allocated:
|
---|
181 | if (zone->pfInit)
|
---|
182 | zone->pfInit(zone->pData, (void *)&it[1], zone->size, M_DONTWAIT);
|
---|
183 | return (void *)&it[1];
|
---|
184 | }
|
---|
185 |
|
---|
186 | static void slirp_uma_free(void *item, int size, uint8_t flags)
|
---|
187 | {
|
---|
188 | struct item *it;
|
---|
189 | uma_zone_t zone;
|
---|
190 | Assert(item);
|
---|
191 | it = &((struct item *)item)[-1];
|
---|
192 | Assert(it->magic == ITEM_MAGIC);
|
---|
193 | zone = it->zone;
|
---|
194 | Assert(zone->magic == ZONE_MAGIC);
|
---|
195 | LIST_REMOVE(it, list);
|
---|
196 | LIST_INSERT_HEAD(&zone->free_items, it, list);
|
---|
197 | }
|
---|
198 |
|
---|
199 | uma_zone_t uma_zcreate(PNATState pData, char *name, size_t size,
|
---|
200 | ctor_t ctor, dtor_t dtor, zinit_t init, zfini_t fini, int flags1, int flags2)
|
---|
201 | {
|
---|
202 | uma_zone_t zone = RTMemAllocZ(sizeof(struct uma_zone) + size);
|
---|
203 | Assert((pData));
|
---|
204 | zone->magic = ZONE_MAGIC;
|
---|
205 | zone->pData = pData;
|
---|
206 | zone->name = name;
|
---|
207 | zone->size = size;
|
---|
208 | zone->pfCtor = ctor;
|
---|
209 | zone->pfDtor = dtor;
|
---|
210 | zone->pfInit = init;
|
---|
211 | zone->pfFini = fini;
|
---|
212 | zone->pfAlloc = slirp_uma_alloc;
|
---|
213 | zone->pfFree = slirp_uma_free;
|
---|
214 | return zone;
|
---|
215 |
|
---|
216 | }
|
---|
217 | uma_zone_t uma_zsecond_create(char *name, ctor_t ctor,
|
---|
218 | dtor_t dtor, zinit_t init, zfini_t fini, uma_zone_t master)
|
---|
219 | {
|
---|
220 | uma_zone_t zone;
|
---|
221 | #if 0
|
---|
222 | if (master->pfAlloc != NULL)
|
---|
223 | zone = (uma_zone_t)master->pfAlloc(master, sizeof(struct uma_zone), NULL, 0);
|
---|
224 | #endif
|
---|
225 | zone = RTMemAllocZ(sizeof(struct uma_zone));
|
---|
226 | if (zone == NULL)
|
---|
227 | {
|
---|
228 | return NULL;
|
---|
229 | }
|
---|
230 | Assert((master && master->pData));
|
---|
231 | zone->magic = ZONE_MAGIC;
|
---|
232 | zone->pData = master->pData;
|
---|
233 | zone->name = name;
|
---|
234 | zone->pfCtor = ctor;
|
---|
235 | zone->pfDtor = dtor;
|
---|
236 | zone->pfInit = init;
|
---|
237 | zone->pfFini = fini;
|
---|
238 | zone->pfAlloc = slirp_uma_alloc;
|
---|
239 | zone->pfFree = slirp_uma_free;
|
---|
240 | return zone;
|
---|
241 | }
|
---|
242 | void uma_zone_set_max(uma_zone_t zone, int max)
|
---|
243 | {
|
---|
244 | zone->max_items = max;
|
---|
245 | }
|
---|
246 | void uma_zone_set_allocf(uma_zone_t zone, uma_alloc_t pfAlloc)
|
---|
247 | {
|
---|
248 | zone->pfAlloc = pfAlloc;
|
---|
249 | }
|
---|
250 | void uma_zone_set_freef(uma_zone_t zone, uma_free_t pfFree)
|
---|
251 | {
|
---|
252 | zone->pfFree = pfFree;
|
---|
253 | }
|
---|
254 |
|
---|
255 | uint32_t *uma_find_refcnt(uma_zone_t zone, void *mem)
|
---|
256 | {
|
---|
257 | /*@todo (r-vvl) this function supposed to work with special zone storing
|
---|
258 | reference counters */
|
---|
259 | struct item *it = (struct item *)mem; /* 1st element */
|
---|
260 | Assert(zone->magic == ZONE_MAGIC);
|
---|
261 | /* for returning pointer to counter we need get 0 elemnt */
|
---|
262 | Assert(it[-1].magic == ITEM_MAGIC);
|
---|
263 | return &it[-1].ref_count;
|
---|
264 | }
|
---|
265 | void *uma_zalloc_arg(uma_zone_t zone, void *args, int how)
|
---|
266 | {
|
---|
267 | void *mem;
|
---|
268 | Assert(zone->magic == ZONE_MAGIC);
|
---|
269 | if (zone->pfAlloc == NULL)
|
---|
270 | return NULL;
|
---|
271 | mem = zone->pfAlloc(zone, zone->size, NULL, 0);
|
---|
272 | if (zone->pfCtor)
|
---|
273 | zone->pfCtor(zone->pData, mem, zone->size, args, M_DONTWAIT);
|
---|
274 | return mem;
|
---|
275 | }
|
---|
276 |
|
---|
277 | void uma_zfree(uma_zone_t zone, void *item)
|
---|
278 | {
|
---|
279 | uma_zfree_arg(zone, item, NULL);
|
---|
280 | }
|
---|
281 |
|
---|
282 | void uma_zfree_arg(uma_zone_t zone, void *mem, void *flags)
|
---|
283 | {
|
---|
284 | struct item *it;
|
---|
285 | Assert(zone->magic == ZONE_MAGIC);
|
---|
286 | if (zone->pfFree == NULL)
|
---|
287 | return;
|
---|
288 | Assert((mem));
|
---|
289 | it = &((struct item *)mem)[-1];
|
---|
290 | if (it->magic != ITEM_MAGIC)
|
---|
291 | {
|
---|
292 | Log(("NAT:UMA: %p seems to be allocated on heap ... freeing\n", mem));
|
---|
293 | RTMemFree(mem);
|
---|
294 | return;
|
---|
295 | }
|
---|
296 | Assert((zone->magic == ZONE_MAGIC && zone == it->zone));
|
---|
297 |
|
---|
298 | if (zone->pfDtor)
|
---|
299 | zone->pfDtor(zone->pData, mem, zone->size, flags);
|
---|
300 | zone->pfFree(mem, 0, 0);
|
---|
301 | }
|
---|
302 | int uma_zone_exhausted_nolock(uma_zone_t zone)
|
---|
303 | {
|
---|
304 | return 0;
|
---|
305 | }
|
---|
306 | void zone_drain(uma_zone_t zone)
|
---|
307 | {
|
---|
308 |
|
---|
309 | }
|
---|
310 |
|
---|
311 | void slirp_null_arg_free(void *mem, void *arg)
|
---|
312 | {
|
---|
313 | /*@todo (r=vvl) make it wiser*/
|
---|
314 | Assert(mem);
|
---|
315 | RTMemFree(mem);
|
---|
316 | }
|
---|
317 |
|
---|
318 | void *uma_zalloc(uma_zone_t zone, int len)
|
---|
319 | {
|
---|
320 | return NULL;
|
---|
321 | }
|
---|
322 | #endif
|
---|