File: exp.c

package info (click to toggle)
libnl3 3.4.0-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,120 kB
  • sloc: ansic: 57,494; sh: 4,447; python: 3,702; makefile: 888; yacc: 478; cpp: 454; lex: 210
file content (165 lines) | stat: -rw-r--r-- 4,317 bytes parent folder | download | duplicates (5)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * src/lib/exp.c		CLI Expectation Helpers
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation version 2.1
 *	of the License.
 *
 * Copyright (c) 2008-2009 Thomas Graf <tgraf@suug.ch>
 * Copyright (c) 2012 Rich Fought <rich.fought@watchguard.com>
 */

/**
 * @ingroup cli
 * @defgroup cli_exp Expectation Tracking
 *
 * @{
 */

#include <netlink/cli/utils.h>
#include <netlink/cli/exp.h>

struct nfnl_exp *nl_cli_exp_alloc(void)
{
	struct nfnl_exp *exp;

	exp = nfnl_exp_alloc();
	if (!exp)
		nl_cli_fatal(ENOMEM, "Unable to allocate expectation object");

	return exp;
}

struct nl_cache *nl_cli_exp_alloc_cache(struct nl_sock *sk)
{
	return nl_cli_alloc_cache(sk, "expectation", nfnl_exp_alloc_cache);
}

void nl_cli_exp_parse_family(struct nfnl_exp *exp, char *arg)
{
	int family;

	if ((family = nl_str2af(arg)) == AF_UNSPEC)
		nl_cli_fatal(EINVAL,
			     "Unable to nl_cli_exp_parse family \"%s\": %s",
			     arg, nl_geterror(NLE_INVAL));

	nfnl_exp_set_family(exp, family);
}

void nl_cli_exp_parse_timeout(struct nfnl_exp *exp, char *arg)
{
	uint32_t timeout = nl_cli_parse_u32(arg);
	nfnl_exp_set_timeout(exp, timeout);
}

void nl_cli_exp_parse_id(struct nfnl_exp *exp, char *arg)
{
	uint32_t id = nl_cli_parse_u32(arg);
	nfnl_exp_set_id(exp, id);
}

void nl_cli_exp_parse_helper_name(struct nfnl_exp *exp, char *arg)
{
	nfnl_exp_set_helper_name(exp, arg);
}

void nl_cli_exp_parse_zone(struct nfnl_exp *exp, char *arg)
{
	uint32_t zone = nl_cli_parse_u32(arg);
	nfnl_exp_set_zone(exp, zone);
}

void nl_cli_exp_parse_flags(struct nfnl_exp *exp, char *arg)
{
	uint32_t flags = nl_cli_parse_u32(arg);
	nfnl_exp_set_flags(exp, flags);
}

void nl_cli_exp_parse_class(struct nfnl_exp *exp, char *arg)
{
	uint32_t class = nl_cli_parse_u32(arg);
	nfnl_exp_set_class(exp, class);
}

void nl_cli_exp_parse_nat_dir(struct nfnl_exp *exp, char *arg)
{
	uint32_t nat_dir = nl_cli_parse_u32(arg);
	nfnl_exp_set_nat_dir(exp, nat_dir);
}

void nl_cli_exp_parse_fn(struct nfnl_exp *exp, char *arg)
{
	nfnl_exp_set_fn(exp, arg);
}

void nl_cli_exp_parse_src(struct nfnl_exp *exp, int tuple, char *arg)
{
	int err;
	struct nl_addr *a = nl_cli_addr_parse(arg, nfnl_exp_get_family(exp));
	if ((err = nfnl_exp_set_src(exp, tuple, a)) < 0)
		nl_cli_fatal(err, "Unable to set source address: %s",
			     nl_geterror(err));
}

void nl_cli_exp_parse_dst(struct nfnl_exp *exp, int tuple, char *arg)
{
	int err;
	struct nl_addr *a = nl_cli_addr_parse(arg, nfnl_exp_get_family(exp));
	if ((err = nfnl_exp_set_dst(exp, tuple, a)) < 0)
		nl_cli_fatal(err, "Unable to set destination address: %s",
			     nl_geterror(err));
}

void nl_cli_exp_parse_l4protonum(struct nfnl_exp *exp, int tuple, char *arg)
{
	int l4protonum;

	if ((l4protonum = nl_str2ip_proto(arg)) < 0)
		nl_cli_fatal(l4protonum,
			"Unable to nl_cli_exp_parse protocol \"%s\": %s",
			arg, nl_geterror(l4protonum));

	nfnl_exp_set_l4protonum(exp, tuple, l4protonum);
}

void nl_cli_exp_parse_src_port(struct nfnl_exp *exp, int tuple, char *arg)
{
	uint32_t sport = nl_cli_parse_u32(arg);
	uint16_t dport = nfnl_exp_get_dst_port(exp, tuple);
	nfnl_exp_set_ports(exp, tuple, sport, dport);
}

void nl_cli_exp_parse_dst_port(struct nfnl_exp *exp, int tuple, char *arg)
{
	uint32_t dport = nl_cli_parse_u32(arg);
	uint16_t sport = nfnl_exp_get_src_port(exp, tuple);
	nfnl_exp_set_ports(exp, tuple, sport, dport);
}

void nl_cli_exp_parse_icmp_id(struct nfnl_exp *exp, int tuple, char *arg)
{
	uint32_t id = nl_cli_parse_u32(arg);
	uint8_t type = nfnl_exp_get_icmp_type(exp, tuple);
	uint8_t code = nfnl_exp_get_icmp_code(exp, tuple);
	nfnl_exp_set_icmp(exp, tuple, id, type, code);
}

void nl_cli_exp_parse_icmp_type(struct nfnl_exp *exp, int tuple, char *arg)
{
	uint32_t type = nl_cli_parse_u32(arg);
	uint16_t id = nfnl_exp_get_icmp_id(exp, tuple);
	uint8_t code = nfnl_exp_get_icmp_code(exp, tuple);
	nfnl_exp_set_icmp(exp, tuple, id, type, code);
}

void nl_cli_exp_parse_icmp_code(struct nfnl_exp *exp, int tuple, char *arg)
{
	uint32_t code = nl_cli_parse_u32(arg);
	uint16_t id = nfnl_exp_get_icmp_id(exp, tuple);
	uint8_t type = nfnl_exp_get_icmp_type(exp, tuple);
	nfnl_exp_set_icmp(exp, tuple, id, type, code);
}

/** @} */