2

我想捕获所有请求我的服务器的客户端 MAC 地址。我使用以下程序获取客户端的 MAC 地址,但它显示 "ioctl: Protocol Family Not Supported" 。如何获取MAC地址?...

#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<errno.h>
#include<string.h>
#include<netinet/if_ether.h>
#include<net/if.h>
#include<linux/sockios.h>
int main()
{
  int socket1, socket2;
  socket1 = 0;
  socket2 = 0;
  struct sockaddr_in server, client;
  int returnstatus = 0;
socket1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(socket1 == -1)
{
  perror("socket()");
}

server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
returnstatus = bind(socket1, (struct sockaddr*)&server, sizeof(server));
if(returnstatus == -1)
{
  perror("bind()");
}
returnstatus = listen(socket1, 5);
if(returnstatus == -1)
{
 perror("listen()");
}
for(;;)
{
  int buffr[4];
  int addrlen, n;
  addrlen = sizeof(client);
  socket2 = accept(socket1, (struct sockaddr *)&client, &addrlen);
    printf("%s\n",inet_ntoa(client.sin_addr));
    printf("%d\n",(int) ntohs(client.sin_port));
struct arpreq arpreq_;
bzero(&arpreq_, sizeof(struct arpreq));

if( ( n = ioctl(socket2, SIOCGARP, &arpreq_) ) < 0 ){
perror("ioctl");
}

unsigned char *ptr = &arpreq_.arp_ha.sa_data[0];
printf("MAC: %x:%x:%x:%x:%x:%x\n", *ptr, *(ptr+1), *(ptr+2), *(ptr+3),
*(ptr+4), *(ptr+5));

if (socket2 == -1)
{
  perror("accept()");
}
returnstatus = read(socket2, buffr, sizeof(buffr));

if(returnstatus == -1)
{
  perror("read()");
}
int c[2]; 
int *w;
c[0] = buffr[0] + buffr[1];
w = c;
returnstatus = write(socket2, w, sizeof(c));
if(returnstatus == -1)
{
 perror("write()");
}
close(socket2);

}
close(socket1);
return 0;
}

int addition(int x, int y)
{
 int z;
 z = x + y;
 return(z);
}
4

2 回答 2

3

我有一些代码似乎可以做你想做的事。

我一直在尝试获取一些 C++ 代码,这些代码将从我网络上的远程计算机获取 MAC 地址。花了相当长的时间才弄清楚这一点。我设法将几个示例合并并修改为一个工作演示。修改它以使用 Socket 连接应该很容易。

这段代码将网络设备和 IP 地址作为参数。(例如 eth0 192.168.1.1)

程序将检查 IP 的正确性,然后 ping IP 地址以将其添加到 ARP 表中,然后从 ARP 表中提取 MAC 地址,然后显示 MAC 地址。

我不是 C++ 编码员(或者至少我是新手),所以代码可能会被刷新。

此代码仅在 Raspbian 上测试过!

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


bool isValidIpAddress(char *ipAddress) {
    int result = 0;
    struct sockaddr_in sa;
    result = inet_pton(AF_INET, ipAddress, &(sa.sin_addr));
    return result;
}

/**
 * @brief Convert String to Number
 */
template <typename TP>
TP str2num( std::string const& value ){
    std::stringstream sin;
    sin << value;
    TP output;
    sin >> output;
    return output;
}

/**
 * @brief Convert number to string
 */
template <typename TP>
std::string num2str( TP const& value ){
    std::stringstream sin;
    sin << value;
    return sin.str();
}

/**
 * @brief Execute Generic Shell Command
 *
 * @param[in]   command Command to execute.
 * @param[out]  output  Shell output.
 * @param[in]   mode read/write access
 *
 * @return 0 for success, 1 otherwise.
 *
*/
int Execute_Command( const std::string&  command,
    std::string& output,
    const std::string& mode = "r") {

    // Create the stringstream
    std::stringstream sout;

    // Run Popen
    FILE *in;
    char buff[512];

    // Test output
    if(!(in = popen(command.c_str(), mode.c_str()))){
        return 1;
    }

    // Parse output
    while(fgets(buff, sizeof(buff), in)!=NULL){
        sout << buff;
    }

    // Close
    int exit_code = pclose(in);

    // set output
    output = sout.str();

    // Return exit code
    return exit_code;
}

