/
session.c
137 lines (116 loc) · 3.48 KB
/
session.c
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
#include "usr/argp/wargp/session.h"
#include "common/config.h"
#include "common/constants.h"
#include "common/session.h"
#include "usr/util/str_utils.h"
#include "usr/nl/core.h"
#include "usr/nl/session.h"
#include "usr/argp/dns.h"
#include "usr/argp/log.h"
#include "usr/argp/userspace-types.h"
#include "usr/argp/wargp.h"
#include "usr/argp/xlator_type.h"
struct display_args {
struct wargp_bool no_headers;
struct wargp_bool csv;
struct wargp_bool numeric;
struct wargp_l4proto proto;
};
static struct wargp_option display_opts[] = {
WARGP_TCP(struct display_args, proto, "Print the TCP table (default)"),
WARGP_UDP(struct display_args, proto, "Print the UDP table"),
WARGP_ICMP(struct display_args, proto, "Print the ICMP table"),
WARGP_NO_HEADERS(struct display_args, no_headers),
WARGP_CSV(struct display_args, csv),
WARGP_NUMERIC(struct display_args, numeric),
{ 0 },
};
static char *tcp_state_to_string(tcp_state state)
{
switch (state) {
case ESTABLISHED:
return "ESTABLISHED";
case V4_INIT:
return "V4_INIT";
case V6_INIT:
return "V6_INIT";
case V4_FIN_RCV:
return "V4_FIN_RCV";
case V6_FIN_RCV:
return "V6_FIN_RCV";
case V4_FIN_V6_FIN_RCV:
return "V4_FIN_V6_FIN_RCV";
case TRANS:
return "TRANS";
}
return "UNKNOWN";
}
static struct jool_result handle_display_response(
struct session_entry_usr const *entry, void *args)
{
struct display_args *dargs = args;
l4_protocol proto = dargs->proto.proto;
char timeout[TIMEOUT_BUFLEN];
timeout2str(entry->dying_time, timeout);
if (dargs->csv.value) {
printf("%s,", l4proto_to_string(proto));
print_addr6(&entry->src6, dargs->numeric.value, ",", proto);
printf(",");
print_addr6(&entry->dst6, true, ",", proto);
printf(",");
print_addr4(&entry->src4, true, ",", proto);
printf(",");
print_addr4(&entry->dst4, dargs->numeric.value, ",", proto);
printf(",");
printf("%s", timeout);
if (proto == L4PROTO_TCP)
printf(",%s", tcp_state_to_string(entry->state));
printf("\n");
} else {
if (proto == L4PROTO_TCP)
printf("(%s) ", tcp_state_to_string(entry->state));
printf("Expires in %s\n", timeout);
printf("Remote: ");
print_addr4(&entry->dst4, dargs->numeric.value, "#", proto);
printf("\t");
print_addr6(&entry->src6, dargs->numeric.value, "#", proto);
printf("\n");
printf("Local: ");
print_addr4(&entry->src4, true, "#", proto);
printf("\t");
print_addr6(&entry->dst6, true, "#", proto);
printf("\n");
printf("---------------------------------\n");
}
return result_success();
}
int handle_session_display(char *iname, int argc, char **argv, void const *arg)
{
struct display_args dargs = { 0 };
struct joolnl_socket sk;
struct jool_result result;
result.error = wargp_parse(display_opts, argc, argv, &dargs);
if (result.error)
return result.error;
result = joolnl_setup(&sk, xt_get());
if (result.error)
return pr_result(&result);
if (!dargs.csv.value) {
printf("---------------------------------\n");
} else if (show_csv_header(dargs.no_headers.value, dargs.csv.value)) {
printf("Protocol,");
printf("IPv6 Remote Address,IPv6 Remote L4-ID,");
printf("IPv6 Local Address,IPv6 Local L4-ID,");
printf("IPv4 Local Address,IPv4 Local L4-ID,");
printf("IPv4 Remote Address,IPv4 Remote L4-ID,");
printf("Expires in,State\n");
}
result = joolnl_session_foreach(&sk, iname, dargs.proto.proto,
handle_display_response, &dargs);
joolnl_teardown(&sk);
return pr_result(&result);
}
void autocomplete_session_display(void const *args)
{
print_wargp_opts(display_opts);
}