Skip to content

Commit

Permalink
Use time.Time instead of our own invention
Browse files Browse the repository at this point in the history
  • Loading branch information
miekg committed Jul 11, 2012
1 parent 43544f4 commit 694900d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
13 changes: 3 additions & 10 deletions decode.go
Expand Up @@ -6,7 +6,6 @@ import (
"net"
"reflect"
"strings"
"time"
)

const (
Expand Down Expand Up @@ -86,12 +85,6 @@ func (p *Packet) Decode() {
}
}

// TimeString returns the packet time in a human-readable string.
func (p *Packet) TimeString() string {
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 {
// If there's just one header, return that.
if len(headers) == 1 {
Expand All @@ -104,7 +97,7 @@ func (p *Packet) headerString(headers []interface{}) string {
// Commonly the first header is an address.
if addr, ok := p.Headers[0].(addrHdr); ok {
if hdr, ok := p.Headers[1].(addrStringer); ok {
return fmt.Sprintf("%s %s", p.TimeString(), hdr.String(addr))
return fmt.Sprintf("%s %s", p.Time, hdr.String(addr))
}
}
}
Expand All @@ -131,9 +124,9 @@ func (p *Packet) headerString(headers []interface{}) string {
func (p *Packet) String() string {
// If there are no headers, print "unsupported protocol".
if len(p.Headers) == 0 {
return fmt.Sprintf("%s unsupported protocol %d", p.TimeString(), int(p.Type))
return fmt.Sprintf("%s unsupported protocol %d", p.Time, int(p.Type))
}
return fmt.Sprintf("%s %s", p.TimeString(), p.headerString(p.Headers))
return fmt.Sprintf("%s %s", p.Time, p.headerString(p.Headers))
}

// Arphdr is a ARP packet header.
Expand Down
22 changes: 10 additions & 12 deletions io.go
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
"fmt"
"io"
"time"
)

// FileHeader is the parsed header of a pcap file.
Expand All @@ -25,10 +26,10 @@ type PacketTime struct {

// Packet is a single packet parsed from a pcap file.
type Packet struct {
Time PacketTime // packet send/receive time
Caplen uint32 // bytes stored in the file (caplen <= len)
Len uint32 // bytes sent/received
Data []byte // packet data
Time time.Time // packet send/receive time
Caplen uint32 // bytes stored in the file (caplen <= len)
Len uint32 // bytes sent/received
Data []byte // packet data

Type int // protocol type, see LINKTYPE_*
DestMac uint64
Expand Down Expand Up @@ -94,10 +95,7 @@ func (r *Reader) Next() *Packet {
return nil
}
return &Packet{
Time: PacketTime{
Sec: int32(timeSec),
Usec: int32(timeUsec),
},
Time: time.Unix(int64(timeSec), int64(timeUsec)),
Caplen: capLen,
Len: origLen,
Data: data,
Expand Down Expand Up @@ -170,11 +168,11 @@ func NewWriter(writer io.Writer, header *FileHeader) (*Writer, error) {

// Writer writes a packet to the underlying writer.
func (w *Writer) Write(pkt *Packet) error {
binary.LittleEndian.PutUint32(w.buf, uint32(pkt.Time.Sec))
binary.LittleEndian.PutUint32(w.buf[4:], uint32(pkt.Time.Usec))
binary.LittleEndian.PutUint32(w.buf[8:], uint32(pkt.Time.Sec))
binary.LittleEndian.PutUint32(w.buf, uint32(pkt.Time.Unix()))
binary.LittleEndian.PutUint32(w.buf[4:], uint32(pkt.Time.Nanosecond()))
binary.LittleEndian.PutUint32(w.buf[8:], uint32(pkt.Time.Unix()))
binary.LittleEndian.PutUint32(w.buf[12:], pkt.Len)
if _, err := w.writer.Write(w.buf[:16]); err !=nil {
if _, err := w.writer.Write(w.buf[:16]); err != nil {
return err
}
_, err := w.writer.Write(pkt.Data)
Expand Down
4 changes: 2 additions & 2 deletions pcap.go
Expand Up @@ -17,6 +17,7 @@ import (
"net"
"syscall"
"unsafe"
"time"
)

type Pcap struct {
Expand Down Expand Up @@ -109,8 +110,7 @@ func (p *Pcap) NextEx() (pkt *Packet, result int32) {
return
}
pkt = new(Packet)
pkt.Time.Sec = int32(pkthdr.ts.tv_sec)
pkt.Time.Usec = int32(pkthdr.ts.tv_usec)
pkt.Time = time.Unix(int64(pkthdr.ts.tv_sec), int64(pkthdr.ts.tv_usec))
pkt.Caplen = uint32(pkthdr.caplen)
pkt.Len = uint32(pkthdr.len)
pkt.Data = make([]byte, pkthdr.caplen)
Expand Down

0 comments on commit 694900d

Please sign in to comment.