/
Inicio :: Foros

 F.A.Q.F.A.Q.                  Conéctese para revisar sus mensajesConéctese para revisar sus mensajes   

Direccionamiento mac en c? como funciona... ayuda

 
      Índice del Foro elrincondelc.com -> Programación en Servidores
Ver tema anterior :: Ver siguiente tema  
AutorMensaje
felix123



Registrado: 21 Feb 2007
Mensajes: 2

MensajePublicado: 21/02/2007 7:47 am
Título: Direccionamiento mac en c? como funciona... ayuda

me encontre un programa que envia una trama arp por la red pero cuando se le configura la mac en en programa no entiendo como funciona alguien me podria explicar por favor la sintaxis me confunde gracias
/*****************************************************************************
*
* autor: Daniel Lerch
* url: http://daniellerch.com
*
******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <sys/socket.h>
#include <net/ethernet.h>
#include <net/if_arp.h>
#include <arpa/inet.h>


/* Cabecera ARP */
struct arp_hdr {
unsigned short int hardware;
unsigned short int protocol;
char hw_addr_len;
char proto_addr_len;
unsigned short operation;
char src_addr[6];
char src_ip[4];
char dst_addr[6];
char dst_ip[4];
};


int main () {

/* socket */
int sock;

/* Tama~o del buffer capaz de contener un paquete ARP */
unsigned int buffer_size =
sizeof(struct arp_hdr) + sizeof(struct ether_header);

/* Buffer que contendra el paquete ARP */
unsigned char buffer[buffer_size];
memset(buffer,0,buffer_size);

/* Cabecera ethernet */
struct ether_header *eth = (struct ether_header *)buffer;

/* Cabecera ARP */
struct arp_hdr *arp =
(struct arp_hdr *)(buffer + sizeof(struct ether_header));

/* Direcciones MAC */
char src_mac[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
char dst_mac[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};

/* Direcciones IP */
char src_ip[] = {0x01, 0x02, 0x03, 0x04};
char dst_ip[] = {0x01, 0x02, 0x03, 0x04};

/* Dispositivo */
char dev[5];
strncpy(dev, "eth0", 5);

/* Creacion del socket */
if ((sock = socket(AF_INET,SOCK_PACKET,htons(ETH_P_ARP)))==-1) {

perror("socket()");
exit(EXIT_FAILURE);
}

/* Rellena la cabecera ethernet */
memcpy(eth->ether_dhost,dst_mac,ETHER_ADDR_LEN);
memcpy(eth->ether_shost,src_mac,ETHER_ADDR_LEN);
eth->ether_type = htons(ETHERTYPE_ARP);

/* Rellena la cabecera ARP */
arp->hardware = htons(ARPHRD_ETHER);
arp->protocol = htons(ETH_P_IP);
arp->hw_addr_len = 6;
arp->proto_addr_len = 4;
arp->operation = htons(ARPOP_REPLY);
memcpy(arp->src_addr, src_mac,6);
memcpy(arp->src_ip, src_ip, 4);
memcpy(arp->dst_addr, dst_mac, 6);
memcpy(arp->dst_ip, dst_ip, 4);

/* Dispositivo utilizado "eth0" */
struct sockaddr addr;
strncpy(addr.sa_data, dev, sizeof(addr.sa_data));

/* Envio del paquete ARP */
if ((sendto(sock, buffer, buffer_size, 0,
&addr, sizeof(struct sockaddr)))==-1) {

perror("sendto()");
exit(EXIT_FAILURE);
}

return 0;
}
Volver arriba
felix123



Registrado: 21 Feb 2007
Mensajes: 2

MensajePublicado: 22/02/2007 4:19 am
Título:

me explicaron y es supersencillo el codigo correspondiente al direccionamiento mac es simplemente hexadecimal osea que una notacion en mac de 0D es quivalente 0x0D
Volver arriba
      Índice del Foro elrincondelc.com -> Programación en Servidores
Página 1 de 1Todas las horas están en GMT - 8 Horas

 
No puede crear mensajes
No puede responder temas
No puede editar sus mensajes
No puede borrar sus mensajes
No puede votar en encuestas

(c) ElRincondelC.com

Un proyecto de UrlanHeat.com