-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgolibwireshark.go
166 lines (140 loc) · 3.91 KB
/
golibwireshark.go
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
/*
Package golibwireshark use libwireshark library to decode pcap file and analyse dissection data.
*/
package golibwireshark
/*
#cgo pkg-config: glib-2.0
#cgo LDFLAGS: -L${SRCDIR}/libs -lwiretap -lwsutil -lwireshark
#cgo LDFLAGS: -Wl,-rpath,${SRCDIR}/libs
#cgo CFLAGS: -I${SRCDIR}/include/wireshark
#cgo CFLAGS: -I${SRCDIR}/include/wireshark/wiretap
#cgo CFLAGS: -I${SRCDIR}/include
#cgo CFLAGS: -std=c99
#include "./include/lib.h"
*/
import "C"
import (
"fmt"
"unsafe"
)
//Packet data index after dissection
type Packet struct {
Edt *C.struct_epan_dissect //packet data index after dissected
Field *C.struct__proto_node //packet field index
}
//Init initializing the dissection. If opening inputfile or savefile fail,
//return err. After dissection finish, should use Clean() to end the dissection.
func Init(inputfile, outputfile string) error {
var err C.int
if outputfile == "" {
err = C.init(C.CString(inputfile), nil)
} else {
err = C.init(C.CString(inputfile), C.CString(outputfile))
}
if err != 0 {
return fmt.Errorf("can't open file")
}
return nil
}
//Clean to end the dissection.
func Clean() {
C.clean()
}
//ReOpenInputFile open a new input file after CloseInputFile.
//If open file failed, return error.
func ReOpenInputFile(filename string) error {
var err C.int
err = C.init_cfile(C.CString(filename))
if err != 0 {
return fmt.Errorf("can't open file")
}
return nil
}
//ReOpenOutputFile open a new output file after CloseOutputFile.
//if open file failed, return error.
func ReOpenOutputFile(filename string) error {
var err C.int
err = C.init_pdh(C.CString(filename))
if err != 0 {
return fmt.Errorf("can't create output file")
}
return nil
}
//CloseInputFile close input file. Using ReOpenInputFile to open a new input file.
func CloseInputFile() {
C.clean_cfile()
}
//CloseOutputFile close output file. Using ReOpenOutputFile to open a new output file.
func CloseOutputFile() {
C.clean_pdh()
}
//IsKey find a key in packet dissection data. If key exists, ok=ture,
//value is key value, otherwise ok=false.
func (p Packet) IsKey(key string) (values []string, ok bool) {
finfoArray := C.get_field_values(p.Edt, C.CString(key))
defer C.g_ptr_array_free(finfoArray, 1)
if finfoArray != nil {
for i := 0; i < int(finfoArray.len); i++ {
finfo := C.g_ptr_array_data(finfoArray, C.int(i))
value := C.finfo_to_value(unsafe.Pointer(finfo))
values = append(values, C.GoString(value))
}
}
if len(values) == 0 {
return values, false
}
return values, true
}
//GetPacket get one packet data index which has been dissected. If no more
//packet to be dissected, Edt return nil.
//After analysing packet data, should use FreePacket() to free packet
//data.
func (p *Packet) GetPacket() {
var edt *C.struct_epan_dissect
edt = C.next_packet()
if edt == nil {
p.Edt = nil
}
p.Edt = edt
}
//FreePacket to release packet memory
func (p *Packet) FreePacket() {
C.free_packet(p.Edt)
}
//GetField get field index by key. If key exists, return true, Field item equal index,
//otherwise return false and Field item equal nil.
func (p *Packet) GetField(key string) bool {
p.Field = C.get_field(p.Edt, C.CString(key))
if p.Field != nil {
return true
}
return false
}
//String do human readable printout. If Field equal nil, print out the packet.
//If Field doesn't equal nil, print out the Field.
func (p Packet) String() string {
var node *C.struct__proto_node
var buf string
if p.Field != nil {
node = p.Field
cbuf := C.print_node(node)
defer C.free(unsafe.Pointer(cbuf))
buf = C.GoString(cbuf)
} else {
node = (p.Edt).tree
cbuf := C.print_packet(node)
defer C.free(unsafe.Pointer(cbuf))
buf = C.GoString(cbuf)
}
return buf
}
//WriteToFile write a packet to file. If Output file are not initialized,
//return error.
func (p *Packet) WriteToFile() error {
if i := C.write_to_file(); i == 0 {
return nil
} else if i == 1 {
return fmt.Errorf("output file isn't opened")
}
return nil
}