File: prochandlers.go

package info (click to toggle)
termshark 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,800 kB
  • sloc: sh: 98; makefile: 9
file content (340 lines) | stat: -rw-r--r-- 8,551 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2019-2021 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 ui contains user-interface functions and helpers for termshark.
package ui

import (
	"fmt"
	"os"
	"strings"
	"time"

	"github.com/gcla/gowid"
	"github.com/gcla/gowid/widgets/table"
	"github.com/gcla/termshark/v2"
	"github.com/gcla/termshark/v2/pcap"
	log "github.com/sirupsen/logrus"
)

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

type NoHandlers struct{}

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

type updateCurrentCaptureInTitle struct {
	Ld *pcap.PacketLoader
}

var _ pcap.IBeforeBegin = updateCurrentCaptureInTitle{}
var _ pcap.IClear = updateCurrentCaptureInTitle{}

func MakeUpdateCurrentCaptureInTitle() updateCurrentCaptureInTitle {
	return updateCurrentCaptureInTitle{
		Ld: Loader,
	}
}

func (t updateCurrentCaptureInTitle) BeforeBegin(code pcap.HandlerCode, app gowid.IApp) {
	if code&pcap.PsmlCode != 0 {
		currentCapture.SetText(t.Ld.String(), app)
		currentCaptureWidgetHolder.SetSubWidget(currentCaptureWidget, app)
	}
}

func (t updateCurrentCaptureInTitle) OnClear(code pcap.HandlerCode, app gowid.IApp) {
	currentCaptureWidgetHolder.SetSubWidget(nullw, app)
}

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

type updatePacketViews struct {
	Ld *pcap.PacketLoader
}

var _ pcap.IOnError = updatePacketViews{}
var _ pcap.IClear = updatePacketViews{}
var _ pcap.IBeforeBegin = updatePacketViews{}
var _ pcap.IAfterEnd = updatePacketViews{}

func MakePacketViewUpdater() updatePacketViews {
	res := updatePacketViews{}
	res.Ld = Loader
	return res
}

func (t updatePacketViews) OnClear(code pcap.HandlerCode, app gowid.IApp) {
	clearPacketViews(app)
}

func (t updatePacketViews) BeforeBegin(code pcap.HandlerCode, app gowid.IApp) {
	if code&pcap.PsmlCode == 0 {
		return
	}
	ch2 := Loader.PsmlFinishedChan
	clearPacketViews(app)
	t.Ld.PsmlLoader.Lock()
	defer t.Ld.PsmlLoader.Unlock()
	setPacketListWidgets(t.Ld, app)

	// Start this after widgets have been cleared, to get focus change
	termshark.TrackedGo(func() {
		fn2 := func() {
			app.Run(gowid.RunFunction(func(app gowid.IApp) {
				updatePacketListWithData(Loader, app)
			}))
		}

		termshark.RunOnDoubleTicker(ch2, fn2,
			time.Duration(100)*time.Millisecond,
			time.Duration(2000)*time.Millisecond,
			10)
	}, Goroutinewg)
}

func (t updatePacketViews) AfterEnd(code pcap.HandlerCode, app gowid.IApp) {
	if code&pcap.PsmlCode == 0 {
		return
	}
	updatePacketListWithData(t.Ld, app)
	StopEmptyStructViewTimer()
	StopEmptyHexViewTimer()
	log.Infof("Load operation complete")
}

func (t updatePacketViews) OnError(code pcap.HandlerCode, app gowid.IApp, err error) {
	if code&pcap.PsmlCode == 0 {
		return
	}
	log.Error(err)
	if !Running {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		RequestQuit()
	} else {

		var errstr string
		if kverr, ok := err.(gowid.KeyValueError); ok {
			errstr = fmt.Sprintf("%v\n\n", kverr.Cause())
			kvs := make([]string, 0, len(kverr.KeyVals))
			for k, v := range kverr.KeyVals {
				kvs = append(kvs, fmt.Sprintf("%v: %v", k, v))
			}
			errstr = errstr + strings.Join(kvs, "\n")
		} else {
			errstr = fmt.Sprintf("%v", err)
		}

		OpenLongError(errstr, app)
		StopEmptyStructViewTimer()
		StopEmptyHexViewTimer()
	}
}

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

type SimpleErrors struct{}

var _ pcap.IOnError = SimpleErrors{}

func (t SimpleErrors) OnError(code pcap.HandlerCode, app gowid.IApp, err error) {
	if code&pcap.NoneCode == 0 {
		return
	}
	log.Error(err)
	// Hack to avoid picking up errors at other parts of the load
	// cycle. There should be specific handlers for specific errors.
	app.Run(gowid.RunFunction(func(app gowid.IApp) {
		OpenError(fmt.Sprintf("%v", err), app)
	}))
}

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

type SaveRecents struct {
	Pcap   string
	Filter string
}

var _ pcap.IBeforeBegin = SaveRecents{}

func MakeSaveRecents(pcap string, filter string) SaveRecents {
	return SaveRecents{
		Pcap:   pcap,
		Filter: filter,
	}
}

func (t SaveRecents) BeforeBegin(code pcap.HandlerCode, app gowid.IApp) {
	if code&pcap.PsmlCode == 0 {
		return
	}
	// Run on main goroutine to avoid problems flagged by -race
	if t.Pcap != "" {
		termshark.AddToRecentFiles(t.Pcap)
	}
	if t.Filter != "" {
		// Run on main goroutine to avoid problems flagged by -race
		termshark.AddToRecentFilters(t.Filter)
	}
}

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

type CancelledMessage struct{}

var _ pcap.IAfterEnd = CancelledMessage{}

func (t CancelledMessage) AfterEnd(code pcap.HandlerCode, app gowid.IApp) {
	if code&pcap.PsmlCode == 0 {
		return
	}
	// Run on main goroutine to avoid problems flagged by -race
	if Loader.LoadWasCancelled() {
		// Only do this if the user isn't quitting the app,
		// otherwise it looks clumsy.
		if !QuitRequested {
			OpenError("Loading was cancelled.", app)
		}
	}
}

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

type StartUIWhenThereArePackets struct{}

var _ pcap.IPsmlHeader = StartUIWhenThereArePackets{}

func (t StartUIWhenThereArePackets) OnPsmlHeader(code pcap.HandlerCode, app gowid.IApp) {
	StartUIOnce.Do(func() {
		close(StartUIChan)
	})
}

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

type ClearMarksHandler struct{}

var _ pcap.IClear = checkGlobalJumpAfterPsml{}
var _ pcap.INewSource = checkGlobalJumpAfterPsml{}

func clearMarks() {
	for k := range marksMap {
		delete(marksMap, k)
	}
	lastJumpPos = -1
}

func (t checkGlobalJumpAfterPsml) OnNewSource(code pcap.HandlerCode, app gowid.IApp) {
	clearMarks()
}

func (t checkGlobalJumpAfterPsml) OnClear(code pcap.HandlerCode, app gowid.IApp) {
	clearMarks()
}

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

type checkGlobalJumpAfterPsml struct {
	Jump termshark.GlobalJumpPos
}

var _ pcap.IAfterEnd = checkGlobalJumpAfterPsml{}

func MakeCheckGlobalJumpAfterPsml(jmp termshark.GlobalJumpPos) checkGlobalJumpAfterPsml {
	return checkGlobalJumpAfterPsml{
		Jump: jmp,
	}
}

func (t checkGlobalJumpAfterPsml) AfterEnd(code pcap.HandlerCode, app gowid.IApp) {
	// Run on main goroutine to avoid problems flagged by -race
	if code&pcap.PsmlCode == 0 {
		return
	}
	if QuitRequested {
		return
	}
	if t.Jump.Filename == Loader.Pcap() {
		if packetListView != nil {
			tableRow, err := tableRowFromPacketNumber(t.Jump.Pos)
			if err != nil {
				OpenError(err.Error(), app)
			} else {

				tableCol := 0
				curTablePos, err := packetListView.FocusXY()
				if err == nil {
					tableCol = curTablePos.Column
				}

				packetListView.SetFocusXY(app, table.Coords{Column: tableCol, Row: tableRow})
			}
		}
	}
}

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

// used for the pdml loader
type SetStructWidgets struct {
	Ld *pcap.PacketLoader
}

var _ pcap.IOnError = SetStructWidgets{}

var _ pcap.IBeforeBegin = SetStructWidgets{}
var _ pcap.IAfterEnd = SetStructWidgets{}

func (s SetStructWidgets) BeforeBegin(code pcap.HandlerCode, app gowid.IApp) {
	if code&pcap.PdmlCode == 0 {
		return
	}
	s2ch := s.Ld.Stage2FinishedChan

	termshark.TrackedGo(func() {
		fn2 := func() {
			app.Run(gowid.RunFunction(func(app gowid.IApp) {
				setLowerWidgets(app)
			}))
		}

		termshark.RunOnDoubleTicker(s2ch, fn2,
			time.Duration(100)*time.Millisecond,
			time.Duration(2000)*time.Millisecond,
			10)
	}, Goroutinewg)
}

// Close the channel before the callback. When the global loader state is idle,
// app.Quit() will stop accepting app callbacks, so the goroutine that waits
// for ch to be closed will never terminate.
func (s SetStructWidgets) AfterEnd(code pcap.HandlerCode, app gowid.IApp) {
	if code&pcap.PdmlCode == 0 {
		return
	}
	setLowerWidgets(app)
	StopEmptyHexViewTimer()
	StopEmptyStructViewTimer()
}

func (s SetStructWidgets) OnError(code pcap.HandlerCode, app gowid.IApp, err error) {
	if code&pcap.PdmlCode == 0 {
		return
	}
	log.Error(err)
	// Hack to avoid picking up errors at other parts of the load
	// cycle. There should be specific handlers for specific errors.
	if s.Ld.PdmlLoader.IsLoading() {
		app.Run(gowid.RunFunction(func(app gowid.IApp) {
			OpenLongError(fmt.Sprintf("%v", err), app)
		}))
	}
}

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