您的位置:首页 > 运维架构 > Linux

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

2015-03-23 14:26 876 查看
目标:在linux下使用C语言的原始套接字来接收以太网数据链路层上的数据,如果接收的数据是IEC61850-9-2 SV类型,则打印。。。。仅供参考!

源代码:

[cpp] view
plaincopy

#include <stdio.h>

#include <unistd.h>

#include <sys/socket.h>

#include <sys/types.h>

#include <linux/if_ether.h>

#include <linux/in.h>

#define BUFFER_MAX 2048

int main(int argc, char *argv[])

{

int sock, n_read, eth_type;

char buffer[BUFFER_MAX];

char *eth_head;

if((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)

{

fprintf(stdout, "create socket error/n");

exit(0);

}

while(1)

{

n_read = recvfrom(sock, buffer, 2048, 0, NULL, NULL);

if(n_read < 42)

{

fprintf(stdout, "Incomplete header, packet corrupt/n");

continue;

}

eth_head = buffer;

eth_type=((unsigned char)eth_head[16])*16*16+(unsigned char)eth_head[17];

if(eth_type==0x88ba){ //judge wether the eth_type is iec61850 sv

printf("\n----------------IEC61850-9-2 SV---------------------\n");

int i=0;

for(i=0;i<n_read;i++){

printf("%.2X ",(unsigned char)eth_head[i]);

if(((i+1)%16)==0) printf("\n");

}

printf("\n----------------------------------------------------\n");

}

}

}

运行效果:



(------------------完---------------------)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: