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