📄 cl_msg.c
字号:
return(HA_FAIL); } if (convert(bp, baselen, depth, NL_TO_SYM) != HA_OK){ cl_log(LOG_ERR , "msg2string_buf(): convert failed"); return(HA_FAIL); } bp += strlen(bp); } strcat(bp,"\n"); bp++; } if (needhead){ strcat(bp, MSG_END); bp += strlen(MSG_END); } bp[0] = 0; if (bp > buf + len){ cl_log(LOG_ERR, "msg2string_buf: out of memory bound," "bp=%p, buf + len=%p, len=%ld \n", bp, buf + len, (long)len); cl_log_message(m); return(HA_FAIL); } return(HA_OK);}char *msg2string(const struct ha_msg *m){ void *buf; int len; AUDITMSG(m); if (m->nfields <= 0) { cl_log(LOG_ERR, "msg2string: Message with zero fields"); return(NULL); } len = get_stringlen(m, 0); buf = ha_malloc(len); if (buf == NULL) { cl_log(LOG_ERR, "msg2string: no memory for string"); return(NULL); }else if (msg2string_buf(m, buf, len ,0, NEEDHEAD) != HA_OK){ cl_log(LOG_ERR, "msg2string: msg2string_buf failed"); ha_free(buf); return(NULL); } return(buf);}intget_stringlen(const struct ha_msg *m, int depth){ int stringlen = m->stringlen; int i; if (depth >= MAXDEPTH){ cl_log(LOG_ERR, "get_stringlen(), MAXDEPTH exceeded"); return(0); } for (i = 0; i < m->nfields; i++){ if (m->types[i] == FT_STRUCT){ int tmp; tmp = get_stringlen((struct ha_msg*)m->values[i] , depth + 1); if (tmp == 0){ cl_log(LOG_ERR, "get_stringlen(), 0 is returned"); return(0); } stringlen += tmp; } } return(stringlen);}intget_netstringlen(const struct ha_msg *m, int depth){ int netstringlen = m->netstringlen; int i; if (depth >= MAXDEPTH){ cl_log(LOG_ERR, "get_netstringlen(), MAXDEPTH exceeded"); return(0); } for (i = 0; i < m->nfields; i++){ if (m->types[i] == FT_STRUCT){ int tmp; int namelen = m->nlens[i]; tmp = get_netstringlen((struct ha_msg*)m->values[i] , depth + 1); if (tmp <= 0){ cl_log(LOG_ERR , "get_stringlen(), %d is returned" , tmp); return(0); } netstringlen += intlen(namelen) + namelen + 2; /* for name */ netstringlen += 4; /* for type */ netstringlen += intlen(tmp) + tmp + 2; /* for child message */ } } return(netstringlen);}char*msg2wirefmt(const struct ha_msg*m, size_t* len){ if (msgfmt == MSGFMT_NETSTRING) { return(msg2netstring(m, len)); } else{ char *tmp; tmp = msg2string(m);/* *len = m->stringlen; cl_log(LOG_INFO, "m->stringlen =%d,strlen(tmp)=%d", m->stringlen, strlen(tmp));*/ *len = strlen(tmp) + 1; return(tmp); }}static struct ha_msg*wirefmt2msg_ll(const char* s, size_t length, int need_auth){ int startlen; startlen = sizeof(MSG_START)-1; if (strncmp(s, MSG_START, startlen) == 0) { return(string2msg_ll(s, length, 0, need_auth)); } startlen = sizeof(MSG_START_NETSTRING) - 1; if (strncmp(s, MSG_START_NETSTRING, startlen) == 0) { return netstring2msg(s, length, need_auth); } return NULL;}struct ha_msg*wirefmt2msg(const char* s, size_t length){ return(wirefmt2msg_ll(s, length, 1));}voidcl_log_message (const struct ha_msg *m){ int j; AUDITMSG(m); cl_log(LOG_INFO, "MSG: Dumping message with %d fields", m->nfields); for (j=0; j < m->nfields; ++j) { switch(m->types[j]){ case(FT_BINARY): case(FT_STRUCT): cl_log(LOG_INFO, "MSG[%d] : [(%s)%s=%p]",j , FT_strings[m->types[j]] , m->names[j] ? m->names[j] : "NULL" , m->values[j] ? m->values[j] : "NULL"); if (m->types[j] == FT_STRUCT && m->values[j]){ cl_log_message((struct ha_msg*)m->values[j]); } break; default: /* case(FT_STRING): */ cl_log(LOG_INFO, "MSG[%d] : [%s=%s]", j , m->names[j] ? m->names[j] : "NULL" , (const char*)(m->values[j] ? m->values[j] : "NULL")); } }}#ifdef TESTMAIN_MSGSintmain(int argc, char ** argv){ struct ha_msg* m; while (!feof(stdin)) { if ((m=controlfifo2msg(stdin)) != NULL) { fprintf(stderr, "Got message!\n"); if (msg2stream(m, stdout) == HA_OK) { fprintf(stderr, "Message output OK!\n"); }else{ fprintf(stderr, "Could not output Message!\n"); } }else{ fprintf(stderr, "Could not get message!\n"); } } return(0);}#endif/* * $Log: cl_msg.c,v $ * Revision 1.6.2.12 2005/01/03 20:28:45 gshi * fixed an alignment warning for IA64 * * Revision 1.6.2.11 2004/09/26 00:45:50 alan * BEAM FIX: We sometimes might cl_free() something gotten from ha_msg_new(). * * Revision 1.6.2.10 2004/09/22 17:01:09 gshi * brought HEAD change into STABLE branch * * Revision 1.6.2.9 2004/09/18 06:20:33 alan * Changed a message on data corruption so that it won't come out * if it's been suppressed. * * Revision 1.6.2.8 2004/09/15 06:27:58 alan * BEAM FIX: BEAM caught an subtle and extremely difficult to find bug. * It noticed that in some branches a void* was from ha_malloc, and in other * cases it was from ha_msg_new(), and that when I was freeing it, I could * sometimes free the ha_msg with ha_malloc(). I fixed the code, and now * it is quite happy. AWESOME! I'm impressed! * * Revision 1.6.2.7 2004/09/15 06:09:06 alan * A few BEAM fixes - mainly for memory leaks -- and apparent ones ;-) * This one includes a BEAM workaround that is works around what is * IMHO a subtle BEAM bug... * * Revision 1.6.2.6 2004/09/14 18:15:48 alan * BEAM FIX: Two possible NULL pointer references eliminated. * * Revision 1.6.2.5 2004/09/11 06:36:33 msoffen * Moved variable definitions to top of functions/blocks, changed comments to c style comments. * * Revision 1.6.2.4 2004/08/31 22:29:19 alan * Fixed a memory leak (brought back from HEAD branch) * * Revision 1.6.2.3 2004/08/05 18:30:47 alan * Put in changes to support a new option which allows one to suppress * notification of packet corruption. * * Revision 1.6.2.2 2004/04/29 01:23:44 alan * Repaired a completely broken fix to a slight portability problem for FreeBSD. * %xd is NOT remotely equivalent to %zd. * * Revision 1.8 2004/04/28 17:27:13 gshi * Fix potential (though probably unlikely) memory leak * similar to the one found in cl_netstring.c by kevin * * Revision 1.7 2004/04/21 14:33:56 msoffen * %z is not a standard formatting character. Changed to a %x * * Revision 1.6 2004/04/17 13:45:13 alan * More FreeBSD/64-bit problems fixed. Problems found by ward.viaene@student.khleuven.be. * * Revision 1.5 2004/04/17 07:54:50 alan * Fixed a number of 64-bit portability issues discovered by Ward Viaene in FreeBSD. * * Revision 1.4 2004/04/02 12:06:39 andrew * Link the size of the receive buffer/limit to that of the send buffer/limit * * Revision 1.3 2004/03/31 23:34:44 alan * Fixed a bug I introduced into the netstrings stuff - when I moved things * from the heartbeat directory to the lib directory * * Revision 1.2 2004/03/25 08:05:23 alan * Moved libraries from heartbeat to lib directory * also fixed numerous signed/unsigned problems... * * Revision 1.1 2004/03/24 17:04:08 alan * Moved ha_msg.c and netstring.c to the lib/clplumbing directory. * * Revision 1.52 2004/03/10 22:52:46 andrew * Allow people to distinguish between this error and one further up. * * Revision 1.51 2004/03/05 17:25:18 alan * cleanup of netstring patch. * Hopefully it also cleaned up the size_t vs int problems in the code. * * Revision 1.50 2004/03/03 05:31:50 alan * Put in Gochun Shi's new netstrings on-the-wire data format code. * this allows sending binary data, among many other things! * * Revision 1.49 2004/02/17 22:11:57 lars * Pet peeve removal: _Id et al now gone, replaced with consistent Id header. * * Revision 1.48 2004/01/21 11:34:14 horms * - Replaced numerous malloc + strcpy/strncpy invocations with strdup * * This usually makes the code a bit cleaner * * Also is easier not to make code with potential buffer over-runs * - Added STRDUP to pils modules * - Removed some spurious MALLOC and FREE redefinitions * _that could never be used_ * - Make sure the return value of strdup is honoured in error conditions * * Revision 1.47 2003/11/10 08:55:20 lars * Bugfixes by Deng, Pan: * * - While receiving a ha_msg, the default number of fields is MINFIELDS, * which is 20. After the reception, if more than 20 fields needed to be * added, it will fail. I changed the MINFIELDS to 30. It is not a * graceful fix, but it can work for checkpoint service. I think the max * fields should not be fixed. * * - The message create routine ha_msg_new() in ha_msg.c. It takes a * parameter nfields, but the function does not use it at all. If nfields * > MINFIELDS, the allocated fields should be nfields. * * Revision 1.46 2003/10/29 04:05:00 alan * Changed things so that the API uses IPC instead of FIFOs. * This isn't 100% done - python API code needs updating, and need to check authorization * for the ability to "sniff" other people's packets. * * Revision 1.45 2003/07/14 04:30:49 alan * This patch from Kurosawa-san (by way of Horms): * Heartbeat uses poll() in order to check messages in API FIFO and * stdio functions to read messages. stdio functions (fgets() in * msgfromstream() in this case) uses a internal buffer. When an application * sends 2 messages at a time to API FIFO, heartbeat's fgets() in * msgfromstream() may read 2 messages to the internal buffer at a time. * But heartbeat processes only one message and leaves the latter * message, because there is no poll() event for the file descriptor. * * Revision 1.44 2003/06/24 06:36:51 alan * Fixed an unsafe sprintf which occurred only when high levels of debug * were turned on. * * Revision 1.43 2003/05/09 15:15:37 alan * Turned off the most expensive and onerous debugging code. * * Revision 1.42 2003/04/18 06:33:54 alan * Changed the audit code for messages to tolerate NULL message pointers. * * Revision 1.41 2003/04/18 06:09:46 alan * Fixed an off-by-one error in writing messages to the FIFO. * Also got rid of some now-unused functions, and fixed a minor glitch in BasicSanitCheck. * * Revision 1.40 2003/04/15 23:05:01 alan * Added new message copying function, and code * to check the integrity of messages. Too slow now, will turn it down later. * * Revision 1.39 2003/03/27 07:04:26 alan * 1st step in heartbeat process restructuring. * Create fifo_child() processes to read the FIFO written by the shell scripts. * * Revision 1.38 2003/02/07 08:37:16 horms * Removed inclusion of portability.h from .h files * so that it does not need to be installed. * * Revision 1.37 2003/02/05 09:06:33 horms * Lars put a lot of work into making sure that portability.h * is included first, everywhere. However this broke a few * things when building against heartbeat headers that * have been installed (usually somewhere under /usr/include or * /usr/local/include). * * This patch should resolve this problem without undoing all of * Lars's hard work. * * As an asside: I think that portability.h is a virus that has * infected all of heartbeat's code and now must also infect all * code that builds against heartbeat. I wish that it didn't need * to be included all over the place. Especially in headers to * be installed on the system. However, I respect Lars's opinion * that this is the best way to resolve some weird build problems * in the current tree. * * Revision 1.36 2002/11/22 07:04:39 horms * make lots of symbols static * * Revision 1.35 2002/10/30 17:17:40 alan * Added some debugging, and changed one message from an ERROR to a WARNING. * * Revision 1.34 2002/10/22 17:41:58 alan * Added some documentation about deadtime, etc. * Switched one of the sets of FIFOs to IPC channels. * Added msg_from_IPC to ha_msg.c make that easier. * Fixed a few compile errors that were introduced earlier. * Moved hb_api_core.h out of the global include directory, * and back into a local directory. I also make sure it doesn't get * installed. This *shouldn't* cause problems. * Added a ipc_waitin() function to the IPC code to allow you to wait for * input synchronously if you really want to. * Changes the STONITH test to default to enabled. * * Revision 1.33 2002/10/21 10:17:18 horms * hb api clients may now be built outside of the heartbeat tree * * Revision 1.32 2002/10/18 07:16:08 alan * Put in Horms big patch plus a patch for the apcmastersnmp code where * a macro named MIN returned the MAX instead. The code actually wanted * the MAX, so when the #define for MIN was surrounded by a #ifndef, then * it no longer worked... This fix courtesy of Martin Bene. * There was also a missing #include needed on older Linux systems. * * Revision 1.31 2002/10/08 14:33:18 msoffen * Changed cl_log_message to be NULL safe. * * Revision 1.30 2002/10/02 13:36:42 alan * Put in a fix from Nathan Wallwork for a potential security vulnerability. * * Revision 1.29 2002/09/26 06:09:38 horms * log a debug message if it looks like an feild in a heartbeat message has been truncated * * Revision 1.28 2002/09/20 02:09:50 alan * Switched heartbeat to do everything with longclock_t instead of clock_t. * Switched heartbeat to be configured fundamentally from millisecond times. * Changed heartbeat to not use alarms for much of anything. * These are relatively major changes, but the seem to work fine. * * Revision 1.27 2002/09/17 20:48:06 alan * Put in a check for NULL in ha_msg_mod(). * * Revision 1.26 2002/08/10 02:13:32 alan * Better error logging when ha_msg functions are given bad name/value pairs. * * Revision 1.25 2002/07/08 04:14:12 alan * Updated comments in the front of various files. * Removed Matt's Solaris fix (which seems to be illegal on Linux). * * Revision 1.24 2002/04/13 22:35:08 alan * Changed ha_msg_add_nv to take an end pointer to make it safer. * Added a length parameter to string2msg so it would be safer. * Changed the various networking plugins to use the new string2msg(). * * Revision 1.23 2002/04/11 05:57:44 alan * Made some of the debugging output clearer. * * Revision 1.22 2002/02/21 21:43:33 alan * Put in a few fixes to make the client API work more reliably. * Put in a few changes to the process exit handling code which * also cause heartbeat to (attempt to) restart when it finds one of it's * own processes dies. Restarting was already broken :-( * * Revision 1.21 2002/02/14 14:09:29 alan * Put in a change requested by Ram Pai to allow message values to be * empty strings. * * Revision 1.20 2001/10/24 20:46:28 alan * A large number of patches. They are in these categories: * Fixes from Matt Soffen * Fixes to test environment things - including changing some ERRORs to * WARNings and vice versa. * etc. * * Revision 1.19 2001/08/21 15:37:13 alan * Put in code to make sure the calls in msg2stream get checked for errors... * * Revision 1.18 2001/06/19 13:56:28 alan * FreeBSD portability patch from Matt Soffen. * Mainly added #include "portability.h" to lots of files. * Also added a library to Makefile.am * * Revision 1.17 2001/06/12 17:05:47 alan * Fixed bug reported by Emily Ratliff <ratliff@austin.ibm.com> * In ha_msg_mod() the code fails to update the stringlen value for * fields modified by the input parameters. * This could potentially cause a crash. * Thanks to Emily for reporting this bug! * * Revision 1.16 2001/05/11 14:55:06 alan * Followed David Lee's suggestion about splitting out all the heartbeat process * management stuff into a separate header file... * Also changed to using PATH_MAX for maximum pathname length. * * Revision 1.15 2000/07/26 05:17:19 alan * Added GPL license statements to all the code. * * Revision 1.14 2000/07/19 23:03:53 alan * Working version of most of the API code. It still has the security bug... * * Revision 1.13 2000/07/11 14:42:42 alan * More progress on API code. * * Revision 1.12 2000/07/11 00:25:52 alan * Added a little more API code. It looks like the rudiments are now working. * * Revision 1.11 2000/05/11 22:47:50 alan * Minor changes, plus code to put in hooks for the new API. * * Revision 1.10 2000/04/12 23:03:49 marcelo * Added per-link status instead per-host status. Now we will able * to develop link<->service dependacy scheme. * * Revision 1.9 1999/11/22 20:28:23 alan * First pass of putting real packet retransmission. * Still need to request missing packets from time to time * in case retransmit requests get lost. * * Revision 1.8 1999/10/25 15:35:03 alan * Added code to move a little ways along the path to having error recovery * in the heartbeat protocol. * Changed the code for serial.c and ppp-udp.c so that they reauthenticate * packets they change the ttl on (before forwarding them). * * Revision 1.7 1999/10/10 20:11:56 alanr * New malloc/free (untested) * * Revision 1.6 1999/10/05 06:00:55 alanr * Added RPM Cflags to Makefiles * * Revision 1.5 1999/10/03 03:13:43 alanr * Moved resource acquisition to 'heartbeat', also no longer attempt to make the FIFO, it's now done in heartbeat. It should now be possible to start it up more readily... * * Revision 1.4 1999/09/29 03:22:05 alanr * Added the ability to reread auth config file on SIGHUP * * Revision 1.3 1999/09/26 21:59:58 alanr * Allow multiple auth strings in auth file... (I hope?) * * Revision 1.2 1999/09/26 14:01:01 alanr * Added Mijta's code for authentication and Guenther Thomsen's code for serial locking and syslog reform * * Revision 1.9 1999/09/16 05:50:20 alanr * Getting ready for 0.4.3... * * Revision 1.8 1999/08/25 06:34:26 alanr * Added code to log outgoing messages in a FIFO... * * Revision 1.7 1999/08/18 04:28:48 alanr * added function to dump a message to the log... * * Revision 1.6 1999/08/17 03:46:48 alanr * added log entry... * */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -