Introducción a la librería de desarrollo librawnet

Documentos relacionados
Apellidos: Nombre: Matrícula: Examen Programación para Sistemas Grado en Ingeniería Informática (2009)

PROTOCOLOS PARA LA INTERCONEXIÓN DE REDES

Entorno de programación de nivel 1: La librería PCAP

Práctica 1 Programación de Aplicaciones Distribuidas: Sockets UDP. Laboratorio de Comunicación de Datos ITT Telemática Septiembre 2011

Biblioteca de sistema

Comunicación IP en una LAN Ethernet ARP

Comunicación IP en una LAN Ethernet ARP

Comunicación IP en una LAN Ethernet ARP

Programación I Funciones

Comunicación IP en una LAN Ethernet ARP

Diagrama de transiciones del autómata. Tabla de transiciones

Apartado a.- Obtenga un listado de todos los ficheros regulares del sistema terminados en.y ó.c, con permiso de lectura para el grupo.

09/02/2016 ET1032 ET1032. Informática Industrial. Informática Industrial. Hacer programas 1. Hacer programas 2

ET1032. Informática Industrial. Hacer programas 1

1 Primitivas básicas de OpenMP

Archivos & Cadenas CURSO DE PROGRAMACIÓN EN C. Centro de Investigación y de Estudios Avanzados del IPN. CINVESTAV - Tamaulipas.

Herramientas Visuales de Compilación en C. LUIS MENGUAL (c)

Servicios de Internet

Introducción general al Lenguaje C (2010/2011)

Comunicación IP en una LAN Ethernet ARP Tema 3.- Interconexión de redes IP

Herramientas Visuales de Compilación en C. LUIS MENGUAL (c)

Herramient tas Vi Visuales de l d Compilación en C LUIS MENGUAL (c)

Aplicación Multiproceso sobre HTTP Ejemplo -

Prácticas de Sistemas operativos

Clase 9 Comunicación IP en una LAN Ethernet ARP Tema 3.- Interconexión de redes IP

8. Vectores (arrays)

SISTEMAS OPERATIVOS: COMUNICACIÓN Y SINCRONIZACIÓN ENTRE PROCESOS. Desarrollo de servidores concurrentes

Estructuras de Datos Declaraciones Tipos de Datos

Examen Teórico (1/3 de la nota final)

Programación I Tipos de datos y operadores básicos

Introducción a Sistemas Operativos: Ficheros

PROGRAMACION ESTRUCTURADA: Tema 1. El lenguaje de programación C

Ejercicios de la sesión 4 de C resueltos

Redirecciones y Tuberías

Implementación de la Pila TCP/IP

Esquema de un programa en C: bloques básicos

EJERCICIOS PRÁCTICOS: LLAMADAS A PROCEDIMIENTOS REMOTOS

Programación científica Punteros y arreglos

El lenguaje de Programación C. Fernando J. Pereda

EDITRAN/TR. Windows/Unix. Manual de referencia

Clases 04 y 05: Repaso de programación de sistemas basados en UNIX

Curso de Introducción a C en Plan 9

/* Compilador del Lenguaje Micro (Fischer) */ #include <stdio.h> #include <string.h> #include <ctype.h>

Threads. Hilos - Lightweight process - Procesos ligeros

Programación I Teoría : Entrada/Salida - Archivos.

Punteros. Programación en C 1

Examen de Laboratorio de Sistemas Operativos. Primera Convocatoria. 2007/2008. Tercero Ingeniería Informática. Apellidos: 18 de Junio de 2008

UNIVERSIDAD AUTÓNOMA DE BAJA CALIFORNIA FACULTAD DE CIENCIAS PRACTICA DE PROCESOS HERRAMIENTAS

Taller de Sistemas Operativos Introducción

Repaso Lenguaje C Área de Servicios Programación (Ing. Elect. y Prof. Tec.), Programación I (TUG y TUR) y Electrónica programable (TUE)

Práctica 3. Generación de números primos

2^6 2^5 2^4 2^3 2^2 2^1 2^0 SUMA

Enunciado 3 Buscar palabras de estructura similar

Área de Arquitectura y Tecnología de Computadores. Universidad Carlos III de Madrid SISTEMAS OPERATIVOS. Ejercicio. Programación en bash

Prácticas de Sistemas operativos

Problemas de Redes de Computadores. Ingeniería Técnica en Informática de Gestión Conjunto de problemas 3

Introducción al lenguaje C

Lenguaje de Programación: C++, INPUT OUTPUT(Variables)

[Cuestión 1.]Resuelva con una orden UNIX las siguientes tareas:

PROGRAMACION MODULAR FUNCIONES

Archivos de números. Tema 10c. Archivos de números. Archivos de números. Archivos mixtos. Archivos mixtos. Manejo de archivos de datos en C

Programación Estructurada. Sesión 2:El lenguaje de programación C

Algoritmos y Estructuras de Datos Ingeniería en Informática, Curso 2º SEMINARIO DE C Sesión 4

Problemas de Redes de Computadores. Conjunto de problemas 1

Introducción a Sistemas Operativos: Concurrencia

Lo guardamos como prog.c, lo compilamos y lo ejecutamos en la máquina tlm14 obteniendo el siguiente resultado:

SISTEMAS OPERATIVOS:

Message Passing Interface (MPI)

Nombre y apellidos: DNI: 1

INTRODUCCIÓN AL LENGUAJE DE PROGRAMACIÓN C

Examen Teoría. Problema [ 7 ptos. ]

Equivalencia de herramientas. Implementar las primitivas de los semáforos a partir de las regiones críticas condicionales. (2.5pt)

Fundamentos de la programación

Relojes de Tiempo Real Relojes de Tiempo Real. Métricas Métricas. de Tiempo de Tiempo. Sistemas Sistemas de Relojes de Relojes

Tipos de Datos en C. Palabras reservadas en C

EDITRAN/API. Manual de referencia. API para aplicaciones. Windows/Unix

Sintaxis de los aspectos generales de un lenguaje de programación

Plataformas de Tiempo Real

Redes de Computadores Nivel de Aplicación: Programación con sockets 3

Prácticas de Sistemas operativos

Laboratorio de Arquitecturas Avanzadas con Cell y PlayStation 3

SEMINARIO C++ Introducción a la Programación Orientada a Objetos. Herramientas de programación y Práctica 0 v

Nombre y apellidos: DNI: 1

Algoritmos y Estructuras de Datos Ingeniería en Informática, Curso 2º SEMINARIO DE C Sesión 2

UNIVERSIDAD TECNICA FEDERICO SANTA MARIA DEPARTAMENTO DE ELECTRONICA ELO320 Estructuras de Datos y Algoritmos. 3. Estructuras. Tomás Arredondo Vidal

DEFINICIÓN FORMAL DE UN AFP

Lenguaje C. Lenguaje C. Erwin Meza Vega

Sistemas Operativos. Grado Ingeniería Informática. TGR Procesos.

Introducción a C. Grupo de Sistemas y Comunicaciones. Febrero 2008 C 1. Programación imperativa estructurada.

Segundo control de teoría Q1

Tema 05: Elementos de un programa en C

Abelardo Pardo. Iria Estévez Ayres. Damaris Fuentes Lorenzo. Pablo Basanta Val. Pedro J. Muñoz Merino. Hugo A. Parada.

REDES Área de Ingeniería Telemática. VLANs. Area de Ingeniería Telemática Redes 4º Ingeniería Informática

/**********************Prototipos de ciones************************/ TOKEN scanner(); int columna(int c);

Para C un chero no es más que una porción de almacenamiento

Sistemas Operativos Práctica 3

Transcripción:

Introducción a la librería de desarrollo librawnet Redes y Servicios de Comunicaciones Avanzadas Departamento de Ingeniería Telemática Manuel Urueña Carlos J. Bernardos muruenya@it.uc3m.es cjbc@it.uc3m.es

Pila de Protocolos de RIPv2 RIPv2 Nivel de Aplicación: Protocolo de Encaminamiento UDP Nivel de Transporte IP Nivel de Red ARP Ethernet Nivel de Enlace librawnet Nivel Físico 2

librawnet Librería para la programación de protocolos de red en crudo : Permite acceder directamente a los interfaces de red del equipo. Los paquetes enviados/recibidos no han sido procesados por la pila de protocolos del equipo: Es necesario añadir/procesar las cabeceras de todos los protocolos de la pila que se desea desarrollar. Realmente el kernel y librawnet reciben una copia de todos los paquetes recibidos por la tarjeta de red. librawnet proporciona dos módulos: 1. Temporizadores con precisión de milisegundos: timerms.h (man timerms) 2. Gestión de los interfaces de red, envío/recepción de paquetes crudos : rawnet.h (man rawnet) 3

timerms.h Obtener tiempo actual: long long int timerms_time(); (Re)iniciar temporizador: long long int timerms_reset ( timerms_t * timer, long int timeout ); /* timeout >= 0: timer expira en timeout ms timeout < 0: Temporizador infinito */ Consultar estado temporizador: long int timerms_elapsed ( timerms_t * timer ); long int timerms_left ( timerms_t * timer ); 4

timerms.h: Ejemplo de uso (I) Temporizador que expira a los 500 ms: timerms_t timer; long int timeout = 500; long long int now = timerms_reset(&timer, timeout); long elapsed_time = timerms_elapsed(&timer);/* elapsed_time == 0 */ long time_left = timerms_left(&timer); /* time_left == 500 */...[+100 ms] elapsed_time = timerms_elapsed(&timer); /* elapsed_time == 100 */ time_left = timerms_left(&timer); /* time_left == 400 */...[+400 ms] elapsed_time = timerms_elapsed(&timer); /* elapsed_time == 500 */ time_left = timerms_left(&timer); /* time_left == 0 */...[+500 ms] elapsed_time = timerms_elapsed(&timer); /* elapsed_time == 1000 */ time_left = timerms_left(&timer); /* time_left == 0 */ 5

timerms.h: Ejemplo de uso (II) Temporizador infinito : timerms_t timer; long int timeout = -1; long long int now = timerms_reset(&timer, timeout); long elapsed_time = timerms_elapsed(&timer);/* elapsed_time == 0 */ long int time_left = timerms_left(&timer); /* time_left < -1 */...[+1000 ms] elapsed_time = timerms_elapsed(&timer); /* elapsed_time == 1000 */ time_left = timerms_left(&timer); /* time_left < -1 */ 6

rawnet.h Operaciones con interfaces crudos : rawiface_t * rawiface_open ( char* ifname ); char* rawiface_getname ( rawiface_t * iface ); int rawiface_getaddr ( rawiface_t * iface, uchar addr[] ); int rawiface_getmtu ( rawiface_t * iface ); int rawiface_close ( rawiface_t * iface ); Enviar/Recibir paquetes crudos : int rawnet_send ( rawiface_t * iface, uchar * pkt, int pkt_len ); int rawnet_recv ( rawiface_t * iface, uchar buffer[], int buf_len, long int timeout ); int rawnet_poll ( rawiface_t * ifaces[], int ifnum, long int timeout ); Gestión de errores (no multi-thread): char* rawnet_strerror(); 7

Ejemplo: Capa Ethernet eth_client.c eth_server.c Cliente Ethernet Servidor Ethernet Ethernet eth.h eth.c Ethernet librawnet rawnet.h timerms.h librawnet.so librawnet 8

eth.h Direcciones MAC: #define MAC_ADDR_SIZE 6 /* 6 bytes == 48 bits */ typedef unsigned char mac_addr_t [MAC_ADDR_SIZE]; mac_addr_t MAC_BCAST_ADDR = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF; #define MAC_STR_LENGTH 18 /* strlen( 12:45:78:01:34:67 + \0 ) */ void mac_addr_str ( mac_addr_t addr, char str[] ); int mac_str_addr ( char* str, mac_addr_t addr ); Operaciones con interfaces Ethernet: eth_iface_t * eth_open ( char* ifname ); char * eth_getname ( eth_iface_t * iface ); void eth_getaddr ( eth_iface_t * iface, mac_addr_t addr ); int eth_close ( eth_iface_t * iface ); Enviar/Recibir tramas Ethernet: int eth_send ( eth_iface_t * iface, mac_addr_t dst, uint16_t type, unsgined char * payload, int payload_len ); int eth_recv ( eth_iface_t * iface, mac_addr_t src, uint16_t type, unsigned char buffer[], int buf_len, long int timeout ); int eth_poll ( eth_iface_t * ifaces[], int ifnum, long int timeout ); 9

