File: parse.go

package info (click to toggle)
termshark 2.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,424 kB
  • sloc: sh: 98; makefile: 10
file content (142 lines) | stat: -rw-r--r-- 2,727 bytes parent folder | download | duplicates (3)
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
// Copyright 2019-2022 Graham Clark. All rights reserved.  Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.

package streams

import (
	"encoding/hex"
	"fmt"
	"strings"
)

//======================================================================

type IChunk interface {
	Direction() Direction
	StreamData() []byte
}

type IOnStreamChunk interface {
	OnStreamChunk(chunk IChunk)
}

type IOnStreamHeader interface {
	OnStreamHeader(header FollowHeader)
}

//======================================================================

type parseContext interface {
	Err() error
}

type StreamParseError struct{}

func (e StreamParseError) Error() string {
	return "Stream reassembly parse error"
}

var _ error = StreamParseError{}

//======================================================================

type Protocol int

const (
	Unspecified Protocol = 0
	TCP         Protocol = iota
	UDP         Protocol = iota
)

var _ fmt.Stringer = Protocol(0)

func (p Protocol) String() string {
	switch p {
	case Unspecified:
		return "Unspecified"
	case TCP:
		return "TCP"
	case UDP:
		return "UDP"
	default:
		panic(nil)
	}
}

//======================================================================

type Direction int

const (
	Client Direction = 0
	Server Direction = iota
)

func (d Direction) String() string {
	switch d {
	case Client:
		return "Client"
	case Server:
		return "Server"
	default:
		return "Unknown!"
	}
}

//======================================================================

type Bytes struct {
	Dirn Direction
	Data []byte
}

var _ fmt.Stringer = Bytes{}
var _ IChunk = Bytes{}

func (b Bytes) Direction() Direction {
	return b.Dirn
}

func (b Bytes) StreamData() []byte {
	return b.Data
}

func (b Bytes) String() string {
	return fmt.Sprintf("Direction: %v\n%s", b.Dirn, hex.Dump(b.Data))
}

//======================================================================

type FollowHeader struct {
	Follow string
	Filter string
	Node0  string
	Node1  string
}

func (h FollowHeader) String() string {
	return fmt.Sprintf("[client:%s server:%s follow:%s filter:%s]", h.Node0, h.Node1, h.Follow, h.Filter)
}

type FollowStream struct {
	FollowHeader
	Bytes []Bytes
}

var _ fmt.Stringer = FollowStream{}

func (f FollowStream) String() string {
	datastrs := make([]string, 0, len(f.Bytes))
	for _, b := range f.Bytes {
		datastrs = append(datastrs, b.String())
	}
	data := strings.Join(datastrs, "\n")
	return fmt.Sprintf("Follow: %s\nFilter: %s\nNode0: %s\nNode1: %s\nData:\n%s", f.Follow, f.Filter, f.Node0, f.Node1, data)
}

//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: