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 | * @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94
|
---|
34 | * udp_usrreq.c,v 1.4 1994/10/02 17:48:45 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 | #include <slirp.h>
|
---|
46 | #include "ip_icmp.h"
|
---|
47 |
|
---|
48 | #ifndef VBOX
|
---|
49 | struct udpstat udpstat;
|
---|
50 |
|
---|
51 | struct socket udb;
|
---|
52 | #endif /* !VBOX */
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * UDP protocol implementation.
|
---|
56 | * Per RFC 768, August, 1980.
|
---|
57 | */
|
---|
58 | #ifdef VBOX
|
---|
59 | #define udpcksum 1
|
---|
60 | #else /* !VBOX */
|
---|
61 | #ifndef COMPAT_42
|
---|
62 | int udpcksum = 1;
|
---|
63 | #else
|
---|
64 | int udpcksum = 0; /* XXX */
|
---|
65 | #endif
|
---|
66 | #endif /* !VBOX */
|
---|
67 |
|
---|
68 | #ifndef VBOX
|
---|
69 | struct socket *udp_last_so = &udb;
|
---|
70 | #endif /* !VBOX */
|
---|
71 |
|
---|
72 | void
|
---|
73 | #ifdef VBOX
|
---|
74 | udp_init(PNATState pData)
|
---|
75 | #else /* !VBOX */
|
---|
76 | udp_init()
|
---|
77 | #endif /* !VBOX */
|
---|
78 | {
|
---|
79 | #ifdef VBOX
|
---|
80 | udp_last_so = &udb;
|
---|
81 | #endif /* VBOX */
|
---|
82 | udb.so_next = udb.so_prev = &udb;
|
---|
83 | }
|
---|
84 | /* m->m_data points at ip packet header
|
---|
85 | * m->m_len length ip packet
|
---|
86 | * ip->ip_len length data (IPDU)
|
---|
87 | */
|
---|
88 | void
|
---|
89 | #ifdef VBOX
|
---|
90 | udp_input(PNATState pData, register struct mbuf *m, int iphlen)
|
---|
91 | #else /* !VBOX */
|
---|
92 | udp_input(m, iphlen)
|
---|
93 | register struct mbuf *m;
|
---|
94 | int iphlen;
|
---|
95 | #endif /* !VBOX */
|
---|
96 | {
|
---|
97 | register struct ip *ip;
|
---|
98 | register struct udphdr *uh;
|
---|
99 | /* struct mbuf *opts = 0;*/
|
---|
100 | int len;
|
---|
101 | struct ip save_ip;
|
---|
102 | struct socket *so;
|
---|
103 |
|
---|
104 | DEBUG_CALL("udp_input");
|
---|
105 | DEBUG_ARG("m = %lx", (long)m);
|
---|
106 | DEBUG_ARG("iphlen = %d", iphlen);
|
---|
107 |
|
---|
108 | udpstat.udps_ipackets++;
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Strip IP options, if any; should skip this,
|
---|
112 | * make available to user, and use on returned packets,
|
---|
113 | * but we don't yet have a way to check the checksum
|
---|
114 | * with options still present.
|
---|
115 | */
|
---|
116 | if(iphlen > sizeof(struct ip)) {
|
---|
117 | ip_stripoptions(m, (struct mbuf *)0);
|
---|
118 | iphlen = sizeof(struct ip);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /*
|
---|
122 | * Get IP and UDP header together in first mbuf.
|
---|
123 | */
|
---|
124 | ip = mtod(m, struct ip *);
|
---|
125 | uh = (struct udphdr *)((caddr_t)ip + iphlen);
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * Make mbuf data length reflect UDP length.
|
---|
129 | * If not enough data to reflect UDP length, drop.
|
---|
130 | */
|
---|
131 | len = ntohs((u_int16_t)uh->uh_ulen);
|
---|
132 |
|
---|
133 | if (ip->ip_len != len) {
|
---|
134 | if (len > ip->ip_len) {
|
---|
135 | udpstat.udps_badlen++;
|
---|
136 | goto bad;
|
---|
137 | }
|
---|
138 | m_adj(m, len - ip->ip_len);
|
---|
139 | ip->ip_len = len;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /*
|
---|
143 | * Save a copy of the IP header in case we want restore it
|
---|
144 | * for sending an ICMP error message in response.
|
---|
145 | */
|
---|
146 | save_ip = *ip;
|
---|
147 | save_ip.ip_len+= iphlen; /* tcp_input subtracts this */
|
---|
148 |
|
---|
149 | /*
|
---|
150 | * Checksum extended UDP header and data.
|
---|
151 | */
|
---|
152 | if (udpcksum && uh->uh_sum) {
|
---|
153 | ((struct ipovly *)ip)->ih_next = 0;
|
---|
154 | ((struct ipovly *)ip)->ih_prev = 0;
|
---|
155 | ((struct ipovly *)ip)->ih_x1 = 0;
|
---|
156 | ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
|
---|
157 | /* keep uh_sum for ICMP reply
|
---|
158 | * uh->uh_sum = cksum(m, len + sizeof (struct ip));
|
---|
159 | * if (uh->uh_sum) {
|
---|
160 | */
|
---|
161 | if(cksum(m, len + sizeof(struct ip))) {
|
---|
162 | udpstat.udps_badsum++;
|
---|
163 | goto bad;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * handle DHCP/BOOTP
|
---|
169 | */
|
---|
170 | if (ntohs(uh->uh_dport) == BOOTP_SERVER) {
|
---|
171 | #ifdef VBOX
|
---|
172 | bootp_input(pData, m);
|
---|
173 | #else /* !VBOX */
|
---|
174 | bootp_input(m);
|
---|
175 | #endif /* !VBOX */
|
---|
176 | goto bad;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /*
|
---|
180 | * handle TFTP
|
---|
181 | */
|
---|
182 | if (ntohs(uh->uh_dport) == TFTP_SERVER) {
|
---|
183 | #ifdef VBOX
|
---|
184 | tftp_input(pData, m);
|
---|
185 | #else /* !VBOX */
|
---|
186 | tftp_input(m);
|
---|
187 | #endif /* !VBOX */
|
---|
188 | goto bad;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /*
|
---|
192 | * Locate pcb for datagram.
|
---|
193 | */
|
---|
194 | so = udp_last_so;
|
---|
195 | if (so->so_lport != uh->uh_sport ||
|
---|
196 | so->so_laddr.s_addr != ip->ip_src.s_addr) {
|
---|
197 | struct socket *tmp;
|
---|
198 |
|
---|
199 | for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next) {
|
---|
200 | if (tmp->so_lport == uh->uh_sport &&
|
---|
201 | tmp->so_laddr.s_addr == ip->ip_src.s_addr) {
|
---|
202 | #ifndef VBOX /* Move this further down, as it must be done also for the cache hit case. */
|
---|
203 | tmp->so_faddr.s_addr = ip->ip_dst.s_addr;
|
---|
204 | tmp->so_fport = uh->uh_dport;
|
---|
205 | #endif /* !VBOX */
|
---|
206 | so = tmp;
|
---|
207 | break;
|
---|
208 | }
|
---|
209 | }
|
---|
210 | if (tmp == &udb) {
|
---|
211 | so = NULL;
|
---|
212 | } else {
|
---|
213 | udpstat.udpps_pcbcachemiss++;
|
---|
214 | udp_last_so = so;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | if (so == NULL) {
|
---|
219 | /*
|
---|
220 | * If there's no socket for this packet,
|
---|
221 | * create one
|
---|
222 | */
|
---|
223 | if ((so = socreate()) == NULL) goto bad;
|
---|
224 | #ifdef VBOX
|
---|
225 | if(udp_attach(pData, so) == -1) {
|
---|
226 | #else /* !VBOX */
|
---|
227 | if(udp_attach(so) == -1) {
|
---|
228 | #endif /* !VBOX */
|
---|
229 | DEBUG_MISC((dfd," udp_attach errno = %d-%s\n",
|
---|
230 | errno,strerror(errno)));
|
---|
231 | #ifdef VBOX
|
---|
232 | sofree(pData, so);
|
---|
233 | #else /* !VBOX */
|
---|
234 | sofree(so);
|
---|
235 | #endif /* !VBOX */
|
---|
236 | goto bad;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /*
|
---|
240 | * Setup fields
|
---|
241 | */
|
---|
242 | /* udp_last_so = so; */
|
---|
243 | so->so_laddr = ip->ip_src;
|
---|
244 | so->so_lport = uh->uh_sport;
|
---|
245 |
|
---|
246 | if ((so->so_iptos = udp_tos(so)) == 0)
|
---|
247 | so->so_iptos = ip->ip_tos;
|
---|
248 |
|
---|
249 | /*
|
---|
250 | * XXXXX Here, check if it's in udpexec_list,
|
---|
251 | * and if it is, do the fork_exec() etc.
|
---|
252 | */
|
---|
253 | }
|
---|
254 |
|
---|
255 | so->so_faddr = ip->ip_dst; /* XXX */
|
---|
256 | so->so_fport = uh->uh_dport; /* XXX */
|
---|
257 |
|
---|
258 | iphlen += sizeof(struct udphdr);
|
---|
259 | m->m_len -= iphlen;
|
---|
260 | m->m_data += iphlen;
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * Now we sendto() the packet.
|
---|
264 | */
|
---|
265 | if (so->so_emu)
|
---|
266 | #ifdef VBOX
|
---|
267 | udp_emu(pData, so, m);
|
---|
268 | #else /* !VBOX */
|
---|
269 | udp_emu(so, m);
|
---|
270 | #endif /* !VBOX */
|
---|
271 |
|
---|
272 | #ifdef VBOX
|
---|
273 | if(sosendto(pData, so,m) == -1) {
|
---|
274 | #else /* !VBOX */
|
---|
275 | if(sosendto(so,m) == -1) {
|
---|
276 | #endif /* !VBOX */
|
---|
277 | m->m_len += iphlen;
|
---|
278 | m->m_data -= iphlen;
|
---|
279 | *ip=save_ip;
|
---|
280 | DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno)));
|
---|
281 | #ifdef VBOX
|
---|
282 | icmp_error(pData, m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
|
---|
283 | #else /* !VBOX */
|
---|
284 | icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));
|
---|
285 | #endif /* !VBOX */
|
---|
286 | }
|
---|
287 |
|
---|
288 | #ifdef VBOX
|
---|
289 | m_free(pData, so->so_m); /* used for ICMP if error on sorecvfrom */
|
---|
290 | #else /* !VBOX */
|
---|
291 | m_free(so->so_m); /* used for ICMP if error on sorecvfrom */
|
---|
292 | #endif /* !VBOX */
|
---|
293 |
|
---|
294 | /* restore the orig mbuf packet */
|
---|
295 | m->m_len += iphlen;
|
---|
296 | m->m_data -= iphlen;
|
---|
297 | *ip=save_ip;
|
---|
298 | so->so_m=m; /* ICMP backup */
|
---|
299 |
|
---|
300 | return;
|
---|
301 | bad:
|
---|
302 | #ifdef VBOX
|
---|
303 | m_freem(pData, m);
|
---|
304 | #else /* !VBOX */
|
---|
305 | m_freem(m);
|
---|
306 | #endif /* !VBOX */
|
---|
307 | /* if (opts) m_freem(opts); */
|
---|
308 | return;
|
---|
309 | }
|
---|
310 |
|
---|
311 | #ifdef VBOX
|
---|
312 | int udp_output2(PNATState pData, struct socket *so, struct mbuf *m,
|
---|
313 | struct sockaddr_in *saddr, struct sockaddr_in *daddr,
|
---|
314 | int iptos)
|
---|
315 | #else /* !VBOX */
|
---|
316 | int udp_output2(struct socket *so, struct mbuf *m,
|
---|
317 | struct sockaddr_in *saddr, struct sockaddr_in *daddr,
|
---|
318 | int iptos)
|
---|
319 | #endif /* !VBOX */
|
---|
320 | {
|
---|
321 | register struct udpiphdr *ui;
|
---|
322 | int error = 0;
|
---|
323 |
|
---|
324 | DEBUG_CALL("udp_output");
|
---|
325 | DEBUG_ARG("so = %lx", (long)so);
|
---|
326 | DEBUG_ARG("m = %lx", (long)m);
|
---|
327 | DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
|
---|
328 | DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
|
---|
329 |
|
---|
330 | /*
|
---|
331 | * Adjust for header
|
---|
332 | */
|
---|
333 | m->m_data -= sizeof(struct udpiphdr);
|
---|
334 | m->m_len += sizeof(struct udpiphdr);
|
---|
335 |
|
---|
336 | /*
|
---|
337 | * Fill in mbuf with extended UDP header
|
---|
338 | * and addresses and length put into network format.
|
---|
339 | */
|
---|
340 | ui = mtod(m, struct udpiphdr *);
|
---|
341 | ui->ui_next = ui->ui_prev = 0;
|
---|
342 | ui->ui_x1 = 0;
|
---|
343 | ui->ui_pr = IPPROTO_UDP;
|
---|
344 | ui->ui_len = htons(m->m_len - sizeof(struct ip)); /* + sizeof (struct udphdr)); */
|
---|
345 | /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
|
---|
346 | ui->ui_src = saddr->sin_addr;
|
---|
347 | ui->ui_dst = daddr->sin_addr;
|
---|
348 | ui->ui_sport = saddr->sin_port;
|
---|
349 | ui->ui_dport = daddr->sin_port;
|
---|
350 | ui->ui_ulen = ui->ui_len;
|
---|
351 |
|
---|
352 | /*
|
---|
353 | * Stuff checksum and output datagram.
|
---|
354 | */
|
---|
355 | ui->ui_sum = 0;
|
---|
356 | if (udpcksum) {
|
---|
357 | if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
|
---|
358 | ui->ui_sum = 0xffff;
|
---|
359 | }
|
---|
360 | ((struct ip *)ui)->ip_len = m->m_len;
|
---|
361 |
|
---|
362 | ((struct ip *)ui)->ip_ttl = ip_defttl;
|
---|
363 | ((struct ip *)ui)->ip_tos = iptos;
|
---|
364 |
|
---|
365 | udpstat.udps_opackets++;
|
---|
366 |
|
---|
367 | #ifdef VBOX
|
---|
368 | error = ip_output(pData, so, m);
|
---|
369 | #else /* !VBOX */
|
---|
370 | error = ip_output(so, m);
|
---|
371 | #endif /* !VBOX */
|
---|
372 |
|
---|
373 | return (error);
|
---|
374 | }
|
---|
375 |
|
---|
376 | #ifdef VBOX
|
---|
377 | int udp_output(PNATState pData, struct socket *so, struct mbuf *m,
|
---|
378 | struct sockaddr_in *addr)
|
---|
379 | #else /* !VBOX */
|
---|
380 | int udp_output(struct socket *so, struct mbuf *m,
|
---|
381 | struct sockaddr_in *addr)
|
---|
382 | #endif /* !VBOX */
|
---|
383 | {
|
---|
384 | struct sockaddr_in saddr, daddr;
|
---|
385 |
|
---|
386 | saddr = *addr;
|
---|
387 | if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
|
---|
388 | saddr.sin_addr.s_addr = so->so_faddr.s_addr;
|
---|
389 | if ((so->so_faddr.s_addr & htonl(0x000000ff)) == htonl(0xff))
|
---|
390 | saddr.sin_addr.s_addr = alias_addr.s_addr;
|
---|
391 | }
|
---|
392 | daddr.sin_addr = so->so_laddr;
|
---|
393 | daddr.sin_port = so->so_lport;
|
---|
394 |
|
---|
395 | #ifdef VBOX
|
---|
396 | return udp_output2(pData, so, m, &saddr, &daddr, so->so_iptos);
|
---|
397 | #else /* !VBOX */
|
---|
398 | return udp_output2(so, m, &saddr, &daddr, so->so_iptos);
|
---|
399 | #endif /* !VBOX */
|
---|
400 | }
|
---|
401 |
|
---|
402 | int
|
---|
403 | #ifdef VBOX
|
---|
404 | udp_attach(PNATState pData, struct socket *so)
|
---|
405 | #else /* !VBOX */
|
---|
406 | udp_attach(so)
|
---|
407 | struct socket *so;
|
---|
408 | #endif /* !VBPX */
|
---|
409 | {
|
---|
410 | struct sockaddr_in addr;
|
---|
411 |
|
---|
412 | if((so->s = socket(AF_INET,SOCK_DGRAM,0)) != -1) {
|
---|
413 | /*
|
---|
414 | * Here, we bind() the socket. Although not really needed
|
---|
415 | * (sendto() on an unbound socket will bind it), it's done
|
---|
416 | * here so that emulation of ytalk etc. don't have to do it
|
---|
417 | */
|
---|
418 | addr.sin_family = AF_INET;
|
---|
419 | addr.sin_port = 0;
|
---|
420 | addr.sin_addr.s_addr = INADDR_ANY;
|
---|
421 | if(bind(so->s, (struct sockaddr *)&addr, sizeof(addr))<0) {
|
---|
422 | int lasterrno=errno;
|
---|
423 | closesocket(so->s);
|
---|
424 | so->s=-1;
|
---|
425 | #ifdef _WIN32
|
---|
426 | WSASetLastError(lasterrno);
|
---|
427 | #else
|
---|
428 | errno=lasterrno;
|
---|
429 | #endif
|
---|
430 | } else {
|
---|
431 | /* success, insert in queue */
|
---|
432 | so->so_expire = curtime + SO_EXPIRE;
|
---|
433 | so->so_expire = curtime + SO_EXPIRE;
|
---|
434 | insque(pData, so,&udb);
|
---|
435 | }
|
---|
436 | }
|
---|
437 | return(so->s);
|
---|
438 | }
|
---|
439 |
|
---|
440 | void
|
---|
441 | #ifdef VBOX
|
---|
442 | udp_detach(PNATState pData, struct socket *so)
|
---|
443 | #else /* !VBOX */
|
---|
444 | udp_detach(so)
|
---|
445 | struct socket *so;
|
---|
446 | #endif /* !VBOX */
|
---|
447 | {
|
---|
448 | #ifdef VBOX
|
---|
449 | /* Correctly update list if detaching last socket in list. */
|
---|
450 | if (so == udp_last_so) udp_last_so = &udb;
|
---|
451 | #endif /* VBOX */
|
---|
452 | closesocket(so->s);
|
---|
453 | /* if (so->so_m) m_free(so->so_m); done by sofree */
|
---|
454 |
|
---|
455 | #ifdef VBOX
|
---|
456 | sofree(pData, so);
|
---|
457 | #else /* !VBOX */
|
---|
458 | sofree(so);
|
---|
459 | #endif /* !VBOX */
|
---|
460 | }
|
---|
461 |
|
---|
462 | #ifdef VBOX
|
---|
463 | static const struct tos_t udptos[] = {
|
---|
464 | #else /* !VBOX */
|
---|
465 | struct tos_t udptos[] = {
|
---|
466 | #endif /* !VBOX */
|
---|
467 | {0, 53, IPTOS_LOWDELAY, 0}, /* DNS */
|
---|
468 | {517, 517, IPTOS_LOWDELAY, EMU_TALK}, /* talk */
|
---|
469 | {518, 518, IPTOS_LOWDELAY, EMU_NTALK}, /* ntalk */
|
---|
470 | {0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME}, /* Cu-Seeme */
|
---|
471 | {0, 0, 0, 0}
|
---|
472 | };
|
---|
473 |
|
---|
474 | u_int8_t
|
---|
475 | udp_tos(so)
|
---|
476 | struct socket *so;
|
---|
477 | {
|
---|
478 | int i = 0;
|
---|
479 |
|
---|
480 | while(udptos[i].tos) {
|
---|
481 | if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) ||
|
---|
482 | (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) {
|
---|
483 | so->so_emu = udptos[i].emu;
|
---|
484 | return udptos[i].tos;
|
---|
485 | }
|
---|
486 | i++;
|
---|
487 | }
|
---|
488 |
|
---|
489 | return 0;
|
---|
490 | }
|
---|
491 |
|
---|
492 | #ifdef EMULATE_TALK
|
---|
493 | #include "talkd.h"
|
---|
494 | #endif
|
---|
495 |
|
---|
496 | /*
|
---|
497 | * Here, talk/ytalk/ntalk requests must be emulated
|
---|
498 | */
|
---|
499 | void
|
---|
500 | #ifdef VBOX
|
---|
501 | udp_emu(PNATState pData, struct socket *so, struct mbuf *m)
|
---|
502 | #else /* !VBOX */
|
---|
503 | udp_emu(so, m)
|
---|
504 | struct socket *so;
|
---|
505 | struct mbuf *m;
|
---|
506 | #endif /* !VBOX */
|
---|
507 | {
|
---|
508 | struct sockaddr_in addr;
|
---|
509 | #ifndef VBOX
|
---|
510 | int addrlen = sizeof(addr);
|
---|
511 | #else /* VBOX */
|
---|
512 | socklen_t addrlen = sizeof(addr);
|
---|
513 | #endif /* VBOX */
|
---|
514 | #ifdef EMULATE_TALK
|
---|
515 | CTL_MSG_OLD *omsg;
|
---|
516 | CTL_MSG *nmsg;
|
---|
517 | char buff[sizeof(CTL_MSG)];
|
---|
518 | u_char type;
|
---|
519 |
|
---|
520 | struct talk_request {
|
---|
521 | struct talk_request *next;
|
---|
522 | struct socket *udp_so;
|
---|
523 | struct socket *tcp_so;
|
---|
524 | } *req;
|
---|
525 |
|
---|
526 | static struct talk_request *req_tbl = 0;
|
---|
527 |
|
---|
528 | #endif
|
---|
529 |
|
---|
530 | struct cu_header {
|
---|
531 | uint16_t d_family; /* destination family */
|
---|
532 | uint16_t d_port; /* destination port */
|
---|
533 | uint32_t d_addr; /* destination address */
|
---|
534 | uint16_t s_family; /* source family */
|
---|
535 | uint16_t s_port; /* source port */
|
---|
536 | uint32_t so_addr; /* source address */
|
---|
537 | uint32_t seqn; /* sequence number */
|
---|
538 | uint16_t message; /* message */
|
---|
539 | uint16_t data_type; /* data type */
|
---|
540 | uint16_t pkt_len; /* packet length */
|
---|
541 | } *cu_head;
|
---|
542 |
|
---|
543 | switch(so->so_emu) {
|
---|
544 |
|
---|
545 | #ifdef EMULATE_TALK
|
---|
546 | case EMU_TALK:
|
---|
547 | case EMU_NTALK:
|
---|
548 | /*
|
---|
549 | * Talk emulation. We always change the ctl_addr to get
|
---|
550 | * some answers from the daemon. When an ANNOUNCE comes,
|
---|
551 | * we send LEAVE_INVITE to the local daemons. Also when a
|
---|
552 | * DELETE comes, we send copies to the local daemons.
|
---|
553 | */
|
---|
554 | if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
|
---|
555 | return;
|
---|
556 |
|
---|
557 | #define IS_OLD (so->so_emu == EMU_TALK)
|
---|
558 |
|
---|
559 | #define COPY_MSG(dest, src) { dest->type = src->type; \
|
---|
560 | dest->id_num = src->id_num; \
|
---|
561 | dest->pid = src->pid; \
|
---|
562 | dest->addr = src->addr; \
|
---|
563 | dest->ctl_addr = src->ctl_addr; \
|
---|
564 | memcpy(&dest->l_name, &src->l_name, NAME_SIZE_OLD); \
|
---|
565 | memcpy(&dest->r_name, &src->r_name, NAME_SIZE_OLD); \
|
---|
566 | memcpy(&dest->r_tty, &src->r_tty, TTY_SIZE); }
|
---|
567 |
|
---|
568 | #define OTOSIN(ptr, field) ((struct sockaddr_in *)&ptr->field)
|
---|
569 | /* old_sockaddr to sockaddr_in */
|
---|
570 |
|
---|
571 |
|
---|
572 | if (IS_OLD) { /* old talk */
|
---|
573 | omsg = mtod(m, CTL_MSG_OLD*);
|
---|
574 | nmsg = (CTL_MSG *) buff;
|
---|
575 | type = omsg->type;
|
---|
576 | OTOSIN(omsg, ctl_addr)->sin_port = addr.sin_port;
|
---|
577 | OTOSIN(omsg, ctl_addr)->sin_addr = our_addr;
|
---|
578 | strncpy(omsg->l_name, getlogin(), NAME_SIZE_OLD);
|
---|
579 | } else { /* new talk */
|
---|
580 | omsg = (CTL_MSG_OLD *) buff;
|
---|
581 | nmsg = mtod(m, CTL_MSG *);
|
---|
582 | type = nmsg->type;
|
---|
583 | OTOSIN(nmsg, ctl_addr)->sin_port = addr.sin_port;
|
---|
584 | OTOSIN(nmsg, ctl_addr)->sin_addr = our_addr;
|
---|
585 | strncpy(nmsg->l_name, getlogin(), NAME_SIZE_OLD);
|
---|
586 | }
|
---|
587 |
|
---|
588 | if (type == LOOK_UP)
|
---|
589 | return; /* for LOOK_UP this is enough */
|
---|
590 |
|
---|
591 | if (IS_OLD) { /* make a copy of the message */
|
---|
592 | COPY_MSG(nmsg, omsg);
|
---|
593 | nmsg->vers = 1;
|
---|
594 | nmsg->answer = 0;
|
---|
595 | } else
|
---|
596 | COPY_MSG(omsg, nmsg);
|
---|
597 |
|
---|
598 | /*
|
---|
599 | * If if is an ANNOUNCE message, we go through the
|
---|
600 | * request table to see if a tcp port has already
|
---|
601 | * been redirected for this socket. If not, we solisten()
|
---|
602 | * a new socket and add this entry to the table.
|
---|
603 | * The port number of the tcp socket and our IP
|
---|
604 | * are put to the addr field of the message structures.
|
---|
605 | * Then a LEAVE_INVITE is sent to both local daemon
|
---|
606 | * ports, 517 and 518. This is why we have two copies
|
---|
607 | * of the message, one in old talk and one in new talk
|
---|
608 | * format.
|
---|
609 | */
|
---|
610 |
|
---|
611 | if (type == ANNOUNCE) {
|
---|
612 | int s;
|
---|
613 | u_short temp_port;
|
---|
614 |
|
---|
615 | for(req = req_tbl; req; req = req->next)
|
---|
616 | if (so == req->udp_so)
|
---|
617 | break; /* found it */
|
---|
618 |
|
---|
619 | if (!req) { /* no entry for so, create new */
|
---|
620 | req = (struct talk_request *)
|
---|
621 | malloc(sizeof(struct talk_request));
|
---|
622 | req->udp_so = so;
|
---|
623 | req->tcp_so = solisten(0,
|
---|
624 | OTOSIN(omsg, addr)->sin_addr.s_addr,
|
---|
625 | OTOSIN(omsg, addr)->sin_port,
|
---|
626 | SS_FACCEPTONCE);
|
---|
627 | req->next = req_tbl;
|
---|
628 | req_tbl = req;
|
---|
629 | }
|
---|
630 |
|
---|
631 | /* replace port number in addr field */
|
---|
632 | addrlen = sizeof(addr);
|
---|
633 | getsockname(req->tcp_so->s,
|
---|
634 | (struct sockaddr *) &addr,
|
---|
635 | &addrlen);
|
---|
636 | OTOSIN(omsg, addr)->sin_port = addr.sin_port;
|
---|
637 | OTOSIN(omsg, addr)->sin_addr = our_addr;
|
---|
638 | OTOSIN(nmsg, addr)->sin_port = addr.sin_port;
|
---|
639 | OTOSIN(nmsg, addr)->sin_addr = our_addr;
|
---|
640 |
|
---|
641 | /* send LEAVE_INVITEs */
|
---|
642 | temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
|
---|
643 | OTOSIN(omsg, ctl_addr)->sin_port = 0;
|
---|
644 | OTOSIN(nmsg, ctl_addr)->sin_port = 0;
|
---|
645 | omsg->type = nmsg->type = LEAVE_INVITE;
|
---|
646 |
|
---|
647 | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
---|
648 | addr.sin_addr = our_addr;
|
---|
649 | addr.sin_family = AF_INET;
|
---|
650 | addr.sin_port = htons(517);
|
---|
651 | sendto(s, (char *)omsg, sizeof(*omsg), 0,
|
---|
652 | (struct sockaddr *)&addr, sizeof(addr));
|
---|
653 | addr.sin_port = htons(518);
|
---|
654 | sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
|
---|
655 | (struct sockaddr *) &addr, sizeof(addr));
|
---|
656 | closesocket(s) ;
|
---|
657 |
|
---|
658 | omsg->type = nmsg->type = ANNOUNCE;
|
---|
659 | OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
|
---|
660 | OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
|
---|
661 | }
|
---|
662 |
|
---|
663 | /*
|
---|
664 | * If it is a DELETE message, we send a copy to the
|
---|
665 | * local daemons. Then we delete the entry corresponding
|
---|
666 | * to our socket from the request table.
|
---|
667 | */
|
---|
668 |
|
---|
669 | if (type == DELETE) {
|
---|
670 | struct talk_request *temp_req, *req_next;
|
---|
671 | int s;
|
---|
672 | u_short temp_port;
|
---|
673 |
|
---|
674 | temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
|
---|
675 | OTOSIN(omsg, ctl_addr)->sin_port = 0;
|
---|
676 | OTOSIN(nmsg, ctl_addr)->sin_port = 0;
|
---|
677 |
|
---|
678 | s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
---|
679 | addr.sin_addr = our_addr;
|
---|
680 | addr.sin_family = AF_INET;
|
---|
681 | addr.sin_port = htons(517);
|
---|
682 | sendto(s, (char *)omsg, sizeof(*omsg), 0,
|
---|
683 | (struct sockaddr *)&addr, sizeof(addr));
|
---|
684 | addr.sin_port = htons(518);
|
---|
685 | sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
|
---|
686 | (struct sockaddr *)&addr, sizeof(addr));
|
---|
687 | closesocket(s);
|
---|
688 |
|
---|
689 | OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
|
---|
690 | OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
|
---|
691 |
|
---|
692 | /* delete table entry */
|
---|
693 | if (so == req_tbl->udp_so) {
|
---|
694 | temp_req = req_tbl;
|
---|
695 | req_tbl = req_tbl->next;
|
---|
696 | free(temp_req);
|
---|
697 | } else {
|
---|
698 | temp_req = req_tbl;
|
---|
699 | for(req = req_tbl->next; req; req = req_next) {
|
---|
700 | req_next = req->next;
|
---|
701 | if (so == req->udp_so) {
|
---|
702 | temp_req->next = req_next;
|
---|
703 | free(req);
|
---|
704 | break;
|
---|
705 | } else {
|
---|
706 | temp_req = req;
|
---|
707 | }
|
---|
708 | }
|
---|
709 | }
|
---|
710 | }
|
---|
711 |
|
---|
712 | return;
|
---|
713 | #endif
|
---|
714 |
|
---|
715 | case EMU_CUSEEME:
|
---|
716 |
|
---|
717 | /*
|
---|
718 | * Cu-SeeMe emulation.
|
---|
719 | * Hopefully the packet is more that 16 bytes long. We don't
|
---|
720 | * do any other tests, just replace the address and port
|
---|
721 | * fields.
|
---|
722 | */
|
---|
723 | if (m->m_len >= sizeof (*cu_head)) {
|
---|
724 | if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
|
---|
725 | return;
|
---|
726 | cu_head = mtod(m, struct cu_header *);
|
---|
727 | cu_head->s_port = addr.sin_port;
|
---|
728 | cu_head->so_addr = our_addr.s_addr;
|
---|
729 | }
|
---|
730 |
|
---|
731 | return;
|
---|
732 | }
|
---|
733 | }
|
---|
734 |
|
---|
735 | struct socket *
|
---|
736 | #ifdef VBOX
|
---|
737 | udp_listen(PNATState pData, u_int port, u_int32_t laddr, u_int lport, int flags)
|
---|
738 | #else /* !VBOX */
|
---|
739 | udp_listen(port, laddr, lport, flags)
|
---|
740 | u_int port;
|
---|
741 | u_int32_t laddr;
|
---|
742 | u_int lport;
|
---|
743 | int flags;
|
---|
744 | #endif /* !VBOX */
|
---|
745 | {
|
---|
746 | struct sockaddr_in addr;
|
---|
747 | struct socket *so;
|
---|
748 | #ifndef VBOX
|
---|
749 | int addrlen = sizeof(struct sockaddr_in), opt = 1;
|
---|
750 | #else /* VBOX */
|
---|
751 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
752 | int opt = 1;
|
---|
753 | #endif /* VBOX */
|
---|
754 |
|
---|
755 | if ((so = socreate()) == NULL) {
|
---|
756 | free(so);
|
---|
757 | return NULL;
|
---|
758 | }
|
---|
759 | so->s = socket(AF_INET,SOCK_DGRAM,0);
|
---|
760 | so->so_expire = curtime + SO_EXPIRE;
|
---|
761 | insque(pData, so,&udb);
|
---|
762 |
|
---|
763 | addr.sin_family = AF_INET;
|
---|
764 | addr.sin_addr.s_addr = INADDR_ANY;
|
---|
765 | addr.sin_port = port;
|
---|
766 |
|
---|
767 | if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
|
---|
768 | #ifdef VBOX
|
---|
769 | udp_detach(pData, so);
|
---|
770 | #else /* !VBOX */
|
---|
771 | udp_detach(so);
|
---|
772 | #endif /* !VBOX */
|
---|
773 | return NULL;
|
---|
774 | }
|
---|
775 | setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
|
---|
776 | /* setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int)); */
|
---|
777 |
|
---|
778 | getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
|
---|
779 | so->so_fport = addr.sin_port;
|
---|
780 | if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
|
---|
781 | so->so_faddr = alias_addr;
|
---|
782 | else
|
---|
783 | so->so_faddr = addr.sin_addr;
|
---|
784 |
|
---|
785 | so->so_lport = lport;
|
---|
786 | so->so_laddr.s_addr = laddr;
|
---|
787 | if (flags != SS_FACCEPTONCE)
|
---|
788 | so->so_expire = 0;
|
---|
789 |
|
---|
790 | so->so_state = SS_ISFCONNECTED;
|
---|
791 |
|
---|
792 | return so;
|
---|
793 | }
|
---|