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

📄 cl_msg.c

📁 linux集群服务器软件代码包
💻 C
📖 第 1 页 / 共 5 页
字号:
		size_t	startlen = sizeof(MSG_START)-1;	size_t	startlen_netstring = sizeof(MSG_START_NETSTRING) -1 ;	int	internal_type;		int (*addfield) (struct ha_msg* msg, char* name, size_t namelen,			 void* value, size_t vallen, int depth);			if (!msg || msg->names == NULL || (msg->values == NULL) ) {		cl_log(LOG_ERR,	"ha_msg_addraw_ll: cannot add field to ha_msg");		return(HA_FAIL);	}		if (msg->nfields >= msg->nalloc) {		if( ha_msg_expand(msg) != HA_OK){			cl_log(LOG_ERR, "message expanding failed");			return(HA_FAIL);		}			}		if (namelen >= startlen && strncmp(name, MSG_START, startlen) == 0) {		if(!cl_msg_quiet_fmterr) {			cl_log(LOG_ERR, "ha_msg_addraw_ll: illegal field");		}		return(HA_FAIL);	}	if (namelen >= startlen_netstring	    && strncmp(name, MSG_START_NETSTRING, startlen_netstring) == 0){		if(!cl_msg_quiet_fmterr) {			cl_log(LOG_ERR, "ha_msg_addraw_ll: illegal field");		}	}		if (name == NULL || (value == NULL)	    ||	namelen <= 0 || vallen < 0) {		cl_log(LOG_ERR, "ha_msg_addraw_ll: "		       "cannot add name/value to ha_msg");		return(HA_FAIL);	}		internal_type = type;		HA_MSG_ASSERT(type < DIMOF(fieldtypefuncs));		addfield =  fieldtypefuncs[type].addfield;	if (!addfield || 	    addfield(msg, name, namelen, value, vallen,depth) != HA_OK){		cl_log(LOG_ERR, "ha_msg_addraw_ll: addfield failed");		return(HA_FAIL);	}		AUDITMSG(msg);	return(HA_OK);}static intha_msg_addraw(struct ha_msg * msg, const char * name, size_t namelen,	      const void * value, size_t vallen, int type, int depth){	char	*cpvalue = NULL;	char	*cpname = NULL;	int	ret;	if ((cpname = ha_malloc(namelen+1)) == NULL) {		cl_log(LOG_ERR, "ha_msg_addraw: no memory for string (name)");		return(HA_FAIL);	}	strncpy(cpname, name, namelen);	cpname[namelen] = EOS;		HA_MSG_ASSERT(type < DIMOF(fieldtypefuncs));		if (fieldtypefuncs[type].dup){		cpvalue = fieldtypefuncs[type].dup(value, vallen);		}	if (cpvalue == NULL){		cl_log(LOG_ERR, "ha_msg_addraw: copying message failed");		ha_free(cpname);		return(HA_FAIL);	}		ret = ha_msg_addraw_ll(msg, cpname, namelen, cpvalue, vallen	,	type, depth);	if (ret != HA_OK){		cl_log(LOG_ERR, "ha_msg_addraw(): ha_msg_addraw_ll failed");		ha_free(cpname);		fieldtypefuncs[type].memfree(cpvalue);	}	return(ret);}/*Add a null-terminated name and binary value to a message*/intha_msg_addbin(struct ha_msg * msg, const char * name,	      const void * value, size_t vallen){	return(ha_msg_addraw(msg, name, strlen(name),			     value, vallen, FT_BINARY, 0));}int ha_msg_adduuid(struct ha_msg* msg, const char *name, const uuid_t u){	return(ha_msg_addraw(msg, name, strlen(name)	,	u, sizeof(uuid_t), FT_BINARY, 0));}/*Add a null-terminated name and struct value to a message*/intha_msg_addstruct(struct ha_msg * msg, const char * name, const void * value){		return ha_msg_addraw(msg, name, strlen(name), value, 			     sizeof(struct ha_msg), FT_STRUCT, 0);}intha_msg_add_int(struct ha_msg * msg, const char * name, int value){	char buf[MAX_INT_LEN];	snprintf(buf, MAX_INT_LEN, "%d", value);	return (ha_msg_add(msg, name, buf));	}intha_msg_mod_int(struct ha_msg * msg, const char * name, int value){	char buf[MAX_INT_LEN];	snprintf(buf, MAX_INT_LEN, "%d", value);	return (cl_msg_modstring(msg, name, buf));	}intha_msg_value_int(const struct ha_msg * msg, const char * name, int* value){	const char* svalue = ha_msg_value(msg, name);	if(NULL == svalue) {		return HA_FAIL;	}	*value = atoi(svalue);	return HA_OK;}intha_msg_add_uuid(struct ha_msg * msg, const char * name, const uuid_t id){	char buf[UUID_SLEN];	uuid_unparse(id, buf);	return (ha_msg_nadd(msg, name, strlen(name), buf, strlen(buf)));}intha_msg_value_uuid(struct ha_msg * msg, const char * name, uuid_t id){	const char* value = ha_msg_value(msg, name);	char buf[UUID_SLEN];	if (NULL == value) {		return HA_FAIL;	}	strncpy(buf,value,UUID_SLEN);	if( 0 != uuid_parse(value, id)) {		return HA_FAIL;	}	return HA_OK;}/* * ha_msg_value_str_list()/ha_msg_add_str_list(): * transform a string list suitable for putting into an ha_msg is by a convention * of naming the fields into the following format: *	listname1=foo *	listname2=bar *	listname3=stuff *	etc. */GList* ha_msg_value_str_list(struct ha_msg * msg, const char * name){		int i = 1;	int len = 0;	const char* value;	char* element;	GList* list = NULL;			if( NULL==msg||NULL==name||strnlen(name, MAX_NAME_LEN)>=MAX_NAME_LEN ){		return NULL;	}		len = cl_msg_list_length(msg,name);	for(i=0; i<len; i++) {		value = cl_msg_list_nth_data(msg,name,i);		if (NULL == value) {			break;		}		element = g_strdup(value);		list = g_list_append(list, element);	}	return list;}intha_msg_add_str_list(struct ha_msg * msg, const char * name, GList* list){	int i = 1;	if( NULL==msg||NULL==name||strnlen(name, MAX_NAME_LEN)>=MAX_NAME_LEN ){		return HA_FAIL;	}		if (NULL != list) {		GList* element = g_list_first(list);		while (NULL != element) {			char* value = (char*)element->data;			if( HA_OK != cl_msg_list_add_string(msg,name,value)) {				cl_log(LOG_ERR,				"cl_msg_list_add_string failed");				return HA_FAIL;			}			element = g_list_next(element);			i++;		}	}	return HA_OK;}static voidpair_to_msg(gpointer key, gpointer value, gpointer user_data){	struct ha_msg* msg = (struct ha_msg*)user_data;	if( HA_OK != ha_msg_add(msg, key, value)) {		cl_log(LOG_ERR, "ha_msg_add in pair_to_msg failed");	}}static struct ha_msg*str_table_to_msg(GHashTable* hash_table){	struct ha_msg* hash_msg;	if ( NULL == hash_table) {		return NULL;	}	hash_msg = ha_msg_new(5);	g_hash_table_foreach(hash_table, pair_to_msg, hash_msg);	return hash_msg;}static GHashTable*msg_to_str_table(struct ha_msg * msg){	int i;	GHashTable* hash_table;	if ( NULL == msg) {		return NULL;	}	hash_table = g_hash_table_new(g_str_hash, g_str_equal);	for (i = 0; i < msg->nfields; i++) {		if( FT_STRING != msg->types[i] ) {			continue;		}		g_hash_table_insert(hash_table,				    g_strndup(msg->names[i],msg->nlens[i]),				    g_strndup(msg->values[i],msg->vlens[i]));	}	return hash_table;}GHashTable*ha_msg_value_str_table(struct ha_msg * msg, const char * name){	struct ha_msg* hash_msg;	GHashTable * hash_table = NULL;	if (NULL == msg || NULL == name) {		return NULL;	}	hash_msg = cl_get_struct(msg, name);	if (NULL == hash_msg) {		return NULL;	}	hash_table = msg_to_str_table(hash_msg);	return hash_table;}intha_msg_add_str_table(struct ha_msg * msg, const char * name,			GHashTable* hash_table){	struct ha_msg* hash_msg;	if (NULL == msg || NULL == name || NULL == hash_table) {		return HA_FAIL;	}	hash_msg = str_table_to_msg(hash_table);	if( HA_OK != ha_msg_addstruct(msg, name, hash_msg)) {		ha_msg_del(hash_msg);		cl_log(LOG_ERR, "ha_msg_add in ha_msg_add_str_table failed");		return HA_FAIL;	}	ha_msg_del(hash_msg);	return HA_OK;}intcl_msg_list_add_string(struct ha_msg* msg, const char* name, const char* value){	GList* list = NULL;	int ret;	char buf[MAXMSG];		if(!msg || !name || !value){		cl_log(LOG_ERR, "cl_msg_list_add_string: input invalid");		return HA_FAIL;	}			strncpy(buf, value, MAXMSG);	list = g_list_append(list, buf);	if (!list){		cl_log(LOG_ERR, "cl_msg_list_add_string: append element to"		       "a glist failed");		return HA_FAIL;	}		ret = ha_msg_addraw(msg, name, strlen(name), list, 			    string_list_pack_length(list),			    FT_LIST, 0);		g_list_free(list);		return ret;}/* Add a null-terminated name and value to a message */intha_msg_add(struct ha_msg * msg, const char * name, const char * value){	if(name == NULL || value == NULL) {		return HA_FAIL;	}	return(ha_msg_nadd(msg, name, strlen(name), value, strlen(value)));}/* Add a name/value pair to a message (with sizes for name and value) */intha_msg_nadd(struct ha_msg * msg, const char * name, int namelen	    ,	const char * value, int vallen){	return(ha_msg_addraw(msg, name, namelen, value, vallen, FT_STRING, 0));}/* Add a name/value/type to a message (with sizes for name and value) */intha_msg_nadd_type(struct ha_msg * msg, const char * name, int namelen	    ,	const char * value, int vallen, int type){	return(ha_msg_addraw(msg, name, namelen, value, vallen, type, 0));}/* Add a "name=value" line to the name, value pairs in a message */static intha_msg_add_nv_depth(struct ha_msg* msg, const char * nvline,		    const char * bufmax, int depth){	int		namelen;	const char *	valp;	int		vallen;	if (!nvline) {		cl_log(LOG_ERR, "ha_msg_add_nv: NULL nvline");		return(HA_FAIL);	}	/* How many characters before the '='? */	if ((namelen = strcspn(nvline, EQUAL)) <= 0	||	nvline[namelen] != '=') {		if (!cl_msg_quiet_fmterr) {			cl_log(LOG_WARNING			,	"ha_msg_add_nv_depth: line doesn't contain '='");			cl_log(LOG_INFO, "%s", nvline);		}		return(HA_FAIL);	}	valp = nvline + namelen +1; /* Point just *past* the '=' */	if (valp >= bufmax){		return HA_FAIL;	}	vallen = strcspn(valp, CRNL);	if ((valp + vallen) >= bufmax){		return HA_FAIL;	}	if (vallen == 0){		valp = NULL;	}	/* Call ha_msg_nadd to actually add the name/value pair */	return(ha_msg_addraw(msg, nvline, namelen, valp, vallen	,	FT_STRING, depth));}intha_msg_add_nv(struct ha_msg* msg, const char * nvline,	      const char * bufmax){	return(ha_msg_add_nv_depth(msg, nvline, bufmax, 0));}static void *cl_get_value(const struct ha_msg * msg, const char * name,	     size_t * vallen, int *type){	int	j;	if (!msg || !msg->names || !msg->values) {		cl_log(LOG_ERR, "ha_msg_value: NULL msg");		return(NULL);	}	AUDITMSG(msg);	for (j=0; j < msg->nfields; ++j) {		if (strcmp(name, msg->names[j]) == 0) {			if (vallen){				*vallen = msg->vlens[j];			}			if (type){				*type = msg->types[j];			}			return(msg->values[j]);		}	}	return(NULL);}const void *cl_get_binary(const struct ha_msg *msg,	      const char * name, size_t * vallen){	const void	*ret;	int		type;	ret = cl_get_value( msg, name, vallen, &type);		if (ret == NULL){		/*		cl_log(LOG_WARNING, "field %s not found", name);		cl_log_message(msg);		*/		return(NULL);	}	if ( type != FT_BINARY){		cl_log(LOG_WARNING, "field %s is not binary", name);		cl_log_message(LOG_WARNING, msg);		return(NULL);	}	return(ret);}/* UUIDs are stored with a machine-independent byte ordering (even though it's binary) */intcl_get_uuid(const struct ha_msg *msg, const char * name, uuid_t retval){	const void *	vret;	size_t		vretsize;		uuid_clear(retval);	if ((vret = cl_get_binary(msg, name, &vretsize)/*discouraged function*/) == NULL) {		/* But perfectly portable in this case */		return HA_FAIL;	}	if (vretsize != sizeof(uuid_t)) {		cl_log(LOG_WARNING, "Binary field %s is not a uuid.", name);		cl_log(LOG_INFO, "expecting %d bytes, got %d bytes",		       (int)sizeof(uuid_t), (int)vretsize);		cl_log_message(LOG_INFO, msg);		return HA_FAIL;	}	memcpy(retval, vret, sizeof(uuid_t));	return HA_OK;}const char *cl_get_string(const struct ha_msg *msg, const char *name){

⌨️ 快捷键说明

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