VirtualBox

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

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

slirp:icmp: Fix of ttl setting on Darwin

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