Permalink
Browse files

Merge pull request #14 from miekg/master

use time.Time
  • Loading branch information...
2 parents ff016cc + 694900d commit db5c5182e52a50dbeb76dc2b968b27869075620d @akrennmair committed Jul 11, 2012
Showing with 14 additions and 24 deletions.
  1. +3 −10 decode.go
  2. +9 −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
21 io.go
@@ -34,10 +34,10 @@ func (p *PacketTime) Time() time.Time {
// Convenient access to IP, TCP, and UDP headers is provided after Decode()
// is called if the packet is of the appropriate type.
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
@@ -107,10 +107,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,
@@ -183,11 +180,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
@@ -18,6 +18,7 @@ import (
"net"
"syscall"
"unsafe"
+ "time"
)
type Pcap struct {
@@ -106,8 +107,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 db5c518

Please sign in to comment.