OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [bsd_tcpip/] [v2_0/] [src/] [sys/] [kern/] [uipc_socket2.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      src/sys/kern/uipc_socket2.c
4
//
5
//==========================================================================
6
//####BSDCOPYRIGHTBEGIN####
7
//
8
// -------------------------------------------
9
//
10
// Portions of this software may have been derived from OpenBSD, 
11
// FreeBSD or other sources, and are covered by the appropriate
12
// copyright disclaimers included herein.
13
//
14
// Portions created by Red Hat are
15
// Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16
//
17
// -------------------------------------------
18
//
19
//####BSDCOPYRIGHTEND####
20
//==========================================================================
21
 
22
/*
23
 * Copyright (c) 1982, 1986, 1988, 1990, 1993
24
 *      The Regents of the University of California.  All rights reserved.
25
 *
26
 * Redistribution and use in source and binary forms, with or without
27
 * modification, are permitted provided that the following conditions
28
 * are met:
29
 * 1. Redistributions of source code must retain the above copyright
30
 *    notice, this list of conditions and the following disclaimer.
31
 * 2. Redistributions in binary form must reproduce the above copyright
32
 *    notice, this list of conditions and the following disclaimer in the
33
 *    documentation and/or other materials provided with the distribution.
34
 * 3. All advertising materials mentioning features or use of this software
35
 *    must display the following acknowledgement:
36
 *      This product includes software developed by the University of
37
 *      California, Berkeley and its contributors.
38
 * 4. Neither the name of the University nor the names of its contributors
39
 *    may be used to endorse or promote products derived from this software
40
 *    without specific prior written permission.
41
 *
42
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52
 * SUCH DAMAGE.
53
 *
54
 *      @(#)uipc_socket2.c      8.1 (Berkeley) 6/10/93
55
 * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.9 2001/07/26 18:53:02 peter Exp $
56
 */
57
 
58
#include <sys/param.h>
59
#include <sys/domain.h>
60
#include <sys/malloc.h>
61
#include <sys/mbuf.h>
62
#include <sys/protosw.h>
63
#include <sys/socket.h>
64
#include <sys/socketvar.h>
65
 
66
#include <cyg/io/file.h>
67
 
68
int     maxsockets = CYGPKG_NET_MAXSOCKETS;
69
 
70
/*
71
 * Primitive routines for operating on sockets and socket buffers
72
 */
73
 
74
u_long  sb_max = SB_MAX;                /* XXX should be static */
75
 
76
static  u_long sb_efficiency = 8;       /* parameter for sbreserve() */
77
 
78
/*
79
 * Procedures to manipulate state flags of socket
80
 * and do appropriate wakeups.  Normal sequence from the
81
 * active (originating) side is that soisconnecting() is
82
 * called during processing of connect() call,
83
 * resulting in an eventual call to soisconnected() if/when the
84
 * connection is established.  When the connection is torn down
85
 * soisdisconnecting() is called during processing of disconnect() call,
86
 * and soisdisconnected() is called when the connection to the peer
87
 * is totally severed.  The semantics of these routines are such that
88
 * connectionless protocols can call soisconnected() and soisdisconnected()
89
 * only, bypassing the in-progress calls when setting up a ``connection''
90
 * takes no time.
91
 *
92
 * From the passive side, a socket is created with
93
 * two queues of sockets: so_incomp for connections in progress
94
 * and so_comp for connections already made and awaiting user acceptance.
95
 * As a protocol is preparing incoming connections, it creates a socket
96
 * structure queued on so_incomp by calling sonewconn().  When the connection
97
 * is established, soisconnected() is called, and transfers the
98
 * socket structure to so_comp, making it available to accept().
99
 *
100
 * If a socket is closed with sockets on either
101
 * so_incomp or so_comp, these sockets are dropped.
102
 *
103
 * If higher level protocols are implemented in
104
 * the kernel, the wakeups done here will sometimes
105
 * cause software-interrupt process scheduling.
106
 */
107
 
108
void
109
soisconnecting(so)
110
        register struct socket *so;
111
{
112
 
113
        so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
114
        so->so_state |= SS_ISCONNECTING;
115
}
116
 
117
void
118
soisconnected(so)
119
        struct socket *so;
120
{
121
        struct socket *head = so->so_head;
122
 
123
        so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
124
        so->so_state |= SS_ISCONNECTED;
125
        if (head && (so->so_state & SS_INCOMP)) {
126
                if ((so->so_options & SO_ACCEPTFILTER) != 0) {
127
                        so->so_upcall = head->so_accf->so_accept_filter->accf_callback;
128
                        so->so_upcallarg = head->so_accf->so_accept_filter_arg;
129
                        so->so_rcv.sb_flags |= SB_UPCALL;
130
                        so->so_options &= ~SO_ACCEPTFILTER;
131
                        so->so_upcall(so, so->so_upcallarg, 0);
132
                        return;
133
                }
134
                TAILQ_REMOVE(&head->so_incomp, so, so_list);
135
                head->so_incqlen--;
136
                so->so_state &= ~SS_INCOMP;
137
                TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
138
                so->so_state |= SS_COMP;
139
                sorwakeup(head);
140
                wakeup_one(&head->so_timeo);
141
        } else {
142
                wakeup(&so->so_timeo);
143
                sorwakeup(so);
144
                sowwakeup(so);
145
        }
146
}
147
 
148
void
149
soisdisconnecting(so)
150
        register struct socket *so;
151
{
152
 
153
        so->so_state &= ~SS_ISCONNECTING;
154
        so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
155
        wakeup((caddr_t)&so->so_timeo);
156
        sowwakeup(so);
157
        sorwakeup(so);
158
}
159
 
160
void
161
soisdisconnected(so)
162
        register struct socket *so;
163
{
164
 
165
        so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
166
        so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
167
        wakeup((caddr_t)&so->so_timeo);
168
        sowwakeup(so);
169
        sorwakeup(so);
170
}
171
 
172
/*
173
 * Return a random connection that hasn't been serviced yet and
174
 * is eligible for discard.  There is a one in qlen chance that
175
 * we will return a null, saying that there are no dropable
176
 * requests.  In this case, the protocol specific code should drop
177
 * the new request.  This insures fairness.
178
 *
179
 * This may be used in conjunction with protocol specific queue
180
 * congestion routines.
181
 */
182
struct socket *
183
sodropablereq(head)
184
        register struct socket *head;
185
{
186
        register struct socket *so;
187
        unsigned int i, j, qlen;
188
        static int rnd;
189
        static struct timeval old_runtime;
190
        static unsigned int cur_cnt, old_cnt;
191
        struct timeval tv;
192
 
193
        getmicrouptime(&tv);
194
        if ((i = (tv.tv_sec - old_runtime.tv_sec)) != 0) {
195
                old_runtime = tv;
196
                old_cnt = cur_cnt / i;
197
                cur_cnt = 0;
198
        }
199
 
200
        so = TAILQ_FIRST(&head->so_incomp);
201
        if (!so)
202
                return (so);
203
 
204
        qlen = head->so_incqlen;
205
        if (++cur_cnt > qlen || old_cnt > qlen) {
206
                rnd = (314159 * rnd + 66329) & 0xffff;
207
                j = ((qlen + 1) * rnd) >> 16;
208
 
209
                while (j-- && so)
210
                    so = TAILQ_NEXT(so, so_list);
211
        }
212
 
213
        return (so);
214
}
215
 
216
/*
217
 * When an attempt at a new connection is noted on a socket
218
 * which accepts connections, sonewconn is called.  If the
219
 * connection is possible (subject to space constraints, etc.)
220
 * then we allocate a new structure, propoerly linked into the
221
 * data structure of the original socket, and return this.
222
 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
223
 */
224
struct socket *
225
sonewconn(head, connstatus)
226
        register struct socket *head;
227
        int connstatus;
228
{
229
 
230
        return (sonewconn3(head, connstatus, NULL));
231
}
232
 
233
struct socket *
234
sonewconn3(head, connstatus, p)
235
        register struct socket *head;
236
        int connstatus;
237
        struct proc *p;
238
{
239
        register struct socket *so;
240
 
241
        if (head->so_qlen > 3 * head->so_qlimit / 2)
242
                return ((struct socket *)0);
243
        so = soalloc(0);
244
        if (so == NULL)
245
                return ((struct socket *)0);
246
        so->so_head = head;
247
        so->so_type = head->so_type;
248
        so->so_options = head->so_options &~ SO_ACCEPTCONN;
249
        so->so_linger = head->so_linger;
250
        so->so_state = head->so_state | SS_NOFDREF;
251
        so->so_proto = head->so_proto;
252
        so->so_timeo = head->so_timeo;
253
        if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
254
            (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
255
                sodealloc(so);
256
                return ((struct socket *)0);
257
        }
258
 
259
        if (connstatus) {
260
                TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
261
                so->so_state |= SS_COMP;
262
        } else {
263
                TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
264
                so->so_state |= SS_INCOMP;
265
                head->so_incqlen++;
266
        }
267
        head->so_qlen++;
268
        if (connstatus) {
269
                sorwakeup(head);
270
                wakeup((caddr_t)&head->so_timeo);
271
                so->so_state |= connstatus;
272
        }
273
        return (so);
274
}
275
 
276
/*
277
 * Socantsendmore indicates that no more data will be sent on the
278
 * socket; it would normally be applied to a socket when the user
279
 * informs the system that no more data is to be sent, by the protocol
280
 * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
281
 * will be received, and will normally be applied to the socket by a
282
 * protocol when it detects that the peer will send no more data.
283
 * Data queued for reading in the socket may yet be read.
284
 */
285
 
286
void
287
socantsendmore(so)
288
        struct socket *so;
289
{
290
 
291
        so->so_state |= SS_CANTSENDMORE;
292
        sowwakeup(so);
293
}
294
 
295
void
296
socantrcvmore(so)
297
        struct socket *so;
298
{
299
 
300
        so->so_state |= SS_CANTRCVMORE;
301
        sorwakeup(so);
302
}
303
 
304
/*
305
 * Wait for data to arrive at/drain from a socket buffer.
306
 */
307
int
308
sbwait(sb)
309
        struct sockbuf *sb;
310
{
311
 
312
        sb->sb_flags |= SB_WAIT;
313
        return (tsleep((caddr_t)&sb->sb_cc,
314
            (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
315
            sb->sb_timeo));
316
}
317
 
318
/*
319
 * Lock a sockbuf already known to be locked;
320
 * return any error returned from sleep (EINTR).
321
 */
322
int
323
sb_lock(sb)
324
        register struct sockbuf *sb;
325
{
326
        int error;
327
 
328
        while (sb->sb_flags & SB_LOCK) {
329
                sb->sb_flags |= SB_WANT;
330
                error = tsleep((caddr_t)&sb->sb_flags,
331
                    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
332
                    "sblock", 0);
333
                if (error)
334
                        return (error);
335
        }
336
        sb->sb_flags |= SB_LOCK;
337
        return (0);
338
}
339
 
340
/*
341
 * Wakeup processes waiting on a socket buffer.
342
 * Do asynchronous notification via SIGIO
343
 * if the socket has the SS_ASYNC flag set.
344
 */
345
void
346
sowakeup(so, sb)
347
        register struct socket *so;
348
        register struct sockbuf *sb;
349
{
350
        selwakeup(&sb->sb_sel);
351
        sb->sb_flags &= ~SB_SEL;
352
        if (sb->sb_flags & SB_WAIT) {
353
                sb->sb_flags &= ~SB_WAIT;
354
                wakeup((caddr_t)&sb->sb_cc);
355
        }
356
        if (sb->sb_flags & SB_UPCALL)
357
                (*so->so_upcall)(so, so->so_upcallarg, M_DONTWAIT);
358
}
359
 
360
/*
361
 * Socket buffer (struct sockbuf) utility routines.
362
 *
363
 * Each socket contains two socket buffers: one for sending data and
364
 * one for receiving data.  Each buffer contains a queue of mbufs,
365
 * information about the number of mbufs and amount of data in the
366
 * queue, and other fields allowing select() statements and notification
367
 * on data availability to be implemented.
368
 *
369
 * Data stored in a socket buffer is maintained as a list of records.
370
 * Each record is a list of mbufs chained together with the m_next
371
 * field.  Records are chained together with the m_nextpkt field. The upper
372
 * level routine soreceive() expects the following conventions to be
373
 * observed when placing information in the receive buffer:
374
 *
375
 * 1. If the protocol requires each message be preceded by the sender's
376
 *    name, then a record containing that name must be present before
377
 *    any associated data (mbuf's must be of type MT_SONAME).
378
 * 2. If the protocol supports the exchange of ``access rights'' (really
379
 *    just additional data associated with the message), and there are
380
 *    ``rights'' to be received, then a record containing this data
381
 *    should be present (mbuf's must be of type MT_RIGHTS).
382
 * 3. If a name or rights record exists, then it must be followed by
383
 *    a data record, perhaps of zero length.
384
 *
385
 * Before using a new socket structure it is first necessary to reserve
386
 * buffer space to the socket, by calling sbreserve().  This should commit
387
 * some of the available buffer space in the system buffer pool for the
388
 * socket (currently, it does nothing but enforce limits).  The space
389
 * should be released by calling sbrelease() when the socket is destroyed.
390
 */
391
 
392
int
393
soreserve(so, sndcc, rcvcc)
394
        register struct socket *so;
395
        u_long sndcc, rcvcc;
396
{
397
        struct proc *p = curproc;
398
 
399
        if (sbreserve(&so->so_snd, sndcc, so, p) == 0)
400
                goto bad;
401
        if (sbreserve(&so->so_rcv, rcvcc, so, p) == 0)
402
                goto bad2;
403
        if (so->so_rcv.sb_lowat == 0)
404
                so->so_rcv.sb_lowat = 1;
405
        if (so->so_snd.sb_lowat == 0)
406
                so->so_snd.sb_lowat = MCLBYTES;
407
        if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
408
                so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
409
        return (0);
410
bad2:
411
        sbrelease(&so->so_snd, so);
412
bad:
413
        return (ENOBUFS);
414
}
415
 
416
/*
417
 * Allot mbufs to a sockbuf.
418
 * Attempt to scale mbmax so that mbcnt doesn't become limiting
419
 * if buffering efficiency is near the normal case.
420
 */
421
int
422
sbreserve(sb, cc, so, p)
423
        struct sockbuf *sb;
424
        u_long cc;
425
        struct socket *so;
426
        struct proc *p;
427
{
428
 
429
        /*
430
         * p will only be NULL when we're in an interrupt
431
         * (e.g. in tcp_input())
432
         */
433
        if ((u_quad_t)cc > (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES))
434
                return (0);
435
        sb->sb_hiwat = cc;
436
        sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
437
        if (sb->sb_lowat > sb->sb_hiwat)
438
                sb->sb_lowat = sb->sb_hiwat;
439
        return (1);
440
}
441
 
442
/*
443
 * Free mbufs held by a socket, and reserved mbuf space.
444
 */
445
void
446
sbrelease(sb, so)
447
        struct sockbuf *sb;
448
        struct socket *so;
449
{
450
 
451
        sbflush(sb);
452
        sb->sb_mbmax = 0;
453
}
454
 
455
/*
456
 * Routines to add and remove
457
 * data from an mbuf queue.
458
 *
459
 * The routines sbappend() or sbappendrecord() are normally called to
460
 * append new mbufs to a socket buffer, after checking that adequate
461
 * space is available, comparing the function sbspace() with the amount
462
 * of data to be added.  sbappendrecord() differs from sbappend() in
463
 * that data supplied is treated as the beginning of a new record.
464
 * To place a sender's address, optional access rights, and data in a
465
 * socket receive buffer, sbappendaddr() should be used.  To place
466
 * access rights and data in a socket receive buffer, sbappendrights()
467
 * should be used.  In either case, the new data begins a new record.
468
 * Note that unlike sbappend() and sbappendrecord(), these routines check
469
 * for the caller that there will be enough space to store the data.
470
 * Each fails if there is not enough space, or if it cannot find mbufs
471
 * to store additional information in.
472
 *
473
 * Reliable protocols may use the socket send buffer to hold data
474
 * awaiting acknowledgement.  Data is normally copied from a socket
475
 * send buffer in a protocol with m_copy for output to a peer,
476
 * and then removing the data from the socket buffer with sbdrop()
477
 * or sbdroprecord() when the data is acknowledged by the peer.
478
 */
479
 
480
/*
481
 * Append mbuf chain m to the last record in the
482
 * socket buffer sb.  The additional space associated
483
 * the mbuf chain is recorded in sb.  Empty mbufs are
484
 * discarded and mbufs are compacted where possible.
485
 */
486
void
487
sbappend(sb, m)
488
        struct sockbuf *sb;
489
        struct mbuf *m;
490
{
491
        register struct mbuf *n;
492
 
493
        if (m == 0)
494
                return;
495
        n = sb->sb_mb;
496
        if (n) {
497
                while (n->m_nextpkt)
498
                        n = n->m_nextpkt;
499
                do {
500
                        if (n->m_flags & M_EOR) {
501
                                sbappendrecord(sb, m); /* XXXXXX!!!! */
502
                                return;
503
                        }
504
                } while (n->m_next && (n = n->m_next));
505
        }
506
        sbcompress(sb, m, n);
507
}
508
 
509
#ifdef SOCKBUF_DEBUG
510
void
511
sbcheck(sb)
512
        register struct sockbuf *sb;
513
{
514
        register struct mbuf *m;
515
        register struct mbuf *n = 0;
516
        register u_long len = 0, mbcnt = 0;
517
 
518
        for (m = sb->sb_mb; m; m = n) {
519
            n = m->m_nextpkt;
520
            for (; m; m = m->m_next) {
521
                len += m->m_len;
522
                mbcnt += MSIZE;
523
                if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
524
                        mbcnt += m->m_ext.ext_size;
525
            }
526
        }
527
        if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
528
                printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc,
529
                    mbcnt, sb->sb_mbcnt);
530
                panic("sbcheck");
531
        }
532
}
533
#endif
534
 
535
/*
536
 * As above, except the mbuf chain
537
 * begins a new record.
538
 */
539
void
540
sbappendrecord(sb, m0)
541
        register struct sockbuf *sb;
542
        register struct mbuf *m0;
543
{
544
        register struct mbuf *m;
545
 
546
        if (m0 == 0)
547
                return;
548
        m = sb->sb_mb;
549
        if (m)
550
                while (m->m_nextpkt)
551
                        m = m->m_nextpkt;
552
        /*
553
         * Put the first mbuf on the queue.
554
         * Note this permits zero length records.
555
         */
556
        sballoc(sb, m0);
557
        if (m)
558
                m->m_nextpkt = m0;
559
        else
560
                sb->sb_mb = m0;
561
        m = m0->m_next;
562
        m0->m_next = 0;
563
        if (m && (m0->m_flags & M_EOR)) {
564
                m0->m_flags &= ~M_EOR;
565
                m->m_flags |= M_EOR;
566
        }
567
        sbcompress(sb, m, m0);
568
}
569
 
570
/*
571
 * As above except that OOB data
572
 * is inserted at the beginning of the sockbuf,
573
 * but after any other OOB data.
574
 */
575
void
576
sbinsertoob(sb, m0)
577
        register struct sockbuf *sb;
578
        register struct mbuf *m0;
579
{
580
        register struct mbuf *m;
581
        register struct mbuf **mp;
582
 
583
        if (m0 == 0)
584
                return;
585
        for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
586
            m = *mp;
587
            again:
588
                switch (m->m_type) {
589
 
590
                case MT_OOBDATA:
591
                        continue;               /* WANT next train */
592
 
593
                case MT_CONTROL:
594
                        m = m->m_next;
595
                        if (m)
596
                                goto again;     /* inspect THIS train further */
597
                }
598
                break;
599
        }
600
        /*
601
         * Put the first mbuf on the queue.
602
         * Note this permits zero length records.
603
         */
604
        sballoc(sb, m0);
605
        m0->m_nextpkt = *mp;
606
        *mp = m0;
607
        m = m0->m_next;
608
        m0->m_next = 0;
609
        if (m && (m0->m_flags & M_EOR)) {
610
                m0->m_flags &= ~M_EOR;
611
                m->m_flags |= M_EOR;
612
        }
613
        sbcompress(sb, m, m0);
614
}
615
 
616
/*
617
 * Append address and data, and optionally, control (ancillary) data
618
 * to the receive queue of a socket.  If present,
619
 * m0 must include a packet header with total length.
620
 * Returns 0 if no space in sockbuf or insufficient mbufs.
621
 */
