Skip to content

Instantly share code, notes, and snippets.

@humbertowoody
Created August 18, 2016 15:03
Show Gist options
  • Save humbertowoody/c5c797c453314a14d307325598047308 to your computer and use it in GitHub Desktop.
Save humbertowoody/c5c797c453314a14d307325598047308 to your computer and use it in GitHub Desktop.
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <linux/if_packet.h>
#include <net/ethernet.h> /* the L2 protocols */
#include <unistd.h>
#include<sys/ioctl.h>
#include<net/if.h>
#include<string.h>
unsigned char macorigen[6];
unsigned char mask[4];
unsigned char ip[4];
int obtendatos(int ds){
struct ifreq nic;
struct sockaddr no;
char nombre[10];
int indice,a;
printf("dame el nombre\n");
gets(nombre);
strcpy(nic.ifr_name,nombre);
if (ioctl(ds,SIOCGIFINDEX,&nic)==-1)
{
perror("errror \n");
}
else{
indice=nic.ifr_ifindex;
//macorigen=nic.(ifr_hwaddr.sa_data);
printf("el indice es: %d\n",indice);
}
if(ioctl(ds,SIOCGIFHWADDR,&nic)==-1)
{
perror("error \n");
}
else{
memcpy(macorigen,nic.ifr_hwaddr.sa_data,6);
for(a=0;a<6;a++){
printf("%.2x:",macorigen[a]);
}
}
////////mask
if(ioctl(ds,SIOCGIFNETMASK,&nic)==-1)
{
perror("error \n");
}
else{
memcpy(macorigen,nic.ifr_netmask.sa_data+2,4);
printf("\n");
for(a=0;a<4;a++){
printf("%d:",macorigen[a]);
}
}
/////// ip
if(ioctl(ds,SIOCGIFADDR,&nic)==-1)
{
perror("error \n");
}
else{
memcpy(ip,nic.ifr_addr.sa_data+2,4);
printf("\n");
for(a=0;a<4;a++){
printf("%d:",ip[a]);
}
}
return indice;
}
int main(){
int packet_socket,index;
packet_socket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL) );
if(packet_socket < 0){
perror("Error al abrir el socket\n");
} else {
index=obtendatos(packet_socket);
printf("Exito al abrir el socket\n");
}
close(packet_socket);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment