Skip to content

Commit

Permalink
Project 2 spec and starter code
Browse files Browse the repository at this point in the history
  • Loading branch information
0x65-e committed Dec 10, 2022
1 parent f2b604d commit 42685a0
Show file tree
Hide file tree
Showing 31 changed files with 3,741 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Project 2/cs118-fall22-project2-add-acl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pox.hpp
pox.cpp
.DS*
router
*.o
*.dSYM
.vagrant
.vscode
2 changes: 2 additions & 0 deletions Project 2/cs118-fall22-project2-add-acl/ATABLE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
c0a80202&ffff0000 ac40030a&ffff0000 0&0 0&0 0&0 1 deny
c0a80202&ffff0000 ac40030a&ffff0000 0&0 0&0 1&ff 2 deny
691 changes: 691 additions & 0 deletions Project 2/cs118-fall22-project2-add-acl/COPYING.md

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions Project 2/cs118-fall22-project2-add-acl/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM ubuntu:xenial

ARG DEBIAN_FRONTEND=noninteractive

# Install basic dependencies
RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get -y install build-essential vim-nox emacs apt-utils net-tools
RUN apt-get -y install git python-dev python-setuptools flex bison traceroute libbz2-dev libssl-dev python-pip
RUN apt-get -y install mininet expect inetutils-ping
RUN apt-get -y install xauth
RUN apt-get -y install libzeroc-ice35-dev libboost-all-dev

# Install POX controller
RUN rm -Rf /opt/pox
RUN mkdir -p /opt/pox
RUN git clone -b eel https://github.com/noxrepo/pox /opt/pox
ENV PYTHONPATH=":/opt/pox/"

# Copy the project directory
ADD . /

# Install packet redirector
RUN pip install ucla-cs118

# Set up for start
RUN service openvswitch-switch start
CMD /bin/bash
6 changes: 6 additions & 0 deletions Project 2/cs118-fall22-project2-add-acl/IP_CONFIG
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sw0-eth1 192.168.2.1
sw0-eth2 172.64.3.1
sw0-eth3 10.0.1.1
server1 192.168.2.2
server2 172.64.3.10
client 10.0.1.100
25 changes: 25 additions & 0 deletions Project 2/cs118-fall22-project2-add-acl/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CXX=g++
CXXOPTIMIZE= -O2
CXXFLAGS= -g -Wall -pthread -std=c++11 -I. -Ibuild/ $(CXXOPTIMIZE)
LDFLAGS=-lIce -lIceUtil -lboost_system -pthread
SLICE_INCLUDES=-I/usr/share/Ice/slice

USERID=123456789

CLASSES=build/pox.o arp-cache.o routing-table.o acl-table.o simple-router.o core/utils.o core/interface.o core/dumper.o

all: router

build/pox.cpp: core/pox.ice
mkdir -p build
slice2cpp $(SLICE_INCLUDES) --output-dir=build --header-ext=hpp $<

router: $(CLASSES) core/main.o
$(CXX) -o $@ $^ $(LDFLAGS)

clean:
rm -rf *.o *~ *.gch *.swp *.dSYM router *.tar.gz pox.hpp pox.cpp build/ *.pyc core/*.o

dist: tarball
tarball: clean
tar -cvzf /tmp/$(USERID).tar.gz --exclude=./.vagrant . && mv /tmp/$(USERID).tar.gz .
33 changes: 33 additions & 0 deletions Project 2/cs118-fall22-project2-add-acl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
UCLA CS118 2022 Fall Quarter Project 2 (Simple Router)
====================================

The developers are supposed to test and run their code in a virtual environment that has all dependencies installed like a container or a virtual machine .
We have included a [`Vagrantfile`](Vagrantfile) and a [`Dockerfile`](Dockerfile) for the developers to build the virtual development environment,

## Makefile

The provided `Makefile` provides several targets, including to build `router` implementation. The starter code includes only the framework to receive raw Ethernet frames and to send Ethernet frames to the desired interfaces. Your job is to implement the routers logic.

Additionally, the `Makefile` a `clean` target, and `tarball` target to create the submission file as well.

You will need to modify the `Makefile` to add your userid for the `.tar.gz` turn-in at the top of the file.

## Academic Integrity Note

You are encouraged to host your code in private repositories on [GitHub](https://github.com/), [GitLab](https://gitlab.com), or other places. At the same time, you are PROHIBITED to make your code for the class project public during the class or any time after the class. If you do so, you will be violating academic honestly policy that you have signed, as well as the student code of conduct and be subject to serious sanctions.

## Known Limitations

When POX controller is restrated, the simpler router needs to be manually stopped and started again.

## Acknowledgement

This is inherited from UCLA CS118 2017 Spring Quarter Project 3 (http:https://web.cs.ucla.edu/classes/spring17/cs118/project-3.html).

## TODO

###########################################################
## ##
## REPLACE CONTENT OF THIS FILE WITH YOUR PROJECT REPORT ##
## ##
###########################################################
3 changes: 3 additions & 0 deletions Project 2/cs118-fall22-project2-add-acl/RTABLE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0.0.0.0 10.0.1.100 0.0.0.0 sw0-eth3
192.168.2.2 192.168.2.2 255.255.255.0 sw0-eth1
172.64.3.10 172.64.3.10 255.255.0.0 sw0-eth2
45 changes: 45 additions & 0 deletions Project 2/cs118-fall22-project2-add-acl/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

$INSTALL_BASE = <<SCRIPT
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get -y upgrade
apt-get -y install build-essential vim-nox emacs python-pip
apt-get -y install git python-dev python-setuptools flex bison traceroute libbz2-dev libssl-dev
apt-get -y install mininet expect
apt-get -y install xauth
apt-get -y install libzeroc-ice35-dev libboost-all-dev
# Install POX controller
rm -Rf /opt/pox
mkdir -p /opt/pox
git clone -b eel https://github.com/noxrepo/pox /opt/pox
echo "export PYTHONPATH=\"\${PYTHONPATH}:/opt/pox/\"" >> /home/vagrant/.bashrc
source /home/vagrant/.bashrc
# Install packet redirector to simpler router
pip install ucla-cs118
# python2.7 -u /opt/pox/pox.py --verbose ucla_cs118 &
# cp /vagrant/pox.service /etc/systemd/system/
# systemctl daemon-reload
# systemctl enable pox.service
# systemctl start pox.service
SCRIPT

Vagrant.configure(2) do |config|
config.vm.box = "bento/ubuntu-16.04"

config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# Customize the amount of memory on the VM:
vb.memory = "1024"
end

config.vm.provision "shell", privileged: true, inline: $INSTALL_BASE
end


158 changes: 158 additions & 0 deletions Project 2/cs118-fall22-project2-add-acl/acl-table.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2022 Changrong Wu
*
* This program is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation, either version
* 3 of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http:https://www.gnu.org/licenses/>.
*/