eth_open() #include eth.h #include <rawnet.h> #include <timerms.h> typedef struct eth_iface { rawiface_t * raw_iface; /* Manejador del interfaz crudo" */ mac_addr_t mac_address; /* Dirección MAC del interfaz. */ eth_iface_t; eth_iface_t * eth_open ( char* ifname ) { struct eth_iface * eth_iface; eth_iface = malloc(sizeof(struct eth_iface)); rawiface_t * raw_iface = rawiface_open(ifname); if (raw_iface == NULL) { return NULL; eth_iface->raw_iface = raw_iface; rawiface_getaddr(raw_iface, eth_iface->mac_address); return eth_iface; 10

eth_send() (I) struct eth_frame { mac_addr_t dest_addr; /* Dirección MAC destino */ mac_addr_t src_addr; /* Dirección MAC origen */ uint16_t type; /* Campo 'Tipo' */ unsigned char payload[eth_mtu]; /* Campo 'Payload': 1500 bytes */ ; #define ETH_HEADER_SIZE 14 /* 6 + 6 + 2 (sin FCS) */ int eth_send ( eth_iface_t * iface, mac_addr_t dst, uint16_t type, unsigned char * payload, int payload_len ) { /* Crear la Trama Ethernet y rellenar todos los campos */ struct eth_frame eth_frame; memcpy(eth_frame.dest_addr, dst, MAC_ADDR_SIZE); memcpy(eth_frame.src_addr, iface->mac_address, MAC_ADDR_SIZE); eth_frame.type = htons(type); memcpy(eth_frame.payload, payload, payload_len); int eth_frame_len = ETH_HEADER_SIZE + payload_len; /*... */ 11

eth_send() (II) /*... */ /* Imprimir trama Ethernet */ char* iface_name = eth_getname(iface); mac_addr_str(dst, mac_str); printf("eth_send(type=0x%04x, payload[%d]) > %s/%s\n", type, payload_len, iface_name, mac_str); print_pkt((unsigned char *) &eth_frame, eth_frame_len, ETH_HEADER_SIZE); /* Enviar la trama Ethernet creada con rawnet_send() */ bytes_sent = rawnet_send(iface->raw_iface, (unsigned char *) &eth_frame, eth_frame_len); if (bytes_sent == -1) { fprintf(stderr, "eth_send(): ERROR en rawnet_send(): %s\n, rawnet_strerror()); return -1; return (bytes_sent - ETH_HEADER_SIZE); 12

eth_recv() (I) int eth_recv ( eth_iface_t * iface, mac_addr_t src, uint16_t type, unsigned char buffer[], int buf_len, long int timeout ) { timerms_reset(&timer, timeout); do { /* Recibir trama del interfaz Ethernet */ long int time_left = timerms_left(&timer); unsigned char eth_buffer[eth_frame_max_length]; /* 14 + 1500 */ int frame_len = rawnet_recv (iface->raw_iface, eth_buffer, ETH_FRAME_MAX_LENGTH, time_left); if ((frame_len == -1) (frame_len == 0)) { return frame_len; else if (frame_len < ETH_HEADER_SIZE) { continue; /* Comprobar si es la trama que nos han pedido */ struct eth_frame * eth_frame_ptr = (struct eth_frame *) eth_buffer; int is_my_mac = (memcmp(eth_frame_ptr->dest_addr, iface->mac_address, MAC_ADDR_SIZE) == 0); int is_target_type = (ntohs(eth_frame_ptr->type) == type); while (! (is_my_mac && is_target_type) ); /*... */ 13

eth_recv() (II) /*... */ /* Trama recibida con el 'Type' solicitado: Copiar datos y dirección MAC origen */ memcpy(src, eth_frame_ptr->src_addr, MAC_ADDR_SIZE); payload_len = frame_len - ETH_HEADER_SIZE; if (buf_len > payload_len) { buf_len = payload_len; memcpy(buffer, eth_frame_ptr->payload, buf_len); return payload_len; 14

Ejemplo: Cliente / Servidor Ethernet eth_client.c eth_server.c Cliente Ethernet Servidor Ethernet Ethernet eth.h eth.c Ethernet librawnet rawnet.h timerms.h librawnet.so librawnet 15

eth_client.c int main ( int argc, char * argv[] ) { /* Procesar argumentos */ if (argc!= 5) { printf("uso: %s <iface> <tipo> <mac> <long>\n", argv[0]); exit(-1); /* Abrir la interfaz Ethernet */ eth_iface_t * eth_iface = eth_open(iface_name); if (eth_iface == NULL) { exit(-1); /* Enviar trama Ethernet al Servidor */ int err = eth_send(eth_iface, server_addr, eth_type, payload, payload_len); if (err == -1) { exit(-1); /* Recibir trama Ethernet del Servidor y procesar errores */ len = eth_recv(eth_iface, src_addr, eth_type, buffer, ETH_MTU, 2000); if (len <= 0) { fprintf(stderr, "%s: ERROR en eth_recv()\n", argv[0]); else { printf("recibidos %d bytes del Servidor Ethernet\n", len); /* Cerrar interfaz Ethernet */ eth_close(eth_iface); return 0; 16

eth_server.c int main ( int argc, char * argv[] ) { /* Procesar parámetros */ if (argc!= 3) { printf("uso: %s <iface> <tipo>\n", argv[0]); exit(-1); /* Abrir la interfaz Ethernet */ eth_iface_t * eth_iface = eth_open(iface_name); if (eth_iface == NULL) { exit(-1); while(1) { /* Recibir trama Ethernet del Cliente */ printf("escuchando tramas Ethernet (tipo=0x%04x)...\n", eth_type); unsigned char buffer[eth_mtu]; int data_len = eth_recv(eth_iface, src_addr, eth_type, buffer, ETH_MTU, -1); if (data_len == -1) { break; /* Enviar la misma trama Ethernet de vuelta al Cliente */ int len = eth_send(eth_iface, src_addr, eth_type, buffer, data_len); if (len == -1) { break; printf("enviado %d bytes al Cliente Ethernet:\n, payload_len); /* Cerrar interfaz Ethernet */ eth_close(eth_iface); return 0; 17

Desarrollar protocolos con librawnet Acceder a los interfaces y enviar/recibir paquetes en crudo requiere permisos especiales: Compilar con: rawnetcc /tmp/<prog> <f1.c> <f2.c> Ejemplo: rawnetcc /tmp/eth_client eth.c eth_client.c 1. Compila el ejecutable: /usr/bin/gcc -o /tmp/eth_client eth.c eth_client.c lrawnet 2. Otorga permisos especiales: /usr/sbin/setcap cap_net_raw,cap_net_admin=eip /tmp/eth_client ó /usr/bin/chown root.root /tmp/eth_client /usr/bin/chmod 4755 /tmp/eth_client Depurar tráfico enviado con Wireshark wireshark & Ejecutar cliente y servidor: /tmp/eth_server eth0 0x1234 /tmp/eth_client eth0 0x1234 <MAC> 18

#include <stdlib.h> #include <time.h> #include <stdio.h> int main(int argc, char* argv[]) { int nums = atoi(argv[1]); Generación de números pseudo-aleatorios /* Inicializar semilla para rand() */ unsigned int seed = time(null); srand(seed); int i; for (i=0; i<nums; i++) { /* Generar número aleatorio entre 0 y RAND_MAX */ int dice = rand(); /* Número entero aleatorio entre 1 y 10 */ dice = 1 + (int) (10.0 * dice / (RAND_MAX + 1.0)); printf("%i\n", dice); 19

Pila de Protocolos de RIPv2 Nivel de Aplicación: Protocolo de Encaminamiento RIPv2 ripv2_server.c Nivel de Transporte UDP udp.h udp.c udp_client.c udp_server.c Nivel de Red IP ARP ip.h ip.c arp.h arp.c ip_client.c ip_server.c arp_client.c Nivel de Enlace Ethernet eth.h eth.c eth_client.c eth_server.c Nivel Físico librawnet rawnet.h timerms.h librawnet.so 20

arp.h #include eth.h #include ipv4.h // void ipv4_addr_str(ipv4_addr_t addr, char str[]); // int ipv4_str_addr(char* str, ipv4_addr_t addr); int arp_resolve ( eth_iface_t * iface, ipv4_addr_t dest, mac_addr_t mac ); 21