【求助】怎么用原始套接字自定义协议,实现数据包的收发,求大神帮忙。。跪谢。。。。
如题,自己写了个小程序发包,但是sendto的时候老出错
#include "client.h"
#define SERVPORT 0x8808
void sendpack(int, struct sockaddr_ll *);
ne_u8 recvbuf[1024] = {0};
ne_u8 sendbuf[1024] = {0};
int main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_ll my_addr;
struct sockaddr_ll de_addr;
if (argc <2)
{
fprintf(stderr,"please enter the hostname!\n");
exit(1);
}
if ((sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1)
{
perror("socket error!");
exit(1);
}
memset(&my_addr,0,sizeof(my_addr));
my_addr.sll_family = AF_PACKET;
my_addr.sll_protocol = htons(ETH_P_ALL);
my_addr.sll_ifindex = 0;
// my_addr.sll_pkttype = PACKET_BROADCAST;
// my_addr.sll_htype =
// my_addr.sin_addr.s_addr = INADDR_ANY;
// bzero(&(my_addr.sin_zero),8);
if(bind(sockfd, (struct sockaddr * )&my_addr,sizeof(my_addr)) !=0 )
{
perror("errno");
exit(1);
}
sendpack(sockfd,&my_addr);
close(sockfd);
return 0;
}
void sendpack(int sockfd, struct sockaddr_ll *my_addr)
{
int head_len = 0;
bzero(sendbuf , 1024);
struct ether_header *ether_header;
struct protocol *optl;
ne_u8 s_mac[6] = {0x00,0x0c,0x29,0xe4,0x15,0x32};
ne_u8 d_mac[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
ether_header = (struct ether_header*)sendbuf;
ether_header->ether_type = 0x8808;
memcpy(ether_header->ether_shost, s_mac, 6);
memcpy(ether_header->ether_dhost, d_mac, 6);
head_len = sizeof(struct ether_header *) + sizeof(struct protocol *);
optl = (struct protocol *)(sendbuf + 14);
memcpy(optl->so_mac, s_mac, 6);
memcpy(optl->de_mac, d_mac, 6);
optl->port = SERVPORT;
optl->flags = 55;
if (sendto(sockfd, sendbuf, head_len, 0, (struct sockaddr *)my_addr, sizeof(struct sockaddr)) ==-1)
{
perror("send error!");
printf("%d\n",errno);
exit(1);
}
/*
if (send(sockfd, sendbuf, head_len, 0) ==-1 )
{
perror("send error!");
printf("%d\n",errno);
exit(1);
}
*/
}
完整代码如上,求帮助,不胜感激!!