Statistics
| Branch: | Revision:

root / slirp / socket.c @ 66029f6a

History | View | Annotate | Download (16.8 kB)

1
/*
2
 * Copyright (c) 1995 Danny Gasparovski.
3
 *
4
 * Please read the file COPYRIGHT for the
5
 * terms and conditions of the copyright.
6
 */
7

    
8
#define WANT_SYS_IOCTL_H
9
#include <slirp.h>
10
#include "ip_icmp.h"
11
#ifdef __sun__
12
#include <sys/filio.h>
13
#endif
14

    
15
static void sofcantrcvmore(struct socket *so);
16
static void sofcantsendmore(struct socket *so);
17

    
18
#if 0
19
static void
20
so_init()
21
{
22
        /* Nothing yet */
23
}
24
#endif
25

    
26
struct socket *
27
solookup(head, laddr, lport, faddr, fport)
28
        struct socket *head;
29
        struct in_addr laddr;
30
        u_int lport;
31
        struct in_addr faddr;
32
        u_int fport;
33
{
34
        struct socket *so;
35

    
36
        for (so = head->so_next; so != head; so = so->so_next) {
37
                if (so->so_lport == lport &&
38
                    so->so_laddr.s_addr == laddr.s_addr &&
39
                    so->so_faddr.s_addr == faddr.s_addr &&
40
                    so->so_fport == fport)
41
                   break;
42
        }
43

    
44
        if (so == head)
45
           return (struct socket *)NULL;
46
        return so;
47

    
48
}
49

    
50
/*
51
 * Create a new socket, initialise the fields
52
 * It is the responsibility of the caller to
53
 * insque() it into the correct linked-list
54
 */
55
struct socket *
56
socreate()
57
{
58
  struct socket *so;
59

    
60
  so = (struct socket *)malloc(sizeof(struct socket));
61
  if(so) {
62
    memset(so, 0, sizeof(struct socket));
63
    so->so_state = SS_NOFDREF;
64
    so->s = -1;
65
  }
66
  return(so);
67
}
68

    
69
/*
70
 * remque and free a socket, clobber cache
71
 */
72
void
73
sofree(so)
74
        struct socket *so;
75
{
76
  if (so->so_emu==EMU_RSH && so->extra) {
77
        sofree(so->extra);
78
        so->extra=NULL;
79
  }
80
  if (so == tcp_last_so)
81
    tcp_last_so = &tcb;
82
  else if (so == udp_last_so)
83
    udp_last_so = &udb;
84

    
85
  m_free(so->so_m);
86

    
87
  if(so->so_next && so->so_prev)
88
    remque(so);  /* crashes if so is not in a queue */
89

    
90
  free(so);
91
}
92

    
93
/*
94
 * Read from so's socket into sb_snd, updating all relevant sbuf fields
95
 * NOTE: This will only be called if it is select()ed for reading, so
96
 * a read() of 0 (or less) means it's disconnected
97
 */
98
int
99
soread(so)
100
        struct socket *so;