622
int
623
sbappendaddr(sb, asa, m0, control)
624
        register struct sockbuf *sb;
625
        struct sockaddr *asa;
626
        struct mbuf *m0, *control;
627
{
628
        register struct mbuf *m, *n;
629
        int space = asa->sa_len;
630
 
631
if (m0 && (m0->m_flags & M_PKTHDR) == 0)
632
panic("sbappendaddr");
633
        if (m0)
634
                space += m0->m_pkthdr.len;
635
        for (n = control; n; n = n->m_next) {
636
                space += n->m_len;
637
                if (n->m_next == 0)      /* keep pointer to last control buf */
638
                        break;
639
        }
640
        if (space > sbspace(sb))
641
                return (0);
642
        if (asa->sa_len > MLEN)
643
                return (0);
644
        MGET(m, M_DONTWAIT, MT_SONAME);
645
        if (m == 0)
646
                return (0);
647
        m->m_len = asa->sa_len;
648
        bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len);
649
        if (n)
650
                n->m_next = m0;         /* concatenate data to control */
651
        else
652
                control = m0;
653
        m->m_next = control;
654
        for (n = m; n; n = n->m_next)
655
                sballoc(sb, n);
656
        n = sb->sb_mb;
657
        if (n) {
658
                while (n->m_nextpkt)
659
                        n = n->m_nextpkt;
660
                n->m_nextpkt = m;
661
        } else
