예제 #1
0
파일: packet.go 프로젝트: subutai-io/p2p
// Handles a IPv4 packet and sends it to it's destination
func (p *PeerToPeer) handlePacketIPv4(contents []byte, proto int) {
	Log(Trace, "Handling IPv4 Packet")
	f := new(ethernet.Frame)
	if err := f.UnmarshalBinary(contents); err != nil {
		Log(Error, "Failed to unmarshal IPv4 packet")
	}

	if f.EtherType != ethernet.EtherTypeIPv4 {
		return
	}
	msg := CreateNencP2PMessage(p.Crypter, contents, uint16(proto), 1, 1, 1)
	p.SendTo(f.Destination, msg)
}
예제 #2
0
파일: packet.go 프로젝트: xytis/polyp
func parsePacket(buf []byte) (*Packet, *ethernet.Frame, error) {
	f := new(ethernet.Frame)
	if err := f.UnmarshalBinary(buf); err != nil {
		return nil, nil, err
	}

	// Ignore frames which do not have ARP EtherType
	if f.EtherType != ethernet.EtherTypeARP {
		return nil, nil, errInvalidARPPacket
	}

	p := new(Packet)
	if err := p.UnmarshalBinary(f.Payload); err != nil {
		return nil, nil, err
	}
	return p, f, nil
}
// testRequest performs a single AoE request using the input request
// header and returns a single AoE response.
func testRequest(t *testing.T, req *aoe.Header) *aoe.Header {
	recv, send, run, done := testAoEServer(t)
	defer done()

	reqb, err := req.MarshalBinary()
	if err != nil {
		t.Fatalf("failed to marshal request AoE header: %v", err)
	}

	reqf := &ethernet.Frame{
		Payload: reqb,
	}

	reqfb, err := reqf.MarshalBinary()
	if err != nil {
		t.Fatalf("failed to marshal request Ethernet frame: %v", err)
	}

	if _, err := send.Write(reqfb); err != nil {
		t.Fatalf("failed to write request to server: %v", err)
	}

	if err := run(); err != nil {
		t.Fatalf("failed to run server: %v", err)
	}

	b, err := ioutil.ReadAll(recv)
	if err != nil {
		t.Fatalf("failed to read server response: %v", err)
	}

	resf := new(ethernet.Frame)
	if err := resf.UnmarshalBinary(b); err != nil {
		t.Fatalf("failed to unmarshal response Ethernet frame: %v", err)
	}

	resh := new(aoe.Header)
	if err := resh.UnmarshalBinary(resf.Payload); err != nil {
		t.Fatalf("failed to unmarshal response AoE header: %v", err)
	}

	return resh
}
예제 #4
0
파일: request.go 프로젝트: Tsingson1988/arp
// parseRequest unmarshals a raw ethernet frame and an ARP packet into a Request.
func parseRequest(buf []byte) (*Request, error) {
	f := new(ethernet.Frame)
	if err := f.UnmarshalBinary(buf); err != nil {
		return nil, err
	}

	// Ignore frames which do not have ARP EtherType
	if f.EtherType != ethernet.EtherTypeARP {
		return nil, errInvalidARPPacket
	}

	p := new(Packet)
	if err := p.UnmarshalBinary(f.Payload); err != nil {
		return nil, err
	}

	return &Request{
		Operation:          p.Operation,
		SenderHardwareAddr: p.SenderHardwareAddr,
		SenderIP:           p.SenderIP,
		TargetHardwareAddr: p.TargetHardwareAddr,
		TargetIP:           p.TargetIP,
	}, nil
}
예제 #5
0
파일: packet.go 프로젝트: subutai-io/p2p
func (p *PeerToPeer) handlePacketARP(contents []byte, proto int) {
	// Prepare new ethernet frame and fill it with
	// contents of the packet
	f := new(ethernet.Frame)
	if err := f.UnmarshalBinary(contents); err != nil {
		Log(Error, "Failed to Unmarshal ARP Binary")
		return
	}

	if f.EtherType != ethernet.EtherTypeARP {
		Log(Error, "Not ARP")
		return
	}

	packet := new(ARPPacket)
	if err := packet.UnmarshalARP(f.Payload); err != nil {
		Log(Error, "Failed to unmarshal arp")
		return
	}
	Log(Trace, "Peers: %v, Target IP: %s", p.NetworkPeers, packet.TargetIP.String())
	var hwAddr net.HardwareAddr
	id, exists := p.IPIDTable[packet.TargetIP.String()]
	if !exists {
		Log(Debug, "Unknown IP requested")
		return
	}
	peer, exists := p.NetworkPeers[id]
	if !exists {
		Log(Debug, "Specified ID was not found in peer list")
		return
	}
	hwAddr = peer.PeerHW
	// TODO: Put there normal IP from list of ips
	// Send a reply
	if hwAddr == nil {
		Log(Error, "Cannot find hardware address for requested IP")
		_, hwAddr = GenerateMAC()
		peer.PeerHW = hwAddr
		p.NetworkPeers[id] = peer
	}
	if hwAddr.String() == "00:00:00:00:00:00" {
		_, hwAddr = GenerateMAC()
		peer.PeerHW = hwAddr
		p.NetworkPeers[id] = peer
	}
	var reply ARPPacket
	ip := net.ParseIP(packet.TargetIP.String())
	response, err := reply.NewPacket(OperationReply, hwAddr, ip, packet.SenderHardwareAddr, packet.SenderIP)
	if err != nil {
		Log(Error, "Failed to create ARP reply")
		return
	}
	rp, err := response.MarshalBinary()
	if err != nil {
		Log(Error, "Failed to marshal ARP response packet")
		return
	}

	fr := &ethernet.Frame{
		Destination: response.TargetHardwareAddr,
		Source:      response.SenderHardwareAddr,
		EtherType:   ethernet.EtherTypeARP,
		Payload:     rp,
	}

	fb, err := fr.MarshalBinary()
	if err != nil {
		Log(Error, "Failed to marshal ARP Ethernet Frame")
	}
	Log(Debug, "%v", packet.String())
	p.WriteToDevice(fb, uint16(proto), false)
}
예제 #6
0
func TestServeOK(t *testing.T) {
	b := append([]byte{
		// Ethernet frame
		0xde, 0xad, 0xbe, 0xef, 0xde, 0xad,
		0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
		0x08, 0x06,
		// ARP Packet
		0, 1,
		0x08, 0x06,
		6,
		4,
		0, 1,
		0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
		192, 168, 1, 10,
		0xde, 0xad, 0xbe, 0xef, 0xde, 0xad,
		192, 168, 1, 1,
	}, make([]byte, 40)...)

	// Values to be sent in ARP reply and checked later
	wantsHW := net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad}
	wantsIP := net.IP{192, 168, 1, 1}

	wanttHW := net.HardwareAddr{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
	wanttIP := net.IP{192, 168, 1, 10}

	// Count bytes sent
	var n int
	p, err := testServe(b, true, func(w ResponseSender, r *Request) {
		// Build an ARP reply for request
		pkt, err := NewPacket(
			OperationReply,
			r.TargetHardwareAddr,
			r.TargetIP,
			r.SenderHardwareAddr,
			r.SenderIP,
		)
		if err != nil {
			t.Fatal(err)
		}

		n, err = w.Send(pkt)
		if err != nil {
			t.Fatal(err)
		}
		return
	})
	if err != nil {
		t.Fatal(err)
	}

	if want, got := n, p.wb.Len(); want != got {
		t.Fatalf("unexpected response length: %v != %v", want, got)
	}

	// Unmarshal ethernet frame and verify fields
	f := new(ethernet.Frame)
	if err := f.UnmarshalBinary(p.wb.Bytes()); err != nil {
		log.Println(len(p.wb.Bytes()))
		t.Fatal(err)
	}

	if want, got := wanttHW, f.Destination; !bytes.Equal(want, got) {
		t.Fatalf("unexpected ethernet frame destination:\n- want: %v\n-  got: %v",
			want, got)
	}
	if want, got := wantsHW, f.Source; !bytes.Equal(want, got) {
		t.Fatalf("unexpected ethernet frame source:\n- want: %v\n-  got: %v",
			want, got)
	}
	if want, got := ethernet.EtherTypeARP, f.EtherType; want != got {
		t.Fatalf("unexpected ethernet frame EtherType: %v != %v", want, got)
	}

	// Unmarshal ARP packet and verify fields
	pkt := new(Packet)
	if err := pkt.UnmarshalBinary(f.Payload); err != nil {
		t.Fatal(err)
	}

	// Hardware type is hardcoded
	if want, got := uint16(1), pkt.HardwareType; want != got {
		t.Fatalf("unexpected ARP packet hardware type: %v != %v", want, got)
	}
	// Protocol type is hardcoded
	if want, got := uint16(ethernet.EtherTypeIPv4), pkt.ProtocolType; want != got {
		t.Fatalf("unexpected ARP packet protocol type: %v != %v", want, got)
	}

	if want, got := uint8(len(wantsHW)), pkt.HardwareAddrLength; want != got {
		t.Fatalf("unexpected ARP packet hardware address length: %v != %v", want, got)
	}
	if want, got := uint8(len(wantsIP)), pkt.IPLength; want != got {
		t.Fatalf("unexpected ARP packet IP length: %v != %v", want, got)
	}

	if want, got := OperationReply, pkt.Operation; want != got {
		t.Fatalf("unexpected ARP packet operation: %v != %v", want, got)
	}

	if want, got := wantsHW, pkt.SenderHardwareAddr; !bytes.Equal(want, got) {
		t.Fatalf("unexpected ARP packet sender hardware address:\n- want: %v\n-  got: %v",
			want, got)
	}
	if want, got := wantsIP, pkt.SenderIP; !bytes.Equal(want, got) {
		t.Fatalf("unexpected ARP packet sender IP:\n- want: %v\n-  got: %v",
			want, got)
	}
	if want, got := wanttHW, pkt.TargetHardwareAddr; !bytes.Equal(want, got) {
		t.Fatalf("unexpected ARP packet target hardware address:\n- want: %v\n-  got: %v",
			want, got)
	}
	if want, got := wanttIP, pkt.TargetIP; !bytes.Equal(want, got) {
		t.Fatalf("unexpected ARP packet target IP:\n- want: %v\n-  got: %v",
			want, got)
	}
}