Chinaunix首页 | 论坛 | 博客
  • 博客访问: 179924
  • 博文数量: 108
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 1065
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-29 08:56
文章分类

全部博文(108)

文章存档

2011年(11)

2010年(46)

2009年(29)

2008年(22)

我的朋友

分类: LINUX

2010-11-16 11:11:11

#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;
}



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

上一篇:手动生成shell

下一篇:后台起shell交互

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