Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Updated for weekly.2012-02-07 #10

Merged
merged 3 commits into from about 2 years ago

2 participants

Dustin Sallings Andreas Krennmair
Dustin Sallings

I suspect this will save me a tremendous amount of time today. But I spent a bit getting it updated for what's approaching go1.

Andreas Krennmair akrennmair merged commit 02a6a13 into from February 16, 2012
Andreas Krennmair akrennmair closed this February 16, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
5  .gitignore
... ...
@@ -0,0 +1,5 @@
  1
+#*
  2
+*~
  3
+/tools/pass/pass
  4
+/tools/pcaptest/pcaptest
  5
+/tools/tcpdump/tcpdump
21  Makefile
... ...
@@ -1,21 +0,0 @@
1  
-include $(GOROOT)/src/Make.inc
2  
-
3  
-TARG=pcap
4  
-GOFILES=decode.go\
5  
-	io.go
6  
-CGOFILES=pcap.go
7  
-CLEANFILES=pcaptest tcpdump
8  
-
9  
-include $(GOROOT)/src/Make.pkg
10  
-
11  
-pcaptest: pcaptest.go install
12  
-	$(GC) pcaptest.go
13  
-	$(LD) -o $@ pcaptest.$(O)
14  
-
15  
-tcpdump: tcpdump.go install
16  
-	$(GC) tcpdump.go
17  
-	$(LD) -o $@ tcpdump.$(O)
18  
-
19  
-pass: pass.go install
20  
-	$(GC) pass.go
21  
-	$(LD) -o $@ pass.$(O)
6  decode.go
@@ -3,9 +3,9 @@ package pcap
3 3
 import (
4 4
 	"fmt"
5 5
 	"net"
6  
-	"time"
7 6
 	"reflect"
8 7
 	"strings"
  8
+	"time"
9 9
 )
10 10
 
