#include <iostream>
#include <cstdlib>
#include <fstream>
#include <ctime>
#include <string>
#include<cstdio>
using namespace std;
/*
*Leer el log y extraer la primera dirección IP (nnn.nnn.nnn.nnn).
*Guardar la dirección ip en una lista -evitar que se repite la dirección ip.
*Resumir cuantas veces se repite cada ip y visualizar en un SVG el resumen.
*/
struct miDataStruct {
string direccionIP;
int id;
};
struct nodo {
miDataStruct datoDelNodo;
nodo *ant;
nodo *next;
};
//PROTOTIPOS
void archivos_lectura(nodo * list);
int getPosition(string linea,int opcion,int ini);
//FUNCIONES DE MI LISTA DOBLE
nodo * new_list();
nodo * insert_right(nodo *list, nodo data);
void printListAsc(nodo *head);
void printListDesc(nodo *head);
void printList( nodo* list);
void borrarNodo(nodo **head, nodo *node);
void removerDuplicado(nodo **head);
int main () {
nodo *head = new_list();
nodo *current = head;
archivos_lectura(current);
printList( current);
removerDuplicado(&head);
return 0;
}
nodo * new_list(){
nodo *newelement= new nodo;
newelement->datoDelNodo.direccionIP = - 1 ;
newelement->next = newelement;
newelement->ant = newelement;
return newelement;
}
void addList( nodo *list , string number){
nodo *newelement = new nodo;
newelement->datoDelNodo.direccionIP = number;
newelement->next = list;
newelement->ant = list->ant;
list->ant = newelement;
newelement->ant->next = newelement;
}
void printListAsc(nodo *head) {
nodo *current = head;
while (current->next != head) {
current = current->next;
printList(current);
}
}
void printListDesc(nodo *head) {
nodo *current = head;
//printNode(current);
while (current->ant != head) {
current = current->ant;
printList(current);
}
}
void printList( nodo* list){
nodo *head = list;
nodo *current = list;
int i=1;
char caracter;
while (current != head->ant){
current=current->next;
cout<<"LINEA --------> "<<i++<<endl<<endl;
cout<<"Numero de IP: "<<current->datoDelNodo.direccionIP<<endl;
cout<<"__________________________________"<<endl<<endl;
// caracter = getchar();
//getch();
}
cout<< endl ;
}
// Proceso de los ficheros...
//Obteniendo la posicion de los datos del .log
int getPosition(string linea,int opcion,int ini){
int r;
switch(opcion) {
case 1:
r = linea.find("UDP") + 4;break;
case 2:
r=linea.find(" ",ini);break;
}
// -----> En caso de mas parametros a obtener, mas case; <-
return r;
}
void archivos_lectura(nodo * list) {
nodo *current=list;
string line;
ifstream myfile ("firewall_1.log");
int p1,p2;
string numero;
nodo myDataIP;
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
p1 =getPosition(line,1,0); // p1=29
p2=getPosition(line,2,p1);
numero=line.substr(p1,p2-p1);
addList(current,numero);
//cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
}
// FUNCIONES PARA ELIMINAR LOS DUPLICADOS
void removerDuplicado(nodo **head)
{
if((*head)->next == NULL) return;
nodo *current = *head;
nodo *aux;
while(current) {
aux = current->next;
while(aux) {
if(current->datoDelNodo.direccionIP == aux->datoDelNodo.direccionIP) {
borrarNodo(head, aux);
}
aux = aux->next;
}
current = current->next;
}
return;
}
void borrarNodo(nodo **head, nodo *node)
{
nodo *current = *head;
nodo *ant = *head;
if(node == *head) {
if((*head)->next != NULL) {
*head = (*head)->next;
}
return;
}
while(current) {
if(current == node) {
ant->next = current->next;
return;
}
ant = current;
current = current->next;
}
}