📄 serial.cpp
字号:
#include"Serial.h"
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include<errno.h>
#include<string.h>
#include<pthread.h>
#include<pcap.h>
#if 0 /* Only exists on some versions. */
#include <ioctls.h>
#endif
#include <sys/types.h>
#include <sys/ioctl.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#ifdef USE_SENDMSG
#include <iovec.h>
#endif
UBYTE outpack[1024];
static int opt_no_src_addr = 0, opt_broadcast = 0;
int ucsock;
PV pv;
pcap_t *pd;
u_int snaplen;
extern pthread_t gthreadid,gthreadid1;
void IEC61850Loop(char *user, struct pcap_pkthdr * pkthdr, u_char * pkt);
void ProcessCheckNsData();
void ProcessCheckData();
int InitSerial(UBYTE netid)
{
int ret,dret;
if(netid == 0)
{
pv.interface = (char *)malloc(strlen("eth0") + 1);
strcpy(pv.interface, "eth0");
}
else if(netid == 1)
{
pv.interface = (char *)malloc(strlen("eth1") + 1);
strcpy(pv.interface, "eth1");
}
else
{
#ifdef _DEBUG
printf("NetId Failued!\n");
#endif
return 0;
}
OpenPcap();
if ((ucsock = socket(AF_INET, SOCK_PACKET, SOCK_PACKET)) < 0)
{
if (errno == EPERM)
{
#ifdef _DEBUG
printf("Create Socket Failued!\n");
#endif
return 0;
}
else
{
#ifdef _DEBUG
printf("Create Socket Success!\n");
#endif
return 1;
}
}
ret = pthread_create(>hreadid, NULL, (void *(*)(void*))ProcessLoop, NULL);
if(ret)
{
#ifdef _DEBUG
printf("ProcessLoop Thread Create Failued!\n");
#endif
return 0;
}
dret = pthread_create(>hreadid1, NULL, (void *(*)(void*))ProcessData_Bak, NULL);
if(dret)
{
#ifdef _DEBUG
printf("ProcessDataThread Create Failued!\n");
#endif
return 0;
}
return 1;
}
void ProcessLoop(void )
{
if(pcap_loop(pd, pv.pkt_cnt, (pcap_handler) IEC61850Loop, NULL) < 0)
{
exit(1);
}
}
void ProcessData_Bak(void )
{
ProcessCheckData();
return;
}
int OpenPcap()
{
bpf_u_int32 localnet, netmask;
struct bpf_program fcode;
char errorbuf[PCAP_ERRBUF_SIZE];
bpf_u_int32 defaultnet = 0xFFFFFF00;
if(pv.interface == NULL)
{
if (!pv.readmode_flag)
{
#ifdef _DEBUG
printf("pv.interface is NULL, looking up interface.... \n ");
#endif
pv.interface = pcap_lookupdev(errorbuf);
#ifdef _DEBUG
printf( "found interface\n");
#endif
if(pv.interface == NULL)
{
#ifdef _DEBUG
printf( "OpenPcap() interface lookup\n");
#endif
}
}
else
{
pv.interface = "[reading from a file]";
}
}
if (!pv.readmode_flag)
{
if(pv.pkt_snaplen)
{
if(pv.pkt_snaplen < MIN_SNAPLEN)
{
snaplen = MIN_SNAPLEN;
}
else
{
snaplen = pv.pkt_snaplen;
}
}
else
{
snaplen = SNAPLEN;
}
pd = pcap_open_live(pv.interface, snaplen,
pv.promisc_flag ? PROMISC : 0, READ_TIMEOUT, errorbuf);
}
else
{
pd = pcap_open_offline(pv.readfile, errorbuf);
if(pd == NULL)
{
#ifdef _DEBUG
printf("unable to open file for readback: \n" );
#endif
}
snaplen = pcap_snapshot(pd);
if(!pv.quiet_flag)
#ifdef _DEBUG
printf("snaplen = %d\n\n" , snaplen);
#endif
}
if(pd == NULL)
{
if(strstr(errorbuf, "Permission denied"))
{
}
else
{
}
}
if(pcap_lookupnet(pv.interface, &localnet, &netmask, errorbuf) < 0)
{
if (!pv.readmode_flag)
{
#ifdef _DEBUG
printf( "OpenPcap() device network lookup: \n" );
#endif
}
netmask = htonl(defaultnet);
}
else
{
DefineIfaceVar(PRINT_INTERFACE(pv.interface),
(u_char *) &localnet,
(u_char *) &netmask);
}
return 0;
}
void DefineIfaceVar(char *iname, u_char * network, u_char * netmask)
{
char valbuf[32];
char varbuf[BUFSIZ];
snprintf(varbuf, BUFSIZ, "%s_ADDRESS", iname);
snprintf(valbuf, 32, "%d.%d.%d.%d/%d.%d.%d.%d",
network[0] & 0xff, network[1] & 0xff, network[2] & 0xff,
network[3] & 0xff, netmask[0] & 0xff, netmask[1] & 0xff,
netmask[2] & 0xff, netmask[3] & 0xff);
}
bool ether_send(UBYTE *buf,int length,int d)
{
struct sockaddr whereto;
int one = 1;
int errflag = 0, verbose = 0, do_version = 0;
int i, c;
char *ifname = NULL;
switch(d)
{
case 0:
ifname = "eth0";
break;
case 1:
ifname = "eth1";
break;
case 2:
ifname = "eth2";
break;
default:
ifname = "eth3";
break;
}
if (! opt_no_src_addr)
{
struct ifreq if_hwaddr;
unsigned char *hwaddr = (unsigned char * )if_hwaddr.ifr_hwaddr.sa_data;
strcpy(if_hwaddr.ifr_name, ifname);
if (ioctl(ucsock, SIOCGIFHWADDR, &if_hwaddr) < 0)
{
#ifdef _DEBUG
fprintf(stderr, "SIOCGIFHWADDR on %ucsock failed: %ucsock\n", ifname,
strerror(errno));
#endif
return false;
}
}
if (setsockopt(ucsock, SOL_SOCKET, SO_BROADCAST, (char *)&one, sizeof(one)) < 0)
{
#ifdef _DEBUG
perror("setsockopt: SO_BROADCAST");
return false;
#endif
}
whereto.sa_family = 0;
strcpy(whereto.sa_data, ifname);
if ((i = sendto(ucsock, buf, length, 0, &whereto, sizeof(whereto))) < 0)
{
#ifdef _DEBUG
printf("Send Error!\n");
#endif
return false;
}
else
{
return true;
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -