示例#1
0
		public byte[] ofp_match(uint32_t wildcards, ofp_port in_port, byte[] dl_src, byte[] dl_dst, uint16_t dl_vlan, uint8_t dl_vlan_pcp, Eth_type dl_type, uint8_t nw_tos, uint8_t nw_proto, IPAddress nw_src, IPAddress nw_dst, uint16_t tp_src, uint16_t tp_dst) {
			byte[] ret = new byte[40];
			uint32_to_bytes(wildcards, ref ret, 0);
			uint16_to_bytes((uint16_t) in_port, ref ret, 4);
			Array.Copy(dl_src, 0, ret, 6, OFP_ETH_ALEN);
			Array.Copy(dl_dst, 0, ret, 12, OFP_ETH_ALEN);
			uint16_to_bytes(dl_vlan, ref ret, 18);
			ret[20]=dl_vlan_pcp;
			uint16_to_bytes((uint16_t) dl_type, ref ret, 22);
			ret[24] = nw_tos;
			ret[25] = nw_proto;
			//uint32_to_bytes(nw_src, ref ret, 28);
			//uint32_to_bytes(nw_dst, ref ret, 32);
			Array.Copy(nw_src.GetAddressBytes(), 0, ret, 28, nw_src.GetAddressBytes().Length); 
			Array.Copy(nw_dst.GetAddressBytes(), 0, ret, 32, nw_dst.GetAddressBytes().Length); 
			uint16_to_bytes(tp_src, ref ret, 36);
			uint16_to_bytes(tp_dst, ref ret, 38);
			return ret;
		}