662
                sb->sb_mb = m;
663
        return (1);
664
}
665
 
666
int
667
sbappendcontrol(sb, m0, control)
668
        struct sockbuf *sb;
669
        struct mbuf *control, *m0;
670
{
671
        register struct mbuf *m, *n;
672
        int space = 0;
673
 
674
        if (control == 0)
675
                panic("sbappendcontrol");
676
        for (m = control; ; m = m->m_next) {
677
                space += m->m_len;
678
                if (m->m_next == 0)
679
                        break;
680
        }
681
        n = m;                  /* save pointer to last control buffer */
682
        for (m = m0; m; m = m->m_next)
683
                space += m->m_len;
684
        if (space > sbspace(sb))
685
                return (0);
686
        n->m_next = m0;                 /* concatenate data to control */
687
        for (m = control; m; m = m->m_next)
688
                sballoc(sb, m);
689
        n = sb->sb_mb;
690
        if (n) {
691
                while (n->m_nextpkt)
692
                        n = n->m_nextpkt;
693
                n->m_nextpkt = control;
694
        } else
695
                sb->sb_mb = control;
696
        return (1);
697
}
698
 
699
/*
700
 * Compress mbuf chain m into the socket
701
 * buffer sb following mbuf n.  If n
702
 * is null, the buffer is presumed empty.
703
 */
