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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [tcpip/] [current/] [src/] [sys/] [kern/] [uipc_socket2.c] - Blame information for rev 786

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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