VirtualBox

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

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

NAT:NSOK_INC/DEC added to count sockets number for allocation enough pollfd buffer
(and latter epoll_event (for Linux only)).

  • Property svn:eol-style set to native
File size: 22.4 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) == -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 }
255
256 m_free(pData, so->so_m); /* used for ICMP if error on sorecvfrom */
257
258 /* restore the orig mbuf packet */
259 m->m_len += iphlen;
260 m->m_data -= iphlen;
261 *ip = save_ip;
262 so->so_m = m; /* ICMP backup */
263
264 return;
265
266bad:
267 m_freem(pData, m);
268 return;
269}
270
271int udp_output2(PNATState pData, struct socket *so, struct mbuf *m,
272 struct sockaddr_in *saddr, struct sockaddr_in *daddr,
273 int iptos)
274{
275 register struct udpiphdr *ui;
276 int error = 0;
277
278 DEBUG_CALL("udp_output");
279 DEBUG_ARG("so = %lx", (long)so);
280 DEBUG_ARG("m = %lx", (long)m);
281 DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
282 DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
283
284 /*
285 * Adjust for header
286 */
287 m->m_data -= sizeof(struct udpiphdr);
288 m->m_len += sizeof(struct udpiphdr);
289
290 /*
291 * Fill in mbuf with extended UDP header
292 * and addresses and length put into network format.
293 */
294 ui = mtod(m, struct udpiphdr *);
295 memset(ui->ui_x1, 0, 9);
296 ui->ui_pr = IPPROTO_UDP;
297 ui->ui_len = htons(m->m_len - sizeof(struct ip)); /* + sizeof (struct udphdr)); */
298 /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
299 ui->ui_src = saddr->sin_addr;
300 ui->ui_dst = daddr->sin_addr;
301 ui->ui_sport = saddr->sin_port;
302 ui->ui_dport = daddr->sin_port;
303 ui->ui_ulen = ui->ui_len;
304
305 /*
306 * Stuff checksum and output datagram.
307 */
308 ui->ui_sum = 0;
309 if (udpcksum)
310 {
311 if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
312 ui->ui_sum = 0xffff;
313 }
314 ((struct ip *)ui)->ip_len = m->m_len;
315 ((struct ip *)ui)->ip_ttl = ip_defttl;
316 ((struct ip *)ui)->ip_tos = iptos;
317
318 udpstat.udps_opackets++;
319
320 error = ip_output(pData, so, m);
321
322 return error;
323}
324
325int udp_output(PNATState pData, struct socket *so, struct mbuf *m,
326 struct sockaddr_in *addr)
327{
328 struct sockaddr_in saddr, daddr;
329
330 saddr = *addr;
331 if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr)
332 {
333 saddr.sin_addr.s_addr = so->so_faddr.s_addr;
334 if ((so->so_faddr.s_addr & htonl(~pData->netmask)) == htonl(~pData->netmask))
335 saddr.sin_addr.s_addr = alias_addr.s_addr;
336 }
337
338 /* Any UDP packet to the loopback address must be translated to be from
339 * the forwarding address, i.e. 10.0.2.2. */
340 if ( (saddr.sin_addr.s_addr & htonl(IN_CLASSA_NET))
341 == htonl(INADDR_LOOPBACK & IN_CLASSA_NET))
342 saddr.sin_addr.s_addr = alias_addr.s_addr;
343
344 daddr.sin_addr = so->so_laddr;
345 daddr.sin_port = so->so_lport;
346
347 return udp_output2(pData, so, m, &saddr, &daddr, so->so_iptos);
348}
349
350int
351udp_attach(PNATState pData, struct socket *so)
352{
353 struct sockaddr_in addr;
354 struct sockaddr sa_addr;
355 socklen_t socklen = sizeof(struct sockaddr);
356 int status;
357
358 if ((so->s = socket(AF_INET,SOCK_DGRAM,0)) != -1)
359 {
360 /*
361 * Here, we bind() the socket. Although not really needed
362 * (sendto() on an unbound socket will bind it), it's done
363 * here so that emulation of ytalk etc. don't have to do it
364 */
365 addr.sin_family = AF_INET;
366 addr.sin_port = 0;
367 addr.sin_addr.s_addr = INADDR_ANY;
368 if (bind(so->s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
369 {
370 int lasterrno = errno;
371 closesocket(so->s);
372 so->s = -1;
373#ifdef RT_OS_WINDOWS
374 WSASetLastError(lasterrno);
375#else
376 errno = lasterrno;
377#endif
378 }
379 else
380 {
381 int opt = 1;
382 /* success, insert in queue */
383 so->so_expire = curtime + SO_EXPIRE;
384 /* enable broadcast for later use */
385 setsockopt(so->s, SOL_SOCKET, SO_BROADCAST, (const char *)&opt, sizeof(opt));
386 status = getsockname(so->s, &sa_addr, &socklen);
387 Assert(status == 0 && sa_addr.sa_family == AF_INET);
388 so->so_hlport = ((struct sockaddr_in *)&sa_addr)->sin_port;
389 so->so_hladdr.s_addr = ((struct sockaddr_in *)&sa_addr)->sin_addr.s_addr;
390 SOCKET_LOCK_CREATE(so);
391 QSOCKET_LOCK(udb);
392 insque(pData, so,&udb);
393 NSOCK_INC();
394 QSOCKET_UNLOCK(udb);
395 }
396 }
397 return so->s;
398}
399
400void
401udp_detach(PNATState pData, struct socket *so)
402{
403 if (so != &pData->icmp_socket)
404 {
405 QSOCKET_LOCK(udb);
406 SOCKET_LOCK(so);
407 QSOCKET_UNLOCK(udb);
408 closesocket(so->s);
409 sofree(pData, so);
410 SOCKET_UNLOCK(so);
411 }
412}
413
414static const struct tos_t udptos[] =
415{
416 { 0, 53, IPTOS_LOWDELAY, 0 }, /* DNS */
417 { 517, 517, IPTOS_LOWDELAY, EMU_TALK }, /* talk */
418 { 518, 518, IPTOS_LOWDELAY, EMU_NTALK }, /* ntalk */
419 { 0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME }, /* Cu-Seeme */
420 { 0, 0, 0, 0 }
421};
422
423u_int8_t
424udp_tos(struct socket *so)
425{
426 int i = 0;
427
428 while(udptos[i].tos)
429 {
430 if ( (udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport)
431 || (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport))
432 {
433 so->so_emu = udptos[i].emu;
434 return udptos[i].tos;
435 }
436 i++;
437 }
438
439 return 0;
440}
441
442#ifdef EMULATE_TALK
443#include "talkd.h"
444#endif
445
446/*
447 * Here, talk/ytalk/ntalk requests must be emulated
448 */
449void
450udp_emu(PNATState pData, struct socket *so, struct mbuf *m)
451{
452 struct sockaddr_in addr;
453 socklen_t addrlen = sizeof(addr);
454#ifdef EMULATE_TALK
455 CTL_MSG_OLD *omsg;
456 CTL_MSG *nmsg;
457 char buff[sizeof(CTL_MSG)];
458 u_char type;
459
460 struct talk_request
461 {
462 struct talk_request *next;
463 struct socket *udp_so;
464 struct socket *tcp_so;
465 } *req;
466
467 static struct talk_request *req_tbl = 0;
468
469#endif
470
471 struct cu_header
472 {
473 uint16_t d_family; /* destination family */
474 uint16_t d_port; /* destination port */
475 uint32_t d_addr; /* destination address */
476 uint16_t s_family; /* source family */
477 uint16_t s_port; /* source port */
478 uint32_t so_addr; /* source address */
479 uint32_t seqn; /* sequence number */
480 uint16_t message; /* message */
481 uint16_t data_type; /* data type */
482 uint16_t pkt_len; /* packet length */
483 } *cu_head;
484
485 switch(so->so_emu)
486 {
487#ifdef EMULATE_TALK
488 case EMU_TALK:
489 case EMU_NTALK:
490 /*
491 * Talk emulation. We always change the ctl_addr to get
492 * some answers from the daemon. When an ANNOUNCE comes,
493 * we send LEAVE_INVITE to the local daemons. Also when a
494 * DELETE comes, we send copies to the local daemons.
495 */
496 if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
497 return;
498
499#define IS_OLD (so->so_emu == EMU_TALK)
500
501#define COPY_MSG(dest, src) \
502 do { \
503 dest->type = src->type; \
504 dest->id_num = src->id_num; \
505 dest->pid = src->pid; \
506 dest->addr = src->addr; \
507 dest->ctl_addr = src->ctl_addr; \
508 memcpy(&dest->l_name, &src->l_name, NAME_SIZE_OLD); \
509 memcpy(&dest->r_name, &src->r_name, NAME_SIZE_OLD); \
510 memcpy(&dest->r_tty, &src->r_tty, TTY_SIZE);
511 } while (0)
512
513#define OTOSIN(ptr, field) ((struct sockaddr_in *)&ptr->field)
514/* old_sockaddr to sockaddr_in */
515
516
517 if (IS_OLD)
518 {
519 /* old talk */
520 omsg = mtod(m, CTL_MSG_OLD*);
521 nmsg = (CTL_MSG *) buff;
522 type = omsg->type;
523 OTOSIN(omsg, ctl_addr)->sin_port = addr.sin_port;
524 OTOSIN(omsg, ctl_addr)->sin_addr = our_addr;
525 strncpy(omsg->l_name, getlogin(), NAME_SIZE_OLD);
526 }
527 else
528 {
529 /* new talk */
530 omsg = (CTL_MSG_OLD *) buff;
531 nmsg = mtod(m, CTL_MSG *);
532 type = nmsg->type;
533 OTOSIN(nmsg, ctl_addr)->sin_port = addr.sin_port;
534 OTOSIN(nmsg, ctl_addr)->sin_addr = our_addr;
535 strncpy(nmsg->l_name, getlogin(), NAME_SIZE_OLD);
536 }
537
538 if (type == LOOK_UP)
539 return; /* for LOOK_UP this is enough */
540
541 if (IS_OLD)
542 {
543 /* make a copy of the message */
544 COPY_MSG(nmsg, omsg);
545 nmsg->vers = 1;
546 nmsg->answer = 0;
547 }
548 else
549 COPY_MSG(omsg, nmsg);
550
551 /*
552 * If if is an ANNOUNCE message, we go through the
553 * request table to see if a tcp port has already
554 * been redirected for this socket. If not, we solisten()
555 * a new socket and add this entry to the table.
556 * The port number of the tcp socket and our IP
557 * are put to the addr field of the message structures.
558 * Then a LEAVE_INVITE is sent to both local daemon
559 * ports, 517 and 518. This is why we have two copies
560 * of the message, one in old talk and one in new talk
561 * format.
562 */
563
564 if (type == ANNOUNCE)
565 {
566 int s;
567 u_short temp_port;
568
569 for(req = req_tbl; req; req = req->next)
570 if (so == req->udp_so)
571 break; /* found it */
572
573 if (!req)
574 {
575 /* no entry for so, create new */
576 req = (struct talk_request *)RTMemAlloc(sizeof(struct talk_request));
577 req->udp_so = so;
578 req->tcp_so = solisten(0,
579 OTOSIN(omsg, addr)->sin_addr.s_addr,
580 OTOSIN(omsg, addr)->sin_port,
581 SS_FACCEPTONCE);
582 req->next = req_tbl;
583 req_tbl = req;
584 }
585
586 /* replace port number in addr field */
587 addrlen = sizeof(addr);
588 getsockname(req->tcp_so->s, (struct sockaddr *) &addr, &addrlen);
589 OTOSIN(omsg, addr)->sin_port = addr.sin_port;
590 OTOSIN(omsg, addr)->sin_addr = our_addr;
591 OTOSIN(nmsg, addr)->sin_port = addr.sin_port;
592 OTOSIN(nmsg, addr)->sin_addr = our_addr;
593
594 /* send LEAVE_INVITEs */
595 temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
596 OTOSIN(omsg, ctl_addr)->sin_port = 0;
597 OTOSIN(nmsg, ctl_addr)->sin_port = 0;
598 omsg->type = nmsg->type = LEAVE_INVITE;
599
600 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
601 addr.sin_addr = our_addr;
602 addr.sin_family = AF_INET;
603 addr.sin_port = htons(517);
604 sendto(s, (char *)omsg, sizeof(*omsg), 0,
605 (struct sockaddr *)&addr, sizeof(addr));
606 addr.sin_port = htons(518);
607 sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
608 (struct sockaddr *) &addr, sizeof(addr));
609 closesocket(s) ;
610
611 omsg->type = nmsg->type = ANNOUNCE;
612 OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
613 OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
614 }
615
616 /*
617 * If it is a DELETE message, we send a copy to the
618 * local daemons. Then we delete the entry corresponding
619 * to our socket from the request table.
620 */
621
622 if (type == DELETE)
623 {
624 struct talk_request *temp_req, *req_next;
625 int s;
626 u_short temp_port;
627
628 temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
629 OTOSIN(omsg, ctl_addr)->sin_port = 0;
630 OTOSIN(nmsg, ctl_addr)->sin_port = 0;
631
632 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
633 addr.sin_addr = our_addr;
634 addr.sin_family = AF_INET;
635 addr.sin_port = htons(517);
636 sendto(s, (char *)omsg, sizeof(*omsg), 0,
637 (struct sockaddr *)&addr, sizeof(addr));
638 addr.sin_port = htons(518);
639 sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
640 (struct sockaddr *)&addr, sizeof(addr));
641 closesocket(s);
642
643 OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
644 OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
645
646 /* delete table entry */
647 if (so == req_tbl->udp_so)
648 {
649 temp_req = req_tbl;
650 req_tbl = req_tbl->next;
651 RTMemFree(temp_req);
652 }
653 else
654 {
655 temp_req = req_tbl;
656 for (req = req_tbl->next; req; req = req_next)
657 {
658 req_next = req->next;
659 if (so == req->udp_so)
660 {
661 temp_req->next = req_next;
662 RTMemFree(req);
663 break;
664 }
665 else
666 temp_req = req;
667 }
668 }
669 }
670
671 return;
672#endif
673
674 case EMU_CUSEEME:
675 /*
676 * Cu-SeeMe emulation.
677 * Hopefully the packet is more that 16 bytes long. We don't
678 * do any other tests, just replace the address and port
679 * fields.
680 */
681 if (m->m_len >= sizeof (*cu_head))
682 {
683 if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
684 return;
685 cu_head = mtod(m, struct cu_header *);
686 cu_head->s_port = addr.sin_port;
687 cu_head->so_addr = our_addr.s_addr;
688 }
689 return;
690 }
691}
692
693struct socket *
694udp_listen(PNATState pData, u_int port, u_int32_t laddr, u_int lport, int flags)
695{
696 struct sockaddr_in addr;
697 struct socket *so;
698 socklen_t addrlen = sizeof(struct sockaddr_in);
699 int opt = 1;
700
701 if ((so = socreate()) == NULL)
702 return NULL;
703
704 so->s = socket(AF_INET,SOCK_DGRAM,0);
705 so->so_expire = curtime + SO_EXPIRE;
706 SOCKET_LOCK_CREATE(so);
707 QSOCKET_LOCK(udb);
708 insque(pData, so,&udb);
709 NSOCK_INC();
710 QSOCKET_UNLOCK(udb);
711
712 addr.sin_family = AF_INET;
713 addr.sin_addr.s_addr = INADDR_ANY;
714 addr.sin_port = port;
715
716 if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0)
717 {
718 udp_detach(pData, so);
719 return NULL;
720 }
721 setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
722/* setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int)); */
723
724 getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
725 so->so_fport = addr.sin_port;
726 /* The original check was completely broken, as the commented out
727 * if statement was always true (INADDR_ANY=0). */
728 /* if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr) */
729 if (1 == 0) /* always use the else part */
730 so->so_faddr = alias_addr;
731 else
732 so->so_faddr = addr.sin_addr;
733
734 so->so_lport = lport;
735 so->so_laddr.s_addr = laddr;
736 if (flags != SS_FACCEPTONCE)
737 so->so_expire = 0;
738
739 so->so_state = SS_ISFCONNECTED;
740
741 return so;
742}
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