VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/udp.c@ 17224

Last change on this file since 17224 was 17191, checked in by vboxsync, 16 years ago

spaces

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette