tcpflow  1.6.1
About: tcpflow is a TCP/IP packet demultiplexer that captures data transmitted as part of TCP connections (flows), and stores the data in a way that is convenient for protocol analysis and debugging.
  Fossies Dox: tcpflow-1.6.1.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

utils.cpp
Go to the documentation of this file.
1 /**
2  * A collection of utility functions that are useful.
3  */
4 
5 // Just for this module
6 #define _FILE_OFFSET_BITS 64
7 
8 /* required per C++ standard */
9 #ifndef __STDC_FORMAT_MACROS
10 #define __STDC_FORMAT_MACROS
11 #endif
12 
13 #include "config.h"
14 #include "cppmutex.h"
15 #include "utils.h"
16 
17 #ifdef HAVE_UNISTD_H
18 #include <unistd.h>
19 #endif
20 
21 #include <stdio.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <ctype.h>
25 #include <assert.h>
26 #include <inttypes.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #include <string.h>
30 
31 #ifndef HAVE_ERR
32 #include <stdarg.h>
33 void err(int eval,const char *fmt,...)
34 {
35  va_list ap;
36  va_start(ap,fmt);
37  vfprintf(stderr,fmt,ap);
38  va_end(ap);
39  fprintf(stderr,": %s\n",strerror(errno));
40  exit(eval);
41 }
42 #endif
43 
44 #ifndef HAVE_ERRX
45 #include <stdarg.h>
46 void errx(int eval,const char *fmt,...)
47 {
48  va_list ap;
49  va_start(ap,fmt);
50  vfprintf(stderr,fmt,ap);
51  fprintf(stderr,"%s\n",strerror(errno));
52  va_end(ap);
53  exit(eval);
54 }
55 #endif
56 
57 #ifndef HAVE_WARN
58 #include <stdarg.h>
59 void warn(const char *fmt, ...)
60 {
61  va_list args;
62  va_start(args,fmt);
63  vfprintf(stderr,fmt, args);
64  fprintf(stderr,": %s\n",strerror(errno));
65 }
66 #endif
67 
68 #ifndef HAVE_WARNX
69 #include <stdarg.h>
70 void warnx(const char *fmt,...)
71 {
72  va_list ap;
73  va_start(ap,fmt);
74  vfprintf(stderr,fmt,ap);
75  va_end(ap);
76 }
77 #endif
78 
79 /** Extract a buffer...
80  * @param buf - the buffer to extract;
81  * @param buflen - the size of the page to extract
82  * @param pos0 - the byte position of buf[0]
83  */
84 
85 #ifndef HAVE_LOCALTIME_R
86 /* locking localtime_r implementation */
88 void localtime_r(time_t *t,struct tm *tm)
89 {
91  *tm = *localtime(t);
92 }
93 #endif
94 
95 #ifndef HAVE_GMTIME_R
96 /* locking gmtime_r implementation */
98 void gmtime_r(time_t *t,struct tm *tm)
99 {
100  if(t && tm){
102  struct tm *tmret = gmtime(t);
103  if(tmret){
104  *tm = *tmret;
105  } else {
106  memset(tm,0,sizeof(*tm));
107  }
108  }
109 }
110 #endif
111 
112 
113 
114 bool ends_with(const std::string &buf,const std::string &with)
115 {
116  size_t buflen = buf.size();
117  size_t withlen = with.size();
118  return buflen>withlen && buf.substr(buflen-withlen,withlen)==with;
119 }
120 
121 bool ends_with(const std::wstring &buf,const std::wstring &with)
122 {
123  size_t buflen = buf.size();
124  size_t withlen = with.size();
125  return buflen>withlen && buf.substr(buflen-withlen,withlen)==with;
126 }
127 
128 
129 #include <sstream>
130 
131 /****************************************************************/
132 /* C++ string splitting code from http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c */
133 std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
134 {
135  std::stringstream ss(s);
136  std::string item;
137  while(std::getline(ss, item, delim)) {
138  elems.push_back(item);
139  }
140  return elems;
141 }
142 
143 std::vector<std::string> split(const std::string &s, char delim)
144 {
145  std::vector<std::string> elems;
146  return split(s, delim, elems);
147 }
148 
149 
150 
151 
bool ends_with(const std::string &buf, const std::string &with)
Definition: utils.cpp:114
void localtime_r(time_t *t, struct tm *tm)
Definition: utils.cpp:88
void warnx(const char *fmt,...)
Definition: utils.cpp:70
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
Definition: utils.cpp:133
cppmutex localtime_mutex
Definition: utils.cpp:87
void gmtime_r(time_t *t, struct tm *tm)
Definition: utils.cpp:98
void err(int eval, const char *fmt,...)
Definition: utils.cpp:33
cppmutex gmtime_mutex
Definition: utils.cpp:97
void errx(int eval, const char *fmt,...)
Definition: utils.cpp:46
void warn(const char *fmt,...)
Definition: utils.cpp:59