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