Statistics
| Branch: | Revision:

root / slirp / udp.c @ f0cbd3ec

History | View | Annotate | Download (16.4 kB)

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
struct udpstat udpstat;
49

    
50
struct socket udb;
51

    
52
/*
53
 * UDP protocol implementation.
54
 * Per RFC 768, August, 1980.
55
 */
56
#ifndef        COMPAT_42
57
int        udpcksum = 1;
58
#else
59
int        udpcksum = 0;                /* XXX */
60
#endif
61

    
62
struct        socket *udp_last_so = &udb;
63

    
64
void
65
udp_init()
66
{
67
        udb.so_next = udb.so_prev = &udb;
68
}
69
/* m->m_data  points at ip packet header 
70
 * m->m_len   length ip packet 
71
 * ip->ip_len length data (IPDU)
72
 */
73
void
74
udp_input(m, iphlen)
75
        register struct mbuf *m;
76
        int iphlen;
77
{
78
        register struct ip *ip;
79
        register struct udphdr *uh;
80
/*        struct mbuf *opts = 0;*/
81
        int len;
82
        struct ip save_ip; 
83
        struct socket *so;
84
        
85
        DEBUG_CALL("udp_input");
86
        DEBUG_ARG("m = %lx", (long)m);
87
        DEBUG_ARG("iphlen = %d", iphlen);
88
        
89
        udpstat.udps_ipackets++;
90

    
91
        /*
92
         * Strip IP options, if any; should skip this,
93
         * make available to user, and use on returned packets,
94
         * but we don't yet have a way to check the checksum
95
         * with options still present.
96
         */
97
        if(iphlen > sizeof(struct ip)) {
98
                ip_stripoptions(m, (struct mbuf *)0);
99
                iphlen = sizeof(struct ip);
100
        }
101

    
102
        /*
103
         * Get IP and UDP header together in first mbuf.
104
         */
105
        ip = mtod(m, struct ip *);
106
        uh = (struct udphdr *)((caddr_t)ip + iphlen);
107

    
108
        /*
109
         * Make mbuf data length reflect UDP length.
110
         * If not enough data to reflect UDP length, drop.
111
         */
112
        len = ntohs((u_int16_t)uh->uh_ulen);
113

    
114
        if (ip->ip_len != len) {
115
                if (len > ip->ip_len) {
116
                        udpstat.udps_badlen++;
117
                        goto bad;
118
                }
119
                m_adj(m, len - ip->ip_len);
120
                ip->ip_len = len;
121
        }
122
        
123
        /*
124
         * Save a copy of the IP header in case we want restore it
125
         * for sending an ICMP error message in response.
126
         */
127
        save_ip = *ip; 
128
        save_ip.ip_len+= iphlen;         /* tcp_input subtracts this */
129

    
130
        /*
131
         * Checksum extended UDP header and data.
132
         */
133
        if (udpcksum && uh->uh_sum) {
134
          ((struct ipovly *)ip)->ih_next = 0;
135
          ((struct ipovly *)ip)->ih_prev = 0;
136
          ((struct ipovly *)ip)->ih_x1 = 0;
137
          ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
138
          /* keep uh_sum for ICMP reply
139
           * uh->uh_sum = cksum(m, len + sizeof (struct ip)); 
140
           * if (uh->uh_sum) { 
141
           */
142
          if(cksum(m, len + sizeof(struct ip))) {
143
            udpstat.udps_badsum++;
144
            goto bad;
145
          }
146
        }
147

    
148
        /*
149
         *  handle DHCP/BOOTP
150
         */
151
        if (ntohs(uh->uh_dport) == BOOTP_SERVER) {
152
            bootp_input(m);
153
            goto bad;
154
        }
155

    
156
        /*
157
         * Locate pcb for datagram.
158
         */
159
        so = udp_last_so;
160
        if (so->so_lport != uh->uh_sport ||
161
            so->so_laddr.s_addr != ip->ip_src.s_addr) {
162
                struct socket *tmp;
163
                
164
                for (tmp = udb.so_next; tmp != &udb; tmp = tmp->so_next) {
165
                        if (tmp->so_lport == uh->uh_sport &&
166
                            tmp->so_laddr.s_addr == ip->ip_src.s_addr) {
167
                                tmp->so_faddr.s_addr = ip->ip_dst.s_addr;
168
                                tmp->so_fport = uh->uh_dport;
169
                                so = tmp;
170
                                break;
171
                        }
172
                }
173
                if (tmp == &udb) {
174
                  so = NULL;
175
                } else {
176
                  udpstat.udpps_pcbcachemiss++;
177
                  udp_last_so = so;
178
                }
179
        }
180
        
181
        if (so == NULL) {
182
          /*
183
           * If there's no socket for this packet,
184
           * create one
185
           */
186
          if ((so = socreate()) == NULL) goto bad;
187
          if(udp_attach(so) == -1) {
188
            DEBUG_MISC((dfd," udp_attach errno = %d-%s\n", 
189
                        errno,strerror(errno)));
190
            sofree(so);
191
            goto bad;
192
          }
193
          
194
          /*
195
           * Setup fields
196
           */
197
          /* udp_last_so = so; */
198
          so->so_laddr = ip->ip_src;
199
          so->so_lport = uh->uh_sport;
200
          so->so_faddr = ip->ip_dst; /* XXX */
201
          so->so_fport = uh->uh_dport; /* XXX */
202
          
203
          if ((so->so_iptos = udp_tos(so)) == 0)
204
            so->so_iptos = ip->ip_tos;
205
          
206
          /*
207
           * XXXXX Here, check if it's in udpexec_list,
208
           * and if it is, do the fork_exec() etc.
209
           */
210
        }
211

    
212
        iphlen += sizeof(struct udphdr);
213
        m->m_len -= iphlen;
214
        m->m_data += iphlen;
215

    
216
        /*
217
         * Now we sendto() the packet.
218
         */
219
        if (so->so_emu)
220
           udp_emu(so, m);
221

    
222
        if(sosendto(so,m) == -1) {
223
          m->m_len += iphlen;
224
          m->m_data -= iphlen;
225
          *ip=save_ip;
226
          DEBUG_MISC((dfd,"udp tx errno = %d-%s\n",errno,strerror(errno)));
227
          icmp_error(m, ICMP_UNREACH,ICMP_UNREACH_NET, 0,strerror(errno));  
228
        }
229

    
230
        m_free(so->so_m);   /* used for ICMP if error on sorecvfrom */
231

    
232
        /* restore the orig mbuf packet */
233
        m->m_len += iphlen;
234
        m->m_data -= iphlen;
235
        *ip=save_ip;
236
        so->so_m=m;         /* ICMP backup */
237

    
238
        return;
239
bad:
240
        m_freem(m);
241
        /* if (opts) m_freem(opts); */
242
        return;
243
}
244

    
245
int udp_output2(struct socket *so, struct mbuf *m, 
246
                struct sockaddr_in *saddr, struct sockaddr_in *daddr,
247
                int iptos)
248
{
249
        register struct udpiphdr *ui;
250
        int error = 0;
251

    
252
        DEBUG_CALL("udp_output");
253
        DEBUG_ARG("so = %lx", (long)so);
254
        DEBUG_ARG("m = %lx", (long)m);
255
        DEBUG_ARG("saddr = %lx", (long)saddr->sin_addr.s_addr);
256
        DEBUG_ARG("daddr = %lx", (long)daddr->sin_addr.s_addr);
257

    
258
        /*
259
         * Adjust for header
260
         */
261
        m->m_data -= sizeof(struct udpiphdr);
262
        m->m_len += sizeof(struct udpiphdr);
263
        
264
        /*
265
         * Fill in mbuf with extended UDP header
266
         * and addresses and length put into network format.
267
         */
268
        ui = mtod(m, struct udpiphdr *);
269
        ui->ui_next = ui->ui_prev = 0;
270
        ui->ui_x1 = 0;
271
        ui->ui_pr = IPPROTO_UDP;
272
        ui->ui_len = htons(m->m_len - sizeof(struct ip)); /* + sizeof (struct udphdr)); */
273
        /* XXXXX Check for from-one-location sockets, or from-any-location sockets */
274
        ui->ui_src = saddr->sin_addr;
275
        ui->ui_dst = daddr->sin_addr;
276
        ui->ui_sport = saddr->sin_port;
277
        ui->ui_dport = daddr->sin_port;
278
        ui->ui_ulen = ui->ui_len;
279

    
280
        /*
281
         * Stuff checksum and output datagram.
282
         */
283
        ui->ui_sum = 0;
284
        if (udpcksum) {
285
            if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0)
286
                ui->ui_sum = 0xffff;
287
        }
288
        ((struct ip *)ui)->ip_len = m->m_len;
289

    
290
        ((struct ip *)ui)->ip_ttl = ip_defttl;
291
        ((struct ip *)ui)->ip_tos = iptos;
292
        
293
        udpstat.udps_opackets++;
294
        
295
        error = ip_output(so, m);
296
        
297
        return (error);
298
}
299

    
300
int udp_output(struct socket *so, struct mbuf *m, 
301
               struct sockaddr_in *addr)
302

    
303
{
304
    struct sockaddr_in saddr, daddr;
305

    
306
    saddr = *addr;
307
    if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr)
308
        saddr.sin_addr.s_addr = so->so_faddr.s_addr;
309
    daddr.sin_addr = so->so_laddr;
310
    daddr.sin_port = so->so_lport;
311
    
312
    return udp_output2(so, m, &saddr, &daddr, so->so_iptos);
313
}
314

    
315
int
316
udp_attach(so)
317
     struct socket *so;
318
{
319
  struct sockaddr_in addr;
320
        
321
  if((so->s = socket(AF_INET,SOCK_DGRAM,0)) != -1) {
322
    /*
323
     * Here, we bind() the socket.  Although not really needed
324
     * (sendto() on an unbound socket will bind it), it's done
325
     * here so that emulation of ytalk etc. don't have to do it
326
     */
327
    addr.sin_family = AF_INET;
328
    addr.sin_port = 0;
329
    addr.sin_addr.s_addr = INADDR_ANY;
330
    if(bind(so->s, (struct sockaddr *)&addr, sizeof(addr))<0) {
331
      int lasterrno=errno;
332
      close(so->s);
333
      so->s=-1;
334
      errno=lasterrno;
335
    } else {
336
      /* success, insert in queue */
337
      so->so_expire = curtime + SO_EXPIRE;
338
      insque(so,&udb);
339
    }
340
  }
341
  return(so->s);
342
}
343

    
344
void
345
udp_detach(so)
346
        struct socket *so;
347
{
348
        close(so->s);
349
        /* if (so->so_m) m_free(so->so_m);    done by sofree */
350

    
351
        sofree(so);
352
}
353

    
354
struct tos_t udptos[] = {
355
        {0, 53, IPTOS_LOWDELAY, 0},                        /* DNS */
356
        {517, 517, IPTOS_LOWDELAY, EMU_TALK},        /* talk */
357
        {518, 518, IPTOS_LOWDELAY, EMU_NTALK},        /* ntalk */
358
        {0, 7648, IPTOS_LOWDELAY, EMU_CUSEEME},        /* Cu-Seeme */
359
        {0, 0, 0, 0}
360
};
361

    
362
u_int8_t
363
udp_tos(so)
364
        struct socket *so;
365
{
366
        int i = 0;
367
        
368
        while(udptos[i].tos) {
369
                if ((udptos[i].fport && ntohs(so->so_fport) == udptos[i].fport) ||
370
                    (udptos[i].lport && ntohs(so->so_lport) == udptos[i].lport)) {
371
                            so->so_emu = udptos[i].emu;
372
                        return udptos[i].tos;
373
                }
374
                i++;
375
        }
376
        
377
        return 0;
378
}
379

    
380
#ifdef EMULATE_TALK
381
#include "talkd.h"
382
#endif
383

    
384
/*
385
 * Here, talk/ytalk/ntalk requests must be emulated
386
 */
387
void
388
udp_emu(so, m)
389
        struct socket *so;
390
        struct mbuf *m;
391
{
392
        struct sockaddr_in addr;
393
        int addrlen = sizeof(addr);
394
#ifdef EMULATE_TALK
395
        CTL_MSG_OLD *omsg;
396
        CTL_MSG *nmsg;
397
        char buff[sizeof(CTL_MSG)];
398
        u_char type;
399
        
400
struct talk_request {
401
        struct talk_request *next;
402
        struct socket *udp_so;
403
        struct socket *tcp_so;
404
} *req;
405
        
406
        static struct talk_request *req_tbl = 0;        
407
        
408
#endif
409
        
410
struct cu_header {
411
        char         dest[8];
412
        short         family;
413
        u_short        port;
414
        u_long        addr;
415
} *cu_head;
416

    
417
        switch(so->so_emu) {
418

    
419
#ifdef EMULATE_TALK
420
         case EMU_TALK:
421
         case EMU_NTALK:
422
                /*
423
                 * Talk emulation. We always change the ctl_addr to get
424
                 * some answers from the daemon. When an ANNOUNCE comes,
425
                 * we send LEAVE_INVITE to the local daemons. Also when a
426
                 * DELETE comes, we send copies to the local daemons.
427
                 */
428
                if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
429
                        return;
430
                
431
#define        IS_OLD        (so->so_emu == EMU_TALK)
432

    
433
#define COPY_MSG(dest, src) { dest->type = src->type; \
434
                              dest->id_num = src->id_num; \
435
                              dest->pid = src->pid; \
436
                              dest->addr = src->addr; \
437
                              dest->ctl_addr = src->ctl_addr; \
438
                              memcpy(&dest->l_name, &src->l_name, NAME_SIZE_OLD); \
439
                              memcpy(&dest->r_name, &src->r_name, NAME_SIZE_OLD); \
440
                               memcpy(&dest->r_tty, &src->r_tty, TTY_SIZE); }
441

    
442
#define OTOSIN(ptr, field) ((struct sockaddr_in *)&ptr->field)
443
/* old_sockaddr to sockaddr_in */
444

    
445

    
446
                if (IS_OLD) {                  /* old talk */
447
                        omsg = mtod(m, CTL_MSG_OLD*);
448
                        nmsg = (CTL_MSG *) buff;
449
                        type = omsg->type;
450
                        OTOSIN(omsg, ctl_addr)->sin_port = addr.sin_port;
451
                        OTOSIN(omsg, ctl_addr)->sin_addr = our_addr;
452
                        strncpy(omsg->l_name, getlogin(), NAME_SIZE_OLD);
453
                } else {                /* new talk */        
454
                        omsg = (CTL_MSG_OLD *) buff;
455
                        nmsg = mtod(m, CTL_MSG *);
456
                        type = nmsg->type;
457
                        OTOSIN(nmsg, ctl_addr)->sin_port = addr.sin_port;
458
                        OTOSIN(nmsg, ctl_addr)->sin_addr = our_addr;
459
                        strncpy(nmsg->l_name, getlogin(), NAME_SIZE_OLD);
460
                }
461
                
462
                if (type == LOOK_UP) 
463
                        return;                /* for LOOK_UP this is enough */
464
                        
465
                if (IS_OLD) {                /* make a copy of the message */
466
                        COPY_MSG(nmsg, omsg);
467
                        nmsg->vers = 1;
468
                        nmsg->answer = 0;
469
                } else
470
                        COPY_MSG(omsg, nmsg);
471

    
472
                /*
473
                 * If if is an ANNOUNCE message, we go through the
474
                 * request table to see if a tcp port has already
475
                 * been redirected for this socket. If not, we solisten()
476
                 * a new socket and add this entry to the table.
477
                 * The port number of the tcp socket and our IP
478
                 * are put to the addr field of the message structures.
479
                 * Then a LEAVE_INVITE is sent to both local daemon
480
                 * ports, 517 and 518. This is why we have two copies
481
                 * of the message, one in old talk and one in new talk
482
                 * format.
483
                 */ 
484

    
485
                if (type == ANNOUNCE) {
486
                        int s;
487
                        u_short temp_port;
488
                        
489
                        for(req = req_tbl; req; req = req->next)
490
                                if (so == req->udp_so)
491
                                        break;          /* found it */
492
                                        
493
                        if (!req) {        /* no entry for so, create new */
494
                                req = (struct talk_request *)
495
                                        malloc(sizeof(struct talk_request));
496
                                req->udp_so = so;
497
                                req->tcp_so = solisten(0,                
498
                                        OTOSIN(omsg, addr)->sin_addr.s_addr,        
499
                                        OTOSIN(omsg, addr)->sin_port,
500
                                        SS_FACCEPTONCE);
501
                                req->next = req_tbl;
502
                                req_tbl = req;
503
                        }                        
504
                        
505
                        /* replace port number in addr field */
506
                        addrlen = sizeof(addr);
507
                        getsockname(req->tcp_so->s, 
508
                                        (struct sockaddr *) &addr,
509
                                        &addrlen);                
510
                        OTOSIN(omsg, addr)->sin_port = addr.sin_port;
511
                        OTOSIN(omsg, addr)->sin_addr = our_addr;
512
                        OTOSIN(nmsg, addr)->sin_port = addr.sin_port;
513
                        OTOSIN(nmsg, addr)->sin_addr = our_addr;                
514
                        
515
                        /* send LEAVE_INVITEs */
516
                        temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
517
                        OTOSIN(omsg, ctl_addr)->sin_port = 0;
518
                        OTOSIN(nmsg, ctl_addr)->sin_port = 0;
519
                        omsg->type = nmsg->type = LEAVE_INVITE;                        
520
                        
521
                        s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
522
                        addr.sin_addr = our_addr;
523
                        addr.sin_family = AF_INET;
524
                        addr.sin_port = htons(517);
525
                        sendto(s, (char *)omsg, sizeof(*omsg), 0, 
526
                                (struct sockaddr *)&addr, sizeof(addr));
527
                        addr.sin_port = htons(518);
528
                        sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
529
                                (struct sockaddr *) &addr, sizeof(addr));
530
                        close(s) ;
531

    
532
                        omsg->type = nmsg->type = ANNOUNCE; 
533
                        OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
534
                        OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
535
                }
