📄 ha_function.c
字号:
#include "./common/common.h"
//#include "./common/sybfunc.c"
#include "./common/commonfunc.c"
struct sockaddr_in tx_addr1,tx_addr2,rx_addr2,rx_addr3,tx_addra,tx_addrb;
int socktxfd1,socktxfd2,sockrxfd2,sockrxfd3,socktxfda,socktxfdb;
char tempstr[100];
char errtxt[1024];
char cluster_ip[20],hosta_ip[20],hostb_ip[20];
char host_flag[10];
char mac_addr[40];
char broadcast_addr[40];
void release_common_ip(int sig_num){
char command_str[100];
printf("\nreleasing common ip\n");
if (host_flag[0]=='A'){
sprintf(command_str,"/sbin/ifconfig eth0 %s",hosta_ip);
}else if (host_flag[0]=='B'){
sprintf(command_str,"/sbin/ifconfig eth0 %s",hostb_ip);
}
system(command_str);
if (host_flag[0]=='A'){
strcpy(command_str,"/sbin/ifconfig eth0:1 192.168.15.111");
}else if (host_flag[0]=='B'){
strcpy(command_str,"/sbin/ifconfig eth0:1 192.168.15.222");
}
system(command_str);
}
void catch_alarm(int sig_num)
{
char command_str[100];
bzero(command_str,100);
sprintf(command_str,"/sbin/ifconfig eth0:1 %s",cluster_ip);
printf("\n%s\n",command_str);
system(command_str);
printf("\nget cluster ip\n");
bzero(command_str,100);
sprintf(command_str,"/sbin/send_arp %s %s %s ff:ff:ff:ff:ff:ff 10",cluster_ip,mac_addr,broadcast_addr);
printf("\n%s\n",command_str);
system(command_str);
if (host_flag[0]=='A') alarm(10);
if (host_flag[0]=='B') alarm(20);
}
main(int argc,char **argv)
{
unsigned char buffer[100];
char content[20];
char SIG_REQ[10];
char SIG_ANS[10];
int pid1,pid2;
bzero(tempstr,100);
bzero(cluster_ip,20);
bzero(SIG_REQ,10);
bzero(SIG_ANS,10);
bzero(host_flag,10);
printf("\n**********************************************\n");
printf("\n* Ha Function Block v1.1 *\n");
printf("\n* 2003-03-20 *\n");
printf("\n* www.etonetech.com *\n");
printf("\n**********************************************\n");
printf("\nha function init...\n");
sleep(10);
if(argc==8){
strcpy(tempstr,argv[2]);
if(strlen(tempstr)==0) exit(0);
strcpy(cluster_ip,tempstr);
strcpy(host_flag,argv[1]);
if (host_flag[0]=='A'){
strcpy(SIG_REQ,"A");
strcpy(SIG_ANS,"A");
}else{
strcpy(SIG_REQ,"B");
strcpy(SIG_ANS,"B");
}
strcpy(hosta_ip,argv[3]);
strcpy(hostb_ip,argv[4]);
strcpy(mac_addr,argv[6]);
strcpy(broadcast_addr,argv[7]);
printf("\nhosta_ip:%s\n",hosta_ip);
printf("\nhostb_ip:%s\n",hostb_ip);
printf("\nmac_addr:%s\n",mac_addr);
printf("\nbroadcast_addr:%s\n",broadcast_addr);
}else if (argc!=8){
printf("\nusage:ha_function <A OR B> <cluster_ip_addr> <host a ip> <host b ip> <software switch on/off> <mac_addr> <network_ip>\n");
exit (0);
}
if (strcmp(argv[5],"on")==0) signal(SIGINT, release_common_ip);/*******************signal*********************/
pid1 = fork(); /*******************fork()*********************/
if (pid1==0) pid2 = fork();
if ((pid1>0)&&(pid2>0)){
socktxfd1=socket(AF_INET,SOCK_DGRAM,0);
bzero(&tx_addr1,sizeof(struct sockaddr_in));
tx_addr1.sin_family=AF_INET;
tx_addr1.sin_port=htons(7100);
tx_addr1.sin_addr.s_addr=htonl(INADDR_ANY);
if(inet_aton(cluster_ip,&tx_addr1.sin_addr)==0){
printf("Ip error. \n");
exit(1);
}
printf("\nstart sending SIG to common ip...\n");
/*****************发送心跳信号************************/
for(;;){
sendto(socktxfd1,SIG_REQ,strlen(SIG_REQ),0,(struct sockaddr*)&tx_addr1,sizeof(struct sockaddr_in));
printf("\nsending heartbeat SIG_REQ\n");
sleep(1);
}
}
if ((pid1==0)&&(pid2>0)){ /*******************接收心跳信号*************************/
if((sockrxfd2 = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { /*******************<0*********************/
exit(-1);
}
bzero(&rx_addr2, sizeof(rx_addr2));
rx_addr2.sin_family = AF_INET;
rx_addr2.sin_addr.s_addr = htonl(INADDR_ANY);
rx_addr2.sin_port = htons(7100);
if(bind(sockrxfd2, (struct sockaddr *)&rx_addr2, sizeof(rx_addr2)) < 0) {
exit(-1);
}
socktxfda=socket(AF_INET,SOCK_DGRAM,0);
bzero(&tx_addra,sizeof(struct sockaddr_in));
tx_addra.sin_family=AF_INET;
tx_addra.sin_port=htons(7200);
tx_addra.sin_addr.s_addr=htonl(INADDR_ANY);
if(inet_aton(hosta_ip,&tx_addra.sin_addr)==0){
printf("Ip error. \n");
exit(1);
}
socktxfdb=socket(AF_INET,SOCK_DGRAM,0);
bzero(&tx_addrb,sizeof(struct sockaddr_in));
tx_addrb.sin_family=AF_INET;
tx_addrb.sin_port=htons(7300);
tx_addrb.sin_addr.s_addr=htonl(INADDR_ANY);
if(inet_aton(hostb_ip,&tx_addrb.sin_addr)==0){
printf("Ip error. \n");
exit(1);
}
for(;;){
bzero(buffer, 100);
if(recvfrom(sockrxfd2, buffer, 100, 0, (struct sockaddr *)0, (int *)0) < 0){
printf("\nreceive socket err...\n");
continue;
}
printf("\nreceived SIG_REQ\n");
if ((buffer[0]=='A')&&(host_flag[0]=='B')){
printf("\nHost %s recieve msg from host A\n",host_flag);
sendto(socktxfda,SIG_ANS,strlen(SIG_ANS),0,(struct sockaddr*)&tx_addra,sizeof(struct sockaddr_in));
}
if ((buffer[0]=='A')&&(host_flag[0]=='A')){
printf("\nHost %s recieve msg from host A\n",host_flag);
sendto(socktxfda,SIG_ANS,strlen(SIG_ANS),0,(struct sockaddr*)&tx_addra,sizeof(struct sockaddr_in));
}
if ((buffer[0]=='B')&&(host_flag[0]=='A')){
printf("\nHost %s recieve msg from host B\n",host_flag);
sendto(socktxfdb,SIG_ANS,strlen(SIG_ANS),0,(struct sockaddr*)&tx_addrb,sizeof(struct sockaddr_in));
}
if ((buffer[0]=='B')&&(host_flag[0]=='B')){
printf("\nHost %s recieve msg from host B\n",host_flag);
sendto(socktxfdb,SIG_ANS,strlen(SIG_ANS),0,(struct sockaddr*)&tx_addrb,sizeof(struct sockaddr_in));
}
printf("\nsending SIG_ANS\n");
}
}
/******************告警****************************/
if ((pid1==0)&&(pid2==0)){
if((sockrxfd3 = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
exit(-1);
}
bzero(&rx_addr3, sizeof(rx_addr3));
rx_addr3.sin_family = AF_INET;
rx_addr3.sin_addr.s_addr = htonl(INADDR_ANY);
if (host_flag[0]=='A'){
rx_addr3.sin_port = htons(7200);
}
if (host_flag[0]=='B'){
rx_addr3.sin_port = htons(7300);
}
if(bind(sockrxfd3, (struct sockaddr *)&rx_addr3, sizeof(rx_addr3)) < 0) {
exit(-1);
}
for(;;){
signal(SIGALRM, catch_alarm);
if (host_flag[0]=='A') alarm(10);
if (host_flag[0]=='B') alarm(20);
err:
bzero(buffer, 100);
if(recvfrom(sockrxfd3, buffer, 100, 0, (struct sockaddr *)0, (int *)0) < 0){
continue;
}
if ((buffer[0]!='A')&&(buffer[0]!='B')) goto err;
printf("\nreceiving SIG_REQ\n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -