2009-10-05 10 views

Odpowiedz

26

1 minuta poszukiwania throug google: (nie testowałem to sam, ja pracuję na komputerze z systemem Windows w tej chwili)

/* 
* gethwaddr.c 
* 
* Demonstrates retrieving hardware address of adapter using ioctl() 
* 
* Author: Ben Menking <[email protected]> 
* 
*/ 
#include <stdio.h> 
#include <sys/ioctl.h> 
#include <sys/types.h>  
#include <sys/socket.h> 
#include <net/if.h> 

int main(int argc, char *argv[]) 
{ 
    int s; 
    struct ifreq buffer; 

    s = socket(PF_INET, SOCK_DGRAM, 0); 

    memset(&buffer, 0x00, sizeof(buffer)); 

    strcpy(buffer.ifr_name, "eth0"); 

    ioctl(s, SIOCGIFHWADDR, &buffer); 

    close(s); 

    for(s = 0; s < 6; s++) 
    { 
     printf("%.2X ", (unsigned char)buffer.ifr_hwaddr.sa_data[s]); 
    } 

    printf("\n"); 

    return 0; 
}  
+0

W każdym razie, aby uzyskać to bez kodowania "eth0" ?? –

+0

Musisz przypisać kartę sieciową, w przeciwnym razie nie ma adresu mac, możesz to zrobić za pomocą wejścia lub jako argument, ale będziesz potrzebował adaptera. –

+0

#include dla close() – notlesh

2

Istnieje ogromna biblioteka zarządzać ethernet. Jeśli chcesz przejść do rzeczy o niskim poziomie, na pewno warto się tego nauczyć. To dość trudne API C do nauki.

Lib PCAP.

link to lib pcap sourceforge

Niektóre przykładowy kod:

#include <pcap.h> 
#include <stdlib.h> 
#include <netinet/ip.h> 
#include <netinet/if_ether.h> 

void find_eth_addr(struct in_addr *search_ip, const struct pcap_pkthdr* pkthdr, const u_char *packet) { 
struct ether_header *eth_hdr = (struct ether_header *)packet; 

if (ntohs(eth_hdr->ether_type) == ETHERTYPE_IP) { 
    struct ip *ip_hdr = (struct ip *)(packet + sizeof(struct ether_header)); 
if (ip_hdr->ip_dst.s_addr == search_ip->s_addr) 
    print_eth_addr(eth_hdr->ether_dhost); 
if (ip_hdr->ip_src.s_addr == search_ip->s_addr) 
    print_eth_addr(eth_hdr->ether_shost); 

} 
} 

Istnieje również ładne "funkcje jądra wrapper" jak biblioteki: DNET

który zapewnia dużą funkcjonalność, aby używać go na małej sieci poziomej . (również uzyskiwanie adresów MAC).

DNET

Istnieje UNIX porty & korzystna dla obu bibliotek.

Powiązane problemy