536
                
537
                /*        
538
                 * If it is a DELETE message, we send a copy to the
539
                 * local daemons. Then we delete the entry corresponding
540
                 * to our socket from the request table.
541
                 */
542
                
543
                if (type == DELETE) {
544
                        struct talk_request *temp_req, *req_next;
545
                        int s;
546
                        u_short temp_port;
547
                        
548
                        temp_port = OTOSIN(omsg, ctl_addr)->sin_port;
549
                        OTOSIN(omsg, ctl_addr)->sin_port = 0;
550
                        OTOSIN(nmsg, ctl_addr)->sin_port = 0;
551
                        
552
                        s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
553
                        addr.sin_addr = our_addr;
554
                        addr.sin_family = AF_INET;
555
                        addr.sin_port = htons(517);
556
                        sendto(s, (char *)omsg, sizeof(*omsg), 0,
557
                                (struct sockaddr *)&addr, sizeof(addr));
558
                        addr.sin_port = htons(518);
559
                        sendto(s, (char *)nmsg, sizeof(*nmsg), 0,
560
                                (struct sockaddr *)&addr, sizeof(addr));
561
                        close(s);
562
                        
563
                        OTOSIN(omsg, ctl_addr)->sin_port = temp_port;
564
                        OTOSIN(nmsg, ctl_addr)->sin_port = temp_port;
565

    
566
                        /* delete table entry */
567
                        if (so == req_tbl->udp_so) {
568
                                temp_req = req_tbl;
569
                                req_tbl = req_tbl->next;
570
                                free(temp_req);
571
                        } else {
572
                                temp_req = req_tbl;
573
                                for(req = req_tbl->next; req; req = req_next) {
574
                                        req_next = req->next;
575
                                        if (so == req->udp_so) {
576
                                                temp_req->next = req_next;
577
                                                free(req);
578
                                                break;
579
                                        } else {
580
                                                temp_req = req;
581
                                        }
582
                                }
583
                        }
584
                }
585
                
586
                return;                
587
#endif
588
                
589
        case EMU_CUSEEME:
590
        
591
                /*
592
                 * Cu-SeeMe emulation.
593
                 * Hopefully the packet is more that 16 bytes long. We don't
594
                 * do any other tests, just replace the address and port
595
                 * fields.
596
                 */ 
597
                if (m->m_len >= sizeof (*cu_head)) {
598
                        if (getsockname(so->s, (struct sockaddr *)&addr, &addrlen) < 0)
599
                                return;
600
                        cu_head = mtod(m, struct cu_header *);
601
                        cu_head->port = addr.sin_port;
602
                        cu_head->addr = (u_long) our_addr.s_addr;
603
                }
604
                
605
                return;
606
        }
607
}
608

    
609
struct socket *
610
udp_listen(port, laddr, lport, flags)
611
        u_int port;
612
        u_int32_t laddr;
613
        u_int lport;
614
        int flags;
615
{
616
        struct sockaddr_in addr;
617
        struct socket *so;
618
        int addrlen = sizeof(struct sockaddr_in), opt = 1;
619
        
620
        if ((so = socreate()) == NULL) {
621
                free(so);
622
                return NULL;
623
        }
624
        so->s = socket(AF_INET,SOCK_DGRAM,0);
625
        so->so_expire = curtime + SO_EXPIRE;
626
        insque(so,&udb);
627

    
628
        addr.sin_family = AF_INET;
629
        addr.sin_addr.s_addr = INADDR_ANY;
630
        addr.sin_port = port;
631

    
632
        if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
633
                udp_detach(so);
634
                return NULL;
635
        }
636
        setsockopt(so->s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int));
637
/*        setsockopt(so->s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int)); */
638
        
639
        getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
640
        so->so_fport = addr.sin_port;
641
        if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
642
           so->so_faddr = our_addr;
643
        else
644
           so->so_faddr = addr.sin_addr;
645
        
646
        so->so_lport = lport;
647
        so->so_laddr.s_addr = laddr;
648
        if (flags != SS_FACCEPTONCE)
649
           so->so_expire = 0;
650
        
651
        so->so_state = SS_ISFCONNECTED;
652
        
653
        return so;
654
}