linux原始套接字编程之收发链路层广播(收端)



#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <linux/if_ether.h>
#include <netpacket/packet.h>
#include <net/if.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <unistd.h>

#define ETH_ALEN 6
#define ETH_P_GATE 0x9006
#define ETH_MAX_DATA_LEN 1488

#define ETH_HEAD_LEN 16
#define ETH_BODY_LEN 1496
#define ETH_LEN ETH_HEAD_LEN + ETH_BODY_LEN

struct EtherHead
{
 unsigned char   dest[ETH_ALEN];   /* destinat    ion eth addr */
 unsigned char   source[ETH_ALEN]; /* source e    ther addr    */
 unsigned int    proto;        /* packet type     ID field */
};

struct EtherBody
{
 size_t datalen;
 char data[ETH_MAX_DATA_LEN];
};

int main()
{
 //创建原始套接字
 int sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_GATE));
 if (0 > sock)
 {
  printf("create_sock err %s",  strerror(errno));
  return -1;
 }
 
 //获取网卡信息
 struct ifreq ifr;
 memset(&ifr, 0, sizeof(struct ifreq));
 memcpy(ifr.ifr_name, "eth0", strlen("eth0") + 1);
 ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name);
 
 struct sockaddr_ll sk_ll;
 sk_ll.sll_family = AF_PACKET;
 sk_ll.sll_protocol = htons(ETH_P_GATE);
 sk_ll.sll_hatype = 1;
 sk_ll.sll_halen = ETH_ALEN;
 sk_ll.sll_pkttype = PACKET_MULTICAST;
 sk_ll.sll_ifindex = ifr.ifr_ifindex;//指定网卡
 
 if(0 < bind(sock, (struct sockaddr *)&sk_ll, sizeof(struct sockaddr_ll)))
 {
  printf("bind err %s", strerror(errno));
  exit(0);
 } 

 void *buf = malloc(1512);
 struct sockaddr_in recv;
 socklen_t recvlen = 0;
 while(1)
 {
  if(-1 == recvfrom(sock, buf, 1512, 0, (struct sockaddr *)&recv,  &recvlen))
  {
   printf("sendto err %s",  strerror(errno));
   exit(-1);
  }
  //struct EtherBody *eth = NULL;
  //memcpy(eth, buf+ETH_HEAD_LEN, ETH_BODY_LEN);
  //printf("%s\n", eth->data);
  printf("data:%s\n", ((struct EtherBody *)(buf+ETH_HEAD_LEN))->data);
  sleep(1);
 }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值