704
void
705
sbcompress(sb, m, n)
706
        register struct sockbuf *sb;
707
        register struct mbuf *m, *n;
708
{
709
        register int eor = 0;
710
        register struct mbuf *o;
711
 
712
        while (m) {
713
                eor |= m->m_flags & M_EOR;
714
                if (m->m_len == 0 &&
715
                    (eor == 0 ||
716
                     (((o = m->m_next) || (o = n)) &&
717
                      o->m_type == m->m_type))) {
718
                        m = m_free(m);
719
                        continue;
720
                }
721
                if (n && (n->m_flags & M_EOR) == 0 &&
722
                    M_WRITABLE(n) &&
723
                    m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
724
                    m->m_len <= M_TRAILINGSPACE(n) &&
725
                    n->m_type == m->m_type) {
726
                        bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
727
                            (unsigned)m->m_len);
728
                        n->m_len += m->m_len;
729
                        sb->sb_cc += m->m_len;
730
                        m = m_free(m);
731
                        continue;
732
                }
733
                if (n)
734
                        n->m_next = m;
735
                else
736
                        sb->sb_mb = m;
737
                sballoc(sb, m);
738
                n = m;
739
                m->m_flags &= ~M_EOR;
740
                m = m->m_next;
741
                n->m_next = 0;
742
        }
