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