Replit – Compilazione

Compilazione mediante g++
g++ -c main.cpp
g++ -o test main.o
./test
Compilazione di files multipli mediante g++
g++ -c *.cpp
g++ -o test *.o
./test
Makefile
Esempio di Makefile base:
all: main
CC = g++
OBJS = *.o
main: functions.o main.o
 $(CC) -o main $(OBJS)
clean:
 rm -rf *.o

Utilizzo:
- make: esegue ciò che è indicato in all (make main)
- make main: esegue il make main
- make clean: esegue il make clean
Libreria
Comandi:
g++ -c functions.cpp
ar rvs functions.lib functions.o
g++ -p mylibtest main.cpp functions.lib
./mylibtest

La creazione della libreria consente di "evitare" di includere nel package il file functions.cpp: una volta creata la libreria "rinominare" o "eliminare" il file functions.cpp in modo da verificarne il funzionamento

Access List – CISCO Packet Tracer

Schema
General Schema
Standard ACL
Assume that we want to PERMIT the ping from 192.168.2.100 to 192.168.1.0
Assume that we want to DENY the ping from 192.168.2.101 to 192.168.1.0

With using the Standard ACL we are "closest to the destination" and this means that we need to work on:
- R0 (closest to the destination)
- Gig0/1 on R0 (closest to the destination) outbound

the command to use in order to configure an access list that DENY 192.168.2.101 to reach 192.168.1.0 is:
access-list 1 deny 192.168.2.101 0.0.0.0
Please note that 0.0.0.0 is the wildcard which means basically that we deny ONLY 192.168.2.101;
we could also write this specific command in this way:
access-list 1 deny host 192.168.2.101
in case we want to deny ALL 192.168.2.0 we should put 0.0.0.255
In order to permit everything else:
access-list 1 permit any

R0:
access-list 1 deny 192.168.2.101 0.0.0.0
access-list 1 permit any

It is necessary to associate the created ACL to the correct interface:
interface gigabitEthernet 0/1
ip access-group 1 outbound 

In case we want to "append" the deny for host 192.168.2.100 with a similar command like the one before, it won't work
What happens is that all access-list are read from top to bottom therefore the "permit" will mask the last one inserted.
The rule is that once a match occurs the access-list exits
Please consider that at the end of every access-list there's a default deny all
In order to reset the access-list 1 we can type:
no access-list 1
Extended ACL
Assume that we want to DENY all traffic from 192.168.2.102 wherever

With using the Extended ACL we are "closest to the source" and this means that we need to work on:
- R1 (closest to the source)
- Gig0/1 on R0 (closest to the source) inbound

R1(config)#ip access-list extended 100
R1(config-ext-nacl)#deny icmp host 192.168.2.102 any
R1(config-ext-nacl)#permit icmp any any
R1(config-ext-nacl)#exit
R1(config)#int g0/1
R1(config-if)#ip access-group 100 in

Access List – Introduzione

An ACL is a series of commands that control whether a router forwards or drops packets based on information found in the packet header.

ACL’s can perform the following tasks:

  • Limit network traffic to increase network performance.  For example, video traffic could be blocked if it’s not permitted.
  • Provide traffic flow control.  ACLs can help verify routing updates are from a known source.
  • ACLs provide security for network access and can block a host or a network.
  • Filter traffic based on traffic type such as Telnet traffic.
  • Screen hosts to permit or deny access to network services such as FTP or HTTP. 

An ACL is a sequential list of permit or deny statements, known as access control entries (ACEs) commonly called ACL statements.

When network traffic passes through an interface configured with an ACL, the router compares the information within the packet against each ACE, in sequential order, to determine if the packet matches one of the ACEs; this is referred to as packet filtering.

The last statement of an ACL is always an implicit deny.  This is automatically inserted at the end of each ACL and blocks all traffic.  Because of this, all ACLs should have at least one permit statement.