101
{
102
        int n, nn, lss, total;
103
        struct sbuf *sb = &so->so_snd;
104
        int len = sb->sb_datalen - sb->sb_cc;
105
        struct iovec iov[2];
106
        int mss = so->so_tcpcb->t_maxseg;
107

    
108
        DEBUG_CALL("soread");
109
        DEBUG_ARG("so = %lx", (long )so);
110

    
111
        /*
112
         * No need to check if there's enough room to read.
113
         * soread wouldn't have been called if there weren't
114
         */
115

    
116
        len = sb->sb_datalen - sb->sb_cc;
117

    
118
        iov[0].iov_base = sb->sb_wptr;
119
        iov[1].iov_base = NULL;
120
        iov[1].iov_len = 0;
121
        if (sb->sb_wptr < sb->sb_rptr) {
122
                iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
123
                /* Should never succeed, but... */
124
                if (iov[0].iov_len > len)
125
                   iov[0].iov_len = len;
126
                if (iov[0].iov_len > mss)
127
                   iov[0].iov_len -= iov[0].iov_len%mss;
128
                n = 1;
129
        } else {
130
                iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
131
                /* Should never succeed, but... */
132
                if (iov[0].iov_len > len) iov[0].iov_len = len;
133
                len -= iov[0].iov_len;
134
                if (len) {
135
                        iov[1].iov_base = sb->sb_data;
136
                        iov[1].iov_len = sb->sb_rptr - sb->sb_data;
137
                        if(iov[1].iov_len > len)
138
                           iov[1].iov_len = len;
139
                        total = iov[0].iov_len + iov[1].iov_len;
140
                        if (total > mss) {
141
                                lss = total%mss;
142
                                if (iov[1].iov_len > lss) {
143
                                        iov[1].iov_len -= lss;
144
                                        n = 2;
145
                                } else {
146
                                        lss -= iov[1].iov_len;
147
                                        iov[0].iov_len -= lss;
148
                                        n = 1;
149
                                }
150
                        } else
151
                                n = 2;
152
                } else {
153
                        if (iov[0].iov_len > mss)
154
                           iov[0].iov_len -= iov[0].iov_len%mss;
155
                        n = 1;
156
                }
157
        }
158

    
159
#ifdef HAVE_READV
160
        nn = readv(so->s, (struct iovec *)iov, n);
161
        DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
162
#else
163
        nn = recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
164
#endif
165
        if (nn <= 0) {
166
                if (nn < 0 && (errno == EINTR || errno == EAGAIN))
167
                        return 0;
168
                else {
169
                        DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
170
                        sofcantrcvmore(so);
171
                        tcp_sockclosed(sototcpcb(so));
172
                        return -1;
173
                }
174
        }
175

    
176
#ifndef HAVE_READV
177
        /*
178
         * If there was no error, try and read the second time round
179
         * We read again if n = 2 (ie, there's another part of the buffer)
180
         * and we read as much as we could in the first read
181
         * We don't test for <= 0 this time, because there legitimately
182
         * might not be any more data (since the socket is non-blocking),
183
         * a close will be detected on next iteration.
184
         * A return of -1 wont (shouldn't) happen, since it didn't happen above
185
         */
186
        if (n == 2 && nn == iov[0].iov_len) {
187
            int ret;
188
            ret = recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
189
            if (ret > 0)
190
                nn += ret;
191
        }
192

    
193
        DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
194
#endif
195

    
196
        /* Update fields */
197
        sb->sb_cc += nn;
198
        sb->sb_wptr += nn;
199
        if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
200
                sb->sb_wptr -= sb->sb_datalen;
201
        return nn;
202
}
203

    
204
/*
205
 * Get urgent data
206
 *
207
 * When the socket is created, we set it SO_OOBINLINE,
208
 * so when OOB data arrives, we soread() it and everything
209
 * in the send buffer is sent as urgent data
210
 */
211
void
212
sorecvoob(so)
213
        struct socket *so;
214
{
215
        struct tcpcb *tp = sototcpcb(so);
216

    
217
        DEBUG_CALL("sorecvoob");
218
        DEBUG_ARG("so = %lx", (long)so);
219

    
220
        /*
221
         * We take a guess at how much urgent data has arrived.
222
         * In most situations, when urgent data arrives, the next
223
         * read() should get all the urgent data.  This guess will
224
         * be wrong however if more data arrives just after the
225
         * urgent data, or the read() doesn't return all the
226
         * urgent data.
227
         */
228
        soread(so);
229
        tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
230
        tp->t_force = 1;
231
        tcp_output(tp);
232
        tp->t_force = 0;
233
}
234

    
235
/*
236
 * Send urgent data
237
 * There's a lot duplicated code here, but...
238
 */
