VirtualBox

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

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

slirp: removed the old 64-bit incompatible reassemble code

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