本文整理汇总了Golang中github.com/google/gopacket/layers.TCPPort函数的典型用法代码示例。如果您正苦于以下问题:Golang TCPPort函数的具体用法?Golang TCPPort怎么用?Golang TCPPort使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了TCPPort函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: forgeEthIPTCP
func forgeEthIPTCP(t *testing.T, seed int64) *gopacket.Packet {
var options gopacket.SerializeOptions
rnd := rand.New(rand.NewSource(seed))
rawBytes := []byte{10, 20, 30}
ethernetLayer := &layers.Ethernet{
SrcMAC: net.HardwareAddr{0x00, 0x0F, 0xAA, 0xFA, 0xAA, byte(rnd.Intn(0x100))},
DstMAC: net.HardwareAddr{0x00, 0x0D, 0xBD, 0xBD, byte(rnd.Intn(0x100)), 0xBD},
}
ipLayer := &layers.IPv4{
SrcIP: net.IP{127, 0, 0, byte(rnd.Intn(0x100))},
DstIP: net.IP{byte(rnd.Intn(0x100)), 8, 8, 8},
}
tcpLayer := &layers.TCP{
SrcPort: layers.TCPPort(byte(rnd.Intn(0x10000))),
DstPort: layers.TCPPort(byte(rnd.Intn(0x10000))),
}
// And create the packet with the layers
buffer := gopacket.NewSerializeBuffer()
err := gopacket.SerializeLayers(buffer, options,
ethernetLayer,
ipLayer,
tcpLayer,
gopacket.Payload(rawBytes),
)
if err != nil {
t.Fail()
}
gpacket := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, gopacket.Default)
return &gpacket
}
示例2: forgeTestPacket
/* protos must contain a UDP or TCP layer on top of IPv4 */
func forgeTestPacket(t *testing.T, seed int64, protos ...ProtocolType) *gopacket.Packet {
rnd := rand.New(rand.NewSource(seed))
rawBytes := []byte{10, 20, 30}
var protoStack []gopacket.SerializableLayer
for i, proto := range protos {
switch proto {
case ETH:
ethernetLayer := &layers.Ethernet{
SrcMAC: net.HardwareAddr{0x00, 0x0F, 0xAA, 0xFA, 0xAA, byte(rnd.Intn(0x100))},
DstMAC: net.HardwareAddr{0x00, 0x0D, 0xBD, 0xBD, byte(rnd.Intn(0x100)), 0xBD},
EthernetType: layers.EthernetTypeIPv4,
}
protoStack = append(protoStack, ethernetLayer)
case IPv4:
ipv4Layer := &layers.IPv4{
SrcIP: net.IP{127, 0, 0, byte(rnd.Intn(0x100))},
DstIP: net.IP{byte(rnd.Intn(0x100)), 8, 8, 8},
}
switch protos[i+1] {
case TCP:
ipv4Layer.Protocol = layers.IPProtocolTCP
case UDP:
ipv4Layer.Protocol = layers.IPProtocolUDP
}
protoStack = append(protoStack, ipv4Layer)
case TCP:
tcpLayer := &layers.TCP{
SrcPort: layers.TCPPort(byte(rnd.Intn(0x10000))),
DstPort: layers.TCPPort(byte(rnd.Intn(0x10000))),
}
protoStack = append(protoStack, tcpLayer)
case UDP:
udpLayer := &layers.UDP{
SrcPort: layers.UDPPort(byte(rnd.Intn(0x10000))),
DstPort: layers.UDPPort(byte(rnd.Intn(0x10000))),
}
protoStack = append(protoStack, udpLayer)
default:
t.Log("forgeTestPacket : Unsupported protocol ", proto)
}
}
protoStack = append(protoStack, gopacket.Payload(rawBytes))
buffer := gopacket.NewSerializeBuffer()
options := gopacket.SerializeOptions{FixLengths: true}
err := gopacket.SerializeLayers(buffer, options, protoStack...)
if err != nil {
t.Fail()
}
gpacket := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, gopacket.Default)
return &gpacket
}
示例3: Hash
func (fl *FlowLayer) Hash() []byte {
if fl == nil {
return []byte{}
}
if fl.Protocol == FlowProtocol_ETHERNET {
amac, err := net.ParseMAC(fl.A)
if err != nil {
panic(err)
}
bmac, err := net.ParseMAC(fl.B)
if err != nil {
panic(err)
}
return HashFromValues(amac, bmac)
}
if fl.Protocol == FlowProtocol_IPV4 || fl.Protocol == FlowProtocol_IPV6 {
aip := net.ParseIP(fl.A)
bip := net.ParseIP(fl.B)
return HashFromValues(aip, bip)
}
if fl.Protocol == FlowProtocol_TCPPORT {
aTCPPort, err := strconv.ParseUint(fl.A, 10, 16)
if err != nil {
panic(err)
}
bTCPPort, err := strconv.ParseUint(fl.B, 10, 16)
if err != nil {
panic(err)
}
return HashFromValues(layers.TCPPort(aTCPPort), layers.TCPPort(bTCPPort))
}
if fl.Protocol == FlowProtocol_UDPPORT {
aUDPPort, err := strconv.ParseUint(fl.A, 10, 16)
if err != nil {
panic(err)
}
bUDPPort, err := strconv.ParseUint(fl.B, 10, 16)
if err != nil {
panic(err)
}
return HashFromValues(layers.UDPPort(aUDPPort), layers.UDPPort(bUDPPort))
}
if fl.Protocol == FlowProtocol_SCTPPORT {
aSCTPPort, err := strconv.ParseUint(fl.A, 10, 16)
if err != nil {
panic(err)
}
bSCTPPort, err := strconv.ParseUint(fl.B, 10, 16)
if err != nil {
panic(err)
}
return HashFromValues(layers.SCTPPort(aSCTPPort), layers.SCTPPort(bSCTPPort))
}
return nil
}
示例4: main
func main() {
// Open device
handle, err = pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
if err != nil {
log.Fatal(err)
}
defer handle.Close()
// Send raw bytes over wire
rawBytes := []byte{10, 20, 30}
err = handle.WritePacketData(rawBytes)
if err != nil {
log.Fatal(err)
}
// Create a properly formed packet, just with
// empty details. Should fill out MAC addresses,
// IP addresses, etc.
buffer = gopacket.NewSerializeBuffer()
gopacket.SerializeLayers(buffer, options,
&layers.Ethernet{},
&layers.IPv4{},
&layers.TCP{},
gopacket.Payload(rawBytes),
)
outgoingPacket := buffer.Bytes()
// Send our packet
err = handle.WritePacketData(outgoingPacket)
if err != nil {
log.Fatal(err)
}
// This time lets fill out some information
ipLayer := &layers.IPv4{
SrcIP: net.IP{127, 0, 0, 1},
DstIP: net.IP{8, 8, 8, 8},
}
ethernetLayer := &layers.Ethernet{
SrcMAC: net.HardwareAddr{0xFF, 0xAA, 0xFA, 0xAA, 0xFF, 0xAA, 0xFA, 0xAA},
DstMAC: net.HardwareAddr{0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD},
}
tcpLayer := &layers.TCP{
SrcPort: layers.TCPPort(4321),
DstPort: layers.TCPPort(80),
}
// And create the packet with the layers
buffer = gopacket.NewSerializeBuffer()
gopacket.SerializeLayers(buffer, options,
ethernetLayer,
ipLayer,
tcpLayer,
gopacket.Payload(rawBytes),
)
outgoingPacket = buffer.Bytes()
}
示例5: packet
func packet(raddr net.IP) []byte {
ip := &layers.IPv4{
Version: 0x4,
TOS: 0x0,
TTL: 0x40,
Protocol: layers.IPProtocolTCP,
SrcIP: net.ParseIP(os.Args[2]),
DstIP: raddr,
WithRawINETSocket: true,
}
rand.Seed(time.Now().UnixNano())
tcp := &layers.TCP{
SrcPort: layers.TCPPort(rand.Uint32()),
DstPort: 0x50,
Seq: rand.Uint32(),
DataOffset: 0x5,
SYN: true,
Window: 0xaaaa,
}
tcp.SetNetworkLayerForChecksum(ip)
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{true, true}
check(gopacket.SerializeLayers(buf, opts, ip, tcp))
return buf.Bytes()
}
示例6: TestPcapLogger
func TestPcapLogger(t *testing.T) {
ipFlow, _ := gopacket.FlowFromEndpoints(layers.NewIPEndpoint(net.IPv4(1, 2, 3, 4)), layers.NewIPEndpoint(net.IPv4(2, 3, 4, 5)))
tcpFlow, _ := gopacket.FlowFromEndpoints(layers.NewTCPPortEndpoint(layers.TCPPort(1)), layers.NewTCPPortEndpoint(layers.TCPPort(2)))
flow := types.NewTcpIpFlowFromFlows(ipFlow, tcpFlow)
pcapLogger := NewPcapLogger("fake-dir", flow)
testWriter := NewTestPcapWriter()
pcapLogger.fileWriter = testWriter
pcapLogger.Start()
// test pcap header
want := []byte("\xd4\xc3\xb2\xa1\x02\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01\x00\x00\x00")
if !bytes.Equal(testWriter.lastWrite, want) {
t.Errorf("pcap header is wrong")
t.Fail()
}
// test pcap packet
rawPacket := makeTestPacket()
testWriter.lastWrite = make([]byte, 0)
pcapLogger.WritePacket(rawPacket, time.Now())
if !bytes.Equal(testWriter.lastWrite, rawPacket) {
t.Errorf("pcap packet is wrong")
t.Fail()
}
pcapLogger.Stop()
}
示例7: PrepareLayerCake
// PrepareLayerCake prepares a delicious and fluffy protocol layer cake suitable for hackers.
func (s *SloppyTrace) PrepareLayerCake() (*layers.IPv4, *layers.TCP) {
ipLayer := layers.IPv4{
SrcIP: s.SrcIP,
DstIP: s.DstIP,
Protocol: layers.IPProtocolTCP,
}
tcpLayer := layers.TCP{
SrcPort: layers.TCPPort(s.SrcPort),
DstPort: layers.TCPPort(s.DstPort),
// XXX todo: make configurable
ACK: true,
PSH: true,
}
return &ipLayer, &tcpLayer
}
示例8: TestFlowString
func TestFlowString(t *testing.T) {
ipFlow, _ := gopacket.FlowFromEndpoints(layers.NewIPEndpoint(net.IPv4(1, 2, 3, 4)), layers.NewIPEndpoint(net.IPv4(2, 3, 4, 5)))
tcpFlow, _ := gopacket.FlowFromEndpoints(layers.NewTCPPortEndpoint(layers.TCPPort(1)), layers.NewTCPPortEndpoint(layers.TCPPort(2)))
tcpIpFlow := NewTcpIpFlowFromFlows(ipFlow, tcpFlow)
if !strings.EqualFold("1.2.3.4:1-2.3.4.5:2", tcpIpFlow.String()) {
t.Error("TcpIpFlow.String() fail")
t.Fail()
}
}
示例9: main
func main() {
// Open device
handle, err = pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
if err != nil {
log.Fatal(err)
}
defer handle.Close()
// Create the layers
ethernetLayer := &layers.Ethernet{
SrcMAC: net.HardwareAddr{0xFF, 0xAA, 0xFA, 0xAA, 0xFF, 0xAA},
DstMAC: net.HardwareAddr{0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD},
EthernetType: layers.EthernetTypeIPv4,
}
ipLayer := &layers.IPv4{
SrcIP: net.IP{192, 168, 1, 3},
DstIP: net.IP{8, 8, 8, 8},
Version: 4,
IHL: 5, // 20 bytes standard header size
Length: 24,
}
tcpLayer := &layers.TCP{
SrcPort: layers.TCPPort(4321),
DstPort: layers.TCPPort(80),
}
payload := []byte{10, 20, 30, 40}
// Serialize the layers
buffer = gopacket.NewSerializeBuffer()
gopacket.SerializeLayers(buffer, options,
ethernetLayer,
ipLayer,
tcpLayer,
gopacket.Payload(payload),
)
outgoingPacket := buffer.Bytes()
// Send packet over the wire (or air)
err = handle.WritePacketData(outgoingPacket)
if err != nil {
log.Fatal(err)
}
}
示例10: TestFlows
func TestFlows(t *testing.T) {
ipFlow, _ := gopacket.FlowFromEndpoints(layers.NewIPEndpoint(net.IPv4(1, 2, 3, 4)), layers.NewIPEndpoint(net.IPv4(2, 3, 4, 5)))
tcpFlow, _ := gopacket.FlowFromEndpoints(layers.NewTCPPortEndpoint(layers.TCPPort(1)), layers.NewTCPPortEndpoint(layers.TCPPort(2)))
tcpIpFlow := NewTcpIpFlowFromFlows(ipFlow, tcpFlow)
ipFlow2, tcpFlow2 := tcpIpFlow.Flows()
if ipFlow2 != ipFlow || tcpFlow2 != tcpFlow {
t.Error("Flows method fail")
t.Fail()
}
}
示例11: TestFlowEqual
func TestFlowEqual(t *testing.T) {
ipFlow1, _ := gopacket.FlowFromEndpoints(layers.NewIPEndpoint(net.IPv4(1, 2, 3, 4)), layers.NewIPEndpoint(net.IPv4(2, 3, 4, 5)))
tcpFlow1, _ := gopacket.FlowFromEndpoints(layers.NewTCPPortEndpoint(layers.TCPPort(1)), layers.NewTCPPortEndpoint(layers.TCPPort(2)))
flow1 := NewTcpIpFlowFromFlows(ipFlow1, tcpFlow1)
ipFlow2, _ := gopacket.FlowFromEndpoints(layers.NewIPEndpoint(net.IPv4(1, 2, 3, 4)), layers.NewIPEndpoint(net.IPv4(2, 3, 4, 5)))
tcpFlow2, _ := gopacket.FlowFromEndpoints(layers.NewTCPPortEndpoint(layers.TCPPort(1)), layers.NewTCPPortEndpoint(layers.TCPPort(2)))
flow2 := NewTcpIpFlowFromFlows(ipFlow2, tcpFlow2)
if !flow1.Equal(flow2) {
t.Error("TcpIpFlow.Equal fail")
t.Fail()
}
ipFlow3, _ := gopacket.FlowFromEndpoints(layers.NewIPEndpoint(net.IPv4(8, 2, 3, 4)), layers.NewIPEndpoint(net.IPv4(2, 3, 4, 5)))
tcpFlow3, _ := gopacket.FlowFromEndpoints(layers.NewTCPPortEndpoint(layers.TCPPort(1)), layers.NewTCPPortEndpoint(layers.TCPPort(2)))
flow3 := NewTcpIpFlowFromFlows(ipFlow3, tcpFlow3)
if flow1.Equal(flow3) {
t.Error("TcpIpFlow.Equal fail")
t.Fail()
}
}
示例12: BenchmarkMultiStreamGrow
func BenchmarkMultiStreamGrow(b *testing.B) {
t := layers.TCP{
SrcPort: 1,
DstPort: 2,
Seq: 0,
BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}},
}
a := NewAssembler(NewStreamPool(&testFactory{}))
for i := 0; i < b.N; i++ {
t.SrcPort = layers.TCPPort(i)
a.Assemble(netFlow, &t)
t.Seq += 10
}
}
示例13: TestGetOverlapRingsWithZeroRings
func TestGetOverlapRingsWithZeroRings(t *testing.T) {
ip := layers.IPv4{
SrcIP: net.IP{1, 2, 3, 4},
DstIP: net.IP{2, 3, 4, 5},
Version: 4,
TTL: 64,
Protocol: layers.IPProtocolTCP,
}
tcp := layers.TCP{
SYN: true,
SrcPort: 1,
DstPort: 2,
}
tcp.SetNetworkLayerForChecksum(&ip)
payload := gopacket.Payload([]byte{1, 2, 3, 4})
p := types.PacketManifest{
IP: ip,
TCP: tcp,
Payload: payload,
}
options := ConnectionOptions{
MaxBufferedPagesTotal: 0,
MaxBufferedPagesPerConnection: 0,
MaxRingPackets: 40,
PageCache: nil,
LogDir: "fake-log-dir",
}
f := &DefaultConnFactory{}
conn := f.Build(options).(*Connection)
ipFlow, _ := gopacket.FlowFromEndpoints(layers.NewIPEndpoint(net.IPv4(1, 2, 3, 4)), layers.NewIPEndpoint(net.IPv4(2, 3, 4, 5)))
tcpFlow, _ := gopacket.FlowFromEndpoints(layers.NewTCPPortEndpoint(layers.TCPPort(1)), layers.NewTCPPortEndpoint(layers.TCPPort(2)))
serverFlow := types.NewTcpIpFlowFromFlows(ipFlow, tcpFlow)
clientFlow := serverFlow.Reverse()
conn.serverFlow = serverFlow
conn.clientFlow = clientFlow
head, tail := getOverlapRings(&p, serverFlow, conn.ClientStreamRing)
if head == nil || tail == nil {
return
} else {
t.Fail()
}
return
}
示例14: ianaPort
func ianaPort(layerType gopacket.LayerType, port uint16) string {
if layerType == layers.LayerTypeTCP {
proto, in := layers.TCPPortNames[layers.TCPPort(port)]
if in {
return fmt.Sprintf("%s (TCP)", proto)
} else {
return "N/A (TCP)"
}
} else {
proto, in := layers.UDPPortNames[layers.UDPPort(port)]
if in {
return fmt.Sprintf("%s (UDP)", proto)
} else {
return "N/A (UDP)"
}
}
}
示例15: TestOrderedCoalesceUsedPages
func TestOrderedCoalesceUsedPages(t *testing.T) {
maxBufferedPagesTotal := 1024
maxBufferedPagesPerFlow := 1024
streamRing := types.NewRing(40)
PageCache := newPageCache()
ipFlow, _ := gopacket.FlowFromEndpoints(layers.NewIPEndpoint(net.IPv4(1, 2, 3, 4)), layers.NewIPEndpoint(net.IPv4(2, 3, 4, 5)))
tcpFlow, _ := gopacket.FlowFromEndpoints(layers.NewTCPPortEndpoint(layers.TCPPort(1)), layers.NewTCPPortEndpoint(layers.TCPPort(2)))
flow := types.NewTcpIpFlowFromFlows(ipFlow, tcpFlow)
var nextSeq types.Sequence = types.Sequence(1)
coalesce := NewOrderedCoalesce(nil, flow, PageCache, streamRing, maxBufferedPagesTotal, maxBufferedPagesPerFlow, false)
ip := layers.IPv4{
SrcIP: net.IP{1, 2, 3, 4},
DstIP: net.IP{2, 3, 4, 5},
Version: 4,
TTL: 64,
Protocol: layers.IPProtocolTCP,
}
tcp := layers.TCP{
Seq: 3,
SYN: false,
SrcPort: 1,
DstPort: 2,
}
p := types.PacketManifest{
Timestamp: time.Now(),
Flow: flow,
IP: ip,
TCP: tcp,
Payload: []byte{1, 2, 3, 4, 5, 6, 7},
}
coalesce.insert(&p, nextSeq)
if coalesce.PageCache.used != 1 {
t.Errorf("coalesce.pager.Used() not equal to 1\n")
t.Fail()
}
coalesce.Close()
}