743
        if (eor) {
744
                if (n)
745
                        n->m_flags |= eor;
746
                else
747
                        printf("semi-panic: sbcompress\n");
748
        }
749
}
750
 
751
/*
752
 * Free all mbufs in a sockbuf.
753
 * Check that all resources are reclaimed.
754
 */
755
void
756
sbflush(sb)
757
        register struct sockbuf *sb;
758
{
759
 
760
        if (sb->sb_flags & SB_LOCK)
761
                panic("sbflush: locked");
762
        while (sb->sb_mbcnt) {
763
                /*
764
                 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
765
                 * we would loop forever. Panic instead.
766
                 */
767
                if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
768
                        break;
769
                sbdrop(sb, (int)sb->sb_cc);
770
        }
771
        if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
772
                panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
773
}
774
 
775
/*
776
 * Drop data from (the front of) a sockbuf.
777
 */
778
void
779
sbdrop(sb, len)
780
        register struct sockbuf *sb;
781
        register int len;
782
{
783
        register struct mbuf *m, *mn;
784
        struct mbuf *next;
785
 
786
        next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
787
        while (len > 0) {
788
                if (m == 0) {
789
                        if (next == 0)
790
                                panic("sbdrop");
791
                        m = next;
792
                        next = m->m_nextpkt;
793
                        continue;
794
                }
795
                if (m->m_len > len) {
796
                        m->m_len -= len;
797
                        m->m_data += len;
798
                        sb->sb_cc -= len;
799
                        break;
800
                }
801
                len -= m->m_len;
802
                sbfree(sb, m);
803
                MFREE(m, mn);
804
                m = mn;
805
        }
806
        while (m && m->m_len == 0) {
807
                sbfree(sb, m);
808
                MFREE(m, mn);
809
                m = mn;
810
        }
811
        if (m) {
812
                sb->sb_mb = m;
813
                m->m_nextpkt = next;
814
        } else
815
                sb->sb_mb = next;
816
}
817
 
818
/*
819
 * Drop a record off the front of a sockbuf
820
 * and move the next record to the front.
821
 */
822
void
823
sbdroprecord(sb)
824
        register struct sockbuf *sb;
825
{
826
        register struct mbuf *m, *mn;
827
 
828
        m = sb->sb_mb;
829
        if (m) {
830
                sb->sb_mb = m->m_nextpkt;
831
                do {
832
                        sbfree(sb, m);
833
                        MFREE(m, mn);
834
                        m = mn;
835
                } while (m);
836
        }
837
}
838
 
839
/*
840
 * Create a "control" mbuf containing the specified data
841
 * with the specified type for presentation on a socket buffer.
842
 */
843
struct mbuf *
844
sbcreatecontrol(p, size, type, level)
845
        caddr_t p;
846
        register int size;
847
        int type, level;
848
{
849
        register struct cmsghdr *cp;
850
        struct mbuf *m;
851
 
852
        if (CMSG_SPACE((u_int)size) > MLEN)
853
                return ((struct mbuf *) NULL);
854
        if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
855
                return ((struct mbuf *) NULL);
856
        cp = mtod(m, struct cmsghdr *);
857
        /* XXX check size? */
858
        (void)memcpy(CMSG_DATA(cp), p, size);
859
        m->m_len = CMSG_SPACE(size);
860
        cp->cmsg_len = CMSG_LEN(size);
861
        cp->cmsg_level = level;
862
        cp->cmsg_type = type;
863
        return (m);
864
}
865
 
