免费注册 查看新帖 |

Chinaunix

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

socket编程中一个sendto的问题,求高人指点下 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-27 22:55 |只看该作者 |倒序浏览
本帖最后由 shuimu159951 于 2012-02-27 22:56 编辑

源代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<net/if.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netpacket/packet.h>
#include<linux/if_ether.h>
#include<errno.h>

#include"config.h"

int arp_reply(const char *device,struct arp_msg *msg,int n)
{
        int res;
        int handle;
        char eth_addr[MAC_ADDR_LEN];
        struct sockaddr_in ip_addr;
        struct sockaddr_ll dst_addr;

        /*msg结构赋值*/
        ip_addr.sin_addr.s_addr=msg->arp_head.sip;
        memcpy(eth_addr,msg->arp_head.eth_shost,MAC_ADDR_LEN);
        memcpy(msg->eth_head.shost,msg->eth_head.dhost,MAC_ADDR_LEN);
        memcpy(msg->eth_head.dhost,eth_addr,MAC_ADDR_LEN);
        memcpy(msg->arp_head.eth_shost,msg->arp_head.eth_dhost,MAC_ADDR_LEN);
        msg->arp_head.sip=msg->arp_head.dip;
        memcpy(msg->arp_head.eth_dhost,eth_addr,MAC_ADDR_LEN);
        msg->arp_head.dip=ip_addr.sin_addr.s_addr;
        memset(msg->pad,0,1;

        handle=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ARP));
        dst_addr.sll_family=PF_PACKET;
        dst_addr.sll_ifindex=if_nametoindex(device);
        if(handle==-1)
        {
                fprintf(stderr,"Socket cannot be created!\n";
                return ABNORMAL;
        }

        /*发送与测*/
        printf("%d\n",sizeof(*msg));
        res=sendto(handle,msg,ETH_FRAME_MIN_LEN,0,(struct sockaddr *)&dst_addr,sizeof(dst_addr));
        if(res==-1)
        {
                printf("%d\n",errno);
        }
        return NORMAL;
}
sendto会返回错误代码22:nvalid argument
msg的赋值段和发送于测试段代码放到另一个程序里是可以正常运行的
各位看看我是不是地址结构等参数构造出错了。

论坛徽章:
0
2 [报告]
发表于 2012-02-28 07:44 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP