使用Linux 原始套接字抓取数据链路层上IEC61850-9-2(LE) SV数据包并显示的参考程序

36 篇文章 0 订阅
6 篇文章 0 订阅
目标:在linux下使用C语言的原始套接字来接收以太网数据链路层上的数据,如果接收的数据是IEC61850-9-2 SV类型,则打印。。。。仅供参考!

源代码:

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <sys/socket.h>  
  4. #include <sys/types.h>  
  5. #include <linux/if_ether.h>  
  6. #include <linux/in.h>  
  7. #define BUFFER_MAX 2048  
  8.   
  9. int main(int argc, char *argv[])  
  10. {        
  11. int sock, n_read, eth_type;          
  12. char buffer[BUFFER_MAX];  
  13. char  *eth_head;  
  14.          
  15. if((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)  
  16. {  
  17.     fprintf(stdout, "create socket error/n");  
  18.         exit(0);  
  19. }  
  20.           
  21. while(1)   
  22. {  
  23.     n_read = recvfrom(sock, buffer, 2048, 0, NULL, NULL);  
  24.     if(n_read < 42)   
  25.     {  
  26.         fprintf(stdout, "Incomplete header, packet corrupt/n");  
  27.         continue;  
  28.     }  
  29.                  
  30.     eth_head = buffer;  
  31.     eth_type=((unsigned char)eth_head[16])*16*16+(unsigned char)eth_head[17];  
  32.       
  33.     if(eth_type==0x88ba){    //judge wether the eth_type is iec61850 sv  
  34.         printf("\n----------------IEC61850-9-2 SV---------------------\n");  
  35.             int i=0;  
  36.         for(i=0;i<n_read;i++){  
  37.             printf("%.2X ",(unsigned char)eth_head[i]);  
  38.             if(((i+1)%16)==0) printf("\n");  
  39.         }  
  40.         printf("\n----------------------------------------------------\n");  
  41.     }  
  42. }  
  43. }  

运行效果:


(------------------完---------------------)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值