find_mac_from_arptbl find_ip_from_mac

static int find_ip_from_mac(const char *stamac, char *staip)
{
    FILE *fp = NULL;
    char match_cmd[256] = {0};
    char match_line[256] = {0};

    sprintf(match_cmd, "cat /proc/net/arp | grep %s | awk \'{print $1}\'", stamac);

    if (NULL == (fp = popen(match_cmd, "r"))) {
        printf("popen failed...\n");
        return -1;
    }
    
    /* get ARP. */
    if (NULL == fgets(match_line, sizeof(match_line), fp)){
        pclose(fp);
        return -1;
    }
    else {
        sprintf(staip, "%s", match_line);
        staip[strlen(staip) - 1] = '\0';
        pclose(fp);
        /* match support device list. */
        return 0; 
    }
}

 


static int find_mac_from_arptbl(char *staip, const char *devname, char *buf)
{
    int fd = -1;
    struct arpreq arpr;
    unsigned char *eip;
    int is_find = 0;
    fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (fd < 0) {
        goto end;
    }

    memset(&arpr, 0, sizeof(arpr));
     inet_pton(AF_INET, staip, &((struct sockaddr_in*)&arpr.arp_pa)->sin_addr);
    arpr.arp_pa.sa_family = AF_INET;
    strcpy(arpr.arp_dev, devname);

    if (ioctl(fd, SIOCGARP, (void *)&arpr) < 0) {
        goto end;
    }
    if ((arpr.arp_flags & ATF_COM) == 0) { 
        goto end;
    }
    eip = (unsigned char *)arpr.arp_ha.sa_data;
    if (NULL == eip) {
        goto end;
    }
    
    sprintf(buf, "%02x%02x%02x%02x%02x%02x",
    (unsigned char)eip[0], 
    (unsigned char)eip[1],
    (unsigned char)eip[2],
    (unsigned char)eip[3],
    (unsigned char)eip[4],
    (unsigned char)eip[5]);
    is_find = 1;

end:
    if (fd >= 0) { close(fd); }
    return is_find;
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

huzk4409

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值