Permalink
Browse files

Use time.Time instead of our own invention

  • Loading branch information...
1 parent 43544f4 commit 694900d5a67498e57ef36ad0ef6f88b379a60973 @miekg miekg committed Jul 11, 2012
Showing with 15 additions and 24 deletions.
  1. +3 −10 decode.go
  2. +10 −12 io.go
  3. +2 −2 pcap.go
View
@@ -6,7 +6,6 @@ import (
"net"
"reflect"
"strings"
- "time"
)
const (
@@ -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 {
@@ -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))
}
}
}
@@ -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.
View
22 io.go
@@ -4,6 +4,7 @@ import (
"encoding/binary"
"fmt"
"io"
+ "time"
)
// FileHeader is the parsed header of a pcap file.
@@ -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
@@ -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,
@@ -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)
View
@@ -17,6 +17,7 @@ import (
"net"
"syscall"
"unsafe"
+ "time"
)
type Pcap struct {
@@ -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)

0 comments on commit 694900d

Please sign in to comment.