1 | /*
|
---|
2 | * Copyright (c) 1982, 1986, 1988, 1990, 1993
|
---|
3 | * The Regents of the University of California. All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | * 1. Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer in the
|
---|
12 | * documentation and/or other materials provided with the distribution.
|
---|
13 | * 3. All advertising materials mentioning features or use of this software
|
---|
14 | * must display the following acknowledgement:
|
---|
15 | * This product includes software developed by the University of
|
---|
16 | * California, Berkeley and its contributors.
|
---|
17 | * 4. Neither the name of the University nor the names of its contributors
|
---|
18 | * may be used to endorse or promote products derived from this software
|
---|
19 | * without specific prior written permission.
|
---|
20 | *
|
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
31 | * SUCH DAMAGE.
|
---|
32 | *
|
---|
33 | * @(#)tcp_subr.c 8.1 (Berkeley) 6/10/93
|
---|
34 | * tcp_subr.c,v 1.5 1994/10/08 22:39:58 phk Exp
|
---|
35 | */
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * Changes and additions relating to SLiRP
|
---|
39 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
40 | *
|
---|
41 | * Please read the file COPYRIGHT for the
|
---|
42 | * terms and conditions of the copyright.
|
---|
43 | */
|
---|
44 |
|
---|
45 | #define WANT_SYS_IOCTL_H
|
---|
46 | #include <slirp.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * Tcp initialization
|
---|
51 | */
|
---|
52 | void
|
---|
53 | tcp_init(PNATState pData)
|
---|
54 | {
|
---|
55 | tcp_iss = 1; /* wrong */
|
---|
56 | tcb.so_next = tcb.so_prev = &tcb;
|
---|
57 | tcp_last_so = &tcb;
|
---|
58 | #ifdef VBOX_WITH_BSD_REASS
|
---|
59 | tcp_reass_maxqlen = 48;
|
---|
60 | tcp_reass_maxseg = 256;
|
---|
61 | #endif /* VBOX_WITH_BSD_REASS */
|
---|
62 | }
|
---|
63 |
|
---|
64 | /*
|
---|
65 | * Create template to be used to send tcp packets on a connection.
|
---|
66 | * Call after host entry created, fills
|
---|
67 | * in a skeletal tcp/ip header, minimizing the amount of work
|
---|
68 | * necessary when the connection is used.
|
---|
69 | */
|
---|
70 | /* struct tcpiphdr * */
|
---|
71 | void
|
---|
72 | tcp_template(tp)
|
---|
73 | struct tcpcb *tp;
|
---|
74 | {
|
---|
75 | struct socket *so = tp->t_socket;
|
---|
76 | register struct tcpiphdr *n = &tp->t_template;
|
---|
77 |
|
---|
78 | #if !defined(VBOX_WITH_BSD_REASS)
|
---|
79 | n->ti_next = n->ti_prev = 0;
|
---|
80 | n->ti_x1 = 0;
|
---|
81 | #else
|
---|
82 | memset(n->ti_x1, 0, 9);
|
---|
83 | #endif
|
---|
84 | n->ti_pr = IPPROTO_TCP;
|
---|
85 | n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip));
|
---|
86 | n->ti_src = so->so_faddr;
|
---|
87 | n->ti_dst = so->so_laddr;
|
---|
88 | n->ti_sport = so->so_fport;
|
---|
89 | n->ti_dport = so->so_lport;
|
---|
90 |
|
---|
91 | n->ti_seq = 0;
|
---|
92 | n->ti_ack = 0;
|
---|
93 | n->ti_x2 = 0;
|
---|
94 | n->ti_off = 5;
|
---|
95 | n->ti_flags = 0;
|
---|
96 | n->ti_win = 0;
|
---|
97 | n->ti_sum = 0;
|
---|
98 | n->ti_urp = 0;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /*
|
---|
102 | * Send a single message to the TCP at address specified by
|
---|
103 | * the given TCP/IP header. If m == 0, then we make a copy
|
---|
104 | * of the tcpiphdr at ti and send directly to the addressed host.
|
---|
105 | * This is used to force keep alive messages out using the TCP
|
---|
106 | * template for a connection tp->t_template. If flags are given
|
---|
107 | * then we send a message back to the TCP which originated the
|
---|
108 | * segment ti, and discard the mbuf containing it and any other
|
---|
109 | * attached mbufs.
|
---|
110 | *
|
---|
111 | * In any case the ack and sequence number of the transmitted
|
---|
112 | * segment are as specified by the parameters.
|
---|
113 | */
|
---|
114 | void
|
---|
115 | tcp_respond(PNATState pData, struct tcpcb *tp, struct tcpiphdr *ti, struct mbuf *m, tcp_seq ack, tcp_seq seq, int flags)
|
---|
116 | {
|
---|
117 | register int tlen;
|
---|
118 | int win = 0;
|
---|
119 |
|
---|
120 | DEBUG_CALL("tcp_respond");
|
---|
121 | DEBUG_ARG("tp = %lx", (long)tp);
|
---|
122 | DEBUG_ARG("ti = %lx", (long)ti);
|
---|
123 | DEBUG_ARG("m = %lx", (long)m);
|
---|
124 | DEBUG_ARG("ack = %u", ack);
|
---|
125 | DEBUG_ARG("seq = %u", seq);
|
---|
126 | DEBUG_ARG("flags = %x", flags);
|
---|
127 |
|
---|
128 | if (tp)
|
---|
129 | win = sbspace(&tp->t_socket->so_rcv);
|
---|
130 | if (m == 0) {
|
---|
131 | if ((m = m_get(pData)) == NULL)
|
---|
132 | return;
|
---|
133 | #ifdef TCP_COMPAT_42
|
---|
134 | tlen = 1;
|
---|
135 | #else
|
---|
136 | tlen = 0;
|
---|
137 | #endif
|
---|
138 | m->m_data += if_maxlinkhdr;
|
---|
139 | *mtod(m, struct tcpiphdr *) = *ti;
|
---|
140 | ti = mtod(m, struct tcpiphdr *);
|
---|
141 | flags = TH_ACK;
|
---|
142 | } else {
|
---|
143 | /*
|
---|
144 | * ti points into m so the next line is just making
|
---|
145 | * the mbuf point to ti
|
---|
146 | */
|
---|
147 | m->m_data = (caddr_t)ti;
|
---|
148 |
|
---|
149 | m->m_len = sizeof (struct tcpiphdr);
|
---|
150 | tlen = 0;
|
---|
151 | #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
|
---|
152 | xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_int32_t);
|
---|
153 | xchg(ti->ti_dport, ti->ti_sport, u_int16_t);
|
---|
154 | #undef xchg
|
---|
155 | }
|
---|
156 | ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen));
|
---|
157 | tlen += sizeof (struct tcpiphdr);
|
---|
158 | m->m_len = tlen;
|
---|
159 |
|
---|
160 | #if !defined(VBOX_WITH_BSD_REASS)
|
---|
161 | ti->ti_next = ti->ti_prev = 0;
|
---|
162 | ti->ti_x1 = 0;
|
---|
163 | #else
|
---|
164 | memset(ti->ti_x1, 0, 9);
|
---|
165 | #endif
|
---|
166 | ti->ti_seq = htonl(seq);
|
---|
167 | ti->ti_ack = htonl(ack);
|
---|
168 | ti->ti_x2 = 0;
|
---|
169 | ti->ti_off = sizeof (struct tcphdr) >> 2;
|
---|
170 | ti->ti_flags = flags;
|
---|
171 | if (tp)
|
---|
172 | ti->ti_win = htons((u_int16_t) (win >> tp->rcv_scale));
|
---|
173 | else
|
---|
174 | ti->ti_win = htons((u_int16_t)win);
|
---|
175 | ti->ti_urp = 0;
|
---|
176 | ti->ti_sum = 0;
|
---|
177 | ti->ti_sum = cksum(m, tlen);
|
---|
178 | ((struct ip *)ti)->ip_len = tlen;
|
---|
179 |
|
---|
180 | if(flags & TH_RST)
|
---|
181 | ((struct ip *)ti)->ip_ttl = MAXTTL;
|
---|
182 | else
|
---|
183 | ((struct ip *)ti)->ip_ttl = ip_defttl;
|
---|
184 |
|
---|
185 | (void) ip_output(pData, (struct socket *)0, m);
|
---|
186 | }
|
---|
187 |
|
---|
188 | /*
|
---|
189 | * Create a new TCP control block, making an
|
---|
190 | * empty reassembly queue and hooking it to the argument
|
---|
191 | * protocol control block.
|
---|
192 | */
|
---|
193 | struct tcpcb *
|
---|
194 | tcp_newtcpcb(PNATState pData, struct socket *so)
|
---|
195 | {
|
---|
196 | register struct tcpcb *tp;
|
---|
197 |
|
---|
198 | tp = (struct tcpcb *)malloc(sizeof(*tp));
|
---|
199 | if (tp == NULL)
|
---|
200 | return ((struct tcpcb *)0);
|
---|
201 |
|
---|
202 | memset((char *) tp, 0, sizeof(struct tcpcb));
|
---|
203 | #ifndef VBOX_WITH_BSD_REASS
|
---|
204 | tp->seg_next = tp->seg_prev = ptr_to_u32(pData, (struct tcpiphdr *)tp);
|
---|
205 | #endif /* !VBOX_WITH_BSD_REASS */
|
---|
206 | tp->t_maxseg = tcp_mssdflt;
|
---|
207 |
|
---|
208 | tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
|
---|
209 | tp->t_socket = so;
|
---|
210 |
|
---|
211 | /*
|
---|
212 | * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
|
---|
213 | * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives
|
---|
214 | * reasonable initial retransmit time.
|
---|
215 | */
|
---|
216 | tp->t_srtt = TCPTV_SRTTBASE;
|
---|
217 | tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2;
|
---|
218 | tp->t_rttmin = TCPTV_MIN;
|
---|
219 |
|
---|
220 | TCPT_RANGESET(tp->t_rxtcur,
|
---|
221 | ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1,
|
---|
222 | TCPTV_MIN, TCPTV_REXMTMAX);
|
---|
223 |
|
---|
224 | tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
|
---|
225 | tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
|
---|
226 | tp->t_state = TCPS_CLOSED;
|
---|
227 |
|
---|
228 | so->so_tcpcb = tp;
|
---|
229 |
|
---|
230 | return (tp);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /*
|
---|
234 | * Drop a TCP connection, reporting
|
---|
235 | * the specified error. If connection is synchronized,
|
---|
236 | * then send a RST to peer.
|
---|
237 | */
|
---|
238 | struct tcpcb *tcp_drop(PNATState pData, struct tcpcb *tp, int err)
|
---|
239 | {
|
---|
240 | /* tcp_drop(tp, errno)
|
---|
241 | register struct tcpcb *tp;
|
---|
242 | int errno;
|
---|
243 | {
|
---|
244 | */
|
---|
245 |
|
---|
246 | DEBUG_CALL("tcp_drop");
|
---|
247 | DEBUG_ARG("tp = %lx", (long)tp);
|
---|
248 | DEBUG_ARG("errno = %d", errno);
|
---|
249 |
|
---|
250 | if (TCPS_HAVERCVDSYN(tp->t_state)) {
|
---|
251 | tp->t_state = TCPS_CLOSED;
|
---|
252 | (void) tcp_output(pData, tp);
|
---|
253 | tcpstat.tcps_drops++;
|
---|
254 | } else
|
---|
255 | tcpstat.tcps_conndrops++;
|
---|
256 | /* if (errno == ETIMEDOUT && tp->t_softerror)
|
---|
257 | * errno = tp->t_softerror;
|
---|
258 | */
|
---|
259 | /* so->so_error = errno; */
|
---|
260 | return (tcp_close(pData, tp));
|
---|
261 | }
|
---|
262 |
|
---|
263 | /*
|
---|
264 | * Close a TCP control block:
|
---|
265 | * discard all space held by the tcp
|
---|
266 | * discard internet protocol block
|
---|
267 | * wake up any sleepers
|
---|
268 | */
|
---|
269 | struct tcpcb *
|
---|
270 | tcp_close(PNATState pData, register struct tcpcb *tp)
|
---|
271 | {
|
---|
272 | register struct tcpiphdr *t;
|
---|
273 | struct socket *so = tp->t_socket;
|
---|
274 | register struct mbuf *m;
|
---|
275 |
|
---|
276 | #ifndef VBOX_WITH_BSD_REASS
|
---|
277 | DEBUG_CALL("tcp_close");
|
---|
278 | DEBUG_ARG("tp = %lx", (long )tp);
|
---|
279 |
|
---|
280 | /* free the reassembly queue, if any */
|
---|
281 | t = u32_to_ptr(pData, tp->seg_next, struct tcpiphdr *);
|
---|
282 | while (t != (struct tcpiphdr *)tp) {
|
---|
283 | t = u32_to_ptr(pData, t->ti_next, struct tcpiphdr *);
|
---|
284 | m = REASS_MBUF_GET(u32_to_ptr(pData, t->ti_prev, struct tcpiphdr *));
|
---|
285 | remque_32(pData, u32_to_ptr(pData, t->ti_prev, struct tcpiphdr *));
|
---|
286 | m_freem(pData, m);
|
---|
287 | }
|
---|
288 | /* It's static */
|
---|
289 | /* if (tp->t_template)
|
---|
290 | * (void) m_free(dtom(tp->t_template));
|
---|
291 | */
|
---|
292 | /* free(tp, M_PCB); */
|
---|
293 | u32ptr_done(pData, ptr_to_u32(pData, tp), tp);
|
---|
294 | #else /* VBOX_WITH_BSD_REASS */
|
---|
295 | struct tseg_qent *te;
|
---|
296 | DEBUG_CALL("tcp_close");
|
---|
297 | DEBUG_ARG("tp = %lx", (long )tp);
|
---|
298 | /*XXX: freeing the reassembly queue */
|
---|
299 | LIST_FOREACH(te, &tp->t_segq, tqe_q) {
|
---|
300 | LIST_REMOVE(te, tqe_q);
|
---|
301 | m_freem(pData, te->tqe_m);
|
---|
302 | free(te);
|
---|
303 | tcp_reass_qsize--;
|
---|
304 | }
|
---|
305 | #endif /* VBOX_WITH_BSD_REASS */
|
---|
306 | free(tp);
|
---|
307 | so->so_tcpcb = 0;
|
---|
308 | soisfdisconnected(so);
|
---|
309 | /* clobber input socket cache if we're closing the cached connection */
|
---|
310 | if (so == tcp_last_so)
|
---|
311 | tcp_last_so = &tcb;
|
---|
312 | closesocket(so->s);
|
---|
313 | sbfree(&so->so_rcv);
|
---|
314 | sbfree(&so->so_snd);
|
---|
315 | sofree(pData, so);
|
---|
316 | tcpstat.tcps_closed++;
|
---|
317 | return ((struct tcpcb *)0);
|
---|
318 | }
|
---|
319 |
|
---|
320 | void
|
---|
321 | tcp_drain()
|
---|
322 | {
|
---|
323 | /* XXX */
|
---|
324 | }
|
---|
325 |
|
---|
326 | /*
|
---|
327 | * When a source quench is received, close congestion window
|
---|
328 | * to one segment. We will gradually open it again as we proceed.
|
---|
329 | */
|
---|
330 |
|
---|
331 | #ifdef notdef
|
---|
332 |
|
---|
333 | void
|
---|
334 | tcp_quench(i, errno)
|
---|
335 |
|
---|
336 | int errno;
|
---|
337 | {
|
---|
338 | struct tcpcb *tp = intotcpcb(inp);
|
---|
339 |
|
---|
340 | if (tp)
|
---|
341 | tp->snd_cwnd = tp->t_maxseg;
|
---|
342 | }
|
---|
343 |
|
---|
344 | #endif /* notdef */
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * TCP protocol interface to socket abstraction.
|
---|
348 | */
|
---|
349 |
|
---|
350 | /*
|
---|
351 | * User issued close, and wish to trail through shutdown states:
|
---|
352 | * if never received SYN, just forget it. If got a SYN from peer,
|
---|
353 | * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
|
---|
354 | * If already got a FIN from peer, then almost done; go to LAST_ACK
|
---|
355 | * state. In all other cases, have already sent FIN to peer (e.g.
|
---|
356 | * after PRU_SHUTDOWN), and just have to play tedious game waiting
|
---|
357 | * for peer to send FIN or not respond to keep-alives, etc.
|
---|
358 | * We can let the user exit from the close as soon as the FIN is acked.
|
---|
359 | */
|
---|
360 | void
|
---|
361 | tcp_sockclosed(PNATState pData, struct tcpcb *tp)
|
---|
362 | {
|
---|
363 |
|
---|
364 | DEBUG_CALL("tcp_sockclosed");
|
---|
365 | DEBUG_ARG("tp = %lx", (long)tp);
|
---|
366 |
|
---|
367 | switch (tp->t_state) {
|
---|
368 |
|
---|
369 | case TCPS_CLOSED:
|
---|
370 | case TCPS_LISTEN:
|
---|
371 | case TCPS_SYN_SENT:
|
---|
372 | tp->t_state = TCPS_CLOSED;
|
---|
373 | tp = tcp_close(pData, tp);
|
---|
374 | break;
|
---|
375 |
|
---|
376 | case TCPS_SYN_RECEIVED:
|
---|
377 | case TCPS_ESTABLISHED:
|
---|
378 | tp->t_state = TCPS_FIN_WAIT_1;
|
---|
379 | break;
|
---|
380 |
|
---|
381 | case TCPS_CLOSE_WAIT:
|
---|
382 | tp->t_state = TCPS_LAST_ACK;
|
---|
383 | break;
|
---|
384 | }
|
---|
385 | /* soisfdisconnecting(tp->t_socket); */
|
---|
386 | if (tp && tp->t_state >= TCPS_FIN_WAIT_2)
|
---|
387 | soisfdisconnected(tp->t_socket);
|
---|
388 | if (tp)
|
---|
389 | tcp_output(pData, tp);
|
---|
390 | }
|
---|
391 |
|
---|
392 | /*
|
---|
393 | * Connect to a host on the Internet
|
---|
394 | * Called by tcp_input
|
---|
395 | * Only do a connect, the tcp fields will be set in tcp_input
|
---|
396 | * return 0 if there's a result of the connect,
|
---|
397 | * else return -1 means we're still connecting
|
---|
398 | * The return value is almost always -1 since the socket is
|
---|
399 | * nonblocking. Connect returns after the SYN is sent, and does
|
---|
400 | * not wait for ACK+SYN.
|
---|
401 | */
|
---|
402 | int tcp_fconnect(PNATState pData, struct socket *so)
|
---|
403 | {
|
---|
404 | int ret=0;
|
---|
405 |
|
---|
406 | DEBUG_CALL("tcp_fconnect");
|
---|
407 | DEBUG_ARG("so = %lx", (long )so);
|
---|
408 |
|
---|
409 | if( (ret=so->s=socket(AF_INET,SOCK_STREAM,0)) >= 0) {
|
---|
410 | int opt, s=so->s;
|
---|
411 | struct sockaddr_in addr;
|
---|
412 |
|
---|
413 | fd_nonblock(s);
|
---|
414 | opt = 1;
|
---|
415 | setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(opt ));
|
---|
416 | opt = 1;
|
---|
417 | setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(opt ));
|
---|
418 |
|
---|
419 | addr.sin_family = AF_INET;
|
---|
420 | if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr) {
|
---|
421 | /* It's an alias */
|
---|
422 | switch(ntohl(so->so_faddr.s_addr) & ~pData->netmask) {
|
---|
423 | case CTL_DNS:
|
---|
424 | if (!get_dns_addr(pData, &dns_addr))
|
---|
425 | addr.sin_addr = dns_addr;
|
---|
426 | else
|
---|
427 | addr.sin_addr = loopback_addr;
|
---|
428 | break;
|
---|
429 | case CTL_ALIAS:
|
---|
430 | default:
|
---|
431 | addr.sin_addr = loopback_addr;
|
---|
432 | break;
|
---|
433 | }
|
---|
434 | } else
|
---|
435 | addr.sin_addr = so->so_faddr;
|
---|
436 | addr.sin_port = so->so_fport;
|
---|
437 |
|
---|
438 | DEBUG_MISC((dfd, " connect()ing, addr.sin_port=%d, "
|
---|
439 | "addr.sin_addr.s_addr=%.16s\n",
|
---|
440 | ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
|
---|
441 | /* We don't care what port we get */
|
---|
442 | ret = connect(s,(struct sockaddr *)&addr,sizeof (addr));
|
---|
443 |
|
---|
444 | /*
|
---|
445 | * If it's not in progress, it failed, so we just return 0,
|
---|
446 | * without clearing SS_NOFDREF
|
---|
447 | */
|
---|
448 | soisfconnecting(so);
|
---|
449 | }
|
---|
450 |
|
---|
451 | return(ret);
|
---|
452 | }
|
---|
453 |
|
---|
454 | /*
|
---|
455 | * Accept the socket and connect to the local-host
|
---|
456 | *
|
---|
457 | * We have a problem. The correct thing to do would be
|
---|
458 | * to first connect to the local-host, and only if the
|
---|
459 | * connection is accepted, then do an accept() here.
|
---|
460 | * But, a) we need to know who's trying to connect
|
---|
461 | * to the socket to be able to SYN the local-host, and
|
---|
462 | * b) we are already connected to the foreign host by
|
---|
463 | * the time it gets to accept(), so... We simply accept
|
---|
464 | * here and SYN the local-host.
|
---|
465 | */
|
---|
466 | void
|
---|
467 | tcp_connect(PNATState pData, struct socket *inso)
|
---|
468 | {
|
---|
469 | struct socket *so;
|
---|
470 | struct sockaddr_in addr;
|
---|
471 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
472 | struct tcpcb *tp;
|
---|
473 | int s, opt;
|
---|
474 |
|
---|
475 | DEBUG_CALL("tcp_connect");
|
---|
476 | DEBUG_ARG("inso = %lx", (long)inso);
|
---|
477 |
|
---|
478 | /*
|
---|
479 | * If it's an SS_ACCEPTONCE socket, no need to socreate()
|
---|
480 | * another socket, just use the accept() socket.
|
---|
481 | */
|
---|
482 | if (inso->so_state & SS_FACCEPTONCE) {
|
---|
483 | /* FACCEPTONCE already have a tcpcb */
|
---|
484 | so = inso;
|
---|
485 | } else {
|
---|
486 | if ((so = socreate()) == NULL) {
|
---|
487 | /* If it failed, get rid of the pending connection */
|
---|
488 | closesocket(accept(inso->s,(struct sockaddr *)&addr,&addrlen));
|
---|
489 | return;
|
---|
490 | }
|
---|
491 | if (tcp_attach(pData, so) < 0) {
|
---|
492 | free(so); /* NOT sofree */
|
---|
493 | return;
|
---|
494 | }
|
---|
495 | so->so_laddr = inso->so_laddr;
|
---|
496 | so->so_lport = inso->so_lport;
|
---|
497 | }
|
---|
498 |
|
---|
499 | (void) tcp_mss(pData, sototcpcb(so), 0);
|
---|
500 |
|
---|
501 | if ((s = accept(inso->s,(struct sockaddr *)&addr,&addrlen)) < 0) {
|
---|
502 | tcp_close(pData, sototcpcb(so)); /* This will sofree() as well */
|
---|
503 | return;
|
---|
504 | }
|
---|
505 | fd_nonblock(s);
|
---|
506 | opt = 1;
|
---|
507 | setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
|
---|
508 | opt = 1;
|
---|
509 | setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
|
---|
510 | opt = 1;
|
---|
511 | setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&opt,sizeof(int));
|
---|
512 |
|
---|
513 | so->so_fport = addr.sin_port;
|
---|
514 | so->so_faddr = addr.sin_addr;
|
---|
515 | /* Translate connections from localhost to the real hostname */
|
---|
516 | if (so->so_faddr.s_addr == 0 || so->so_faddr.s_addr == loopback_addr.s_addr)
|
---|
517 | so->so_faddr = alias_addr;
|
---|
518 |
|
---|
519 | /* Close the accept() socket, set right state */
|
---|
520 | if (inso->so_state & SS_FACCEPTONCE) {
|
---|
521 | closesocket(so->s); /* If we only accept once, close the accept() socket */
|
---|
522 | so->so_state = SS_NOFDREF; /* Don't select it yet, even though we have an FD */
|
---|
523 | /* if it's not FACCEPTONCE, it's already NOFDREF */
|
---|
524 | }
|
---|
525 | so->s = s;
|
---|
526 |
|
---|
527 | so->so_iptos = tcp_tos(so);
|
---|
528 | tp = sototcpcb(so);
|
---|
529 |
|
---|
530 | tcp_template(tp);
|
---|
531 |
|
---|
532 | /* Compute window scaling to request. */
|
---|
533 | /* while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
|
---|
534 | * (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
|
---|
535 | * tp->request_r_scale++;
|
---|
536 | */
|
---|
537 |
|
---|
538 | /* soisconnecting(so); */ /* NOFDREF used instead */
|
---|
539 | tcpstat.tcps_connattempt++;
|
---|
540 |
|
---|
541 | tp->t_state = TCPS_SYN_SENT;
|
---|
542 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
|
---|
543 | tp->iss = tcp_iss;
|
---|
544 | tcp_iss += TCP_ISSINCR/2;
|
---|
545 | tcp_sendseqinit(tp);
|
---|
546 | tcp_output(pData, tp);
|
---|
547 | }
|
---|
548 |
|
---|
549 | /*
|
---|
550 | * Attach a TCPCB to a socket.
|
---|
551 | */
|
---|
552 | int
|
---|
553 | tcp_attach(PNATState pData, struct socket *so)
|
---|
554 | {
|
---|
555 | if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL)
|
---|
556 | return -1;
|
---|
557 |
|
---|
558 | insque(pData, so, &tcb);
|
---|
559 |
|
---|
560 | return 0;
|
---|
561 | }
|
---|
562 |
|
---|
563 | /*
|
---|
564 | * Set the socket's type of service field
|
---|
565 | */
|
---|
566 | static const struct tos_t tcptos[] = {
|
---|
567 | {0, 20, IPTOS_THROUGHPUT, 0}, /* ftp data */
|
---|
568 | {21, 21, IPTOS_LOWDELAY, EMU_FTP}, /* ftp control */
|
---|
569 | {0, 23, IPTOS_LOWDELAY, 0}, /* telnet */
|
---|
570 | {0, 80, IPTOS_THROUGHPUT, 0}, /* WWW */
|
---|
571 | {0, 513, IPTOS_LOWDELAY, EMU_RLOGIN|EMU_NOCONNECT}, /* rlogin */
|
---|
572 | {0, 514, IPTOS_LOWDELAY, EMU_RSH|EMU_NOCONNECT}, /* shell */
|
---|
573 | {0, 544, IPTOS_LOWDELAY, EMU_KSH}, /* kshell */
|
---|
574 | {0, 543, IPTOS_LOWDELAY, 0}, /* klogin */
|
---|
575 | {0, 6667, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC */
|
---|
576 | {0, 6668, IPTOS_THROUGHPUT, EMU_IRC}, /* IRC undernet */
|
---|
577 | {0, 7070, IPTOS_LOWDELAY, EMU_REALAUDIO }, /* RealAudio control */
|
---|
578 | {0, 113, IPTOS_LOWDELAY, EMU_IDENT }, /* identd protocol */
|
---|
579 | {0, 0, 0, 0}
|
---|
580 | };
|
---|
581 |
|
---|
582 | /*
|
---|
583 | * Return TOS according to the above table
|
---|
584 | */
|
---|
585 | u_int8_t
|
---|
586 | tcp_tos(so)
|
---|
587 | struct socket *so;
|
---|
588 | {
|
---|
589 | int i = 0;
|
---|
590 |
|
---|
591 | while(tcptos[i].tos) {
|
---|
592 | if ((tcptos[i].fport && (ntohs(so->so_fport) == tcptos[i].fport)) ||
|
---|
593 | (tcptos[i].lport && (ntohs(so->so_lport) == tcptos[i].lport))) {
|
---|
594 | so->so_emu = tcptos[i].emu;
|
---|
595 | return tcptos[i].tos;
|
---|
596 | }
|
---|
597 | i++;
|
---|
598 | }
|
---|
599 |
|
---|
600 | return 0;
|
---|
601 | }
|
---|
602 |
|
---|
603 | /*
|
---|
604 | * Emulate programs that try and connect to us
|
---|
605 | * This includes ftp (the data connection is
|
---|
606 | * initiated by the server) and IRC (DCC CHAT and
|
---|
607 | * DCC SEND) for now
|
---|
608 | *
|
---|
609 | * NOTE: It's possible to crash SLiRP by sending it
|
---|
610 | * unstandard strings to emulate... if this is a problem,
|
---|
611 | * more checks are needed here
|
---|
612 | *
|
---|
613 | * XXX Assumes the whole command came in one packet
|
---|
614 | *
|
---|
615 | * XXX Some ftp clients will have their TOS set to
|
---|
616 | * LOWDELAY and so Nagel will kick in. Because of this,
|
---|
617 | * we'll get the first letter, followed by the rest, so
|
---|
618 | * we simply scan for ORT instead of PORT...
|
---|
619 | * DCC doesn't have this problem because there's other stuff
|
---|
620 | * in the packet before the DCC command.
|
---|
621 | *
|
---|
622 | * Return 1 if the mbuf m is still valid and should be
|
---|
623 | * sbappend()ed
|
---|
624 | *
|
---|
625 | * NOTE: if you return 0 you MUST m_free() the mbuf!
|
---|
626 | */
|
---|
627 | int
|
---|
628 | tcp_emu(PNATState pData, struct socket *so, struct mbuf *m)
|
---|
629 | {
|
---|
630 | u_int n1, n2, n3, n4, n5, n6;
|
---|
631 | char buff[256];
|
---|
632 | u_int32_t laddr;
|
---|
633 | u_int lport;
|
---|
634 | char *bptr;
|
---|
635 |
|
---|
636 | DEBUG_CALL("tcp_emu");
|
---|
637 | DEBUG_ARG("so = %lx", (long)so);
|
---|
638 | DEBUG_ARG("m = %lx", (long)m);
|
---|
639 |
|
---|
640 | switch(so->so_emu) {
|
---|
641 | int x, i;
|
---|
642 |
|
---|
643 | case EMU_IDENT:
|
---|
644 | /*
|
---|
645 | * Identification protocol as per rfc-1413
|
---|
646 | */
|
---|
647 |
|
---|
648 | {
|
---|
649 | struct socket *tmpso;
|
---|
650 | struct sockaddr_in addr;
|
---|
651 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
652 | struct sbuf *so_rcv = &so->so_rcv;
|
---|
653 |
|
---|
654 | memcpy(so_rcv->sb_wptr, m->m_data, m->m_len);
|
---|
655 | so_rcv->sb_wptr += m->m_len;
|
---|
656 | so_rcv->sb_rptr += m->m_len;
|
---|
657 | m->m_data[m->m_len] = 0; /* NULL terminate */
|
---|
658 | if (strchr(m->m_data, '\r') || strchr(m->m_data, '\n')) {
|
---|
659 | if (sscanf(so_rcv->sb_data, "%u%*[ ,]%u", &n1, &n2) == 2) {
|
---|
660 | HTONS(n1);
|
---|
661 | HTONS(n2);
|
---|
662 | /* n2 is the one on our host */
|
---|
663 | for (tmpso = tcb.so_next; tmpso != &tcb; tmpso = tmpso->so_next) {
|
---|
664 | if (tmpso->so_laddr.s_addr == so->so_laddr.s_addr &&
|
---|
665 | tmpso->so_lport == n2 &&
|
---|
666 | tmpso->so_faddr.s_addr == so->so_faddr.s_addr &&
|
---|
667 | tmpso->so_fport == n1) {
|
---|
668 | if (getsockname(tmpso->s,
|
---|
669 | (struct sockaddr *)&addr, &addrlen) == 0)
|
---|
670 | n2 = ntohs(addr.sin_port);
|
---|
671 | break;
|
---|
672 | }
|
---|
673 | }
|
---|
674 | }
|
---|
675 | so_rcv->sb_cc = sprintf(so_rcv->sb_data, "%d,%d\r\n", n1, n2);
|
---|
676 | so_rcv->sb_rptr = so_rcv->sb_data;
|
---|
677 | so_rcv->sb_wptr = so_rcv->sb_data + so_rcv->sb_cc;
|
---|
678 | }
|
---|
679 | m_free(pData, m);
|
---|
680 | return 0;
|
---|
681 | }
|
---|
682 |
|
---|
683 | case EMU_FTP: /* ftp */
|
---|
684 | *(m->m_data+m->m_len) = 0; /* NULL terminate for strstr */
|
---|
685 | if ((bptr = (char *)strstr(m->m_data, "ORT")) != NULL) {
|
---|
686 | /*
|
---|
687 | * Need to emulate the PORT command
|
---|
688 | */
|
---|
689 | x = sscanf(bptr, "ORT %u,%u,%u,%u,%u,%u\r\n%256[^\177]",
|
---|
690 | &n1, &n2, &n3, &n4, &n5, &n6, buff);
|
---|
691 | if (x < 6)
|
---|
692 | return 1;
|
---|
693 |
|
---|
694 | laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
|
---|
695 | lport = htons((n5 << 8) | (n6));
|
---|
696 |
|
---|
697 | if ((so = solisten(pData, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
|
---|
698 | return 1;
|
---|
699 |
|
---|
700 | n6 = ntohs(so->so_fport);
|
---|
701 |
|
---|
702 | n5 = (n6 >> 8) & 0xff;
|
---|
703 | n6 &= 0xff;
|
---|
704 |
|
---|
705 | laddr = ntohl(so->so_faddr.s_addr);
|
---|
706 |
|
---|
707 | n1 = ((laddr >> 24) & 0xff);
|
---|
708 | n2 = ((laddr >> 16) & 0xff);
|
---|
709 | n3 = ((laddr >> 8) & 0xff);
|
---|
710 | n4 = (laddr & 0xff);
|
---|
711 |
|
---|
712 | m->m_len = bptr - m->m_data; /* Adjust length */
|
---|
713 | m->m_len += sprintf(bptr,"ORT %d,%d,%d,%d,%d,%d\r\n%s",
|
---|
714 | n1, n2, n3, n4, n5, n6, x==7?buff:"");
|
---|
715 | return 1;
|
---|
716 | } else if ((bptr = (char *)strstr(m->m_data, "27 Entering")) != NULL) {
|
---|
717 | /*
|
---|
718 | * Need to emulate the PASV response
|
---|
719 | */
|
---|
720 | x = sscanf(bptr, "27 Entering Passive Mode (%u,%u,%u,%u,%u,%u)\r\n%256[^\177]",
|
---|
721 | &n1, &n2, &n3, &n4, &n5, &n6, buff);
|
---|
722 | if (x < 6)
|
---|
723 | return 1;
|
---|
724 |
|
---|
725 | laddr = htonl((n1 << 24) | (n2 << 16) | (n3 << 8) | (n4));
|
---|
726 | lport = htons((n5 << 8) | (n6));
|
---|
727 |
|
---|
728 | if ((so = solisten(pData, 0, laddr, lport, SS_FACCEPTONCE)) == NULL)
|
---|
729 | return 1;
|
---|
730 |
|
---|
731 | n6 = ntohs(so->so_fport);
|
---|
732 |
|
---|
733 | n5 = (n6 >> 8) & 0xff;
|
---|
734 | n6 &= 0xff;
|
---|
735 |
|
---|
736 | laddr = ntohl(so->so_faddr.s_addr);
|
---|
737 |
|
---|
738 | n1 = ((laddr >> 24) & 0xff);
|
---|
739 | n2 = ((laddr >> 16) & 0xff);
|
---|
740 | n3 = ((laddr >> 8) & 0xff);
|
---|
741 | n4 = (laddr & 0xff);
|
---|
742 |
|
---|
743 | m->m_len = bptr - m->m_data; /* Adjust length */
|
---|
744 | m->m_len += sprintf(bptr,"27 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n%s",
|
---|
745 | n1, n2, n3, n4, n5, n6, x==7?buff:"");
|
---|
746 |
|
---|
747 | return 1;
|
---|
748 | }
|
---|
749 |
|
---|
750 | return 1;
|
---|
751 |
|
---|
752 | case EMU_KSH:
|
---|
753 | /*
|
---|
754 | * The kshell (Kerberos rsh) and shell services both pass
|
---|
755 | * a local port port number to carry signals to the server
|
---|
756 | * and stderr to the client. It is passed at the beginning
|
---|
757 | * of the connection as a NUL-terminated decimal ASCII string.
|
---|
758 | */
|
---|
759 | so->so_emu = 0;
|
---|
760 | for (lport = 0, i = 0; i < m->m_len-1; ++i) {
|
---|
761 | if (m->m_data[i] < '0' || m->m_data[i] > '9')
|
---|
762 | return 1; /* invalid number */
|
---|
763 | lport *= 10;
|
---|
764 | lport += m->m_data[i] - '0';
|
---|
765 | }
|
---|
766 | if (m->m_data[m->m_len-1] == '\0' && lport != 0 &&
|
---|
767 | (so = solisten(pData, 0, so->so_laddr.s_addr, htons(lport), SS_FACCEPTONCE)) != NULL)
|
---|
768 | m->m_len = sprintf(m->m_data, "%d", ntohs(so->so_fport))+1;
|
---|
769 | return 1;
|
---|
770 |
|
---|
771 | case EMU_IRC:
|
---|
772 | /*
|
---|
773 | * Need to emulate DCC CHAT, DCC SEND and DCC MOVE
|
---|
774 | */
|
---|
775 | *(m->m_data+m->m_len) = 0; /* NULL terminate the string for strstr */
|
---|
776 | if ((bptr = (char *)strstr(m->m_data, "DCC")) == NULL)
|
---|
777 | return 1;
|
---|
778 |
|
---|
779 | /* The %256s is for the broken mIRC */
|
---|
780 | if (sscanf(bptr, "DCC CHAT %256s %u %u", buff, &laddr, &lport) == 3) {
|
---|
781 | if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
|
---|
782 | return 1;
|
---|
783 |
|
---|
784 | m->m_len = bptr - m->m_data; /* Adjust length */
|
---|
785 | m->m_len += sprintf(bptr, "DCC CHAT chat %lu %u%c\n",
|
---|
786 | (unsigned long)ntohl(so->so_faddr.s_addr),
|
---|
787 | ntohs(so->so_fport), 1);
|
---|
788 | } else if (sscanf(bptr, "DCC SEND %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
|
---|
789 | if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
|
---|
790 | return 1;
|
---|
791 |
|
---|
792 | m->m_len = bptr - m->m_data; /* Adjust length */
|
---|
793 | m->m_len += sprintf(bptr, "DCC SEND %s %lu %u %u%c\n",
|
---|
794 | buff, (unsigned long)ntohl(so->so_faddr.s_addr),
|
---|
795 | ntohs(so->so_fport), n1, 1);
|
---|
796 | } else if (sscanf(bptr, "DCC MOVE %256s %u %u %u", buff, &laddr, &lport, &n1) == 4) {
|
---|
797 | if ((so = solisten(pData, 0, htonl(laddr), htons(lport), SS_FACCEPTONCE)) == NULL)
|
---|
798 | return 1;
|
---|
799 |
|
---|
800 | m->m_len = bptr - m->m_data; /* Adjust length */
|
---|
801 | m->m_len += sprintf(bptr, "DCC MOVE %s %lu %u %u%c\n",
|
---|
802 | buff, (unsigned long)ntohl(so->so_faddr.s_addr),
|
---|
803 | ntohs(so->so_fport), n1, 1);
|
---|
804 | }
|
---|
805 | return 1;
|
---|
806 |
|
---|
807 | #ifdef VBOX
|
---|
808 | /** @todo Disabled EMU_REALAUDIO, because it uses a static variable.
|
---|
809 | * This is not legal when more than one slirp instance is active. */
|
---|
810 | #else /* !VBOX */
|
---|
811 | case EMU_REALAUDIO:
|
---|
812 | /*
|
---|
813 | * RealAudio emulation - JP. We must try to parse the incoming
|
---|
814 | * data and try to find the two characters that contain the
|
---|
815 | * port number. Then we redirect an udp port and replace the
|
---|
816 | * number with the real port we got.
|
---|
817 | *
|
---|
818 | * The 1.0 beta versions of the player are not supported
|
---|
819 | * any more.
|
---|
820 | *
|
---|
821 | * A typical packet for player version 1.0 (release version):
|
---|
822 | *
|
---|
823 | * 0000:50 4E 41 00 05
|
---|
824 | * 0000:00 01 00 02 1B D7 00 00 67 E6 6C DC 63 00 12 50 .....×..gælÜc..P
|
---|
825 | * 0010:4E 43 4C 49 45 4E 54 20 31 30 31 20 41 4C 50 48 NCLIENT 101 ALPH
|
---|
826 | * 0020:41 6C 00 00 52 00 17 72 61 66 69 6C 65 73 2F 76 Al..R..rafiles/v
|
---|
827 | * 0030:6F 61 2F 65 6E 67 6C 69 73 68 5F 2E 72 61 79 42 oa/english_.rayB
|
---|
828 | *
|
---|
829 | * Now the port number 0x1BD7 is found at offset 0x04 of the
|
---|
830 | * Now the port number 0x1BD7 is found at offset 0x04 of the
|
---|
831 | * second packet. This time we received five bytes first and
|
---|
832 | * then the rest. You never know how many bytes you get.
|
---|
833 | *
|
---|
834 | * A typical packet for player version 2.0 (beta):
|
---|
835 | *
|
---|
836 | * 0000:50 4E 41 00 06 00 02 00 00 00 01 00 02 1B C1 00 PNA...........Á.
|
---|
837 | * 0010:00 67 75 78 F5 63 00 0A 57 69 6E 32 2E 30 2E 30 .guxõc..Win2.0.0
|
---|
838 | * 0020:2E 35 6C 00 00 52 00 1C 72 61 66 69 6C 65 73 2F .5l..R..rafiles/
|
---|
839 | * 0030:77 65 62 73 69 74 65 2F 32 30 72 65 6C 65 61 73 website/20releas
|
---|
840 | * 0040:65 2E 72 61 79 53 00 00 06 36 42 e.rayS...6B
|
---|
841 | *
|
---|
842 | * Port number 0x1BC1 is found at offset 0x0d.
|
---|
843 | *
|
---|
844 | * This is just a horrible switch statement. Variable ra tells
|
---|
845 | * us where we're going.
|
---|
846 | */
|
---|
847 |
|
---|
848 | bptr = m->m_data;
|
---|
849 | while (bptr < m->m_data + m->m_len) {
|
---|
850 | u_short p;
|
---|
851 | static int ra = 0;
|
---|
852 | char ra_tbl[4];
|
---|
853 |
|
---|
854 | ra_tbl[0] = 0x50;
|
---|
855 | ra_tbl[1] = 0x4e;
|
---|
856 | ra_tbl[2] = 0x41;
|
---|
857 | ra_tbl[3] = 0;
|
---|
858 |
|
---|
859 | switch (ra) {
|
---|
860 | case 0:
|
---|
861 | case 2:
|
---|
862 | case 3:
|
---|
863 | if (*bptr++ != ra_tbl[ra]) {
|
---|
864 | ra = 0;
|
---|
865 | continue;
|
---|
866 | }
|
---|
867 | break;
|
---|
868 |
|
---|
869 | case 1:
|
---|
870 | /*
|
---|
871 | * We may get 0x50 several times, ignore them
|
---|
872 | */
|
---|
873 | if (*bptr == 0x50) {
|
---|
874 | ra = 1;
|
---|
875 | bptr++;
|
---|
876 | continue;
|
---|
877 | } else if (*bptr++ != ra_tbl[ra]) {
|
---|
878 | ra = 0;
|
---|
879 | continue;
|
---|
880 | }
|
---|
881 | break;
|
---|
882 |
|
---|
883 | case 4:
|
---|
884 | /*
|
---|
885 | * skip version number
|
---|
886 | */
|
---|
887 | bptr++;
|
---|
888 | break;
|
---|
889 |
|
---|
890 | case 5:
|
---|
891 | /*
|
---|
892 | * The difference between versions 1.0 and
|
---|
893 | * 2.0 is here. For future versions of
|
---|
894 | * the player this may need to be modified.
|
---|
895 | */
|
---|
896 | if (*(bptr + 1) == 0x02)
|
---|
897 | bptr += 8;
|
---|
898 | else
|
---|
899 | bptr += 4;
|
---|
900 | break;
|
---|
901 |
|
---|
902 | case 6:
|
---|
903 | /* This is the field containing the port
|
---|
904 | * number that RA-player is listening to.
|
---|
905 | */
|
---|
906 | lport = (((u_char*)bptr)[0] << 8)
|
---|
907 | + ((u_char *)bptr)[1];
|
---|
908 | if (lport < 6970)
|
---|
909 | lport += 256; /* don't know why */
|
---|
910 | if (lport < 6970 || lport > 7170)
|
---|
911 | return 1; /* failed */
|
---|
912 |
|
---|
913 | /* try to get udp port between 6970 - 7170 */
|
---|
914 | for (p = 6970; p < 7071; p++) {
|
---|
915 | if (udp_listen( htons(p),
|
---|
916 | so->so_laddr.s_addr,
|
---|
917 | htons(lport),
|
---|
918 | SS_FACCEPTONCE)) {
|
---|
919 | break;
|
---|
920 | }
|
---|
921 | }
|
---|
922 | if (p == 7071)
|
---|
923 | p = 0;
|
---|
924 | *(u_char *)bptr++ = (p >> 8) & 0xff;
|
---|
925 | *(u_char *)bptr++ = p & 0xff;
|
---|
926 | ra = 0;
|
---|
927 | return 1; /* port redirected, we're done */
|
---|
928 | break;
|
---|
929 |
|
---|
930 | default:
|
---|
931 | ra = 0;
|
---|
932 | }
|
---|
933 | ra++;
|
---|
934 | }
|
---|
935 | return 1;
|
---|
936 | #endif /* !VBOX */
|
---|
937 |
|
---|
938 | default:
|
---|
939 | /* Ooops, not emulated, won't call tcp_emu again */
|
---|
940 | so->so_emu = 0;
|
---|
941 | return 1;
|
---|
942 | }
|
---|
943 | }
|
---|
944 |
|
---|
945 | #if SIZEOF_CHAR_P != 4 && !defined(VBOX_WITH_BSD_REASS)
|
---|
946 | /**
|
---|
947 | * Slow pointer hashing that deals with automatic inserting and collisions.
|
---|
948 | */
|
---|
949 | uint32_t VBoxU32PtrHashSlow(PNATState pData, void *pv)
|
---|
950 | {
|
---|
951 | uint32_t i;
|
---|
952 | if (pv == NULL)
|
---|
953 | i = 0;
|
---|
954 | else
|
---|
955 | {
|
---|
956 | const uint32_t i1 = ((uintptr_t)pv >> 3) % RT_ELEMENTS(pData->apvHash);
|
---|
957 | if (pData->apvHash[i1] == pv)
|
---|
958 | i = i1;
|
---|
959 | else
|
---|
960 | {
|
---|
961 | /*
|
---|
962 | * Try up to 10 times then assume it's an insertion.
|
---|
963 | * If we didn't find a free entry by then, try another 100 times.
|
---|
964 | * If that fails, give up.
|
---|
965 | */
|
---|
966 | const uint32_t i2 = ((uintptr_t)pv >> 2) % 7867;
|
---|
967 | uint32_t i1stFree = pData->apvHash[i1] ? 0 : i1;
|
---|
968 | int cTries = 10;
|
---|
969 | int cTries2 = 100;
|
---|
970 |
|
---|
971 | i = i1;
|
---|
972 | for (;;)
|
---|
973 | {
|
---|
974 | /* check if we should give in.*/
|
---|
975 | if (--cTries > 0)
|
---|
976 | {
|
---|
977 | if (i1stFree != 0)
|
---|
978 | {
|
---|
979 | i = i1stFree;
|
---|
980 | pData->apvHash[i] = pv;
|
---|
981 | pData->cpvHashUsed++;
|
---|
982 | if (i != i1)
|
---|
983 | pData->cpvHashCollisions++;
|
---|
984 | pData->cpvHashInserts++;
|
---|
985 | break;
|
---|
986 | }
|
---|
987 | if (!cTries2)
|
---|
988 | {
|
---|
989 | AssertReleaseMsgFailed(("NAT pointer hash error. pv=%p cpvHashUsed=%d cpvHashCollisions=%u\n",
|
---|
990 | pv, pData->cpvHashUsed, pData->cpvHashCollisions));
|
---|
991 | i = 0;
|
---|
992 | break;
|
---|
993 | }
|
---|
994 | cTries = cTries2;
|
---|
995 | cTries2 = 0;
|
---|
996 | }
|
---|
997 |
|
---|
998 | /* advance to the next hash entry and test it. */
|
---|
999 | i = (i + i2) % RT_ELEMENTS(pData->apvHash);
|
---|
1000 | while (RT_UNLIKELY(!i))
|
---|
1001 | i = (i + i2) % RT_ELEMENTS(pData->apvHash);
|
---|
1002 | if (pData->apvHash[i] == pv)
|
---|
1003 | break;
|
---|
1004 | if (RT_UNLIKELY(!i1stFree && !pData->apvHash[i]))
|
---|
1005 | i1stFree = i;
|
---|
1006 | }
|
---|
1007 | }
|
---|
1008 | }
|
---|
1009 | return i;
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 |
|
---|
1013 | /**
|
---|
1014 | * Removes the pointer from the hash table.
|
---|
1015 | */
|
---|
1016 | void VBoxU32PtrDone(PNATState pData, void *pv, uint32_t iHint)
|
---|
1017 | {
|
---|
1018 | /* We don't count NULL pointers. */
|
---|
1019 | if (pv == NULL)
|
---|
1020 | return;
|
---|
1021 | pData->cpvHashDone++;
|
---|
1022 |
|
---|
1023 | /* try the hint */
|
---|
1024 | if ( iHint
|
---|
1025 | && iHint < RT_ELEMENTS(pData->apvHash)
|
---|
1026 | && pData->apvHash[iHint] == pv)
|
---|
1027 | {
|
---|
1028 | pData->apvHash[iHint] = NULL;
|
---|
1029 | pData->cpvHashUsed--;
|
---|
1030 | return;
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | iHint = ((uintptr_t)pv >> 3) % RT_ELEMENTS(pData->apvHash);
|
---|
1034 | if (RT_UNLIKELY(pData->apvHash[iHint] != pv))
|
---|
1035 | {
|
---|
1036 | /*
|
---|
1037 | * Try up to 120 times then assert.
|
---|
1038 | */
|
---|
1039 | const uint32_t i2 = ((uintptr_t)pv >> 2) % 7867;
|
---|
1040 | int cTries = 120;
|
---|
1041 | for (;;)
|
---|
1042 | {
|
---|
1043 | /* advance to the next hash entry and test it. */
|
---|
1044 | iHint = (iHint + i2) % RT_ELEMENTS(pData->apvHash);
|
---|
1045 | while (RT_UNLIKELY(!iHint))
|
---|
1046 | iHint = (iHint + i2) % RT_ELEMENTS(pData->apvHash);
|
---|
1047 | if (pData->apvHash[iHint] == pv)
|
---|
1048 | break;
|
---|
1049 |
|
---|
1050 | /* check if we should give in.*/
|
---|
1051 | if (--cTries > 0)
|
---|
1052 | {
|
---|
1053 | AssertReleaseMsgFailed(("NAT pointer hash error. pv=%p cpvHashUsed=%u cpvHashCollisions=%u\n",
|
---|
1054 | pv, pData->cpvHashUsed, pData->cpvHashCollisions));
|
---|
1055 | return;
|
---|
1056 | }
|
---|
1057 | }
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | /* found it */
|
---|
1061 | pData->apvHash[iHint] = NULL;
|
---|
1062 | pData->cpvHashUsed--;
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | #endif /* SIZEOF_CHAR_P != 4 && !defined(VBOX_WITH_BSD_REASS */
|
---|