239
int
240
sosendoob(so)
241
        struct socket *so;
242
{
243
        struct sbuf *sb = &so->so_rcv;
244
        char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
245

    
246
        int n, len;
247

    
248
        DEBUG_CALL("sosendoob");
249
        DEBUG_ARG("so = %lx", (long)so);
250
        DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
251

    
252
        if (so->so_urgc > 2048)
253
           so->so_urgc = 2048; /* XXXX */
254

    
255
        if (sb->sb_rptr < sb->sb_wptr) {
256
                /* We can send it directly */
257
                n = send(so->s, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
258
                so->so_urgc -= n;
259

    
260
                DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
261
        } else {
262
                /*
263
                 * Since there's no sendv or sendtov like writev,
264
                 * we must copy all data to a linear buffer then
265
                 * send it all
266
                 */
267
                len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
268
                if (len > so->so_urgc) len = so->so_urgc;
269
                memcpy(buff, sb->sb_rptr, len);
270
                so->so_urgc -= len;
271
                if (so->so_urgc) {
272
                        n = sb->sb_wptr - sb->sb_data;
273
                        if (n > so->so_urgc) n = so->so_urgc;
274
                        memcpy((buff + len), sb->sb_data, n);
275
                        so->so_urgc -= n;
276
                        len += n;
277
                }
278
                n = send(so->s, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
279
#ifdef DEBUG
280
                if (n != len)
281
                   DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
282
#endif
283
                DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
284
        }
285

    
286
        sb->sb_cc -= n;
287
        sb->sb_rptr += n;
288
        if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
289
                sb->sb_rptr -= sb->sb_datalen;
290

    
291
        return n;
292
}
293

    
294
/*
295
 * Write data from so_rcv to so's socket,
296
 * updating all sbuf field as necessary
297
 */
298
int
299
sowrite(so)
300
        struct socket *so;
301
{
302
        int  n,nn;
303
        struct sbuf *sb = &so->so_rcv;
304
        int len = sb->sb_cc;
305
        struct iovec iov[2];
306

    
307
        DEBUG_CALL("sowrite");
308
        DEBUG_ARG("so = %lx", (long)so);
309

    
310
        if (so->so_urgc) {
311
                sosendoob(so);
312
                if (sb->sb_cc == 0)
313
                        return 0;
314
        }
315

    
316
        /*
317
         * No need to check if there's something to write,
318
         * sowrite wouldn't have been called otherwise
319
         */
320

    
321
        len = sb->sb_cc;
322

    
323
        iov[0].iov_base = sb->sb_rptr;
324
        iov[1].iov_base = NULL;
325
        iov[1].iov_len = 0;
326
        if (sb->sb_rptr < sb->sb_wptr) {
327
                iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
328
                /* Should never succeed, but... */
329
                if (iov[0].iov_len > len) iov[0].iov_len = len;
330
                n = 1;
331
        } else {
332
                iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
333
                if (iov[0].iov_len > len) iov[0].iov_len = len;
334
                len -= iov[0].iov_len;
335
                if (len) {
336
                        iov[1].iov_base = sb->sb_data;
337
                        iov[1].iov_len = sb->sb_wptr - sb->sb_data;
338
                        if (iov[1].iov_len > len) iov[1].iov_len = len;
339
                        n = 2;
340
                } else
341
                        n = 1;
342
        }
343
        /* Check if there's urgent data to send, and if so, send it */
344

    
345
#ifdef HAVE_READV
346
        nn = writev(so->s, (const struct iovec *)iov, n);
347

    
348
        DEBUG_MISC((dfd, "  ... wrote nn = %d bytes\n", nn));
349
#else
350
        nn = send(so->s, iov[0].iov_base, iov[0].iov_len,0);
351
#endif
352
        /* This should never happen, but people tell me it does *shrug* */
353
        if (nn < 0 && (errno == EAGAIN || errno == EINTR))
354
                return 0;
355

    
356
        if (nn <= 0) {
357
                DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
358
                        so->so_state, errno));
359
                sofcantsendmore(so);
360
                tcp_sockclosed(sototcpcb(so));
361
                return -1;
362
        }
363

    
364
#ifndef HAVE_READV
365
        if (n == 2 && nn == iov[0].iov_len) {
366
            int ret;
367
            ret = send(so->s, iov[1].iov_base, iov[1].iov_len,0);
368
            if (ret > 0)
369
                nn += ret;
370
        }
371
        DEBUG_MISC((dfd, "  ... wrote nn = %d bytes\n", nn));
372
#endif
373

    
374
        /* Update sbuf */
375
        sb->sb_cc -= nn;
376
        sb->sb_rptr += nn;
377
        if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
378
                sb->sb_rptr -= sb->sb_datalen;
379

    
380
        /*
381
         * If in DRAIN mode, and there's no more data, set
382
         * it CANTSENDMORE
383
         */
384
        if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
385
                sofcantsendmore(so);
386

    
387
        return nn;
388
}
389

    
390
/*
391
 * recvfrom() a UDP socket
392
 */
