felix123
Registrado: 21 Feb 2007 Mensajes: 2
| Publicado: 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; } |
|