Created
January 25, 2014 01:29
-
-
Save mrafayaleem/8610347 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################ | |
# | |
# <website link> | |
# | |
# File: | |
# core.py | |
# | |
# Project: | |
# Software Defined Exchange (SDX) | |
# | |
# Author: | |
# Muhammad Shahbaz | |
# Arpit Gupta | |
# Laurent Vanbever | |
# | |
# Copyright notice: | |
# Copyright (C) 2012, 2013 Georgia Institute of Technology | |
# Network Operations and Internet Security Lab | |
# | |
# Licence: | |
# This file is part of the SDX development base package. | |
# | |
# This file is free code: you can redistribute it and/or modify it under | |
# the terms of the GNU Lesser General Public License version 2.1 as | |
# published by the Free Software Foundation. | |
# | |
# This package is distributed in the hope that it will be useful, but | |
# WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
# Lesser General Public License for more details. | |
# | |
# You should have received a copy of the GNU Lesser General Public | |
# License along with the SDX source package. If not, see | |
# http://www.gnu.org/licenses/. | |
# | |
## Pyretic-specific imports | |
from pyretic.lib.corelib import * | |
from pyretic.lib.std import * | |
## SDX-specific imports | |
from pyretic.sdx.lib.common import * | |
from pyretic.lib.query import * | |
## General imports | |
import json | |
import os | |
cwd = os.getcwd() | |
def parse_config(config_file): | |
participants = json.load(open(config_file, 'r')) | |
for participant_name in participants: | |
for i in range(len(participants[participant_name]["IP"])): | |
participants[participant_name]["IP"][i] = IP(participants[participant_name]["IP"][i]) | |
return participants | |
def policy(participant, fwd): | |
''' | |
Specify participant policy | |
''' | |
participants = parse_config(cwd + "/pyretic/sdx/policies/simple/local.cfg") | |
return ( | |
(parallel([match(dstip=participants["C"]["IP"][i]) for i in range(len(participants["C"]["IP"]))]) >> fwd(participant.phys_ports[0])) + | |
(parallel([match(dstip=participants["A"]["IP"][i]) for i in range(len(participants["A"]["IP"]))]) >> fwd(participant.peers["A"])) | |
) | |
#(parallel([match(dstip=participants["A"]["IP"][i]) for i in range(len(participants["A"]["IP"]))]) >> dpi()) | |
def printer(pkt): | |
print "------packet--------" | |
print pkt | |
if pkt['ethtype'] == IP_TYPE: | |
print "Ethernet packet, try to decode" | |
raw_bytes = [ord(c) for c in pkt['raw']] | |
print "ethernet payload is %d" % pkt['payload_len'] | |
eth_payload_bytes = raw_bytes[pkt['header_len']:] | |
print "ethernet payload is %d bytes" % len(eth_payload_bytes) | |
ip_version = (eth_payload_bytes[0] & 0b11110000) >> 4 | |
ihl = (eth_payload_bytes[0] & 0b00001111) | |
ip_header_len = ihl * 4 | |
ip_payload_bytes = eth_payload_bytes[ip_header_len:] | |
ip_proto = eth_payload_bytes[9] | |
print "ip_version = %d" % ip_version | |
print "ip_header_len = %d" % ip_header_len | |
print "ip_proto = %d" % ip_proto | |
print "ip payload is %d bytes" % len(ip_payload_bytes) | |
if ip_proto == 0x06: | |
print "TCP packet, try to decode" | |
tcp_data_offset = (ip_payload_bytes[12] & 0b11110000) >> 4 | |
tcp_header_len = tcp_data_offset * 4 | |
print "tcp_header_len = %d" % tcp_header_len | |
tcp_payload_bytes = ip_payload_bytes[tcp_header_len:] | |
print "tcp payload is %d bytes" % len(tcp_payload_bytes) | |
if len(tcp_payload_bytes) > 0: | |
print "payload:\t", | |
print ''.join([chr(d) for d in tcp_payload_bytes]) | |
elif ip_proto == 0x11: | |
print "UDP packet, try to decode" | |
udp_header_len = 8 | |
print "udp_header_len = %d" % udp_header_len | |
udp_payload_bytes = ip_payload_bytes[udp_header_len:] | |
print "udp payload is %d bytes" % len(udp_payload_bytes) | |
if len(udp_payload_bytes) > 0: | |
print "payload:\t", | |
print ''.join([chr(d) for d in udp_payload_bytes]) | |
elif ip_proto == 0x01: | |
print "ICMP packet" | |
else: | |
print "Unhandled packet type" | |
if ip_proto == 0x11: | |
return True | |
def dpi(): | |
q = packets() | |
q.register_callback(printer) | |
return q |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment