File: fields.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 (380 lines) | stat: -rw-r--r-- 10,147 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// 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 fields

import (
	"bufio"
	"encoding/gob"
	"os"
	"os/exec"
	"sort"
	"strings"
	"sync"

	"github.com/gcla/termshark/v2"
	log "github.com/sirupsen/logrus"
)

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

type FieldType uint

//
// from epan/ftypes/ftypes.h
//
// enum ftenum {
const (
	FT_NONE              FieldType = iota /* used for text labels with no value */
	FT_PROTOCOL          FieldType = iota
	FT_BOOLEAN           FieldType = iota /* TRUE and FALSE come from <glib.h> */
	FT_CHAR              FieldType = iota /* 1-octet character as 0-255 */
	FT_UINT8             FieldType = iota
	FT_UINT16            FieldType = iota
	FT_UINT24            FieldType = iota /* really a UINT32, but displayed as 6 hex-digits if FD_HEX*/
	FT_UINT32            FieldType = iota
	FT_UINT40            FieldType = iota /* really a UINT64, but displayed as 10 hex-digits if FD_HEX*/
	FT_UINT48            FieldType = iota /* really a UINT64, but displayed as 12 hex-digits if FD_HEX*/
	FT_UINT56            FieldType = iota /* really a UINT64, but displayed as 14 hex-digits if FD_HEX*/
	FT_UINT64            FieldType = iota
	FT_INT8              FieldType = iota
	FT_INT16             FieldType = iota
	FT_INT24             FieldType = iota /* same as for UINT24 */
	FT_INT32             FieldType = iota
	FT_INT40             FieldType = iota /* same as for UINT40 */
	FT_INT48             FieldType = iota /* same as for UINT48 */
	FT_INT56             FieldType = iota /* same as for UINT56 */
	FT_INT64             FieldType = iota
	FT_IEEE_11073_SFLOAT FieldType = iota
	FT_IEEE_11073_FLOAT  FieldType = iota
	FT_FLOAT             FieldType = iota
	FT_DOUBLE            FieldType = iota
	FT_ABSOLUTE_TIME     FieldType = iota
	FT_RELATIVE_TIME     FieldType = iota
	FT_STRING            FieldType = iota
	FT_STRINGZ           FieldType = iota /* for use with proto_tree_add_item() */
	FT_UINT_STRING       FieldType = iota /* for use with proto_tree_add_item() */
	FT_ETHER             FieldType = iota
	FT_BYTES             FieldType = iota
	FT_UINT_BYTES        FieldType = iota
	FT_IPv4              FieldType = iota
	FT_IPv6              FieldType = iota
	FT_IPXNET            FieldType = iota
	FT_FRAMENUM          FieldType = iota /* a UINT32, but if selected lets you go to frame with that number */
	FT_PCRE              FieldType = iota /* a compiled Perl-Compatible Regular Expression object */
	FT_GUID              FieldType = iota /* GUID, UUID */
	FT_OID               FieldType = iota /* OBJECT IDENTIFIER */
	FT_EUI64             FieldType = iota
	FT_AX25              FieldType = iota
	FT_VINES             FieldType = iota
	FT_REL_OID           FieldType = iota /* RELATIVE-OID */
	FT_SYSTEM_ID         FieldType = iota
	FT_STRINGZPAD        FieldType = iota /* for use with proto_tree_add_item() */
	FT_FCWWN             FieldType = iota
	FT_NUM_TYPES         FieldType = iota /* last item number plus one */
)

