Skip to content

Instantly share code, notes, and snippets.

@matteing
Created November 9, 2019 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matteing/6d2f81d64a4dd624bc09329ecfb2343f to your computer and use it in GitHub Desktop.
Save matteing/6d2f81d64a4dd624bc09329ecfb2343f to your computer and use it in GitHub Desktop.
NetworkAnalyzer sorting practice
/*
Nombre: Sergio Mattei
ID: 801183252
Class/Seccion: CCOM 3033 SEC 001
Nombre de archivo: Filter_mattei.cpp
Descripción: This implements the filtering algorithms for the NetworkAnalyzer.
*/
#include <vector>
#include "packet.h"
/// \file
/// \fn void FilterBySrcAddr(vector<Packet> &netdata, string address)
/// \~English
/// Function that filters the packets in the netflow by the source address
/// \param netdata packets vector
/// \param address key address to filter
/// \~Spanish
/// Funcion que filtra los paquetes en el flujo de red por la direccion fuente
/// \param netdata vector de paquetes
/// \param address direccion llave para el fitro
void FilterBySrcAddr(vector<Packet> &netdata, string address){
for (int i = 0; i < netdata.size(); i++) {
if (address != netdata[i].getSrcAddr()) {
netdata[i].disable();
}
}
}
/// \fn void FilterByDstAddr(vector<Packet> &netdata, string address)
/// \~English
/// Function that filters the packets in the netflow by the destination address
/// \param netdata packets vector
/// \param address key address to filter
/// \~Spanish
/// Funcion que filtra los paquetes en el flujo de red por la direccion destino
/// \param netdata vector de paquetes
/// \param address direccion llave para el fitro
void FilterByDstAddr(vector<Packet> &netdata, string address){
for (int i = 0; i < netdata.size(); i++) {
if (address != netdata[i].getDstAddr()) {
netdata[i].disable();
}
}
}
/// \fn void FilterBySrcPort(vector<Packet> &netdata, int port)
/// \~English
/// Function that filters the packets in the netflow by the source port
/// \param netdata packets vector
/// \param port key port to filter
/// \~Spanish
/// Funcion que filtra los paquetes en el flujo de red por el puerto fuente
/// \param netdata vector de paquetes
/// \param port puerto llave para el fitro
void FilterBySrcPort(vector<Packet> &netdata, int port){
for (int i = 0; i < netdata.size(); i++) {
if (port != netdata[i].getSrcPort()) {
netdata[i].disable();
}
}
}
/// \fn void FilterByDstPort(vector<Packet> &netdata, int port)
/// \~English
/// Function that filters the packets in the netflow by the destination port
/// \param netdata packets vector
/// \param port key port to filter
/// \~Spanish
/// Funcion que filtra los paquetes en el flujo de red por el puerto destino
/// \param netdata vector de paquetes
/// \param port puerto llave para el fitro
void FilterByDstPort(vector<Packet> &netdata, int port){
for (int i = 0; i < netdata.size(); i++) {
if (port != netdata[i].getDstPort()) {
netdata[i].disable();
}
}
}
#include <string>
#include <vector>
#include "packet.h"
/*
Nombre: Sergio Mattei
ID: 801183252
Class/Seccion: CCOM 3033 SEC 001
Nombre de archivo: Sort_mattei.cpp
Descripción: This implements the sorting algorithms for the NetworkAnalyzer.
*/
#include <iostream>
/// \fn void SortBySrcAddr(vector<Packet> &netdata)
/// \~English
/// \brief Function that sorts by source address
/// the packets in the netflow file using the Bubble sort algorithm.
/// \param netdata Packet vector that will be sorted.
/// \~Spanish
/// \brief Funcion que ordena por direccion fuente
/// los paquetes en el archivo de netflows usando el algoritmo de Bubble sort (Burbuja).
/// \param netdata Vector de paquetes a ser ordenado.
void SortBySrcAddr(vector<Packet> &netdata){
bool ordered = false;
while (!ordered) {
ordered = true;
for (int i = 0; i < netdata.size() - 1; i++) {
if (netdata[i].getSrcAddr() > netdata[i + 1].getSrcAddr()) {
swap(netdata[i], netdata[i + 1]);
ordered = false;
}
}
}
}
/// \fn void SortByDstAddr(vector<Packet> &netdata)
/// \~English
/// \brief Function that sorts by destination address
/// the packets in the netflow file using the Selection sort algorithm.
/// \param netdata Packet vector that will be sorted.
/// \~Spanish
/// \brief Funcion que ordena por direccion destino
/// los paquetes en el archivo de netflows usando el algoritmo de Selection sort (Seleccion).
/// \param netdata Vector de paquetes a ser ordenado.
void SortByDstAddr(vector<Packet> &netdata){
int minInd;
Packet min;
for (int start = 0; start < netdata.size() - 1; start++) {
min = netdata[start];
minInd = start;
for (int i = start; i < netdata.size(); i++) {
if (netdata[i].getDstAddr() < min.getDstAddr()) {
min = netdata[i];
minInd = i;
}
}
netdata[minInd] = netdata[start];
netdata[start] = min;
}
}
/// \fn void SortBySrcPort(vector<Packet> &netdata)
/// \~English
/// \brief Function that sorts by source port
/// the packets in the netflow file using the Bubble sort algorithm.
/// \param netdata Packet vector that will be sorted.
/// \~Spanish
/// \brief Funcion que ordena por puerto fuente
/// los paquetes en el archivo de netflows usando el algoritmo de Bubble sort (Burbuja).
/// \param netdata Vector de paquetes a ser ordenado.
void SortBySrcPort(vector<Packet> &netdata){
bool ordered = false;
while (!ordered) {
ordered = true;
for (int i = 0; i < netdata.size() - 1; i++) {
if (netdata[i].getSrcPort() > netdata[i + 1].getSrcPort()) {
swap(netdata[i], netdata[i + 1]);
ordered = false;
}
}
}
}
/// \fn void SortByDstPort(vector<Packet> &netdata)
/// \~English
/// \brief Function that sorts by destination port
/// the packets in the netflow file using the Selection sort algorithm.
/// \param netdata Packet vector that will be sorted.
/// \~Spanish
/// \brief Funcion que ordena por puerto destino
/// los paquetes en el archivo de netflows usando el algoritmo de Selection sort (Seleccion).
/// \param netdata Vector de paquetes a ser ordenado.
void SortByDstPort(vector<Packet> &netdata){
int minInd;
Packet min;
for (int start = 0; start < netdata.size() - 1; start++) {
min = netdata[start];
minInd = start;
for (int i = start; i < netdata.size(); i++) {
if (netdata[i].getDstPort() < min.getDstPort()) {
min = netdata[i];
minInd = i;
}
}
netdata[minInd] = netdata[start];
netdata[start] = min;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment