- 论坛徽章:
- 0
|
求教:
1,我想通过套接字在L2层发送广播,可我的ethernet包好像组的有问题,尤其是源地址和目的地址(广播),赋值不成功,总是出错,应该怎么做才好?最好不用自己手动填写本机的物理地址。
2,源地址和目的地址是不是应该转成网络字节序,可是我只知道怎么转IP地址,MAC地址改怎么转?
我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#define SEND_BUF_SIZE 256
char send_buf[SEND_BUF_SIZE];
u_int8_t LL_host[6] = { 0x00, 0x19, 0xb9, 0x2a, 0xa1, 0xfa };//本机地址
u_int8_t LL_dest[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
int sockfd;
void send_PADI()
{
struct ether_header *eth;
eth=(struct ether_header*)&send_buf;
//以太网组包:
memcpy (ð->ether_dhost, LL_host, ETH_ALEN);//总提示这两个地址赋值出错????
memcpy (ð->ether_shost, LL_dest, ETH_ALEN);
eth->ether_type=htons(ETH_P_PPP_DISC);
struct sockaddr_ll sa;
memset (&sa, 0, sizeof (struct sockaddr_ll));
sa.sll_family = AF_PACKET;
sa.sll_halen = ETH_ALEN;//ETH_ALEN=6
sa.sll_ifindex = if_nametoindex("eth0");
memcpy (&sa.sll_addr, LL_dest, ETH_ALEN);
sendto(sockfd,send_buf,sizeof(eth),0,(struct sockaddr *)&sa,sizeof(struct sockaddr_ll));
//验证sendto返回值>0
}
int main(int argc,char* argv[])
{
sockfd=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_PPP_DISC));
if(sockfd < 0) {
perror("socket error");
exit(1);
}
send_PADI();
return 0;
} |
|