⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ucast.c

📁 linux集群服务器软件代码包
💻 C
📖 第 1 页 / 共 2 页
字号:
/* $Id: ucast.c,v 1.23 2004/10/24 13:00:13 lge Exp $ *//* * Adapted from alanr's UDP broadcast heartbeat bcast.c by St閜hane Billiart *	<stephane@reefedge.com> * * (c) 2002  St閜hane Billiart <stephane@reefedge.com> * (c) 2002  Alan Robertson <alanr@unix.sh> * * Brian Tinsley <btinsley@emageon.com>  *  - allow use of hostname in ha.cf *  - set IP type of service of write socket *  - many MALLOC calls were not checked for failure *  - code janitor * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. *  * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. *  * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */#include <portability.h>#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <string.h>#ifdef HAVE_STRINGS_H#include <strings.h>#endif#include <ctype.h>#include <fcntl.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#ifndef HAVE_INET_ATON	extern  int     inet_aton(const char *, struct in_addr *);#endif#include <netinet/in_systm.h>#include <netinet/ip.h>#include <arpa/inet.h>#if defined(SO_BINDTODEVICE)#include <net/if.h>#endif#include <heartbeat.h>#include <HBcomm.h>/* * Plugin information */#define PIL_PLUGINTYPE          HB_COMM_TYPE#define PIL_PLUGINTYPE_S        HB_COMM_TYPE_S#define PIL_PLUGIN              ucast#define PIL_PLUGIN_S            "ucast"#define PIL_PLUGINLICENSE	LICENSE_LGPL#define PIL_PLUGINLICENSEURL	URL_LGPL#include <pils/plugin.h>/* * Macros/Defines */#define ISUCASTOBJECT(mp) ((mp) && ((mp)->vf == (void*)&ucastOps))#define UCASTASSERT(mp)	g_assert(ISUCASTOBJECT(mp))#define LOG		PluginImports->log#define MALLOC		PluginImports->alloc#define STRDUP  	PluginImports->mstrdup#define FREE		PluginImports->mfree#define	MAXBINDTRIES	10/* * Structure Declarations */struct ip_private {        char* interface;		/* Interface name */	struct in_addr heartaddr;	/* Peer node address */        struct sockaddr_in addr;	/* Local address */        int port;			/* UDP port */        int rsocket;			/* Read-socket */        int wsocket;			/* Write-socket */};/* * Function Prototypes */PIL_rc PIL_PLUGIN_INIT(PILPlugin *us, const PILPluginImports *imports);static int ucast_parse(const char *line);static struct hb_media* ucast_new(const char *intf, const char *addr);static int ucast_open(struct hb_media *mp);static int ucast_close(struct hb_media *mp);static void* ucast_read(struct hb_media *mp, int* lenp);static int ucast_write(struct hb_media *mp, void *msg, int len);static int HB_make_receive_sock(struct hb_media *ei);static int HB_make_send_sock(struct hb_media *mp);static struct ip_private* new_ip_interface(const char *ifn,				const char *hbaddr, int port);static int ucast_descr(char **buffer);static int ucast_mtype(char **buffer);static int ucast_isping(void);/* * External Data */extern struct hb_media *sysmedia[];extern int nummedia;/* * Module Public Data */const char hb_media_name[] = "UDP/IP unicast";	static struct hb_media_fns ucastOps = {	NULL,			ucast_parse,	ucast_open,	ucast_close,	ucast_read,	ucast_write,	ucast_mtype,	ucast_descr,	ucast_isping};PIL_PLUGIN_BOILERPLATE2("1.0", Debug)static const PILPluginImports*  PluginImports;static PILPlugin*               OurPlugin;static PILInterface*		OurInterface;static struct hb_media_imports*	OurImports;static void*			interfprivate;static int			localudpport;/* * Implmentation */PIL_rc PIL_PLUGIN_INIT(PILPlugin *us, const PILPluginImports *imports){	/* Force the compiler to do a little type checking */	(void)(PILPluginInitFun)PIL_PLUGIN_INIT;	PluginImports = imports;	OurPlugin = us;	/* Register ourself as a plugin */	imports->register_plugin(us, &OurPIExports);	/*  Register our interface implementation */ 	return imports->register_interface(us, PIL_PLUGINTYPE_S,		PIL_PLUGIN_S, &ucastOps, NULL,		&OurInterface, (void*)&OurImports, interfprivate); }static int ucast_parse(const char *line){ 	const char *bp = line;	int toklen;	struct hb_media *mp;	char dev[MAXLINE];	char ucast[MAXLINE];	/* Skip over white space, then grab the device */	bp += strspn(bp, WHITESPACE);	toklen = strcspn(bp, WHITESPACE);	strncpy(dev, bp, toklen);	bp += toklen;	dev[toklen] = EOS;	if (*dev != EOS)  {#ifdef NOTYET		if (!is_valid_dev(dev)) {			PILCallLog(LOG, PIL_CRIT, "ucast: bad device [%s]", dev);			return HA_FAIL;		}#endif		bp += strspn(bp, WHITESPACE);		toklen = strcspn(bp, WHITESPACE);		strncpy(ucast, bp, toklen);		bp += toklen;		ucast[toklen] = EOS;			if (*ucast == EOS)  {			PILCallLog(LOG, PIL_CRIT,			  "ucast: [%s] missing target IP address/hostname",			  dev);			return HA_FAIL;		}		if (!(mp = ucast_new(dev, ucast)))			return HA_FAIL;		sysmedia[nummedia++] = mp;	}	return HA_OK;}static int ucast_mtype(char **buffer){	*buffer = STRDUP(PIL_PLUGIN_S);	if (!*buffer) {		PILCallLog(LOG, PIL_CRIT, "ucast: memory allocation error (line %d)",				(__LINE__ - 2) );		return 0;	}	return strlen(*buffer);}static int ucast_descr(char **buffer){ 	*buffer = strdup(hb_media_name);	if (!*buffer) {		PILCallLog(LOG, PIL_CRIT, "ucast: memory allocation error (line %d)",				(__LINE__ - 2) );		return 0;	}	return strlen(*buffer);}static int ucast_isping(void){	return 0;}static int ucast_init(void){	struct servent *service;	g_assert(OurImports != NULL);	if (localudpport <= 0) {		const char *chport;		if ((chport  = OurImports->ParamValue("udpport")) != NULL) {			sscanf(chport, "%d", &localudpport);			if (localudpport <= 0) {				PILCallLog(LOG, PIL_CRIT,					"ucast: bad port number %s", chport);				return HA_FAIL;			}		}	}	/* No port specified in the configuration... */	if (localudpport <= 0) {		/* If our service name is in /etc/services, then use it */		if ((service=getservbyname(HA_SERVICENAME, "udp")) != NULL)			localudpport = ntohs(service->s_port);		else			localudpport = UDPPORT;	}	return HA_OK;}/* *	Create new UDP/IP unicast heartbeat object  *	Name of interface and address are passed as parameters */static struct hb_media*ucast_new(const char *intf, const char *addr){	struct ip_private *ipi;	struct hb_media *ret;	char *name;	ucast_init();	if (!(ipi = new_ip_interface(intf, addr, localudpport))) {		PILCallLog(LOG, PIL_CRIT, "ucast: interface [%s] does not exist", intf);		return NULL;	}	if (!(ret = (struct hb_media*)MALLOC(sizeof(struct hb_media)))) {		PILCallLog(LOG, PIL_CRIT, "ucast: memory allocation error (line %d)",			(__LINE__ - 2) );		FREE(ipi->interface);		FREE(ipi);	}	else {		ret->pd = (void*)ipi;		if (!(name = STRDUP(intf))) {			PILCallLog(LOG, PIL_CRIT,				"ucast: memory allocation error (line %d)",				(__LINE__ - 3) );			FREE(ipi->interface);			FREE(ipi);			FREE(ret);			ret = NULL;		}		else {			ret->name = name;		}	}	return ret;}/* *	Open UDP/IP unicast heartbeat interface */static int ucast_open(struct hb_media* mp){	struct ip_private * ei;	UCASTASSERT(mp);	ei = (struct ip_private*)mp->pd;	if ((ei->wsocket = HB_make_send_sock(mp)) < 0)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -