2 * Helpers for netfiler modules. This file provides implementations for basic
3 * functions such as strncasecmp(), etc.
5 * gcc will warn for defined but unused functions, so we only include the
6 * functions requested. The following macros are used:
7 * NF_NEED_STRNCASECMP nf_strncasecmp()
8 * NF_NEED_STRTOU16 nf_strtou16()
9 * NF_NEED_STRTOU32 nf_strtou32()
11 #ifndef _NETFILTER_HELPERS_H
12 #define _NETFILTER_HELPERS_H
14 /* Only include these functions for kernel code. */
17 #include <linux/ctype.h>
18 #define iseol(c) ( (c) == '\r' || (c) == '\n' )
21 * The standard strncasecmp()
23 #ifdef NF_NEED_STRNCASECMP
25 nf_strncasecmp(const char* s1
, const char* s2
, u_int32_t len
)
27 if (s1
== NULL
|| s2
== NULL
)
29 if (s1
== NULL
&& s2
== NULL
)
33 return (s1
== NULL
) ? -1 : 1;
35 while (len
> 0 && tolower(*s1
) == tolower(*s2
))
41 return ( (len
== 0) ? 0 : (tolower(*s1
) - tolower(*s2
)) );
43 #endif /* NF_NEED_STRNCASECMP */
46 * Parse a string containing a 16-bit unsigned integer.
47 * Returns the number of chars used, or zero if no number is found.
49 #ifdef NF_NEED_STRTOU16
51 nf_strtou16(const char* pbuf
, u_int16_t
* pval
)
56 while (isdigit(pbuf
[n
]))
58 *pval
= (*pval
* 10) + (pbuf
[n
] - '0');
64 #endif /* NF_NEED_STRTOU16 */
67 * Parse a string containing a 32-bit unsigned integer.
68 * Returns the number of chars used, or zero if no number is found.
70 #ifdef NF_NEED_STRTOU32
72 nf_strtou32(const char* pbuf
, u_int32_t
* pval
)
77 while (pbuf
[n
] >= '0' && pbuf
[n
] <= '9')
79 *pval
= (*pval
* 10) + (pbuf
[n
] - '0');
85 #endif /* NF_NEED_STRTOU32 */
88 * Given a buffer and length, advance to the next line and mark the current
91 #ifdef NF_NEED_NEXTLINE
93 nf_nextline(char* p
, uint len
, uint
* poff
, uint
* plineoff
, uint
* plinelen
)
103 while (p
[off
] != '\n')
114 /* if we saw a crlf, physlen needs adjusted */
115 if (physlen
> 0 && p
[off
] == '\n' && p
[off
-1] == '\r')
120 /* advance past the newline */
129 #endif /* NF_NEED_NEXTLINE */
131 #endif /* __KERNEL__ */
133 #endif /* _NETFILTER_HELPERS_H */