qq_42204438 2020-01-12 22:03 采纳率: 50%
浏览 329
已采纳

学习socket raw写一个发送,想测试一下src/des ip都填了127.0.0.1 但是sendto失败了,ip问题吗该怎么填呢

学习socket raw写一个发送,想测试一下src/des ip都填了127.0.0.1 但是sendto失败了,ip问题吗该怎么填呢?
错误是:sendto: Invalid argument
sendto() error! -1/
部分代码如下:还是哪里写的不对。。
/*********************************************************************************************************
** Function:iphdr_fill
** Description: ip header fill data
*********************************************************************************************************/
void iphdr_fill(struct ip_hdr iphdr , char *buff)
{
iphdr->ip_length = 5;
iphdr->ip_version= 4;
iphdr->ip_tos = 0;
//printf("buff 0 %s OK\n",buff);
iphdr->ip_total_length = htons(sizeof(buff));
//printf("buff 1 %s OK\n",buff);
iphdr->ip_id = 0;
iphdr->ip_flags = 0x40;
iphdr->ip_ttl = 0x40;
iphdr->ip_protocol = 0x11;
iphdr->ip_cksum = 0;
iphdr->ip_source = inet_addr("127.0.0.1");
iphdr->ip_dest = inet_addr("127.0.0.1");
iphdr->ip_cksum = hdr_cksum((unsigned short
)buff, 20);
}
/*********************************************************************************************************
** Function:udphdr_fill
** Description: udp header fill data
*********************************************************************************************************/
void udphdr_fill(struct udp_hdr *udphdr, char *buff)
{
udphdr->udp_sport = htons(SRC_PORT);
udphdr->udp_dport = htons(DEST_PORT);
udphdr->udp_ulength = htons(sizeof(buff)-20);
udphdr->udp_chksum = 0;
}

/*********************************************************************************************************
** Function:send_msg
** Description: send msg data
*********************************************************************************************************/
void send_msg(int fd, struct sockaddr_in host_addr)
{
char buff[BUFSIZE];
memset(buff, 'a', sizeof(buff) - 1);
while(1)
{
ip_hdr *iphdr;
iphdr = (ip_hdr
)buff;
//printf("buff 3 %s OK\n",buff);
iphdr_fill(iphdr,buff);
udp_hdr udphdr;
udphdr = (udp_hdr
)(buff+20);
udphdr_fill(udphdr,buff);
psd_hdr psd;
psd.psd_src = iphdr->ip_source;
psd.psd_dest = iphdr->ip_dest;
psd.psd_memzero = 0;
psd.psd_proto = 0x11;
psd.psd_vlength = udphdr->udp_ulength;

    char tmp[sizeof(psd)+ntohs(udphdr->udp_ulength)];
    memcpy(tmp, &psd, sizeof(psd));
    memcpy(tmp+sizeof(psd), buff+20, sizeof(buff)-20);
    udphdr->udp_chksum = hdr_cksum((unsigned short*)tmp, sizeof(tmp));

    int res =sendto(fd, buff, sizeof(buff), 0, (struct sockaddr*)&host_addr, sizeof(host_addr));
    if(res<0)
    {
        perror("sendto");
        printf("sendto() error! %d/n",res);

    }
    printf("send %s OK\n",buff);
    sleep(1);
}

}

/*********************************************************************************************************
** Function:main
** Description:main
*********************************************************************************************************/
int main(int argc, char *argv[])
{
int sockfd;
int flag = 1;
struct sockaddr_in host_addr;

if((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP))<0)
{
    printf("socket() error!/n");
    exit(1);
}

memset(&host_addr, 0, sizeof(host_addr));
host_addr.sin_family = AF_INET;
host_addr.sin_port = htons(DEST_PORT);
host_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

if(setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, (void *)&flag, sizeof(flag))<0)
{
    printf("setsockopt() error!/n");
    exit(0);    
}

send_msg(sockfd, &host_addr);

}

  • 写回答

2条回答 默认 最新

  • 九头蛇daze 2020-01-13 11:08
    关注

    你做的是本机发送udp数据包,通过DEST_PORT端口,那么首先这个端口是否被占用,其次你在出错地方可以用perror函数获取错误码来进行排查。
    linux的socket其实和Windows下很相似,并没有什么特别多区别,如果本机不畅,你可以连个其他系统,比如一台Windows系统的机器,做一下通讯,用第三方工具,来排查一下问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