summaryrefslogtreecommitdiffstats
path: root/src/expect/snprintf_default.c
blob: f2b5c971db928d819fb18c31d301332d305fff87 (plain)
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
/*
 * (C) 2005-2011 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include "internal/internal.h"

static int
__snprintf_expect_timeout(char *buf, unsigned int len,
			  const struct nf_expect *exp)
{
	if (test_bit(ATTR_EXP_TIMEOUT, exp->set))
		return snprintf(buf, len, "%u ", exp->timeout);

	return 0;
}

static int
__snprintf_expect_class(char *buf, unsigned int len,
			  const struct nf_expect *exp)
{
	if (test_bit(ATTR_EXP_CLASS, exp->set))
		return snprintf(buf, len, "class=%u ", exp->class);

	return 0;
}

static int __snprintf_expect_proto(char *buf, 
				   unsigned int len,
				   const struct nf_expect *exp)
{
	 return(snprintf(buf, len, "proto=%d ",
			 exp->expected.orig.protonum));
}

int __snprintf_expect_default(char *buf, 
			      unsigned int len,
			      const struct nf_expect *exp,
			      unsigned int msg_type,
			      unsigned int flags) 
{
	int ret = 0, size = 0, offset = 0;
	const char *delim = "";

	switch(msg_type) {
		case NFCT_T_NEW:
			ret = snprintf(buf, len, "%9s ", "[NEW]");
			break;
		case NFCT_T_UPDATE:
			ret = snprintf(buf, len, "%9s ", "[UPDATE]");
			break;
		case NFCT_T_DESTROY:
			ret = snprintf(buf, len, "%9s ", "[DESTROY]");
			break;
		default:
			break;
	}

	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_expect_timeout(buf+offset, len, exp);
	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_expect_proto(buf+offset, len, exp);
	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_address(buf+offset, len, &exp->expected.orig,
				 "src", "dst");
	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_proto(buf+offset, len, &exp->expected.orig);
	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_address(buf+offset, len, &exp->mask.orig,
				 "mask-src", "mask-dst");
	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_proto(buf+offset, len,
				&exp->mask.orig);
	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_address(buf+offset, len, &exp->master.orig,
				 "master-src", "master-dst");
	BUFFER_SIZE(ret, size, len, offset);

	ret = __snprintf_proto(buf+offset, len,
				&exp->master.orig);
	BUFFER_SIZE(ret, size, len, offset);

	if (test_bit(ATTR_EXP_ZONE, exp->set)) {
		ret = snprintf(buf+offset, len, "zone=%u ", exp->zone);
		BUFFER_SIZE(ret, size, len, offset);
	}

	if (exp->flags & NF_CT_EXPECT_PERMANENT) {
		ret = snprintf(buf+offset, len, "PERMANENT");
		BUFFER_SIZE(ret, size, len, offset);
		delim = ",";
	}
	if (exp->flags & NF_CT_EXPECT_INACTIVE) {
		ret = snprintf(buf+offset, len, "%sINACTIVE", delim);
		BUFFER_SIZE(ret, size, len, offset);
		delim = ",";
	}
	if (exp->flags & NF_CT_EXPECT_USERSPACE) {
		ret = snprintf(buf+offset, len, "%sUSERSPACE", delim);
		BUFFER_SIZE(ret, size, len, offset);
	}
	/* extra space not to stick to next field. */
	if (exp->flags) {
		ret = snprintf(buf+offset, len, " ");
		BUFFER_SIZE(ret, size, len, offset);
	}
	ret = __snprintf_expect_class(buf+offset, len, exp);
	BUFFER_SIZE(ret, size, len, offset);

	if (test_bit(ATTR_EXP_HELPER_NAME, exp->set)) {
		ret = snprintf(buf+offset, len, "helper=%s", exp->helper_name);
		BUFFER_SIZE(ret, size, len, offset);
	}

	/* Delete the last blank space if needed */
	if (len > 0 && buf[size-1] == ' ')
		size--;

	return size;
}