/**
 * @brief Ping
 *
 * @param[in] address Address to ping.
 * @param[in] max_attempts Number of attempts to try and ping.
 * @param[out] details Details of failure if one occurs.
 *
 * @return True if responsive, false otherwise.
 *
 * @note { I am redirecting stderr to stdout.  I would recommend
 *         capturing this information separately.}
 */
bool Ping( const std::string& address ) {
    // Format a command string
    std::string command = "ping -c " + num2str(2) + " " + address + " 2>&1";
    std::string output;
    std::string details;

    // Execute the ping command
    int code = Execute_Command( command, details );
    return (code == 0);
}


static char *ethernet_mactoa(struct sockaddr *addr) {
    static char buff[256];
    unsigned char *ptr = (unsigned char *) addr->sa_data;

    sprintf(buff, "%02X:%02X:%02X:%02X:%02X:%02X",
        (ptr[0] & 0xff), (ptr[1] & 0xff), (ptr[2] & 0xff),
        (ptr[3] & 0xff), (ptr[4] & 0xff), (ptr[5] & 0xff));

    return (buff);
}


int main(int argc, char *argv[]) {
    int s;
    struct arpreq areq;
    struct sockaddr_in *sin;
    struct in_addr ipaddr;

    if (argc != 3) {
        fprintf(stderr, "-- Usage: %s device ipaddress\n", argv[0]);
        exit(1);
    }

    /* Get an internet domain socket. */
    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }


    if (!isValidIpAddress(argv[2])) {
        fprintf(stderr, "-- Error: invalid IP Address '%s'.\n",
             argv[2]);
        exit(1);
    }

    if (inet_aton(argv[2], &ipaddr) == 0) {
        fprintf(stderr, "-- Error: bad dotted-decimal IP '%s'.\n",
            argv[2]);
        exit(1);
    }

    /* Ping the Address for ARP table listing */

    if ( !Ping(argv[2]) ) {
        fprintf(stderr, "-- Error: unable to ping IP '%s'.\n",
            argv[2]);
        exit(1);
    }

    /* Make the ARP request. */
    memset(&areq, 0, sizeof(areq));
    sin = (struct sockaddr_in *) &areq.arp_pa;
    sin->sin_family = AF_INET;

    sin->sin_addr = ipaddr;
    sin = (struct sockaddr_in *) &areq.arp_ha;
    sin->sin_family = ARPHRD_ETHER;

    strncpy(areq.arp_dev, argv[1], 15);

    if (ioctl(s, SIOCGARP, (caddr_t) &areq) == -1) {
        perror("-- Error: unable to make ARP request, error");
        exit(1);
    }

    printf("%s -> %s\n",
    inet_ntoa(((struct sockaddr_in *) &areq.arp_pa)->sin_addr),
    ethernet_mactoa(&areq.arp_ha));

    return 0;
}
于 2016-04-02T01:35:41.867 回答
1

您可能只获得ioctl(SIOCGARP)本地子网的 MAC 地址,因为 SIOCGARP 将返回本地 ARP 表中的 MAC 地址。

你使用SIOCGARP了一点点错误的方式。SIOCGARP 的参数是输入输出类型。arpreq_所以你必须在调用之前填写一些字段ioctl。请参见手册页

当对等点不在您的以太网子网中时,流量会通过路由器,并且对等点可能在以太网网络中或不在以太网网络中(例如:手机)。因此,不可能从每个对等方获取以太网 MAC 地址。

当您的主机与对等方通信时,使用第一个路由器的 MAC 地址。

于 2012-08-17T18:38:12.900 回答