diff options
author | cmetz <cmetz@cvs.openbsd.org> | 1999-07-02 01:02:54 +0000 |
---|---|---|
committer | cmetz <cmetz@cvs.openbsd.org> | 1999-07-02 01:02:54 +0000 |
commit | 4eb548238f91ec4f48d5b99eaf76c43e79630a3e (patch) | |
tree | 908bbf31fc9ceee7f5f285e1cd4e15f60ef0bf89 /sys | |
parent | d57519a10bc4e9b97cd8834c226523efa9ecd5f3 (diff) |
Added a simple but potentially very useful new mbuf function, m_apply().
It applies the supplied function f(state, p, len) to every contiguous region
in a mbuf, thus mostly handling all the mbuf-isms for you.
It's used by my TCP MD5 signature implementation to run MD5 over the TCP
payload data in a mbuf so that I don't have to spread mbufism-loops all over.
It might also be useful for IPsec.
Diffstat (limited to 'sys')
-rw-r--r-- | sys/kern/uipc_mbuf.c | 46 | ||||
-rw-r--r-- | sys/sys/mbuf.h | 4 |
2 files changed, 48 insertions, 2 deletions
diff --git a/sys/kern/uipc_mbuf.c b/sys/kern/uipc_mbuf.c index 249d2396687..e35f1c3f307 100644 --- a/sys/kern/uipc_mbuf.c +++ b/sys/kern/uipc_mbuf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uipc_mbuf.c,v 1.12 1999/05/14 02:12:29 cmetz Exp $ */ +/* $OpenBSD: uipc_mbuf.c,v 1.13 1999/07/02 01:02:53 cmetz Exp $ */ /* $NetBSD: uipc_mbuf.c,v 1.15.4.1 1996/06/13 17:11:44 cgd Exp $ */ /* @@ -866,3 +866,47 @@ m_zero(m) m = m->m_next; } } + +/* + * Apply function f to the data in an mbuf chain starting "off" bytes from the + * beginning, continuing for "len" bytes. + */ +int +m_apply(m, off, len, f, fstate) + struct mbuf *m; + int off; + int len; + /* fstate, data, len */ + int (*f)(caddr_t, caddr_t, unsigned int); + caddr_t fstate; +{ + int rval; + unsigned int count; + + if (off < 0 || len < 0) + panic("m_apply"); + while (off > 0) { + if (m == 0) + panic("m_apply"); + if (off < m->m_len) + break; + off -= m->m_len; + m = m->m_next; + } + while (len > 0) { + if (m == 0) + panic("m_apply"); + count = min(m->m_len - off, len); + + rval = f(fstate, mtod(m, caddr_t) + off, count); + if (rval) + return (rval); + + len -= count; + off = 0; + m = m->m_next; + } + + return (0); +} + diff --git a/sys/sys/mbuf.h b/sys/sys/mbuf.h index 62ba4bdc873..82f6b704869 100644 --- a/sys/sys/mbuf.h +++ b/sys/sys/mbuf.h @@ -1,4 +1,4 @@ -/* $OpenBSD: mbuf.h,v 1.10 1999/03/27 21:04:21 provos Exp $ */ +/* $OpenBSD: mbuf.h,v 1.11 1999/07/02 01:02:52 cmetz Exp $ */ /* $NetBSD: mbuf.h,v 1.19 1996/02/09 18:25:14 christos Exp $ */ /* @@ -407,6 +407,8 @@ void m_cat __P((struct mbuf *, struct mbuf *)); struct mbuf *m_devget __P((char *, int, int, struct ifnet *, void (*) __P((const void *, void *, size_t)))); void m_zero __P((struct mbuf *)); +int m_apply __P((struct mbuf *, int, int, + int (*)(caddr_t, caddr_t, unsigned int), caddr_t)); #ifdef MBTYPES int mbtypes[] = { /* XXX */ |