393
void
394
sorecvfrom(so)
395
        struct socket *so;
396
{
397
        struct sockaddr_in addr;
398
        socklen_t addrlen = sizeof(struct sockaddr_in);
399

    
400
        DEBUG_CALL("sorecvfrom");
401
        DEBUG_ARG("so = %lx", (long)so);
402

    
403
        if (so->so_type == IPPROTO_ICMP) {   /* This is a "ping" reply */
404
          char buff[256];
405
          int len;
406

    
407
          len = recvfrom(so->s, buff, 256, 0,
408
                         (struct sockaddr *)&addr, &addrlen);
409
          /* XXX Check if reply is "correct"? */
410

    
411
          if(len == -1 || len == 0) {
412
            u_char code=ICMP_UNREACH_PORT;
413

    
414
            if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
415
            else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
416

    
417
            DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
418
                        errno,strerror(errno)));
419
            icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
420
          } else {
421
            icmp_reflect(so->so_m);
422
            so->so_m = 0; /* Don't m_free() it again! */
423
          }
424
          /* No need for this socket anymore, udp_detach it */
425
          udp_detach(so);
426
        } else {                                    /* A "normal" UDP packet */
427
          struct mbuf *m;
428
          int len, n;
429

    
430
          if (!(m = m_get())) return;
431
          m->m_data += IF_MAXLINKHDR;
432

    
433
          /*
434
           * XXX Shouldn't FIONREAD packets destined for port 53,
435
           * but I don't know the max packet size for DNS lookups
436
           */
437
          len = M_FREEROOM(m);
438
          /* if (so->so_fport != htons(53)) { */
439
          ioctlsocket(so->s, FIONREAD, &n);
440

    
441
          if (n > len) {
442
            n = (m->m_data - m->m_dat) + m->m_len + n + 1;
443
            m_inc(m, n);
444
            len = M_FREEROOM(m);
445
          }
446
          /* } */
447

    
448
          m->m_len = recvfrom(so->s, m->m_data, len, 0,
449
                              (struct sockaddr *)&addr, &addrlen);
450
          DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
451
                      m->m_len, errno,strerror(errno)));
452
          if(m->m_len<0) {
453
            u_char code=ICMP_UNREACH_PORT;
454

    
455
            if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
456
            else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
457

    
458
            DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
459
            icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
460
            m_free(m);
461
          } else {
462
          /*
463
           * Hack: domain name lookup will be used the most for UDP,
464
           * and since they'll only be used once there's no need
465
           * for the 4 minute (or whatever) timeout... So we time them
466
           * out much quicker (10 seconds  for now...)
467
           */
468
            if (so->so_expire) {
469
              if (so->so_fport == htons(53))
470
                so->so_expire = curtime + SO_EXPIREFAST;
471
              else
472
                so->so_expire = curtime + SO_EXPIRE;
473
            }
474

    
475
            /*                if (m->m_len == len) {
476
             *                        m_inc(m, MINCSIZE);
477
             *                        m->m_len = 0;
478
             *                }
479
             */
480

    
481
            /*
482
             * If this packet was destined for CTL_ADDR,
483
             * make it look like that's where it came from, done by udp_output
484
             */
485
            udp_output(so, m, &addr);
486
          } /* rx error */
