This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
netfilter_helpers.h
144 lines (123 loc) · 2.93 KB
/
netfilter_helpers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Helpers for netfiler modules. This file provides implementations for basic
* functions such as strncasecmp(), etc.
*
* gcc will warn for defined but unused functions, so we only include the
* functions requested. The following macros are used:
* NF_NEED_STRNCASECMP nf_strncasecmp()
* NF_NEED_STRTOU16 nf_strtou16()
* NF_NEED_STRTOU32 nf_strtou32()
*/
#ifndef _NETFILTER_HELPERS_H
#define _NETFILTER_HELPERS_H
/* Only include these functions for kernel code. */
#ifdef __KERNEL__
#include <net/netfilter/nf_conntrack_expect.h>
#include <linux/ctype.h>
#define iseol(c) ( (c) == '\r' || (c) == '\n' )
/*
* The standard strncasecmp()
*/
#ifdef NF_NEED_STRNCASECMP
static int
nf_strncasecmp(const char* s1, const char* s2, u_int32_t len)
{
if (s1 == NULL || s2 == NULL)
{
if (s1 == NULL && s2 == NULL)
{
return 0;
}
return (s1 == NULL) ? -1 : 1;
}
while (len > 0 && tolower(*s1) == tolower(*s2))
{
len--;
s1++;
s2++;
}
return ( (len == 0) ? 0 : (tolower(*s1) - tolower(*s2)) );
}
#endif /* NF_NEED_STRNCASECMP */
/*
* Parse a string containing a 16-bit unsigned integer.
* Returns the number of chars used, or zero if no number is found.
*/
#ifdef NF_NEED_STRTOU16
static int
nf_strtou16(const char* pbuf, u_int16_t* pval)
{
int n = 0;
*pval = 0;
while (isdigit(pbuf[n]))
{
*pval = (*pval * 10) + (pbuf[n] - '0');
n++;
}
return n;
}
#endif /* NF_NEED_STRTOU16 */
/*
* Parse a string containing a 32-bit unsigned integer.
* Returns the number of chars used, or zero if no number is found.
*/
#ifdef NF_NEED_STRTOU32
static int
nf_strtou32(const char* pbuf, u_int32_t* pval)
{
int n = 0;
*pval = 0;
while (pbuf[n] >= '0' && pbuf[n] <= '9')
{
*pval = (*pval * 10) + (pbuf[n] - '0');
n++;
}
return n;
}
#endif /* NF_NEED_STRTOU32 */
/*
* Given a buffer and length, advance to the next line and mark the current
* line.
*/
#ifdef NF_NEED_NEXTLINE
static int
nf_nextline(char* p, uint len, uint* poff, uint* plineoff, uint* plinelen)
{
uint off = *poff;
uint physlen = 0;
if (off >= len)
{
return 0;
}
while (p[off] != '\n')
{
if (len-off <= 1)
{
return 0;
}
physlen++;
off++;
}
/* if we saw a crlf, physlen needs adjusted */
if (physlen > 0 && p[off] == '\n' && p[off-1] == '\r')
{
physlen--;
}
/* advance past the newline */
off++;
*plineoff = *poff;
*plinelen = physlen;
*poff = off;
return 1;
}
#endif /* NF_NEED_NEXTLINE */
static inline int rtsp_nf_ct_expect_related(struct nf_conntrack_expect *expect)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,3,0)
return nf_ct_expect_related(expect, 0);
#else
return nf_ct_expect_related(expect);
#endif
}
#endif /* __KERNEL__ */
#endif /* _NETFILTER_HELPERS_H */