Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1727722
  • 博文数量: 782
  • 博客积分: 2455
  • 博客等级: 大尉
  • 技术积分: 4140
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-06 21:37
个人简介

Linux ,c/c++, web,前端,php,js

文章分类

全部博文(782)

文章存档

2015年(8)

2014年(28)

2013年(110)

2012年(307)

2011年(329)

分类: LINUX

2013-08-01 20:05:50

原文地址:RAW socket 收发包 作者:gaocheng

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include         /* the L2 protocols */
#include
#include
#include

#define BUFFER_MAX 2048



int main (int argc, char *argv[])
{

    int sock, n_read, n_write;
    char buffer[BUFFER_MAX];    
    int fd;

    struct sockaddr_ll sll;
    struct ifreq ifstruct;

    printf ("Date:%s || Time:%s \n", __DATE__, __TIME__);

    fd = open("/var/buffer", O_RDWR|O_CREAT);

    if ((sock = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
    {
        fprintf (stdout, "create socket error\n");
        return -1;
    }

     memset (&sll, 0, sizeof (sll));
    sll.sll_family = PF_PACKET;
    sll.sll_protocol = htons (ETH_P_ALL);

    strcpy (ifstruct.ifr_name, "eth1");
    ioctl (sock, SIOCGIFINDEX, &ifstruct);
    sll.sll_ifindex = ifstruct.ifr_ifindex;

    strcpy (ifstruct.ifr_name, "eth1");
    ioctl (sock, SIOCGIFHWADDR, &ifstruct);
    memcpy (sll.sll_addr, ifstruct.ifr_ifru.ifru_hwaddr.sa_data, ETH_ALEN);
    sll.sll_halen = ETH_ALEN;
   
 
    if (bind (sock, (struct sockaddr *) &sll, sizeof (sll)) == -1)
    {
        printf ("bind:   ERROR\n");
        return -1;
    }

    while (1)
    {
        n_read = recvfrom (sock, buffer, 2048, 0, NULL, NULL);
        if (n_read <= 0)
        {
            perror("recvfrom\n");
            return -1;
        }
   
        n_write = write(fd,buffer, n_read);
        if(n_write != n_read)
        {
            perror("write\n");
            return -1;
        }

    }

     return 0;
}





int main (int argc, char *argv[])
{

    int sock;
    int n_write;       
    int n_res;
   
    struct sockaddr_ll sll;
    struct ifreq ifstruct;

    char buffer[1024];
    char MAC_BUFFER[ETH_ALEN]= {0x01,0x02,0x03,0x04,0x05,0x06};
    char TYPE_BUFFER[2] = {0x88,0x66};

    printf ("Date:%s || Time:%s \n", __DATE__, __TIME__);

    if ((sock = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
    {
        fprintf (stdout, "create socket error\n");
        return -1;
    }

    n_res = 0;
    n_write = 0;

     memset (&sll, 0, sizeof (sll));
    sll.sll_family = PF_PACKET;
    sll.sll_protocol = htons (ETH_P_ALL);

    strcpy (ifstruct.ifr_name, "eth1");
    ioctl (sock, SIOCGIFINDEX, &ifstruct);
    sll.sll_ifindex = ifstruct.ifr_ifindex;

    strcpy (ifstruct.ifr_name, "eth1");
    ioctl (sock, SIOCGIFHWADDR, &ifstruct);
    memcpy (sll.sll_addr, ifstruct.ifr_ifru.ifru_hwaddr.sa_data, ETH_ALEN);
    sll.sll_halen = ETH_ALEN;
  
 
    if (bind (sock, (struct sockaddr *) &sll, sizeof (sll)) == -1)
    {
        printf ("bind:   ERROR\n");
        return -1;
    }

    memset(&ifstruct, 0, sizeof(ifstruct));
    strcpy (ifstruct.ifr_name, "eth1");
    if (ioctl (sock, SIOCGIFFLAGS, &ifstruct) == -1)
    {
        perror ("iotcl()\n");
        printf ("Fun:%s Line:%d\n", __func__, __LINE__);
        return -1;
    }

    ifstruct.ifr_flags |= IFF_PROMISC;

    if(ioctl(sock, SIOCSIFFLAGS, &ifstruct) == -1)
    {
       perror("iotcl()\n");
       printf ("Fun:%s Line:%d\n", __func__, __LINE__);
       return -1;
    }


    memcpy(buffer,MAC_BUFFER,ETH_ALEN);
    memcpy(buffer+6,sll.sll_addr,ETH_ALEN);
    memcpy(buffer+12,TYPE_BUFFER,2);

    while (1)
    {
        n_res = sendto ( sock, buffer, 1024, 0,
                         (struct sockaddr *) &sll, sizeof (sll));
       
        if (n_res < 0)
        {
            perror("sendto()\n");
            return -1;
        }

        n_write += n_res;

        if (n_write >= 2048 * 2560)
        {
            break;
        }

    }

    return 0;
}



阅读(1809) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~