487
        } /* if ping packet */
488
}
489

    
490
/*
491
 * sendto() a socket
492
 */
493
int
494
sosendto(so, m)
495
        struct socket *so;
496
        struct mbuf *m;
497
{
498
        int ret;
499
        struct sockaddr_in addr;
500

    
501
        DEBUG_CALL("sosendto");
502
        DEBUG_ARG("so = %lx", (long)so);
503
        DEBUG_ARG("m = %lx", (long)m);
504

    
505
        addr.sin_family = AF_INET;
506
        if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
507
          /* It's an alias */
508
          switch(ntohl(so->so_faddr.s_addr) & 0xff) {
509
          case CTL_DNS:
510
            addr.sin_addr = dns_addr;
511
            break;
512
          case CTL_ALIAS:
513
          default:
514
            addr.sin_addr = loopback_addr;
515
            break;
516
          }
517
        } else
518
          addr.sin_addr = so->so_faddr;
519
        addr.sin_port = so->so_fport;
520

    
521
        DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n", ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
522

    
523
        /* Don't care what port we get */
524
        ret = sendto(so->s, m->m_data, m->m_len, 0,
525
                     (struct sockaddr *)&addr, sizeof (struct sockaddr));
526
        if (ret < 0)
527
                return -1;
528

    
529
        /*
530
         * Kill the socket if there's no reply in 4 minutes,
531
         * but only if it's an expirable socket
532
         */
533
        if (so->so_expire)
534
                so->so_expire = curtime + SO_EXPIRE;
535
        so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
536
        return 0;
537
}
538

    
539
/*
540
 * XXX This should really be tcp_listen
541
 */
542
struct socket *
543
solisten(port, laddr, lport, flags)
544
        u_int port;
545
        u_int32_t laddr;
546
        u_int lport;
547
        int flags;
548
{
549
        struct sockaddr_in addr;
550
        struct socket *so;
551
        int s, opt = 1;
552
        socklen_t addrlen = sizeof(addr);
553

    
554
        DEBUG_CALL("solisten");
555
        DEBUG_ARG("port = %d", port);
556
        DEBUG_ARG("laddr = %x", laddr);
557
        DEBUG_ARG("lport = %d", lport);
558
        DEBUG_ARG("flags = %x", flags);
559

    
560
        if ((so = socreate()) == NULL) {
561
          /* free(so);      Not sofree() ??? free(NULL) == NOP */
562
          return NULL;
563
        }
564

    
565
        /* Don't tcp_attach... we don't need so_snd nor so_rcv */
566
        if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
567
                free(so);
568
                return NULL;
569
        }
570
        insque(so,&tcb);
571

    
572
        /*
573
         * SS_FACCEPTONCE sockets must time out.
574
         */
575
        if (flags & SS_FACCEPTONCE)
576
           so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
577

    
578
        so->so_state = (SS_FACCEPTCONN|flags);
579
        so->so_lport = lport; /* Kept in network format */
580
        so->so_laddr.s_addr = laddr; /* Ditto */
581

    
582
        addr.sin_family = AF_INET;
583
        addr.sin_addr.s_addr = INADDR_ANY;
584
        addr.sin_port = port;
585

    
586
        if (((s = socket(AF_INET,SOCK_STREAM,0)) < 0) ||
587
            (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)) < 0) ||
