xref: /illumos-gate/usr/src/uts/common/inet/ipf/misc.c (revision eda50310abb3984bab11856a2aca8936d26881cb)
1 /*
2  * Copyright (C) 2003 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  */
6 /*
7  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
8  * Use is subject to license terms.
9  */
10 #ifndef __hpux
11 #pragma ident "@(#)$Id: misc.c,v 1.12 2003/11/29 07:11:03 darrenr Exp $"
12 #else
13 struct uio;
14 #endif
15 
16 #pragma ident	"%Z%%M%	%I%	%E% SMI"
17 
18 #include <sys/systm.h>
19 #include <sys/types.h>
20 #include <sys/stream.h>
21 #include <sys/ddi.h>
22 
23 #ifdef	__hpux
24 # define	BCOPY(a,b,c)	bcopy((caddr_t)a, (caddr_t)b, c)
25 #endif
26 #ifdef	sun
27 # define	BCOPY(a,b,c)	bcopy((char *)a, (char *)b, c)
28 #endif
29 
30 void mb_copydata(min, off, len, buf)
31 mblk_t *min;
32 size_t off, len;
33 char *buf;
34 {
35 	u_char *s, *bp = (u_char *)buf;
36 	size_t mlen, olen, clen;
37 	mblk_t *m;
38 
39 	for (m = min; (m != NULL) && (len > 0); m = m->b_cont) {
40 		if (m->b_datap->db_type != M_DATA)
41 			continue;
42 		s = m->b_rptr;
43 		mlen = m->b_wptr - s;
44 		olen = min(off, mlen);
45 		if ((olen == mlen) || (olen < off)) {
46 			off -= olen;
47 			continue;
48 		} else if (olen) {
49 			off -= olen;
50 			s += olen;
51 			mlen -= olen;
52 		}
53 		clen = min(mlen, len);
54 		BCOPY(s, bp, clen);
55 		len -= clen;
56 		bp += clen;
57 	}
58 }
59 
60 
61 void mb_copyback(min, off, len, buf)
62 mblk_t *min;
63 size_t off, len;
64 char *buf;
65 {
66 	u_char *s, *bp = (u_char *)buf;
67 	size_t mlen, olen, clen;
68 	mblk_t *m, *mp;
69 
70 	for (m = min, mp = NULL; (m != NULL) && (len > 0); m = m->b_cont) {
71 		mp = m;
72 		if (m->b_datap->db_type != M_DATA)
73 			continue;
74 
75 		s = m->b_rptr;
76 		mlen = m->b_wptr - s;
77 		olen = min(off, mlen);
78 		if ((olen == mlen) || (olen < off)) {
79 			off -= olen;
80 			continue;
81 		} else if (olen) {
82 			off -= olen;
83 			s += olen;
84 			mlen -= olen;
85 		}
86 		clen = min(mlen, len);
87 		BCOPY(bp, s, clen);
88 		len -= clen;
89 		bp += clen;
90 	}
91 
92 	if ((m == NULL) && (mp != NULL)) {
93 		if (len > 0) {
94 			mlen = mp->b_datap->db_lim - mp->b_wptr;
95 			if (mlen > 0) {
96 				if (mlen > len)
97 					mlen = len;
98 				BCOPY(bp, mp->b_wptr, mlen);
99 				bp += mlen;
100 				len -= mlen;
101 				mp->b_wptr += mlen;
102 #ifdef  STRUIO_IP
103 # if SOLARIS2 < 10
104 				mp->b_datap->db_struiolim = mp->b_wptr;
105 # endif
106 				mp->b_datap->db_struioflag &= ~STRUIO_IP;
107 #endif
108 			}
109 		}
110 
111 		if (len > 0) {
112 			m = allocb(len, BPRI_MED);
113 			if (m != NULL) {
114 				BCOPY(bp, m->b_wptr, len);
115 				m->b_band = mp->b_band;
116 				m->b_wptr += len;
117 				linkb(mp, m);
118 			}
119 		}
120 	}
121 }
122