866
/*
867
 * Some routines that return EOPNOTSUPP for entry points that are not
868
 * supported by a protocol.  Fill in as needed.
869
 */
870
int
871
pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
872
{
873
        return EOPNOTSUPP;
874
}
875
 
876
int
877
pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct proc *p)
878
{
879
        return EOPNOTSUPP;
880
}
881
 
882
int
883
pru_connect2_notsupp(struct socket *so1, struct socket *so2)
884
{
885
        return EOPNOTSUPP;
886
}
887
 
888
int
889
pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
890
                    struct ifnet *ifp, struct proc *p)
891
{
892
        return EOPNOTSUPP;
893
}
894
 
895
int
896
pru_listen_notsupp(struct socket *so, struct proc *p)
897
{
898
        return EOPNOTSUPP;
899
}
900
 
901
int
902
pru_rcvd_notsupp(struct socket *so, int flags)
903
{
904
        return EOPNOTSUPP;
905
}
906
 
907
int
908
pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
909
{
910
        return EOPNOTSUPP;
911
}
912
 
913
/*
914
 * This isn't really a ``null'' operation, but it's the default one
915
 * and doesn't do anything destructive.
916
 */
917
int
918
pru_sense_null(struct socket *so, struct stat *sb)
919
{
920
        return 0;
921
}
922
 
923
/*
924
 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
925
 */
926
struct sockaddr *
927
dup_sockaddr(sa, canwait)
928
        struct sockaddr *sa;
929
        int canwait;
930
{
931
        struct sockaddr *sa2;
932
 
933
        MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME,
934
               canwait ? M_WAITOK : M_NOWAIT);
935
        if (sa2)
936
                bcopy(sa, sa2, sa->sa_len);
937
        return sa2;
938
}
939
 
940
/*
941
 * Create an external-format (``xsocket'') structure using the information
942
 * in the kernel-format socket structure pointed to by so.  This is done
943
 * to reduce the spew of irrelevant information over this interface,
944
 * to isolate user code from changes in the kernel structure, and
945
 * potentially to provide information-hiding if we decide that
946
 * some of this information should be hidden from users.
947
 */
948
void
949
sotoxsocket(struct socket *so, struct xsocket *xso)
950
{
951
        xso->xso_len = sizeof *xso;
952
        xso->xso_so = so;
953
        xso->so_type = so->so_type;
954
        xso->so_options = so->so_options;
955
        xso->so_linger = so->so_linger;
956
        xso->so_state = so->so_state;
957
        xso->so_pcb = so->so_pcb;
958
        xso->xso_protocol = so->so_proto->pr_protocol;
959
        xso->xso_family = so->so_proto->pr_domain->dom_family;
960
        xso->so_qlen = so->so_qlen;
961
        xso->so_incqlen = so->so_incqlen;
962
        xso->so_qlimit = so->so_qlimit;
963
        xso->so_timeo = so->so_timeo;
964
        xso->so_error = so->so_error;
965
        xso->so_oobmark = so->so_oobmark;
966
        sbtoxsockbuf(&so->so_snd, &xso->so_snd);
967
        sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
968
}
969
 
970
/*
971
 * This does the same for sockbufs.  Note that the xsockbuf structure,
972
 * since it is always embedded in a socket, does not include a self
973
 * pointer nor a length.  We make this entry point public in case
974
 * some other mechanism needs it.
975
 */
976
void
977
sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
978
{
979
        xsb->sb_cc = sb->sb_cc;
980
        xsb->sb_hiwat = sb->sb_hiwat;
981
        xsb->sb_mbcnt = sb->sb_mbcnt;
982
        xsb->sb_mbmax = sb->sb_mbmax;
983
        xsb->sb_lowat = sb->sb_lowat;
984
        xsb->sb_flags = sb->sb_flags;
985
        xsb->sb_timeo = sb->sb_timeo;
986
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.