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