ACLs can be configured to apply to inbound traffic and outbound traffic:

  • Inbound ACLs – Incoming packets are processed before they are routed to the outbound interface (coming into the router)
  • Outbound ACLs – Incoming packets are routed to the outbound interface, and then they are processed through the outbound ACL (coming out of the router)

The proper placement of an ACL can make the network operate more efficiently.  For example, and ACL can be placed to reduce unnecessary traffic. Every ACL should be placed where it has the greatest impact on efficiency.

Standard Access List (ACL)

  • Since standard ACLs do not specify destination addresses, they should be configured as close to the destination as possible.
  • id: (1-99)
  • denies or permits source IP address

Extended Access List (ACL)

  • Configure extended ACLs as close as possible to the source of the traffic to be filtered. This will prevent undesirable traffic as close to the source without it crossing the network infrastructure.
  • Id: (100-199)
  • denies or permits source IP address
  • denies or permits destination IP address
  • denies or permits port
Introduzione

Thunkable – Calcolatrice

pt.1

Introduzione alla costruzione di una “calcolatrice”

pt.2

Modello di calcolatrice con 2 campi di input

pt.3

Modello di calcolatrice con:

  • utilizzo di un solo campo di input
  • gestione delle 4 operazioni
  • gestione del CLEAR e dell’uguale
pt.4

Modello di calcolatrice con:

  • utilizzo di un solo campo di output
  • utilizzo dei pulsanti numerici
  • gestione delle 4 operazioni
  • gestione del CLEAR e dell’uguale

Falling Matrix

The Falling Matrix

Tutorial per la realizzazione di un effetto Falling Matrix in C++ mediante Ncurses.

Topics:

  • setup replit.com
  • visualizzazione effetto finale
  • costruzione dell’applicazione con infrastruttura di base (singola “stringa” a lunghezza fissa che cade nello screen a ciclo continuo)
  • gestione dinamicità nella lunghezza e delay
  • utilizzo di struttura dati dinamica
  • gestione velocità differenti di caduta

Reference per setup Ncurses su Replit.com:

Introduzione ad Ncurses e setup su replit.com

STEP1 – Introduzione

pt.1
#include <ncurses.h>
#include <unistd.h>

void initialize_fall_index(int fall_index[],int columns) {
  for (int i=0;i<columns;i++)
    fall_index[i]=0;
}

void initialize_fall_delay(int fall_delay[],int columns) {
  for (int i=0;i<columns;i++)
    fall_delay[i]=0; //rand() % 10;
}

void initialize_tear_length(int tear_length[],int columns) {
  for (int i=0;i<columns;i++)
    tear_length[i]=0; //rand() % 10;
}

void start_fall(int i,int j,int row,int fall_index[],int fall_delay[],int tear_length[]) {
  if ((fall_index[j]==row+2*fall_delay[j]+tear_length[j])&&(i==0)) { 
      // RESET
      fall_index[j]=0;
      fall_delay[j]=0; //rand() % 10;
      tear_length[j]=0; //rand() % 10;
  } else {
    if (fall_index[j]>=fall_delay[j]) 
      mvaddch((i-fall_delay[j]+row) % row,j,'1' | A_BOLD);
    fall_index[j]++;  
  }
}

void finish_fall(int i,int j,int row,int fall_delay[],int tear_length[]) {
  mvaddch((i-fall_delay[j]+row-tear_length[j]) % row,j,' ');
}

int cycle() {
  int row,col;
  int time=100;
  
	initscr();
  noecho();
  getmaxyx(stdscr,row,col);
col=1:

  int fall_index[col];
  initialize_fall_index(fall_index,col);  
  int fall_delay[col];
  initialize_fall_delay(fall_delay,col);  
  int tear_length[col];
  initialize_tear_length(tear_length,col);  
  
  start_color();
	curs_set(0);
	
  nodelay(stdscr, true);
  
  while (true) {

    for (int i=0;i<row;i++) {
      for (int j=0;j<col;j++) {
        start_fall(i,j,row,fall_index,fall_delay,tear_length);
        finish_fall(i,j,row,fall_delay,tear_length);
        refresh();  
      }
      usleep(time*1000); 
    }
       
  }
  
	endwin();

	return 0;
}

int main() {  
  return cycle();
}
pt.2
#include <ncurses.h>
#include <unistd.h>
#include <stdlib.h>

void initialize_fall_index(int fall_index[],int columns) {
  for (int i=0;i<columns;i++)
    fall_index[i]=0;
}

void initialize_fall_delay(int fall_delay[],int columns) {
  for (int i=0;i<columns;i++)
    fall_delay[i]=rand() % 10;
}

void initialize_tear_length(int tear_length[],int columns) {
  for (int i=0;i<columns;i++)
    tear_length[i]=rand() % 10;
}

void start_fall(int i,int j,int row,int fall_index[],int fall_delay[],int tear_length[]) {
  if ((fall_index[j]==row+2*fall_delay[j]+tear_length[j])&&(i==0)) { 
      // RESET
      fall_index[j]=0;
      fall_delay[j]=rand() % 10;
      tear_length[j]=rand() % 10;
  } else {
    if (fall_index[j]>=fall_delay[j]) 
      mvaddch((i-fall_delay[j]+row) % row,j,'1' | A_BOLD);
    fall_index[j]++;  
  }
}

void finish_fall(int i,int j,int row,int fall_delay[],int tear_length[]) {
  mvaddch((i-fall_delay[j]+row-tear_length[j]) % row,j,' ');
}

int cycle() {
  int row,col;
  int time=100;
  
	initscr();
  noecho();
  getmaxyx(stdscr,row,col);
col=5;

  int fall_index[col];
  initialize_fall_index(fall_index,col);  
  int fall_delay[col];
  initialize_fall_delay(fall_delay,col);  
  int tear_length[col];
  initialize_tear_length(tear_length,col);  
  
  start_color();
	curs_set(0);
	
  nodelay(stdscr, true);
  
  while (true) {

    for (int i=0;i<row;i++) {
      for (int j=0;j<col;j++) {
        start_fall(i,j,row,fall_index,fall_delay,tear_length);
        finish_fall(i,j,row,fall_delay,tear_length);
        refresh();  
      }
      usleep(time*1000); 
    }
       
  }
  
	endwin();

	return 0;
}


int main() {  
  return cycle();
}
pt.3

#include <ncurses.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>


typedef struct column_type {
  int index;
	int fall_delay;
	int tear_length;
  //int counter;
  //int limit;
} column;

void initialize_column(column* columns,int i) {
  columns[i].index=0;
  columns[i].fall_delay = rand() % 10;
  columns[i].tear_length = rand() % 15;
  //columns[i].counter = 0;
  //columns[i].limit = rand() % 8;
}

column* initialize_columns(int col) {
  column* columns = (column*)malloc(sizeof(column) * col);
  for(int i = 0; i < col; i++) 
    initialize_column(columns,i);
  return columns;
}

void start_fall(int i, int j, int row,column* columns) {
  if ((columns[j].index==row+columns[j].fall_delay+columns[j].tear_length)&&(i==0)) {
    initialize_column(columns,j);
  } else {
    if (columns[j].index>=columns[j].fall_delay) {
      mvaddch((i-columns[j].fall_delay+row) % row,j,'1' | A_BOLD);
    }
    columns[j].index++;  
  }
}

void finish_fall(int i, int j, int row, column* columns) {
  mvaddch((i-columns[j].fall_delay+row-columns[j].tear_length) % row,j,' ');
}