#include "acl-table.hpp"
#include "core/utils.hpp"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>

std::vector<std::string> splitStr(const std::string& s, const std::string& delimiter) {
int startp = 0;
int split = 0;
std::vector<std::string> res;
while (1) {
// split once per iteration
split = s.find(delimiter, startp);
if (split != -1) {
// get a new segment
if (split != startp)
// non trivial string
res.push_back(s.substr(startp, split - startp));

split = split + delimiter.length();
startp = split;
}
else {
// get the last segment
res.push_back(s.substr(startp, s.length() - startp));
break;
}
}

return res;
}

namespace simple_router {

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// IMPLEMENT THESE METHODS
ACLTableEntry
ACLTable::lookup(uint32_t srcIp, uint32_t dstIp, uint8_t protocol, uint32_t srcPort, uint16_t dstPort) const
{
// FILL THIS IN

throw std::runtime_error("ACL entry not found");
}

void
ACLTable::addRule(ACLTableEntry& entry)
{
// FILL THIS IN
}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

// You should not need to touch the rest of this code.

bool
ACLTable::load(const std::string& fileName)
{
std::ifstream file;
file.open(fileName.c_str());
std::cerr << "Loading ACL Table from " << fileName << std::endl;

if (file.is_open()) {
std::string line;
while (std::getline(file, line)){
try
{
// each line is an ACL entry
std::vector<std::string> fields = splitStr(line, std::string(" "));

if (fields.size() == 7) {
// get a valid entry
std::vector<std::string> temp = splitStr(fields[0], "&");
uint32_t dest = std::strtoul(temp[0].c_str(), NULL, 16);
uint32_t destMask = std::strtoul(temp[1].c_str(), NULL, 16);

temp = splitStr(fields[1], "&");
uint32_t src = std::strtoul(temp[0].c_str(), NULL, 16);
uint32_t srcMask = std::strtoul(temp[1].c_str(), NULL, 16);

temp = splitStr(fields[2], "&");
uint16_t destPort = std::strtoul(temp[0].c_str(), NULL, 16);
uint16_t destPortMask = std::strtoul(temp[0].c_str(), NULL, 16);

temp = splitStr(fields[3], "&");
uint16_t srcPort = std::strtoul(temp[0].c_str(), NULL, 16);
uint16_t srcPortMask = std::strtoul(temp[0].c_str(), NULL, 16);

temp = splitStr(fields[4], "&");
uint8_t protocol = std::strtoul(temp[0].c_str(), NULL, 16);
uint8_t protocolMask = std::strtoul(temp[1].c_str(), NULL, 16);

uint16_t priority = std::strtoul(fields[5].c_str(), NULL, 16);

std::string action = fields[6];

simple_router::ACLTableEntry entry = { dest, destMask, src, srcMask, destPort, destPortMask, srcPort, srcPortMask, protocol, protocolMask, priority, action};
addRule(entry);
}
}
catch(const std::exception& e)
{
std::cerr << "Error encountered in parsing ACL table" << std::endl;
std::cerr << e.what() << '\n';
return false;
}
}
}

return true;
}

std::ostream&
operator<<(std::ostream& os, const ACLTableEntry& entry)
{
os << std::hex << entry.dest << "&" << entry.destMask << "\t"
<< entry.src << "&" << entry.srcMask << "\t"
<< entry.destPort << "&" << entry.destPortMask << "\t"
<< entry.srcPort << "&" << entry.srcPortMask << "\t"
<< (uint16_t)entry.protocol << "\t"
<< entry.priority << "\t"
<< entry.action << std::endl;
return os;
}

std::ostream&
operator<<(std::ostream& os, const ACLTable& table)
{
os << "DestIP\t" << "SrcIP\t" << "DestPort\t" << "SrcPort\t" << "Protocol\t" << "Priority\t" << "Action" << std::endl;
std::list<ACLTableEntry>::const_iterator it = table.m_entries.begin();
while (it != table.m_entries.end()) {
os << *it;
it++;
}
return os;
}

} // namespace simple_router
Loading

0 comments on commit 42685a0

Please sign in to comment.