var FieldTypeMap = map[string]FieldType{
	"FT_NONE":              FT_NONE,
	"FT_PROTOCOL":          FT_PROTOCOL,
	"FT_BOOLEAN":           FT_BOOLEAN,
	"FT_CHAR":              FT_CHAR,
	"FT_UINT8":             FT_UINT8,
	"FT_UINT16":            FT_UINT16,
	"FT_UINT24":            FT_UINT24,
	"FT_UINT32":            FT_UINT32,
	"FT_UINT40":            FT_UINT40,
	"FT_UINT48":            FT_UINT48,
	"FT_UINT56":            FT_UINT56,
	"FT_UINT64":            FT_UINT64,
	"FT_INT8":              FT_INT8,
	"FT_INT16":             FT_INT16,
	"FT_INT24":             FT_INT24,
	"FT_INT32":             FT_INT32,
	"FT_INT40":             FT_INT40,
	"FT_INT48":             FT_INT48,
	"FT_INT56":             FT_INT56,
	"FT_INT64":             FT_INT64,
	"FT_IEEE_11073_SFLOAT": FT_IEEE_11073_SFLOAT,
	"FT_IEEE_11073_FLOAT":  FT_IEEE_11073_FLOAT,
	"FT_FLOAT":             FT_FLOAT,
	"FT_DOUBLE":            FT_DOUBLE,
	"FT_ABSOLUTE_TIME":     FT_ABSOLUTE_TIME,
	"FT_RELATIVE_TIME":     FT_RELATIVE_TIME,
	"FT_STRING":            FT_STRING,
	"FT_STRINGZ":           FT_STRINGZ,
	"FT_UINT_STRING":       FT_UINT_STRING,
	"FT_ETHER":             FT_ETHER,
	"FT_BYTES":             FT_BYTES,
	"FT_UINT_BYTES":        FT_UINT_BYTES,
	"FT_IPv4":              FT_IPv4,
	"FT_IPv6":              FT_IPv6,
	"FT_IPXNET":            FT_IPXNET,
	"FT_FRAMENUM":          FT_FRAMENUM,
	"FT_PCRE":              FT_PCRE,
	"FT_GUID":              FT_GUID,
	"FT_OID":               FT_OID,
	"FT_EUI64":             FT_EUI64,
	"FT_AX25":              FT_AX25,
	"FT_VINES":             FT_VINES,
	"FT_REL_OID":           FT_REL_OID,
	"FT_SYSTEM_ID":         FT_SYSTEM_ID,
	"FT_STRINGZPAD":        FT_STRINGZPAD,
	"FT_FCWWN":             FT_FCWWN,
	"FT_NUM_TYPES":         FT_NUM_TYPES,
}

func ParseFieldType(s string) (res FieldType, ok bool) {
	res, ok = FieldTypeMap[s]
	return
}

type Protocol string

type Field struct {
	Name string
	Type FieldType
}

type FieldsAndProtos struct {
	Fields    interface{} // protocol or field or map[string]interface{}
	Protocols map[string]struct{}
}

func init() {
	gob.Register(make(map[string]interface{}))
	gob.Register(Protocol(""))
	gob.Register(Field{})
}

type TSharkFields struct {
	once sync.Once
	ser  *FieldsAndProtos
}

type IPrefixCompleterCallback interface {
	Call([]string)
}

type IPrefixCompleter interface {
	Completions(prefix string, cb IPrefixCompleterCallback)
}

func New() *TSharkFields {
	return &TSharkFields{}
}

func DeleteCachedFields() error {
	return os.Remove(termshark.CacheFile("tsharkfieldsv3.gob.gz"))
}

// Can be run asynchronously.
// This ought to use interfaces to make it testable.
func (w *TSharkFields) Init() error {
	newer, err := termshark.FileNewerThan(termshark.CacheFile("tsharkfieldsv3.gob.gz"), termshark.DirOfPathCommandUnsafe(termshark.TSharkBin()))
	if err == nil && newer {
		f := &FieldsAndProtos{
			Fields:    make(map[string]interface{}),
			Protocols: make(map[string]struct{}),
		}

		err = termshark.ReadGob(termshark.CacheFile("tsharkfieldsv3.gob.gz"), f)
		if err == nil {
			w.ser = f
			log.Infof("Read cached tshark fields.")
			return nil
		} else {
			log.Infof("Could not read cached tshark fields (%v) - regenerating...", err)
		}
	}

	err = w.InitNoCache()
	if err != nil {
		return err
	}

	err = termshark.WriteGob(termshark.CacheFile("tsharkfieldsv3.gob.gz"), w.ser)
	if err != nil {
		return err
	}

	return nil
}

