xref: /illumos-gate/usr/src/uts/common/inet/ipf/misc.c (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
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 #ifdef __hpux
11 struct uio;
12 #endif
13 
14 #include <sys/systm.h>
15 #include <sys/types.h>
16 #include <sys/stream.h>
17 #include <sys/ddi.h>
18 
19 #ifdef	__hpux
20 # define	BCOPY(a,b,c)	bcopy((caddr_t)a, (caddr_t)b, c)
21 #endif
22 #ifdef	sun
23 # define	BCOPY(a,b,c)	bcopy((char *)a, (char *)b, c)
24 #endif
25 
26 void mb_copydata(min, off, len, buf)
27 mblk_t *min;
28 size_t off, len;
29 char *buf;
30 {
31 	u_char *s, *bp = (u_char *)buf;
32 	size_t mlen, olen, clen;
33 	mblk_t *m;
34 
35 	for (m = min; (m != NULL) && (len > 0); m = m->b_cont) {
36 		if (m->b_datap->db_type != M_DATA)
37 			continue;
38 		s = m->b_rptr;
39 		mlen = m->b_wptr - s;
40 		olen = min(off, mlen);
41 		if ((olen == mlen) || (olen < off)) {
42 			off -= olen;
43 			continue;
44 		} else if (olen) {
45 			off -= olen;
46 			s += olen;
47 			mlen -= olen;
48 		}
49 		clen = min(mlen, len);
50 		BCOPY(s, bp, clen);
51 		len -= clen;
52 		bp += clen;
53 	}
54 }
55 
56 
57 void mb_copyback(min, off, len, buf)
58 mblk_t *min;
59 size_t off, len;
60 char *buf;
61 {
62 	u_char *s, *bp = (u_char *)buf;
63 	size_t mlen, olen, clen;
64 	mblk_t *m, *mp;
65 
66 	for (m = min, mp = NULL; (m != NULL) && (len > 0); m = m->b_cont) {
67 		mp = m;
68 		if (m->b_datap->db_type != M_DATA)
69 			continue;
70 
71 		s = m->b_rptr;
72 		mlen = m->b_wptr - s;
73 		olen = min(off, mlen);
74 		if ((olen == mlen) || (olen < off)) {
75 			off -= olen;
76 			continue;
77 		} else if (olen) {
78 			off -= olen;
79 			s += olen;
80 			mlen -= olen;
81 		}
82 		clen = min(mlen, len);
83 		BCOPY(bp, s, clen);
84 		len -= clen;
85 		bp += clen;
86 	}
87 
88 	if ((m == NULL) && (mp != NULL)) {
89 		if (len > 0) {
90 			mlen = mp->b_datap->db_lim - mp->b_wptr;
91 			if (mlen > 0) {
92 				if (mlen > len)
93 					mlen = len;
94 				BCOPY(bp, mp->b_wptr, mlen);
95 				bp += mlen;
96 				len -= mlen;
97 				mp->b_wptr += mlen;
98 #ifdef  STRUIO_IP
99 # if SOLARIS2 < 10
100 				mp->b_datap->db_struiolim = mp->b_wptr;
101 # endif
102 				mp->b_datap->db_struioflag &= ~STRUIO_IP;
103 #endif
104 			}
105 		}
106 
107 		if (len > 0) {
108 			m = allocb(len, BPRI_MED);
109 			if (m != NULL) {
110 				BCOPY(bp, m->b_wptr, len);
111 				m->b_band = mp->b_band;
112 				m->b_wptr += len;
113 				linkb(mp, m);
114 			}
115 		}
116 	}
117 }
118