示例#1
0
func (srv *HopServer) forwardFrames() {

	// packet map
	srv.pktHandle = map[byte](func(*udpPacket, *HopPacket)){
		HOP_FLG_PSH:               srv.handleKnock,
		HOP_FLG_PSH | HOP_FLG_ACK: srv.handleHeartbeatAck,
		HOP_FLG_HSH:               srv.handleHandshake,
		HOP_FLG_HSH | HOP_FLG_ACK: srv.handleHandshakeAck,
		HOP_FLG_DAT:               srv.handleDataPacket,
		HOP_FLG_DAT | HOP_FLG_MFR: srv.handleDataPacket,
		HOP_FLG_FIN:               srv.handleFinish,
	}

	for {
		select {
		case pack := <-srv.fromIface:
			// logger.Debug("New iface Frame")
			// first byte is left for opcode
			frame := pack[HOP_HDR_LEN:]
			dest := waterutil.IPv4Destination(frame).To4()
			mkey := ip4_uint64(dest)

			// logger.Debug("ip dest: %v", dest)
			if hpeer, found := srv.peers[mkey]; found {
				srv.bufferToClient(hpeer, pack)
			} else {
				logger.Warning("client peer with key %d not found", mkey)
			}

		case packet := <-srv.fromNet:
			srv.handlePacket(packet)
		}

	}
}
示例#2
0
func (srv *HopServer) forwardFrames() {

	// packet map
	pktHandle := map[byte](func(*udpPacket, *HopPacket)){
		HOP_FLG_PSH:               srv.handleKnock,
		HOP_FLG_HSH:               srv.handleHandshake,
		HOP_FLG_HSH | HOP_FLG_ACK: srv.handleHandshakeAck,
		HOP_FLG_DAT:               srv.handleDataPacket,
		HOP_FLG_DAT | HOP_FLG_MFR: srv.handleDataPacket,
		HOP_FLG_FIN:               srv.handleFinish,
	}

	for {
		select {
		case pack := <-srv.fromIface:
			// logger.Debug("New iface Frame")
			// first byte is left for opcode
			frame := pack[HOP_HDR_LEN:]
			dest := waterutil.IPv4Destination(frame).To4()
			mkey := ip4_uint64(dest)

			// logger.Debug("ip dest: %v", dest)
			if hpeer, found := srv.peers[mkey]; found {
				srv.bufferToClient(hpeer, pack)
			} else {
				logger.Warning("client peer with key %d not found", mkey)
			}

		case packet := <-srv.fromNet:

			hPack, err := unpackHopPacket(packet.data)
			if err == nil {
				// logger.Debug("New UDP Packet from: %v", packet.addr)

				if handle_func, ok := pktHandle[hPack.Flag]; ok {
					handle_func(packet, hPack)
				} else {
					logger.Error("Unkown flag: %x", hPack.Flag)
				}
			} else {
				logger.Error(err.Error())
			}
		}

	}
}
示例#3
0
文件: server.go 项目: seansay/gohop
func (srv *HopServer) forwardFrames() {
	for {
		select {
		case frame := <-srv.ifaceInput:
			ethertype := waterutil.MACEthertype(frame)
			logger.Debug("New frame from channel")

			switch ethertype {
			case waterutil.IPv4:
				packet := waterutil.MACPayload(frame)

				if waterutil.IsIPv4(packet) {

					vaddr := waterutil.IPv4Destination(packet)
					key := binary.BigEndian.Uint32(([]byte)(vaddr))
					if raddr, ok := srv.natTable[key]; ok {
						if idx, ok := srv.clientChannel[raddr]; ok {
							uPacket := &udpPacket{raddr, frame, idx}
							srv.netOutputs[idx] <- uPacket
						}
					}
					logger.Debug("Source:      %v [%v]", waterutil.MACSource(frame), waterutil.IPv4Source(packet))
					logger.Debug("Destination: %v [%v]", waterutil.MACDestination(frame), waterutil.IPv4Destination(packet))
					logger.Debug("Protocol:    %v\n", waterutil.IPv4Protocol(packet))
				}
			case waterutil.ARP:
				dest := waterutil.MACDestination(frame)
				logger.Debug("Mac Destionation: %v", dest)
				key := mac2uint64(dest)
				if raddr, ok := srv.macTable[key]; ok {
					if idx, ok := srv.clientChannel[raddr]; ok {
						uPacket := &udpPacket{raddr, frame, idx}
						srv.netOutputs[idx] <- uPacket
					}
				}

			}

		case packet := <-srv.netInput:
			logger.Debug("New UDP Packet from: %v", packet.addr)
			srv.clientChannel[packet.addr] = packet.channel
			srv.iface.Write(packet.data)

			frame := packet.data
			ethertype := waterutil.MACEthertype(frame)

			macSrc := waterutil.MACSource(frame)
			logger.Debug("Mac Source: %v", macSrc)
			key := mac2uint64(macSrc)
			srv.macTable[key] = packet.addr

			if ethertype == waterutil.IPv4 {
				ipPack := waterutil.MACPayload(frame)
				vaddr := waterutil.IPv4Source(ipPack)
				key := binary.BigEndian.Uint32(([]byte)(vaddr))
				srv.natTable[key] = packet.addr

				if waterutil.IsIPv4(ipPack) {
					logger.Debug("Source:      %v [%v]", waterutil.MACSource(frame), waterutil.IPv4Source(ipPack))
					logger.Debug("Destination: %v [%v]", waterutil.MACDestination(frame), waterutil.IPv4Destination(ipPack))
				}
			}

		}

	}
}