本文整理汇总了Python中ryu.lib.packet.tcp.tcp方法的典型用法代码示例。如果您正苦于以下问题:Python tcp.tcp方法的具体用法?Python tcp.tcp怎么用?Python tcp.tcp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ryu.lib.packet.tcp
的用法示例。
在下文中一共展示了tcp.tcp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_parser
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def test_parser(self):
res, ptype, _ = self.ip.parser(self.buf)
eq_(res.version, self.version)
eq_(res.header_length, self.header_length)
eq_(res.tos, self.tos)
eq_(res.total_length, self.total_length)
eq_(res.identification, self.identification)
eq_(res.flags, self.flags)
eq_(res.offset, self.offset)
eq_(res.ttl, self.ttl)
eq_(res.proto, self.proto)
eq_(res.csum, self.csum)
eq_(res.src, self.src)
eq_(res.dst, self.dst)
eq_(ptype, tcp)
示例2: parse_pckt
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def parse_pckt(pkt):
pkt = packet.Packet(array.array('B', pkt))
try:
eth = pkt.get_protocol(ethernet.ethernet)
_ipv4 = pkt.get_protocol(ipv4.ipv4)
if _ipv4.proto == 6:
_tcp = pkt.get_protocol(tcp.tcp)
src_port, dst_port = _tcp.src_port, _tcp.dst_port
elif _ipv4.proto == 17:
_udp = pkt.get_protocol(udp.udp)
src_port, dst_port = _udp.src_port, _udp.dst_port
return [_ipv4.proto, _ipv4.src, src_port, _ipv4.dst, dst_port]
except Exception as err:
print "Error"
raise err
示例3: getProtocol
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def getProtocol(self, pkt):
pkt_ipv4 = pkt.get_protocol(ipv4.ipv4)
tp = pkt.get_protocol(tcp.tcp)
port = 0
if tp:
port = tp.dst_port
ud = pkt.get_protocol(udp.udp)
if ud:
port = ud.dst_port
#print "PORTA: %s" % port
if pkt_ipv4:
protocol = pkt_ipv4.proto
if protocol==1:
return "ICMP"
if protocol==6:
if port==80:
return "HTTP"
if port==443:
return "HTTPS"
return "TCP"
if protocol==17:
if port==53:
return "DNS"
if port==67:
return "DHCP"
return "UDP"
return "Unknown. If you confirm, you will add a general traffic rule (= every type of traffic) between src and dst"
示例4: getMatch
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def getMatch(self, pkt, parser, in_port, dst):
pkt_ipv4 = pkt.get_protocol(ipv4.ipv4)
tp = pkt.get_protocol(tcp.tcp)
port = 0
if tp:
port = tp.dst_port
ud = pkt.get_protocol(udp.udp)
if ud:
port = ud.dst_port
#print "PORTA: %s" % port
if pkt_ipv4:
protocol = pkt_ipv4.proto
if protocol==1:
return parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_type=0x0800, ip_proto=1)
if protocol==6:
if port==80:
return parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_type=0x0800, ip_proto=6, tcp_dst=80)
if port==443:
return parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_type=0x0800, ip_proto=6, tcp_dst=443)
return parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_type=0x0800, ip_proto=6)
if protocol==17:
if port==53:
parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_type=0x0800, ip_proto=17, udp_dst=53)
if port==67:
parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_type=0x0800, ip_proto=17, udp_dst=67)
return parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_type=0x0800, ip_proto=17)
return parser.OFPMatch(in_port=in_port, eth_dst=dst)
#metodo per filtrare mac address in ingresso (=passano dal controller senza conferma dell'utente)
示例5: shortest_forwarding
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def shortest_forwarding(self, msg, eth_type, ip_src, ip_dst):
"""
Calculate shortest forwarding path and Install them into datapaths.
flow_info = (eth_type, src_ip, dst_ip, in_port)
or
flow_info = (eth_type, ip_src, ip_dst, in_port, ip_proto, Flag, L4_port)
"""
datapath = msg.datapath
in_port = msg.match['in_port']
pkt = packet.Packet(msg.data)
tcp_pkt = pkt.get_protocol(tcp.tcp)
udp_pkt = pkt.get_protocol(udp.udp)
ip_proto = None
L4_port = None
Flag = None
# Get ip_proto and L4 port number.
ip_proto, L4_port, Flag = self.get_L4_info(tcp_pkt, udp_pkt, ip_proto, L4_port, Flag)
result = self.get_sw(datapath.id, in_port, ip_src, ip_dst) # result = (src_sw, dst_sw)
if result:
src_sw, dst_sw = result[0], result[1]
if dst_sw:
# Path has already been calculated, just get it.
path = self.get_path(src_sw, dst_sw, weight=self.weight)
if ip_proto and L4_port and Flag:
if ip_proto == 6:
L4_Proto = 'TCP'
elif ip_proto == 17:
L4_Proto = 'UDP'
else:
pass
self.logger.info("[PATH]%s<-->%s(%s Port:%d): %s" % (ip_src, ip_dst, L4_Proto, L4_port, path))
flow_info = (eth_type, ip_src, ip_dst, in_port, ip_proto, Flag, L4_port)
else:
self.logger.info("[PATH]%s<-->%s: %s" % (ip_src, ip_dst, path))
flow_info = (eth_type, ip_src, ip_dst, in_port)
# Install flow entries to datapaths along the path.
self.install_flow(self.datapaths,
self.awareness.link_to_port,
path, flow_info, msg.buffer_id, msg.data)
else:
# Flood is not good.
self.flood(msg)
示例6: initial_event
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def initial_event(self,ev):
if ev.msg.msg_len < ev.msg.total_len:
self.logger.debug("packet truncated: only %s of %s bytes",
ev.msg.msg_len, ev.msg.total_len)
#Call <Pkt_Parse> to Build pkt object
parsPkt = Pkt_Parse()
pkt = parsPkt.handle_pkt(ev)
# Call appropriate handler for arriving packets (add IPv6,DHCP,etc.)
if pkt['udp'] != None:
self.handle_udp(pkt)
elif pkt['tcp'] != None:
self.handle_tcp(pkt)
elif pkt['icmp6'] != None:
self.handle_icmp6(pkt)
elif pkt['icmp'] != None:
self.handle_icmp(pkt)
elif pkt['ip']!= None:
self.handle_ip(pkt)
elif pkt['ip6'] != None:
self.handle_ip6(pkt)
elif pkt['arp']!=None:
self.handle_arp(pkt)
elif pkt['dhcp']!=None:
self.handle_dhcp(pkt)
elif pkt['eth'] != None:
self.handle_eth(pkt)
else:
print "Packet not identified"
self.handle_unk(pkt)
# The following functions all must be overridden. Some may be able to be
# passed, but most will likely be overridden.
示例7: test_serialize
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def test_serialize(self):
offset = 5
csum = 0
src_ip = '192.168.10.1'
dst_ip = '192.168.100.1'
prev = ipv4(4, 5, 0, 0, 0, 0, 0, 64,
inet.IPPROTO_TCP, 0, src_ip, dst_ip)
t = tcp.tcp(self.src_port, self.dst_port, self.seq, self.ack,
offset, self.bits, self.window_size, csum, self.urgent)
buf = t.serialize(bytearray(), prev)
res = struct.unpack(tcp.tcp._PACK_STR, six.binary_type(buf))
eq_(res[0], self.src_port)
eq_(res[1], self.dst_port)
eq_(res[2], self.seq)
eq_(res[3], self.ack)
eq_(res[4], offset << 4)
eq_(res[5], self.bits)
eq_(res[6], self.window_size)
eq_(res[8], self.urgent)
# test __len__
# offset indicates the number of 32 bit (= 4 bytes)
# words in the TCP Header.
# So, we compare len(tcp) with offset * 4, here.
eq_(offset * 4, len(t))
# checksum
ph = struct.pack('!4s4sBBH',
addrconv.ipv4.text_to_bin(src_ip),
addrconv.ipv4.text_to_bin(dst_ip), 0, 6, offset * 4)
d = ph + buf
s = packet_utils.checksum(d)
eq_(0, s)
示例8: test_serialize_option
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def test_serialize_option(self):
# prepare test data
offset = 0
csum = 0
option = [
tcp.TCPOptionMaximumSegmentSize(max_seg_size=1460),
tcp.TCPOptionSACKPermitted(),
tcp.TCPOptionTimestamps(ts_val=287454020, ts_ecr=1432778632),
tcp.TCPOptionNoOperation(),
tcp.TCPOptionWindowScale(shift_cnt=9),
]
option_buf = (
b'\x02\x04\x05\xb4'
b'\x04\x02'
b'\x08\x0a\x11\x22\x33\x44\x55\x66\x77\x88'
b'\x01'
b'\x03\x03\x09'
)
prev = ipv4(4, 5, 0, 0, 0, 0, 0, 64,
inet.IPPROTO_TCP, 0, '192.168.10.1', '192.168.100.1')
# test serializer
t = tcp.tcp(self.src_port, self.dst_port, self.seq, self.ack,
offset, self.bits, self.window_size, csum, self.urgent,
option)
buf = t.serialize(bytearray(), prev)
r_option_buf = buf[tcp.tcp._MIN_LEN:tcp.tcp._MIN_LEN + len(option_buf)]
eq_(option_buf, r_option_buf)
# test parser
(r_tcp, _, _) = tcp.tcp.parser(buf)
eq_(str(option), str(r_tcp.option))
示例9: test_malformed_tcp
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def test_malformed_tcp(self):
m_short_buf = self.buf[1:tcp.tcp._MIN_LEN]
tcp.tcp.parser(m_short_buf)
示例10: test_json
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def test_json(self):
jsondict = self.t.to_jsondict()
t = tcp.tcp.from_jsondict(jsondict['tcp'])
eq_(str(self.t), str(t))
示例11: getProtocol
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def getProtocol(self, pkt):
pkt_ipv4 = pkt.get_protocol(ipv4.ipv4)
tp = pkt.get_protocol(tcp.tcp)
port = 0
if tp:
port = tp.dst_port
ud = pkt.get_protocol(udp.udp)
if ud:
port = ud.dst_port
# print "PORTA: %s" % port
if pkt_ipv4:
protocol = pkt_ipv4.proto
if protocol == 1:
return "ICMP"
if protocol == 6:
if port == 80:
return "HTTP"
if port == 443:
return "HTTPS"
return "TCP"
if protocol == 17:
if port == 53:
return "DNS"
if port == 67:
return "DHCP"
return "UDP"
return "Unknown. If you confirm, you will add a general traffic rule (= every type of traffic) between src and dst"
示例12: getMatch
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def getMatch(self, pkt, parser, in_port, dst):
pkt_ipv4 = pkt.get_protocol(ipv4.ipv4)
tp = pkt.get_protocol(tcp.tcp)
port = 0
if tp:
port = tp.dst_port
ud = pkt.get_protocol(udp.udp)
if ud:
port = ud.dst_port
# print "PORTA: %s" % port
if pkt_ipv4:
protocol = pkt_ipv4.proto
if protocol == 1:
return parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_type=0x0800, ip_proto=1)
if protocol == 6:
if port == 80:
return parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_type=0x0800, ip_proto=6, tcp_dst=80)
if port == 443:
return parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_type=0x0800, ip_proto=6, tcp_dst=443)
return parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_type=0x0800, ip_proto=6)
if protocol == 17:
if port == 53:
parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_type=0x0800, ip_proto=17, udp_dst=53)
if port == 67:
parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_type=0x0800, ip_proto=17, udp_dst=67)
return parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_type=0x0800, ip_proto=17)
return parser.OFPMatch(in_port=in_port, eth_dst=dst)
示例13: packet_in_handler
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def packet_in_handler(self, ev):
msg = ev.msg
dp = msg.datapath
dpid = dp.id
ofproto = dp.ofproto
pkt = packet.Packet(msg.data)
tcp_header = pkt.get_protocol(tcp.tcp)
if tcp_header is None or (tcp_header.src_port is not 179 and
tcp_header.dst_port is not 179):
# ignore non-BGP packet
return
ipv4_header = pkt.get_protocol(ipv4.ipv4)
src_ip = ipv4_header.src
dst_ip = ipv4_header.dst
self.logger.info("BGP from %s to %s", src_ip, dst_ip)
# create a path from src to dst
hosts = topo_api.get_all_host(self)
src_host = None
dst_host = None
for host in hosts:
if src_ip in host.ipv4:
src_host = host
elif dst_ip in host.ipv4:
dst_host = host
if src_host is None or dst_host is None:
# can't find src or dst, drop it
return
src_port = src_host.port
dst_port = dst_host.port
dst_mac = dst_host.mac
to_dst_match = dp.ofproto_parser.OFPMatch(eth_dst=dst_mac,
ipv4_dst=dst_ip,
eth_type=2048)
port_no = self.fwd.setup_shortest_path(src_port.dpid,
dst_port.dpid,
dst_port.port_no,
to_dst_match)
if port_no is None:
# Can't find path to destination, ignore it.
return
self.packet_out(dp, msg, port_no)
示例14: test_default_args
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def test_default_args(self):
prev = ipv4(proto=inet.IPPROTO_TCP)
t = tcp.tcp()
buf = t.serialize(bytearray(), prev)
res = struct.unpack(tcp.tcp._PACK_STR, buf)
eq_(res[0], 1)
eq_(res[1], 1)
eq_(res[2], 0)
eq_(res[3], 0)
eq_(res[4], 5 << 4)
eq_(res[5], 0)
eq_(res[6], 0)
eq_(res[8], 0)
# with option, without offset
t = tcp.tcp(option=[tcp.TCPOptionMaximumSegmentSize(1460)])
buf = t.serialize(bytearray(), prev)
res = struct.unpack(tcp.tcp._PACK_STR + '4s', buf)
eq_(res[0], 1)
eq_(res[1], 1)
eq_(res[2], 0)
eq_(res[3], 0)
eq_(res[4], 6 << 4)
eq_(res[5], 0)
eq_(res[6], 0)
eq_(res[8], 0)
eq_(res[9], b'\x02\x04\x05\xb4')
# with option, with long offset
t = tcp.tcp(offset=7, option=[tcp.TCPOptionWindowScale(shift_cnt=9)])
buf = t.serialize(bytearray(), prev)
res = struct.unpack(tcp.tcp._PACK_STR + '8s', buf)
eq_(res[0], 1)
eq_(res[1], 1)
eq_(res[2], 0)
eq_(res[3], 0)
eq_(res[4], 7 << 4)
eq_(res[5], 0)
eq_(res[6], 0)
eq_(res[8], 0)
eq_(res[9], b'\x03\x03\x09\x00\x00\x00\x00\x00')
示例15: redis_flush_request
# 需要导入模块: from ryu.lib.packet import tcp [as 别名]
# 或者: from ryu.lib.packet.tcp import tcp [as 别名]
def redis_flush_request(self):
while True:
hub.sleep(5)
curr_ts = int(time.time())
http = self.redis.hgetall("HTTP")
https = self.redis.hgetall("HTTPS")
icmp = self.redis.hgetall("ICMP")
tcp = self.redis.hgetall("TCP")
udp = self.redis.hgetall("UDP")
self.logger.warn("RedisFLush @ %s, %s, %s", time.time(), http, icmp)
"""
port_tags = {
"dp_name": self.dp.name,
"port_name": port_name,
}
"""
"""
points = [{
"measurement": "port_state_reason",
"tags": port_tags,
"time": int(rcv_time),
"fields": {"value": reason}}]
"""
point_http = self.collect_points(http, "HTTP", curr_ts)
point_https = self.collect_points(https, "HTTPS", curr_ts)
point_icmp = self.collect_points(icmp, "ICMP", curr_ts)
point_udp = self.collect_points(udp, "UDP", curr_ts)
point_tcp = self.collect_points(tcp, "TCP", curr_ts)
self.ship_points(point_http)
self.remove_flushed(point_http, "HTTP")
self.ship_points(point_https)
self.remove_flushed(point_https, "HTTPS")
self.ship_points(point_icmp)
self.remove_flushed(point_icmp, "ICMP")
self.ship_points(point_udp)
self.remove_flushed(point_udp, "UDP")
self.ship_points(point_tcp)
self.remove_flushed(point_tcp, "TCP")