588
            (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
589
            (listen(s,1) < 0)) {
590
                int tmperrno = errno; /* Don't clobber the real reason we failed */
591

    
592
                close(s);
593
                sofree(so);
594
                /* Restore the real errno */
595
#ifdef _WIN32
596
                WSASetLastError(tmperrno);
597
#else
598
                errno = tmperrno;
599
#endif
600
                return NULL;
601
        }
602
        setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
603

    
604
        getsockname(s,(struct sockaddr *)&addr,&addrlen);
605
        so->so_fport = addr.sin_port;
606
        if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
607
           so->so_faddr = alias_addr;
608
        else
609
           so->so_faddr = addr.sin_addr;
610

    
611
        so->s = s;
612
        return so;
613
}
614

    
615
#if 0
616
/*
617
 * Data is available in so_rcv
618
 * Just write() the data to the socket
619
 * XXX not yet...
620
 */
621
static void
622
sorwakeup(so)
623
        struct socket *so;
624
{
625
/*        sowrite(so); */
626
/*        FD_CLR(so->s,&writefds); */
627
}
628

629
/*
630
 * Data has been freed in so_snd
631
 * We have room for a read() if we want to
632
 * For now, don't read, it'll be done in the main loop
633
 */
634
static void
635
sowwakeup(so)
636
        struct socket *so;
637
{
638
        /* Nothing, yet */
639
}
640
#endif
641

    
642
/*
643
 * Various session state calls
644
 * XXX Should be #define's
645
 * The socket state stuff needs work, these often get call 2 or 3
646
 * times each when only 1 was needed
647
 */
648
void
649
soisfconnecting(so)
650
        register struct socket *so;
651
{
652
        so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
653
                          SS_FCANTSENDMORE|SS_FWDRAIN);
654
        so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
655
}
656

    
657
void
658
soisfconnected(so)
659
        register struct socket *so;
660
{
661
        so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
662
        so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
663
}
664

    
665
static void
666
sofcantrcvmore(struct socket *so)
667
{
668
        if ((so->so_state & SS_NOFDREF) == 0) {
669
                shutdown(so->s,0);
670
                if(global_writefds) {
671
                  FD_CLR(so->s,global_writefds);
672
                }
673
        }
674
        so->so_state &= ~(SS_ISFCONNECTING);
675
        if (so->so_state & SS_FCANTSENDMORE)
676
           so->so_state = SS_NOFDREF; /* Don't select it */ /* XXX close() here as well? */
677
        else
678
           so->so_state |= SS_FCANTRCVMORE;
679
}
680

    
681
static void
682
sofcantsendmore(struct socket *so)
683
{
684
        if ((so->so_state & SS_NOFDREF) == 0) {
685
            shutdown(so->s,1);           /* send FIN to fhost */
686
            if (global_readfds) {
687
                FD_CLR(so->s,global_readfds);
688
            }
689
            if (global_xfds) {
690
                FD_CLR(so->s,global_xfds);
691
            }
692
        }
693
        so->so_state &= ~(SS_ISFCONNECTING);
694
        if (so->so_state & SS_FCANTRCVMORE)
695
           so->so_state = SS_NOFDREF; /* as above */
696
        else
697
           so->so_state |= SS_FCANTSENDMORE;
698
}
699

    
700
void
701
soisfdisconnected(so)
702
        struct socket *so;
703
{
704
/*        so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED); */
705
/*        close(so->s); */
706
/*        so->so_state = SS_ISFDISCONNECTED; */
707
        /*
708
         * XXX Do nothing ... ?
709
         */
710
}
711

    
712
/*
713
 * Set write drain mode
714
 * Set CANTSENDMORE once all data has been write()n
715
 */
716
void
717
sofwdrain(so)
718
        struct socket *so;
719
{
720
        if (so->so_rcv.sb_cc)
721
                so->so_state |= SS_FWDRAIN;
722
        else
723
                sofcantsendmore(so);
724
}
725