botsense  3.2.0
RoadNarrows Client-Server Proxied Services Framework
bsLibUtils.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: BotSense
4 //
5 // Library: libbsclient
6 //
7 // File: bsLibUtils.c
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2010-08-20 11:36:38 -0600 (Fri, 20 Aug 2010) $
12  * $Rev: 568 $
13  *
14  * \brief Library utilities.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \copyright
19  * \h_copy 2009-2017. RoadNarrows LLC.\n
20  * http://www.roadnarrows.com\n
21  * All Rights Reserved
22  */
23 // Permission is hereby granted, without written agreement and without
24 // license or royalty fees, to use, copy, modify, and distribute this
25 // software and its documentation for any purpose, provided that
26 // (1) The above copyright notice and the following two paragraphs
27 // appear in all copies of the source code and (2) redistributions
28 // including binaries reproduces these notices in the supporting
29 // documentation. Substantial modifications to this software may be
30 // copyrighted by their authors and need not follow the licensing terms
31 // described here, provided that the new terms are clearly indicated in
32 // all files where they apply.
33 //
34 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
35 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
36 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
37 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
38 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
39 // THE POSSIBILITY OF SUCH DAMAGE.
40 //
41 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
42 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
43 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
44 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
45 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
46 //
47 ////////////////////////////////////////////////////////////////////////////////
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <libgen.h>
52 #include <string.h>
53 
54 #include "rnr/rnrconfig.h"
55 #include "rnr/log.h"
56 #include "rnr/netmsgs.h"
57 
58 #include "botsense/BotSense.h"
59 #include "botsense/libBotSense.h"
60 #include "botsense/bsProxyMsgs.h"
61 
62 #include "bsLibInternal.h"
63 
64 
65 // ---------------------------------------------------------------------------
66 // Private Interface
67 // ---------------------------------------------------------------------------
68 
69 
70 // ---------------------------------------------------------------------------
71 // Public Interface
72 // ---------------------------------------------------------------------------
73 
74 /*!
75  * \brief Pack \h_botsense bsProxy message header.
76  *
77  * \param [in] pMsgHdr Pointer to message header structure.
78  * \param [out] buf Output message buffer.
79  * \param bufSize Size of output buffer.
80  *
81  * \returns
82  * On success, returns the number of bytes packed.
83  * \copydoc doc_return_ecode
84  */
85 int bsPackMsgHdr(BsProxyMsgHdr_T *pMsgHdr, byte_t buf[], size_t bufSize)
86 {
87  int n = 0;
88 
89  if( bufSize < BSPROXY_MSG_HDR_LEN )
90  {
91  LOGERROR("Buffer size=%zu too small.", bufSize);
92  return -BS_ECODE_INTERNAL;
93  }
94 
95  n += nmPackU16(pMsgHdr->m_hdrMagic, buf+n, bufSize-(size_t)n, NMEndianBig);
96  n += nmPackU8(pMsgHdr->m_hdrTid, buf+n, bufSize-(size_t)n, NMEndianBig);
97  n += nmPackU8(pMsgHdr->m_hdrVConn, buf+n, bufSize-(size_t)n, NMEndianBig);
98  n += nmPackU16(pMsgHdr->m_hdrMsgId, buf+n, bufSize-(size_t)n, NMEndianBig);
99  n += nmPackU16(pMsgHdr->m_hdrBodyLen, buf+n, bufSize-(size_t)n, NMEndianBig);
100 
101  return n;
102 }
103 
104 /*!
105  * \brief Unpack \h_botsense bsProxy message header.
106  *
107  * \param [in] buf Input message buffer.
108  * \param bufSize Size of input buffer.
109  * \param [out] pMsgHdr Pointer to message header structure.
110  *
111  * \returns
112  * On success, returns the number of bytes unpacked.
113  * \copydoc doc_return_ecode
114  */
115 int bsUnpackMsgHdr(byte_t buf[], size_t bufSize, BsProxyMsgHdr_T *pMsgHdr)
116 {
117  int n = 0;
118 
119  if( bufSize < BSPROXY_MSG_HDR_LEN )
120  {
121  LOGERROR("Buffer size=%zu too small.", bufSize);
122  return -BS_ECODE_INTERNAL;
123  }
124 
125  n += nmUnpackU16(buf+n, bufSize-(size_t)n, &pMsgHdr->m_hdrMagic, NMEndianBig);
126  n += nmUnpackU8(buf+n, bufSize-(size_t)n, &pMsgHdr->m_hdrTid, NMEndianBig);
127  n += nmUnpackU8(buf+n, bufSize-(size_t)n, &pMsgHdr->m_hdrVConn, NMEndianBig);
128  n += nmUnpackU16(buf+n, bufSize-(size_t)n, &pMsgHdr->m_hdrMsgId, NMEndianBig);
129  n += nmUnpackU16(buf+n, bufSize-(size_t)n, &pMsgHdr->m_hdrBodyLen,
130  NMEndianBig);
131 
132  return n;
133 }
134 
135 #ifdef LOG
136 /*!
137  * \brief Log data bytes.
138  *
139  * Print diagnostic logging of the contents of a buffer of bytes.
140  *
141  * \param sBufName Name of buffer.
142  * \param buf Buffer.
143  * \param uCount Number of bytes to log.
144  */
145 void bsLogBuf(const char *sBufName, byte_t buf[], size_t uCount)
146 {
147  FILE *fp; // log file pointer
148  size_t i; // working index
149 
150  fp = LOG_GET_LOGFP();
151 
152  fprintf(fp, "%sDiag%d: %s=", LOG_PREFACE, LOG_GET_THRESHOLD()-1, sBufName);
153  for(i=0; i<uCount; ++i)
154  {
155  if( (i % 16) == 0 )
156  {
157  fprintf(fp, "\n");
158  }
159  fprintf(fp, " 0x%02x", buf[i]);
160  }
161  fprintf(fp, "\n");
162 }
163 #endif // LOG
164 
165 #ifdef LOG
166 /*!
167  * \brief Log ascii data bytes.
168  *
169  * Print diagnostic logging of the contents of a buffer of ASCII bytes.
170  *
171  * \param sBufName Name of buffer.
172  * \param buf Buffer.
173  * \param uCount Number of bytes to log.
174  */
175 void bsLogAsciiBuf(const char *sBufName, byte_t buf[], size_t uCount)
176 {
177  FILE *fp; // log file pointer
178  size_t i; // working index
179 
180  fp = LOG_GET_LOGFP();
181 
182  fprintf(fp, "%sDiag%d: %s=", LOG_PREFACE, LOG_GET_THRESHOLD()-1, sBufName);
183  for(i=0; i<uCount; ++i)
184  {
185  if( isprint(buf[i]) || isspace(buf[i]) )
186  {
187  switch(buf[i])
188  {
189  case '\f':
190  fprintf(fp, "\\f");
191  break;
192  case '\n':
193  fprintf(fp, "\\n");
194  break;
195  case '\r':
196  fprintf(fp, "\\r");
197  break;
198  case '\t':
199  fprintf(fp, "\\t");
200  break;
201  case '\v':
202  fprintf(fp, "\\v");
203  break;
204  default:
205  fprintf(fp, "%c", buf[i]);
206  break;
207  }
208  }
209  else
210  {
211  fprintf(fp, "\\x%02x", buf[i]);
212  }
213  }
214  fprintf(fp, "\n");
215 }
216 #endif // LOG
BotSense client application - bsProxy server-terminated core messages.
The libBotSense internal declarations.
void bsLogAsciiBuf(const char *sBufName, byte_t buf[], size_t uCount)
Log ascii data bytes.
Definition: bsLibUtils.c:175
int bsPackMsgHdr(BsProxyMsgHdr_T *pMsgHdr, byte_t buf[], size_t bufSize)
Pack <b><i>BotSense</i></b> bsProxy message header.
Definition: bsLibUtils.c:85
<b><i>BotSense</i></b> client library declarations.
ushort_t m_hdrBodyLen
message body length
Definition: BotSense.h:284
ushort_t m_hdrMsgId
message id (vConnection unique)
Definition: BotSense.h:283
int bsUnpackMsgHdr(byte_t buf[], size_t bufSize, BsProxyMsgHdr_T *pMsgHdr)
Unpack <b><i>BotSense</i></b> bsProxy message header.
Definition: bsLibUtils.c:115
#define BSPROXY_MSG_HDR_LEN
message header length (bytes)
Definition: BotSense.h:258
ushort_t m_hdrMagic
"unique" magic pattern
Definition: BotSense.h:280
BotSense Proxy Message Header Structure.
Definition: BotSense.h:278
void bsLogBuf(const char *sBufName, byte_t buf[], size_t uCount)
Log data bytes.
Definition: bsLibUtils.c:145
#define BS_ECODE_INTERNAL
internal error (bug)
Definition: BotSense.h:93
byte_t m_hdrTid
transaction id
Definition: BotSense.h:281
byte_t m_hdrVConn
virtual connection handle (server unique)
Definition: BotSense.h:282
<b><i>BotSense</i></b> package top-level, unifying header declarations.