示例#2
0
文件: Flow.cs 项目: kyuhojeong/brunet
		public Flow(ref byte[] header, ref byte[] msg, Logger logger) {
		   ofp_version = header[0];
		   type = (ofp_type) header[1];
		   length = byte_reorder16(header, 2);
		   xid = byte_reorder32(header, 4);

		   wildcards = 0;
		   logger.WriteLine("-----------------------------------------------FLOW--------------------------------------------------------");
		   logger.WriteLine("FLOW ofp_header version:{0} type:{1} length:{2} xid:{3}", ofp_version, type, length, xid);

		   switch(type) {
		      case ofp_type.OFPT_HELLO:
		         logger.WriteLine("-----------------------------------------------------------------------------------------------------------");
		         return;
		      case ofp_type.OFPT_ERROR:
		         ofp_error_t = (ofp_error_type) byte_reorder16(msg, 0);
		         ofp_error_code = byte_reorder16(msg, 2);
		         logger.WriteLine("FLOW OFPT_ERROR | type:{0} code:{1} ", ofp_error_t, ofp_error_code);
		         logger.WriteLine("-----------------------------------------------------------------------------------------------------------");
		         return;
		      case ofp_type.OFPT_ECHO_REQUEST:
		         logger.WriteLine("-----------------------------------------------------------------------------------------------------------");
		         return;
				case ofp_type.OFPT_PACKET_IN:
		         buffer_id = byte_reorder32(msg, 0);
		         total_len = byte_reorder16(msg, 4);
		         in_port = (ofp_port) byte_reorder16(msg, 6);
		         reason = (ofp_packet_in_reason) msg[8];
		         logger.WriteLine("buffer_id:{0:x} total_len:{1} in_port:{2} reason:{3}", buffer_id, total_len, in_port, reason);
		         logger.WriteLine("");

		         // Link Layer
		         l2_offset = 10;
		         Array.Copy(msg, l2_offset, dl_dst, 0, OFP_ETH_ALEN);
		         Array.Copy(msg, l2_offset+OFP_ETH_ALEN, dl_src, 0, OFP_ETH_ALEN);

		         //802.11Q and VLAN tag is not yet implemented 
		         if (byte_reorder16(msg, l2_offset+2*OFP_ETH_ALEN) == (uint16_t) Eth_type.ETH_TYPE_VLAN ) {
		            dl_vlan = (uint16_t) (BitConverter.ToUInt16(msg, l2_offset+2*OFP_ETH_ALEN+2) & 0x0fff);
		            dl_vlan_pcp = (uint8_t) (BitConverter.ToUInt16(msg, l2_offset+2*OFP_ETH_ALEN+2) >> 13);
		            eth_type = (Eth_type) byte_reorder16(msg, l2_offset+2*OFP_ETH_ALEN+4);
		            l3_offset = l2_offset+2*OFP_ETH_ALEN+6;
		         } else {
		            eth_type = (Eth_type) byte_reorder16(msg, l2_offset+2*OFP_ETH_ALEN);
		            l3_offset = l2_offset+2*OFP_ETH_ALEN+2;
		         }
		         logger.WriteLine("dl_src:{0:X} dl_dst:{1:X}", BitConverter.ToString(dl_src), BitConverter.ToString(dl_dst));
		         logger.WriteLine("eth_type:{0}", eth_type);
		         switch (eth_type) {
		            case Eth_type.ETH_TYPE_RARP:
		               break;
		            case Eth_type.ETH_TYPE_ARP:
		               this.wildcards = ofp_flow_wildcards.OFPFW_TP_SRC | ofp_flow_wildcards.OFPFW_TP_DST | ofp_flow_wildcards.OFPFW_DL_VLAN | ofp_flow_wildcards.OFPFW_DL_VLAN_PCP;
		               if ( byte_reorder16(msg, l3_offset) == 1 && 
									byte_reorder16(msg, l3_offset+2)  == (uint16_t) Eth_type.ETH_TYPE_IP && 
									msg[l3_offset+4] == OFP_ETH_ALEN && 
									msg[l3_offset+5] == 4 && 
									byte_reorder16(msg, l3_offset+6) <= 0xff) { //referred to flow.c flow_extract
		                  nw_proto = byte_reorder16(msg, l3_offset+6);
		                  //nw_src = new IPAddress(byte_reorder32(msg, l3_offset+14));
		                  //nw_dst = new IPAddress(byte_reorder32(msg, l3_offset+24));
		                  //nw_src = new IPAddress(msg.Skip(l3_offset+14).Take(4).ToArray());
		                  //nw_dst = new IPAddress(msg.Skip(l3_offset+24).Take(4).ToArray());
								nw_src = new IPAddress(BitConverter.ToUInt32(msg, l3_offset+14));
								nw_dst = new IPAddress(BitConverter.ToUInt32(msg, l3_offset+24));

		                  Array.Copy(msg, l3_offset+8, arp_sha, 0, OFP_ETH_ALEN);
		                  Array.Copy(msg, l3_offset+18, arp_tha, 0, OFP_ETH_ALEN);
		               }
		               logger.WriteLine("nw_proto:{0:X} nw_src:{1:X} nw_dst:{2:X} arp_sha:{3:X} arp_tha", nw_proto, nw_src, nw_dst, arp_sha, arp_tha);
		               break;
						case Eth_type.ETH_TYPE_IP:
		               this.wildcards = ofp_flow_wildcards.OFPFW_DL_VLAN | ofp_flow_wildcards.OFPFW_DL_VLAN_PCP;

		               //Extract_IP_ETH_HEADER(ref msg);
		               ipv4_version = (byte) (msg[l3_offset] >> 4);
		               ihl = (byte) (msg[l3_offset] & 0x0f);
							nw_tos = (byte) (msg[l3_offset+1] >> 2); 
		               total_length = byte_reorder16(msg, l3_offset+2);
		               nw_proto = msg[l3_offset+9];

		               //TCP/UDP
		               //nw_src = new IPAddress(byte_reorder32(msg, l3_offset+12));
		               //nw_dst = new IPAddress(byte_reorder32(msg, l3_offset+16));
		               //nw_src = new IPAddress(msg.Skip(l3_offset+12).Take(4));
		               //nw_dst = new IPAddress(msg.Skip(l3_offset+16).Take(4));
		               nw_src = new IPAddress(BitConverter.ToUInt32(msg, l3_offset+12));
		               nw_dst = new IPAddress(BitConverter.ToUInt32(msg, l3_offset+16));
					
		               logger.WriteLine("version:{0} ihl:{1}", ipv4_version, ihl);
		               logger.WriteLine("nw_tos(Type of Service):{0} total_length:{1} protocol:{2}", nw_tos, total_length, nw_proto);
		               if (ihl > 5) {
		                  l4_offset = l3_offset + 24;
		               } else {
		                  l4_offset = l3_offset + 20;
		               }
		               logger.WriteLine("l3_offset:{0}, l4_offset:{1}", l3_offset, l4_offset);
		               tp_src = byte_reorder16(msg, l4_offset);
		               tp_dst = byte_reorder16(msg, l4_offset+2);

		               logger.WriteLine("protocol:{0:X} ", nw_proto);
		               logger.WriteLine("nw_src:{0} nw_dst:{1} tp_src:{2} tp_dst:{3}", nw_src.ToString(), nw_dst.ToString(), tp_src, tp_dst);
		               break;
		            case Eth_type.ETH_TYPE_IPV6:
		               logger.WriteLine("Eth_type.ETH_TYPE_IPV6:");
		               break;
						case (Eth_type) 44:
		               logger.WriteLine("ethernet type 44 0x2c ipv6 framenet ");
							break;
						
		            default:
		               logger.WriteLine("FATAL: UNKNOWN ETHERNET HEADER TYPE");
		               Environment.Exit(1);
		               break;
		         }
		         break;
		      default:
		         logger.WriteLine("Unknown packet from switch");
		         Environment.Exit(1);
		         break;

		   }
		   logger.WriteLine("-----------------------------------------------------------------------------------------------------------");
		}