Permalink
Browse files

Merge pull request #10 from dustin/master

Updated for weekly.2012-02-07
  • Loading branch information...
2 parents fa43a2a + b012e23 commit 02a6a132d5e937dbf9cc3ebdf33390e4da21d4cd @akrennmair committed Feb 16, 2012
Showing with 54 additions and 61 deletions.
  1. +5 −0 .gitignore
  2. +0 −21 Makefile
  3. +3 −3 decode.go
  4. +6 −8 io.go
  5. +18 −12 pcap.go
  6. +14 −11 { → tools/pass}/pass.go
  7. +4 −3 { → tools/pcaptest}/pcaptest.go
  8. +4 −3 { → tools/tcpdump}/tcpdump.go
View
5 .gitignore
@@ -0,0 +1,5 @@
+#*
+*~
+/tools/pass/pass
+/tools/pcaptest/pcaptest
+/tools/tcpdump/tcpdump
View
21 Makefile
@@ -1,21 +0,0 @@
-include $(GOROOT)/src/Make.inc
-
-TARG=pcap
-GOFILES=decode.go\
- io.go
-CGOFILES=pcap.go
-CLEANFILES=pcaptest tcpdump
-
-include $(GOROOT)/src/Make.pkg
-
-pcaptest: pcaptest.go install
- $(GC) pcaptest.go
- $(LD) -o $@ pcaptest.$(O)
-
-tcpdump: tcpdump.go install
- $(GC) tcpdump.go
- $(LD) -o $@ tcpdump.$(O)
-
-pass: pass.go install
- $(GC) pass.go
- $(LD) -o $@ pass.$(O)
View
6 decode.go
@@ -3,9 +3,9 @@ package pcap
import (
"fmt"
"net"
- "time"
"reflect"
"strings"
+ "time"
)
const (
@@ -99,8 +99,8 @@ func (p *Packet) Decode() {
// TimeString returns the packet time in a human-readable string.
func (p *Packet) TimeString() string {
- t := time.SecondsToLocalTime(int64(p.Time.Sec))
- return fmt.Sprintf("%02d:%02d:%02d.%06d ", t.Hour, t.Minute, t.Second, p.Time.Usec)
+ t := time.Unix(int64(p.Time.Sec), 0)
+ return fmt.Sprintf("%02d:%02d:%02d.%06d ", t.Hour(), t.Minute(), t.Second(), p.Time.Usec)
}
func (p *Packet) headerString(headers []interface{}) string {
View
14 io.go
@@ -3,7 +3,6 @@ package pcap
import (
"fmt"
"io"
- "os"
)
// FileHeader is the parsed header of a pcap file.
@@ -42,15 +41,15 @@ type Packet struct {
type Reader struct {
flip bool
buf io.Reader
- err os.Error
+ err error
fourBytes []byte
twoBytes []byte
sixteenBytes []byte
Header FileHeader
}
// NewReader reads pcap data from an os.Reader.
-func NewReader(reader io.Reader) (*Reader, os.Error) {
+func NewReader(reader io.Reader) (*Reader, error) {
r := &Reader{
buf: reader,
fourBytes: make([]byte, 4),
@@ -105,8 +104,8 @@ func (r *Reader) Next() *Packet {
}
}
-func (r *Reader) read(data []byte) os.Error {
- var err os.Error
+func (r *Reader) read(data []byte) error {
+ var err error
n, err := r.buf.Read(data)
for err == nil && n != len(data) {
var chunk int
@@ -146,7 +145,6 @@ func (r *Reader) readUint16() uint16 {
return asUint16(data, r.flip)
}
-
// Writer writes a pcap file.
type Writer struct {
writer io.Writer
@@ -155,7 +153,7 @@ type Writer struct {
// NewWriter creates a Writer that stores output in an io.Writer.
// The FileHeader is written immediately.
-func NewWriter(writer io.Writer, header *FileHeader) (*Writer, os.Error) {
+func NewWriter(writer io.Writer, header *FileHeader) (*Writer, error) {
w := &Writer{
writer: writer,
buf: make([]byte, 24),
@@ -176,7 +174,7 @@ func NewWriter(writer io.Writer, header *FileHeader) (*Writer, os.Error) {
}
// Writer writes a packet to the underlying writer.
-func (w *Writer) Write(pkt *Packet) os.Error {
+func (w *Writer) Write(pkt *Packet) error {
e := encoder{w.buf}
e.put4(uint32(pkt.Time.Sec))
e.put4(uint32(pkt.Time.Usec))
View
30 pcap.go
@@ -4,13 +4,19 @@ package pcap
#cgo LDFLAGS: -lpcap
#include <stdlib.h>
#include <pcap.h>
+
+// Workaround for not knowing how to cast to const u_char**
+int hack_pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
+ u_char **pkt_data) {
+ return pcap_next_ex(p, pkt_header, (const u_char **)pkt_data);
+}
*/
import "C"
import (
- "unsafe"
- "os"
+ "errors"
"net"
"syscall"
+ "unsafe"
)
type Pcap struct {
@@ -87,12 +93,12 @@ func (p *Pcap) Next() (pkt *Packet) {
}
func (p *Pcap) NextEx() (pkt *Packet, result int32) {
- var pkthdr_ptr *_Ctype_struct_pcap_pkthdr
- var pkthdr _Ctype_struct_pcap_pkthdr
+ var pkthdr_ptr *C.struct_pcap_pkthdr
+ var pkthdr C.struct_pcap_pkthdr
- var buf_ptr *_Ctypedef_u_char
+ var buf_ptr *C.u_char
var buf unsafe.Pointer
- result = int32(C.pcap_next_ex(p.cptr, &pkthdr_ptr, &buf_ptr))
+ result = int32(C.hack_pcap_next_ex(p.cptr, &pkthdr_ptr, &buf_ptr))
buf = unsafe.Pointer(buf_ptr)
pkthdr = *pkthdr_ptr
@@ -188,20 +194,20 @@ func Findalldevs() (ifs []Interface, err string) {
var buf *C.char
buf = (*C.char)(C.calloc(ERRBUF_SIZE, 1))
defer C.free(unsafe.Pointer(buf))
- var alldevsp *_Ctypedef_pcap_if_t
+ var alldevsp *C.pcap_if_t
if -1 == C.pcap_findalldevs((**C.pcap_if_t)(&alldevsp), buf) {
return nil, C.GoString(buf)
}
defer C.pcap_freealldevs((*C.pcap_if_t)(alldevsp))
dev := alldevsp
var i uint32
- for i = 0; dev != nil; dev = (*_Ctypedef_pcap_if_t)(dev.next) {
+ for i = 0; dev != nil; dev = (*C.pcap_if_t)(dev.next) {
i++
}
ifs = make([]Interface, i)
dev = alldevsp
- for j := uint32(0); dev != nil; dev = (*_Ctypedef_pcap_if_t)(dev.next) {
+ for j := uint32(0); dev != nil; dev = (*C.pcap_if_t)(dev.next) {
var iface Interface
iface.Name = C.GoString(dev.name)
iface.Description = C.GoString(dev.description)
@@ -218,7 +224,7 @@ func findalladdresses(addresses *_Ctype_struct_pcap_addr) (retval []IFAddress) {
retval = make([]IFAddress, 0, 1)
for curaddr := addresses; curaddr != nil; curaddr = (*_Ctype_struct_pcap_addr)(curaddr.next) {
var a IFAddress
- var err os.Error
+ var err error
a.IP, err = sockaddr_to_IP((*syscall.RawSockaddr)(unsafe.Pointer(curaddr.addr)))
if err != nil {
continue
@@ -232,7 +238,7 @@ func findalladdresses(addresses *_Ctype_struct_pcap_addr) (retval []IFAddress) {
return
}
-func sockaddr_to_IP(rsa *syscall.RawSockaddr) (IP []byte, err os.Error) {
+func sockaddr_to_IP(rsa *syscall.RawSockaddr) (IP []byte, err error) {
switch rsa.Family {
case syscall.AF_INET:
pp := (*syscall.RawSockaddrInet4)(unsafe.Pointer(rsa))
@@ -249,7 +255,7 @@ func sockaddr_to_IP(rsa *syscall.RawSockaddr) (IP []byte, err os.Error) {
}
return
}
- err = os.NewError("Unsupported address type")
+ err = errors.New("Unsupported address type")
return
}
View
25 pass.go → tools/pass/pass.go
@@ -6,8 +6,10 @@ import (
"bufio"
"flag"
"fmt"
+ "io"
"os"
- "pcap"
+
+ "github.com/dustin/gopcap"
)
var input *string = flag.String("input", "", "input file")
@@ -17,23 +19,25 @@ var decode *bool = flag.Bool("decode", false, "print decoded packets")
func copyPcap(dest, src string) {
f, err := os.Open(src)
if err != nil {
- fmt.Printf("couldn't open %q: %v", src, err)
+ fmt.Printf("couldn't open %q: %v\n", src, err)
return
}
+ defer f.Close()
reader, err := pcap.NewReader(bufio.NewReader(f))
if err != nil {
- fmt.Printf("couldn't create reader: %v", err)
+ fmt.Printf("couldn't create reader: %v\n", err)
return
}
w, err := os.Create(dest)
if err != nil {
- fmt.Printf("couldn't open %q: %v", dest, err)
+ fmt.Printf("couldn't open %q: %v\n", dest, err)
return
}
+ defer w.Close()
buf := bufio.NewWriter(w)
writer, err := pcap.NewWriter(buf, &reader.Header)
if err != nil {
- fmt.Printf("couldn't create writer: %v", err)
+ fmt.Printf("couldn't create writer: %v\n", err)
return
}
for {
@@ -48,31 +52,30 @@ func copyPcap(dest, src string) {
writer.Write(pkt)
}
buf.Flush()
- w.Close()
}
func check(dest, src string) {
f, err := os.Open(src)
- defer f.Close()
if err != nil {
- fmt.Printf("couldn't open %q: %v", src, err)
+ fmt.Printf("couldn't open %q: %v\n", src, err)
return
}
+ defer f.Close()
freader := bufio.NewReader(f)
g, err := os.Open(dest)
- defer g.Close()
if err != nil {
- fmt.Printf("couldn't open %q: %v", src, err)
+ fmt.Printf("couldn't open %q: %v\n", src, err)
return
}
+ defer g.Close()
greader := bufio.NewReader(g)
for {
fb, ferr := freader.ReadByte()
gb, gerr := greader.ReadByte()
- if ferr == os.EOF && gerr == os.EOF {
+ if ferr == io.EOF && gerr == io.EOF {
break
}
if fb == gb {
View
7 pcaptest.go → tools/pcaptest/pcaptest.go
@@ -1,10 +1,11 @@
package main
import (
- "pcap"
- "fmt"
"flag"
+ "fmt"
"time"
+
+ "github.com/dustin/gopcap"
)
func min(x uint32, y uint32) uint32 {
@@ -63,7 +64,7 @@ func main() {
for pkt := h.Next(); pkt != nil; pkt = h.Next() {
fmt.Printf("time: %d.%06d (%s) caplen: %d len: %d\nData:",
int64(pkt.Time.Sec), int64(pkt.Time.Usec),
- time.SecondsToLocalTime(int64(pkt.Time.Sec)).String(), int64(pkt.Caplen), int64(pkt.Len))
+ time.Unix(int64(pkt.Time.Sec), 0).String(), int64(pkt.Caplen), int64(pkt.Len))
for i := uint32(0); i < pkt.Caplen; i++ {
if i%32 == 0 {
fmt.Printf("\n")
View
7 tcpdump.go → tools/tcpdump/tcpdump.go
@@ -1,11 +1,12 @@
package main
import (
- "pcap"
- "fmt"
+ "bufio"
"flag"
+ "fmt"
"os"
- "bufio"
+
+ "github.com/dustin/gopcap"
)
const (

0 comments on commit 02a6a13

Please sign in to comment.