Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated for weekly.2012-02-07 +52ba9506bd99

  • Loading branch information...
commit a0bd9dbd0224b74ba40f9fa6e0fcfb17d74d9c85 1 parent fa43a2a
@dustin dustin authored
Showing with 27 additions and 23 deletions.
  1. +3 −3 decode.go
  2. +6 −8 io.go
  3. +18 −12 pcap.go
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,7 +41,7 @@ type Packet struct {
type Reader struct {
flip bool
buf io.Reader
- err os.Error
+ err error
fourBytes []byte
twoBytes []byte
sixteenBytes []byte
@@ -50,7 +49,7 @@ type Reader struct {
}
// 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,7 +194,7 @@ 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)
@@ -196,12 +202,12 @@ func Findalldevs() (ifs []Interface, err string) {
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
}
Please sign in to comment.
Something went wrong with that request. Please try again.