func (w *TSharkFields) InitNoCache() error {
	cmd := exec.Command(termshark.TSharkBin(), []string{"-G", "fields"}...)

	out, err := cmd.StdoutPipe()
	if err != nil {
		return err
	}

	cmd.Start()

	fieldsMap := make(map[string]interface{})
	protMap := make(map[string]struct{})

	scanner := bufio.NewScanner(out)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, "F") { // Wireshark field
			fields := strings.Split(line, "\t")
			protos := strings.SplitN(fields[2], ".", 2)
			if len(protos) > 1 {
				cur := fieldsMap
				for i := 0; i < len(protos)-1; i++ {
					if val, ok := cur[protos[i]]; ok {
						cur = val.(map[string]interface{})
					} else {
						next := make(map[string]interface{})
						cur[protos[i]] = next
						cur = next
					}
				}
				// Get none value if it's not found - so use that
				ty, _ := ParseFieldType(fields[3])
				cur[protos[len(protos)-1]] = Field{
					Name: fields[2],
					Type: ty,
				}
			}
		} else if strings.HasPrefix(line, "P") { // Wireshark protocol
			fields := strings.Split(line, "\t")
			protMap[fields[2]] = struct{}{}
		}
	}

	cmd.Wait()

	w.ser = &FieldsAndProtos{
		Fields:    fieldsMap,
		Protocols: protMap,
	}

	return nil
}

func dedup(s []string) []string {
	if len(s) == 0 {
		return s
	}
	i := 0
	for j := 1; j < len(s); j++ {
		if s[i] == s[j] {
			continue
		}
		i++
		s[i] = s[j]
	}
	return s[0 : i+1]
}

func (t *TSharkFields) LookupField(name string) (bool, Field) {
	fields := strings.Split(name, ".")

	cur := t.ser.Fields.(map[string]interface{})
	for i := 0; i < len(fields); i++ {
		if val, ok := cur[fields[i]]; ok {
			if i == len(fields)-1 {
				switch val := val.(type) {
				// means there's another level of indirection, so our input is too long
				case Field:
					return true, val
				default:
					return false, Field{}
				}
			} else {
				switch val := val.(type) {
				case map[string]interface{}:
					cur = val
				default:
					return false, Field{}
				}
			}
		} else {
			return false, Field{}
		}
	}

	return false, Field{}
}

func (t *TSharkFields) Completions(prefix string, cb IPrefixCompleterCallback) {
	var err error
	res := make([]string, 0, 100)

	t.once.Do(func() {
		err = t.Init()
	})

	if err != nil {
		log.Warnf("Field completion error: %v", err)
	}

	// might be nil if I am still loading from tshark -G
	if t.ser == nil {
		cb.Call(res)
		return
	}

	field := ""
	txt := prefix
	if !strings.HasSuffix(txt, " ") && txt != "" {
		fields := strings.Fields(txt)
		if len(fields) > 0 {
			field = fields[len(fields)-1]
		}
	}

	fields := strings.SplitN(field, ".", 2)

	prefs := make([]string, 0, 10)
	cur := t.ser.Fields.(map[string]interface{})
	failed := false
loop:
	for i := 0; i < len(fields); i++ {
		if val, ok := cur[fields[i]]; ok {
			if i == len(fields)-1 {
				switch val.(type) {
				// means there's another level of indirection, so our input is too long
				case map[string]interface{}:
					failed = true
				}
			} else {
				switch val := val.(type) {
				case map[string]interface{}:
					prefs = append(prefs, fields[i])
					cur = val
				default:
					failed = true
					break loop
				}
			}
		}
	}

	if !failed {
		for k, _ := range cur {
			if strings.HasPrefix(k, fields[len(fields)-1]) {
				res = append(res, strings.Join(append(prefs, k), "."))
			}
		}
	}
	for k, _ := range t.ser.Protocols {
		if strings.HasPrefix(k, field) {
			res = append(res, k)
		}
	}

	sort.Strings(res)
	res = dedup(res)

	cb.Call(res)
}

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