botsense  3.2.0
RoadNarrows Client-Server Proxied Services Framework
bsLibUtils.c File Reference

Library utilities. More...

#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <string.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/netmsgs.h"
#include "botsense/BotSense.h"
#include "botsense/libBotSense.h"
#include "botsense/bsProxyMsgs.h"
#include "bsLibInternal.h"

Go to the source code of this file.

Functions

int bsPackMsgHdr (BsProxyMsgHdr_T *pMsgHdr, byte_t buf[], size_t bufSize)
 Pack BotSense bsProxy message header. More...
 
int bsUnpackMsgHdr (byte_t buf[], size_t bufSize, BsProxyMsgHdr_T *pMsgHdr)
 Unpack BotSense bsProxy message header. More...
 
void bsLogBuf (const char *sBufName, byte_t buf[], size_t uCount)
 Log data bytes. More...
 
void bsLogAsciiBuf (const char *sBufName, byte_t buf[], size_t uCount)
 Log ascii data bytes. More...
 

Detailed Description

Library utilities.

LastChangedDate
2010-08-20 11:36:38 -0600 (Fri, 20 Aug 2010)
Rev
568
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file bsLibUtils.c.

Function Documentation

void bsLogAsciiBuf ( const char *  sBufName,
byte_t  buf[],
size_t  uCount 
)

Log ascii data bytes.

Print diagnostic logging of the contents of a buffer of ASCII bytes.

Parameters
sBufNameName of buffer.
bufBuffer.
uCountNumber of bytes to log.

Definition at line 175 of file bsLibUtils.c.

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 }
void bsLogBuf ( const char *  sBufName,
byte_t  buf[],
size_t  uCount 
)

Log data bytes.

Print diagnostic logging of the contents of a buffer of bytes.

Parameters
sBufNameName of buffer.
bufBuffer.
uCountNumber of bytes to log.

Definition at line 145 of file bsLibUtils.c.

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 }
int bsPackMsgHdr ( BsProxyMsgHdr_T pMsgHdr,
byte_t  buf[],
size_t  bufSize 
)

Pack BotSense bsProxy message header.

Parameters
[in]pMsgHdrPointer to message header structure.
[out]bufOutput message buffer.
bufSizeSize of output buffer.
Returns
On success, returns the number of bytes packed.

On error, the appropriate < 0 negated BotSense Error Code is returned.

Definition at line 85 of file bsLibUtils.c.

References BS_ECODE_INTERNAL, BSPROXY_MSG_HDR_LEN, BsProxyMsgHdr_T::m_hdrBodyLen, BsProxyMsgHdr_T::m_hdrMagic, BsProxyMsgHdr_T::m_hdrMsgId, BsProxyMsgHdr_T::m_hdrTid, and BsProxyMsgHdr_T::m_hdrVConn.

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 }
ushort_t m_hdrBodyLen
message body length
Definition: BotSense.h:284
ushort_t m_hdrMsgId
message id (vConnection unique)
Definition: BotSense.h:283
#define BSPROXY_MSG_HDR_LEN
message header length (bytes)
Definition: BotSense.h:258
ushort_t m_hdrMagic
"unique" magic pattern
Definition: BotSense.h:280
#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
int bsUnpackMsgHdr ( byte_t  buf[],
size_t  bufSize,
BsProxyMsgHdr_T pMsgHdr 
)

Unpack BotSense bsProxy message header.

Parameters
[in]bufInput message buffer.
bufSizeSize of input buffer.
[out]pMsgHdrPointer to message header structure.
Returns
On success, returns the number of bytes unpacked.

On error, the appropriate < 0 negated BotSense Error Code is returned.

Definition at line 115 of file bsLibUtils.c.

References BS_ECODE_INTERNAL, BSPROXY_MSG_HDR_LEN, BsProxyMsgHdr_T::m_hdrBodyLen, BsProxyMsgHdr_T::m_hdrMagic, BsProxyMsgHdr_T::m_hdrMsgId, BsProxyMsgHdr_T::m_hdrTid, and BsProxyMsgHdr_T::m_hdrVConn.

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 }
ushort_t m_hdrBodyLen
message body length
Definition: BotSense.h:284
ushort_t m_hdrMsgId
message id (vConnection unique)
Definition: BotSense.h:283
#define BSPROXY_MSG_HDR_LEN
message header length (bytes)
Definition: BotSense.h:258
ushort_t m_hdrMagic
"unique" magic pattern
Definition: BotSense.h:280
#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