1# Copyright 2018 Netflix, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# from include/net/tcp_states.h:
16tcpstate = {}
17tcpstate[1] = 'ESTABLISHED'
18tcpstate[2] = 'SYN_SENT'
19tcpstate[3] = 'SYN_RECV'
20tcpstate[4] = 'FIN_WAIT1'
21tcpstate[5] = 'FIN_WAIT2'
22tcpstate[6] = 'TIME_WAIT'
23tcpstate[7] = 'CLOSE'
24tcpstate[8] = 'CLOSE_WAIT'
25tcpstate[9] = 'LAST_ACK'
26tcpstate[10] = 'LISTEN'
27tcpstate[11] = 'CLOSING'
28tcpstate[12] = 'NEW_SYN_RECV'
29
30# from include/net/tcp.h:
31TCPHDR_FIN = 0x01;
32TCPHDR_SYN = 0x02;
33TCPHDR_RST = 0x04;
34TCPHDR_PSH = 0x08;
35TCPHDR_ACK = 0x10;
36TCPHDR_URG = 0x20;
37TCPHDR_ECE = 0x40;
38TCPHDR_CWR = 0x80;
39
40def flags2str(flags):
41    arr = [];
42    if flags & TCPHDR_FIN:
43        arr.append("FIN");
44    if flags & TCPHDR_SYN:
45        arr.append("SYN");
46    if flags & TCPHDR_RST:
47        arr.append("RST");
48    if flags & TCPHDR_PSH:
49        arr.append("PSH");
50    if flags & TCPHDR_ACK:
51        arr.append("ACK");
52    if flags & TCPHDR_URG:
53        arr.append("URG");
54    if flags & TCPHDR_ECE:
55        arr.append("ECE");
56    if flags & TCPHDR_CWR:
57        arr.append("CWR");
58    return "|".join(arr);
59