Permalink
Browse files

Merge branch 'master' of https://github.com/miekg/pcap into miekg-master

Conflicts:
	pcap.go
  • Loading branch information...
2 parents 73863ef + 43544f4 commit 5067da326bea06a56cfe2b84fe60935f76196151 @akrennmair committed May 8, 2012
Showing with 91 additions and 159 deletions.
  1. +5 −1 README → README.mkd
  2. +47 −78 decode.go
  3. +29 −58 io.go
  4. +10 −22 pcap.go
@@ -1,7 +1,11 @@
-gopcap
+# PCAP
This is a simple wrapper around libpcap for Go. Originally written by Andreas
Krennmair <ak@synflood.at> and only minorly touched up by Mark Smith <mark@qq.is>.
Please see the included pcaptest.go and tcpdump.go programs for instructions on
how to use this library.
+
+Miek Gieben <miek@miek.nl> has created a more Go-like package and replaced functionality
+with standard functions from the standard library. The package has also been renamed to
+pcap.
View
@@ -1,6 +1,7 @@
package pcap
import (
+ "encoding/binary"
"fmt"
"net"
"reflect"
@@ -22,7 +23,7 @@ const (
const (
ERRBUF_SIZE = 256
- // according to pcap-linktype(7)
+ // According to pcap-linktype(7).
LINKTYPE_NULL = 0
LINKTYPE_ETHERNET = 1
LINKTYPE_TOKEN_RING = 6
@@ -56,10 +57,6 @@ type addrHdr interface {
Len() int
}
-type stringer interface {
- String() string
-}
-
type addrStringer interface {
String(addr addrHdr) string
}
@@ -72,17 +69,9 @@ func decodemac(pkt []byte) uint64 {
return mac
}
-func decodeuint16(pkt []byte) uint16 {
- return uint16(pkt[0])<<8 + uint16(pkt[1])
-}
-
-func decodeuint32(pkt []byte) uint32 {
- return uint32(pkt[0])<<24 + uint32(pkt[1])<<16 + uint32(pkt[2])<<8 + uint32(pkt[3])
-}
-
-// Decode decodes the headers of a packet.
+// Decode decodes the headers of a Packet.
func (p *Packet) Decode() {
- p.Type = int(decodeuint16(p.Data[12:14]))
+ p.Type = int(binary.BigEndian.Uint16(p.Data[12:14]))
p.DestMac = decodemac(p.Data[0:6])
p.SrcMac = decodemac(p.Data[6:12])
p.Payload = p.Data[14:]
@@ -106,7 +95,7 @@ func (p *Packet) TimeString() string {
func (p *Packet) headerString(headers []interface{}) string {
// If there's just one header, return that.
if len(headers) == 1 {
- if hdr, ok := headers[0].(stringer); ok {
+ if hdr, ok := headers[0].(fmt.Stringer); ok {
return hdr.String()
}
}
@@ -147,6 +136,7 @@ func (p *Packet) String() string {
return fmt.Sprintf("%s %s", p.TimeString(), p.headerString(p.Headers))
}
+// Arphdr is a ARP packet header.
type Arphdr struct {
Addrtype uint16
Protocol uint16
@@ -159,36 +149,31 @@ type Arphdr struct {
DestProtAddress []byte
}
-func Arpop(op uint16) string {
- switch op {
+func (arp *Arphdr) String() (s string) {
+ switch arp.Operation {
case 1:
- return "Request"
+ s = "ARP request"
case 2:
- return "Reply"
+ s = "ARP Reply"
}
- return ""
-}
-
-func (arp *Arphdr) String() string {
- result := fmt.Sprintf("ARP %s ", Arpop(arp.Operation))
if arp.Addrtype == LINKTYPE_ETHERNET && arp.Protocol == TYPE_IP {
- result = fmt.Sprintf("%012x (%s) > %012x (%s)",
+ s = fmt.Sprintf("%012x (%s) > %012x (%s)",
decodemac(arp.SourceHwAddress), arp.SourceProtAddress,
decodemac(arp.DestHwAddress), arp.DestProtAddress)
} else {
- result = fmt.Sprintf("addrtype = %d protocol = %d", arp.Addrtype, arp.Protocol)
+ s = fmt.Sprintf("addrtype = %d protocol = %d", arp.Addrtype, arp.Protocol)
}
- return result
+ return
}
func (p *Packet) decodeArp() {
pkt := p.Payload
arp := new(Arphdr)
- arp.Addrtype = decodeuint16(pkt[0:2])
- arp.Protocol = decodeuint16(pkt[2:4])
+ arp.Addrtype = binary.BigEndian.Uint16(pkt[0:2])
+ arp.Protocol = binary.BigEndian.Uint16(pkt[2:4])
arp.HwAddressSize = pkt[4]
arp.ProtAddressSize = pkt[5]
- arp.Operation = decodeuint16(pkt[6:8])
+ arp.Operation = binary.BigEndian.Uint16(pkt[6:8])
arp.SourceHwAddress = pkt[8 : 8+arp.HwAddressSize]
arp.SourceProtAddress = pkt[8+arp.HwAddressSize : 8+arp.HwAddressSize+arp.ProtAddressSize]
arp.DestHwAddress = pkt[8+arp.HwAddressSize+arp.ProtAddressSize : 8+2*arp.HwAddressSize+arp.ProtAddressSize]
@@ -198,6 +183,7 @@ func (p *Packet) decodeArp() {
p.Payload = p.Payload[8+2*arp.HwAddressSize+2*arp.ProtAddressSize:]
}
+// IPadr is the header of an IP packet.
type Iphdr struct {
Version uint8
Ihl uint8
@@ -220,14 +206,14 @@ func (p *Packet) decodeIp() {
ip.Version = uint8(pkt[0]) >> 4
ip.Ihl = uint8(pkt[0]) & 0x0F
ip.Tos = pkt[1]
- ip.Length = decodeuint16(pkt[2:4])
- ip.Id = decodeuint16(pkt[4:6])
- flagsfrags := decodeuint16(pkt[6:8])
+ ip.Length = binary.BigEndian.Uint16(pkt[2:4])
+ ip.Id = binary.BigEndian.Uint16(pkt[4:6])
+ flagsfrags := binary.BigEndian.Uint16(pkt[6:8])
ip.Flags = uint8(flagsfrags >> 13)
ip.FragOffset = flagsfrags & 0x1FFF
ip.Ttl = pkt[8]
ip.Protocol = pkt[9]
- ip.Checksum = decodeuint16(pkt[10:12])
+ ip.Checksum = binary.BigEndian.Uint16(pkt[10:12])
ip.SrcIp = pkt[12:16]
ip.DestIp = pkt[16:20]
pEnd := int(ip.Length)
@@ -250,17 +236,9 @@ func (p *Packet) decodeIp() {
}
}
-func (ip *Iphdr) SrcAddr() string {
- return net.IP(ip.SrcIp).String()
-}
-
-func (ip *Iphdr) DestAddr() string {
- return net.IP(ip.DestIp).String()
-}
-
-func (ip *Iphdr) Len() int {
- return int(ip.Length)
-}
+func (ip *Iphdr) SrcAddr() string { return net.IP(ip.SrcIp).String() }
+func (ip *Iphdr) DestAddr() string { return net.IP(ip.DestIp).String() }
+func (ip *Iphdr) Len() int { return int(ip.Length) }
type Tcphdr struct {
SrcPort uint16
@@ -290,15 +268,15 @@ const (
func (p *Packet) decodeTcp() {
pkt := p.Payload
tcp := new(Tcphdr)
- tcp.SrcPort = decodeuint16(pkt[0:2])
- tcp.DestPort = decodeuint16(pkt[2:4])
- tcp.Seq = decodeuint32(pkt[4:8])
- tcp.Ack = decodeuint32(pkt[8:12])
+ tcp.SrcPort = binary.BigEndian.Uint16(pkt[0:2])
+ tcp.DestPort = binary.BigEndian.Uint16(pkt[2:4])
+ tcp.Seq = binary.BigEndian.Uint32(pkt[4:8])
+ tcp.Ack = binary.BigEndian.Uint32(pkt[8:12])
tcp.DataOffset = (pkt[12] & 0xF0) >> 4
- tcp.Flags = uint16(decodeuint16(pkt[12:14]) & 0x1FF)
- tcp.Window = decodeuint16(pkt[14:16])
- tcp.Checksum = decodeuint16(pkt[16:18])
- tcp.Urgent = decodeuint16(pkt[18:20])
+ tcp.Flags = binary.BigEndian.Uint16(pkt[12:14]) & 0x1FF
+ tcp.Window = binary.BigEndian.Uint16(pkt[14:16])
+ tcp.Checksum = binary.BigEndian.Uint16(pkt[16:18])
+ tcp.Urgent = binary.BigEndian.Uint16(pkt[18:20])
p.Payload = pkt[tcp.DataOffset*4:]
p.Headers = append(p.Headers, tcp)
p.TCP = tcp
@@ -352,10 +330,10 @@ type Udphdr struct {
func (p *Packet) decodeUdp() {
pkt := p.Payload
udp := new(Udphdr)
- udp.SrcPort = decodeuint16(pkt[0:2])
- udp.DestPort = decodeuint16(pkt[2:4])
- udp.Length = decodeuint16(pkt[4:6])
- udp.Checksum = decodeuint16(pkt[6:8])
+ udp.SrcPort = binary.BigEndian.Uint16(pkt[0:2])
+ udp.DestPort = binary.BigEndian.Uint16(pkt[2:4])
+ udp.Length = binary.BigEndian.Uint16(pkt[4:6])
+ udp.Checksum = binary.BigEndian.Uint16(pkt[6:8])
p.Headers = append(p.Headers, udp)
p.UDP = udp
p.Payload = pkt[8:]
@@ -381,9 +359,9 @@ func (p *Packet) decodeIcmp() *Icmphdr {
icmp := new(Icmphdr)
icmp.Type = pkt[0]
icmp.Code = pkt[1]
- icmp.Checksum = decodeuint16(pkt[2:4])
- icmp.Id = decodeuint16(pkt[4:6])
- icmp.Seq = decodeuint16(pkt[6:8])
+ icmp.Checksum = binary.BigEndian.Uint16(pkt[2:4])
+ icmp.Id = binary.BigEndian.Uint16(pkt[4:6])
+ icmp.Seq = binary.BigEndian.Uint16(pkt[6:8])
p.Payload = pkt[8:]
p.Headers = append(p.Headers, icmp)
return icmp
@@ -394,8 +372,7 @@ func (icmp *Icmphdr) String(hdr addrHdr) string {
hdr.SrcAddr(), hdr.DestAddr(), icmp.Type, icmp.Code)
}
-func (icmp *Icmphdr) TypeString() string {
- result := ""
+func (icmp *Icmphdr) TypeString() (result string) {
switch icmp.Type {
case 0:
result = fmt.Sprintf("Echo reply seq=%d", icmp.Seq)
@@ -417,7 +394,7 @@ func (icmp *Icmphdr) TypeString() string {
case 30:
result = "Traceroute"
}
- return result
+ return
}
type Ip6hdr struct {
@@ -436,9 +413,9 @@ func (p *Packet) decodeIp6() {
pkt := p.Payload
ip6 := new(Ip6hdr)
ip6.Version = uint8(pkt[0]) >> 4
- ip6.TrafficClass = uint8((decodeuint16(pkt[0:2]) >> 4) & 0x00FF)
- ip6.FlowLabel = decodeuint32(pkt[0:4]) & 0x000FFFFF
- ip6.Length = decodeuint16(pkt[4:6])
+ ip6.TrafficClass = uint8((binary.BigEndian.Uint16(pkt[0:2]) >> 4) & 0x00FF)
+ ip6.FlowLabel = binary.BigEndian.Uint32(pkt[0:4]) & 0x000FFFFF
+ ip6.Length = binary.BigEndian.Uint16(pkt[4:6])
ip6.NextHeader = pkt[6]
ip6.HopLimit = pkt[7]
ip6.SrcIp = pkt[8:24]
@@ -458,14 +435,6 @@ func (p *Packet) decodeIp6() {
}
}
-func (ip6 *Ip6hdr) SrcAddr() string {
- return net.IP(ip6.SrcIp).String()
-}
-
-func (ip6 *Ip6hdr) DestAddr() string {
- return net.IP(ip6.DestIp).String()
-}
-
-func (ip6 *Ip6hdr) Len() int {
- return int(ip6.Length)
-}
+func (ip6 *Ip6hdr) SrcAddr() string { return net.IP(ip6.SrcIp).String() }
+func (ip6 *Ip6hdr) DestAddr() string { return net.IP(ip6.DestIp).String() }
+func (ip6 *Ip6hdr) Len() int { return int(ip6.Length) }
Oops, something went wrong.

0 comments on commit 5067da3

Please sign in to comment.