int un_cycle() {
  int row,col;
  int _time=100;
  srand(time(NULL));
  
	initscr();
  noecho();
  getmaxyx(stdscr,row,col);

  column* columns=initialize_columns(col);
  
  start_color();
	curs_set(0);
	
  nodelay(stdscr, true);
  
  while (true) {

    for (int i=0;i<row;i++) {
      for (int j=0;j<col;j++) {
        start_fall(i,j,row,columns);
        finish_fall(i,j,row,columns);
        refresh();  
      }
      usleep(_time*1000); 
    }
       
  }
  
	endwin();

  free(columns);

	return 0;
}

int main() {  
  return un_cycle();
}
pt.4

#include <ncurses.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>


typedef struct column_type {
  int index;
	int fall_delay;
	int tear_length;
  int counter;
  int limit;
} column;

void initialize_column(column* columns,int i) {
  columns[i].index=0;
  columns[i].fall_delay = rand() % 10;
  columns[i].tear_length = rand() % 15;
  columns[i].counter = 0;
  columns[i].limit = rand() % 8;
}

column* initialize_columns(int col) {
  column* columns = (column*)malloc(sizeof(column) * col);
  for(int i = 0; i < col; i++) 
    initialize_column(columns,i);
  return columns;
}

void start_smart_fall(int j, int row,column* columns) {
  //mvaddch((columns[j].index+row) % row,j,'1' | A_BOLD);
  //columns[j].index++;  
 if ((columns[j].index==row+columns[j].fall_delay+columns[j].tear_length) &&
    ((columns[j].index % row)==0)) {
    initialize_column(columns,j);
  } else {
    if (columns[j].index>=columns[j].fall_delay) {
      mvaddch((columns[j].index-columns[j].fall_delay+row) % row,j,'1' | A_BOLD | COLOR_PAIR(2));
      mvchgat((columns[j].index-1-columns[j].fall_delay+row) % row,j,1,A_NORMAL,2,NULL);
      mvchgat((columns[j].index-2-columns[j].fall_delay+row) % row,j,1,A_DIM,2,NULL);
    }
    columns[j].index++;  
  }
}

void finish_smart_fall(int j, int row, column* columns) {
  mvaddch((columns[j].index-1-columns[j].fall_delay+row-columns[j].tear_length) % row,j,' ');
  //mvaddch((columns[j].index-1+row-columns[j].tear_length) % row,j,' ');
}

int time_cycle() {
  int row,col;
  int _time=100;
  srand(time(NULL));
  
	initscr();
  noecho();
  getmaxyx(stdscr,row,col);

  start_color();
  init_pair(1, COLOR_WHITE, COLOR_BLACK);
	init_pair(2, COLOR_GREEN, COLOR_BLACK);

  column* columns=initialize_columns(col);
  
  start_color();
	curs_set(0);
	
  nodelay(stdscr, true);


  while (true) {
    
      for (int j=0;j<col;j++) {
        if (columns[j].counter==columns[j].limit) {
          columns[j].counter=0;
          start_smart_fall(j,row,columns);
          finish_smart_fall(j,row,columns);
          refresh();  
        } else
          columns[j].counter++;
      }

      usleep(_time*1000); 
      //sleep(1);
       
  }
  
	endwin();

  free(columns);

	return 0;
}


int main() {  
  return time_cycle();
}

Ncurses – Replit.com

STEPS

  1. create a new c++ repl
  2. choose “show hidden files”
  3. in the .replit file change the 1st row with the following: compile = “clang -std=c++11 -lncurses main.cpp -o main”
  4. in the replit.nix file add the following: pkgs.ncurses
  5. now you can include in your main.cpp file: #include <ncurses.h>
  6. remove the <iostream.h> and test with printf(“Hello world”);

BEWARE

It is possible that the code still has the ccls error (red underlined error) but the code works when it runs and if closing and re-opening the project it should disappear

It is possible to use “panel.h”

STEPS

  1. you can include in your main.cpp file: #include <panel.h>
  2. in the .replit file change the 1st row with the following: compile = “clang -std=c++11 –lpanel -lncurses main.cpp -o main”
Ncurses Introduzione e configurazione in replit.com