11 11
 const (
@@ -99,8 +99,8 @@ func (p *Packet) Decode() {
99 99
 
100 100
 // TimeString returns the packet time in a human-readable string.
101 101
 func (p *Packet) TimeString() string {
102  
-	t := time.SecondsToLocalTime(int64(p.Time.Sec))
103  
-	return fmt.Sprintf("%02d:%02d:%02d.%06d ", t.Hour, t.Minute, t.Second, p.Time.Usec)
  102
+	t := time.Unix(int64(p.Time.Sec), 0)
  103
+	return fmt.Sprintf("%02d:%02d:%02d.%06d ", t.Hour(), t.Minute(), t.Second(), p.Time.Usec)
104 104
 }
105 105
 
106 106
 func (p *Packet) headerString(headers []interface{}) string {
14  io.go
@@ -3,7 +3,6 @@ package pcap
3 3
 import (
4 4
 	"fmt"
5 5
 	"io"
6  
-	"os"
7 6
 )
8 7
 
9 8
 // FileHeader is the parsed header of a pcap file.
@@ -42,7 +41,7 @@ type Packet struct {
42 41
 type Reader struct {
43 42
 	flip         bool
44 43
 	buf          io.Reader
45  
-	err          os.Error
  44
+	err          error
46 45
 	fourBytes    []byte
47 46
 	twoBytes     []byte
48 47
 	sixteenBytes []byte
@@ -50,7 +49,7 @@ type Reader struct {
50 49
 }
51 50
 
52 51
 // NewReader reads pcap data from an os.Reader.
53  
-func NewReader(reader io.Reader) (*Reader, os.Error) {
  52
+func NewReader(reader io.Reader) (*Reader, error) {
54 53
 	r := &Reader{
55 54
 		buf:          reader,
56 55
 		fourBytes:    make([]byte, 4),
@@ -105,8 +104,8 @@ func (r *Reader) Next() *Packet {
105 104
 	}
106 105
 }
107 106
 
108  
-func (r *Reader) read(data []byte) os.Error {
109  
-	var err os.Error
  107
+func (r *Reader) read(data []byte) error {
  108
+	var err error
110 109
 	n, err := r.buf.Read(data)
111 110
 	for err == nil && n != len(data) {
112 111
 		var chunk int
@@ -146,7 +145,6 @@ func (r *Reader) readUint16() uint16 {
146 145
 	return asUint16(data, r.flip)
147 146
 }
148 147
 
149  
-
150 148
 // Writer writes a pcap file.
151 149
 type Writer struct {
152 150
 	writer io.Writer
@@ -155,7 +153,7 @@ type Writer struct {
155 153
 
156 154
 // NewWriter creates a Writer that stores output in an io.Writer.
157 155
 // The FileHeader is written immediately.
158  
-func NewWriter(writer io.Writer, header *FileHeader) (*Writer, os.Error) {
  156
+func NewWriter(writer io.Writer, header *FileHeader) (*Writer, error) {
159 157
 	w := &Writer{
160 158
 		writer: writer,
161 159
 		buf:    make([]byte, 24),
@@ -176,7 +174,7 @@ func NewWriter(writer io.Writer, header *FileHeader) (*Writer, os.Error) {
176 174
 }
177 175
 
178 176
 // Writer writes a packet to the underlying writer.
179  
-func (w *Writer) Write(pkt *Packet) os.Error {
  177
+func (w *Writer) Write(pkt *Packet) error {
180 178
 	e := encoder{w.buf}
181 179
 	e.put4(uint32(pkt.Time.Sec))
182 180
 	e.put4(uint32(pkt.Time.Usec))
30  pcap.go
@@ -4,13 +4,19 @@ package pcap
4 4
 #cgo LDFLAGS: -lpcap
5 5
 #include <stdlib.h>
6 6
 #include <pcap.h>
  7
+
  8
+// Workaround for not knowing how to cast to const u_char**
  9
+int hack_pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
  10
+                      u_char **pkt_data) {
  11
+    return pcap_next_ex(p, pkt_header, (const u_char **)pkt_data);
  12
+}
7 13
 */
8 14
 import "C"
9 15
 import (
10  
-	"unsafe"
11  
-	"os"
  16
+	"errors"
12 17
 	"net"
13 18
 	"syscall"
  19
+	"unsafe"
14 20
 )
15 21
 
16 22
 type Pcap struct {
@@ -87,12 +93,12 @@ func (p *Pcap) Next() (pkt *Packet) {
87 93
 }
88 94
 
89 95
 func (p *Pcap) NextEx() (pkt *Packet, result int32) {
90  
-	var pkthdr_ptr *_Ctype_struct_pcap_pkthdr
91  
-	var pkthdr _Ctype_struct_pcap_pkthdr
  96
+	var pkthdr_ptr *C.struct_pcap_pkthdr
  97
+	var pkthdr C.struct_pcap_pkthdr
92 98
 
93  
-	var buf_ptr *_Ctypedef_u_char
  99
+	var buf_ptr *C.u_char
94 100
 	var buf unsafe.Pointer
95  
-	result = int32(C.pcap_next_ex(p.cptr, &pkthdr_ptr, &buf_ptr))
  101
+	result = int32(C.hack_pcap_next_ex(p.cptr, &pkthdr_ptr, &buf_ptr))
96 102
 
97 103
 	buf = unsafe.Pointer(buf_ptr)
98 104
 	pkthdr = *pkthdr_ptr
@@ -188,7 +194,7 @@ func Findalldevs() (ifs []Interface, err string) {
188 194
 	var buf *C.char
189 195
 	buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1))
190 196
 	defer C.free(unsafe.Pointer(buf))
191  
-	var alldevsp *_Ctypedef_pcap_if_t
  197
+	var alldevsp *C.pcap_if_t
192 198
 
193 199
 	if -1 == C.pcap_findalldevs((**C.pcap_if_t)(&alldevsp), buf) {
194 200
 		return nil, C.GoString(buf)
@@ -196,12 +202,12 @@ func Findalldevs() (ifs []Interface, err string) {
196 202
 	defer C.pcap_freealldevs((*C.pcap_if_t)(alldevsp))
197 203
 	dev := alldevsp
198 204
 	var i uint32
199  
-	for i = 0; dev != nil; dev = (*_Ctypedef_pcap_if_t)(dev.next) {
  205
+	for i = 0; dev != nil; dev = (*C.pcap_if_t)(dev.next) {
200 206
 		i++
201 207
 	}
202 208
 	ifs = make([]Interface, i)
203 209
 	dev = alldevsp
204  
-	for j := uint32(0); dev != nil; dev = (*_Ctypedef_pcap_if_t)(dev.next) {
  210
+	for j := uint32(0); dev != nil; dev = (*C.pcap_if_t)(dev.next) {
205 211
 		var iface Interface
206 212
 		iface.Name = C.GoString(dev.name)
207 213
 		iface.Description = C.GoString(dev.description)
@@ -218,7 +224,7 @@ func findalladdresses(addresses *_Ctype_struct_pcap_addr) (retval []IFAddress) {
218 224
 	retval = make([]IFAddress, 0, 1)
219 225
 	for curaddr := addresses; curaddr != nil; curaddr = (*_Ctype_struct_pcap_addr)(curaddr.next) {
220 226
 		var a IFAddress
221  
-		var err os.Error
  227
+		var err error
222 228
 		a.IP, err = sockaddr_to_IP((*syscall.RawSockaddr)(unsafe.Pointer(curaddr.addr)))
223 229
 		if err != nil {
224 230
 			continue
@@ -232,7 +238,7 @@ func findalladdresses(addresses *_Ctype_struct_pcap_addr) (retval []IFAddress) {
232 238
 	return
233 239
 }
234 240
 
235  
-func sockaddr_to_IP(rsa *syscall.RawSockaddr) (IP []byte, err os.Error) {
  241
+func sockaddr_to_IP(rsa *syscall.RawSockaddr) (IP []byte, err error) {
236 242
 	switch rsa.Family {
237 243
 	case syscall.AF_INET:
238 244
 		pp := (*syscall.RawSockaddrInet4)(unsafe.Pointer(rsa))
@@ -249,7 +255,7 @@ func sockaddr_to_IP(rsa *syscall.RawSockaddr) (IP []byte, err os.Error) {
249 255
 		}
250 256
 		return
251 257
 	}
252  
-	err = os.NewError("Unsupported address type")
  258
+	err = errors.New("Unsupported address type")
253 259
 	return
254 260
 }
255 261
 
25  pass.go → tools/pass/pass.go
@@ -6,8 +6,10 @@ import (
6 6
 	"bufio"
7 7
 	"flag"
8 8
 	"fmt"
  9
+	"io"
9 10
 	"os"
10  
-	"pcap"
  11
+
  12
+	"github.com/dustin/gopcap"
11 13
 )
12 14
 
13 15
 var input *string = flag.String("input", "", "input file")
@@ -17,23 +19,25 @@ var decode *bool = flag.Bool("decode", false, "print decoded packets")
17 19
 func copyPcap(dest, src string) {
18 20
 	f, err := os.Open(src)
19 21
 	if err != nil {
20  
-		fmt.Printf("couldn't open %q: %v", src, err)
  22
+		fmt.Printf("couldn't open %q: %v\n", src, err)
21 23
 		return
22 24
 	}
  25
+	defer f.Close()
23 26
 	reader, err := pcap.NewReader(bufio.NewReader(f))
24 27
 	if err != nil {
25  
-		fmt.Printf("couldn't create reader: %v", err)
  28
+		fmt.Printf("couldn't create reader: %v\n", err)
26 29
 		return
27 30
 	}
28 31
 	w, err := os.Create(dest)
29 32
 	if err != nil {
30  
-		fmt.Printf("couldn't open %q: %v", dest, err)
  33
+		fmt.Printf("couldn't open %q: %v\n", dest, err)
31 34
 		return
32 35
 	}
  36
+	defer w.Close()
33 37
 	buf := bufio.NewWriter(w)
34 38
 	writer, err := pcap.NewWriter(buf, &reader.Header)
35 39
 	if err != nil {
36  
-		fmt.Printf("couldn't create writer: %v", err)
  40
+		fmt.Printf("couldn't create writer: %v\n", err)
37 41
 		return
38 42
 	}
39 43
 	for {
@@ -48,31 +52,30 @@ func copyPcap(dest, src string) {
48 52
 		writer.Write(pkt)
49 53
 	}
50 54
 	buf.Flush()
51  
-	w.Close()
52 55
 }
53 56
 
54 57
 func check(dest, src string) {
55 58
 	f, err := os.Open(src)
56  
-	defer f.Close()
57 59
 	if err != nil {
58  
-		fmt.Printf("couldn't open %q: %v", src, err)
  60
+		fmt.Printf("couldn't open %q: %v\n", src, err)
59 61
 		return
60 62
 	}
  63
+	defer f.Close()
61 64
 	freader := bufio.NewReader(f)
62 65
 
63 66
 	g, err := os.Open(dest)
64  
-	defer g.Close()
65 67
 	if err != nil {
66  
-		fmt.Printf("couldn't open %q: %v", src, err)
  68
+		fmt.Printf("couldn't open %q: %v\n", src, err)
67 69
 		return
68 70
 	}
  71
+	defer g.Close()
69 72
 	greader := bufio.NewReader(g)
70 73
 
71 74
 	for {
72 75
 		fb, ferr := freader.ReadByte()
73 76
 		gb, gerr := greader.ReadByte()
74 77
 
75  
-		if ferr == os.EOF && gerr == os.EOF {
  78
+		if ferr == io.EOF && gerr == io.EOF {
76 79
 			break
77 80
 		}
78 81
 		if fb == gb {
7  pcaptest.go → tools/pcaptest/pcaptest.go
... ...
@@ -1,10 +1,11 @@
1 1
 package main
2 2
 
3 3
 import (
4  
-	"pcap"
5  
-	"fmt"
6 4
 	"flag"
  5
+	"fmt"
7 6
 	"time"
  7
+
  8
+	"github.com/dustin/gopcap"
8 9
 )
9 10
 
10 11
 func min(x uint32, y uint32) uint32 {
@@ -63,7 +64,7 @@ func main() {
63 64
 	for pkt := h.Next(); pkt != nil; pkt = h.Next() {
64 65
 		fmt.Printf("time: %d.%06d (%s) caplen: %d len: %d\nData:",
65 66
 			int64(pkt.Time.Sec), int64(pkt.Time.Usec),
66  
-			time.SecondsToLocalTime(int64(pkt.Time.Sec)).String(), int64(pkt.Caplen), int64(pkt.Len))
  67
+			time.Unix(int64(pkt.Time.Sec), 0).String(), int64(pkt.Caplen), int64(pkt.Len))
67 68
 		for i := uint32(0); i < pkt.Caplen; i++ {
68 69
 			if i%32 == 0 {
69 70
 				fmt.Printf("\n")
7  tcpdump.go → tools/tcpdump/tcpdump.go
... ...
@@ -1,11 +1,12 @@
1 1
 package main
2 2
 
3 3
 import (
4  
-	"pcap"
5  
-	"fmt"
  4
+	"bufio"
6 5
 	"flag"
  6
+	"fmt"
7 7
 	"os"
8  
-	"bufio"
  8
+
  9
+	"github.com/dustin/gopcap"
9 10
 )
10 11
 
11 12
 const (
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.