suricata
util-print.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  * Print utility functions
24  */
25 
26 #include "suricata-common.h"
27 #include "util-print.h"
28 #include "util-error.h"
29 #include "util-debug.h"
30 
31 /**
32  * \brief print a buffer as hex on a single line
33  *
34  * Prints in the format "00 AA BB"
35  *
36  * \param nbuf buffer into which the output is written
37  * \param offset of where to start writting into the buffer
38  * \param max_size the size of the output buffer
39  * \param buf buffer to print from
40  * \param buflen length of the input buffer
41  */
42 void PrintBufferRawLineHex(char *nbuf, int *offset, int max_size, uint8_t *buf, uint32_t buflen)
43 {
44  uint32_t u = 0;
45 
46  for (u = 0; u < buflen; u++) {
47  PrintBufferData(nbuf, offset, max_size, "%02X ", buf[u]);
48  }
49 }
50 
51 /**
52  * \brief print a buffer as hex on a single line in to retbuf buffer
53  *
54  * Prints in the format "00 AA BB"
55  *
56  * \param retbuf pointer to the buffer which will have the result
57  * \param rebuflen lenght of the buffer
58  * \param buf buffer to print from
59  * \param buflen length of the input buffer
60  */
61 void PrintRawLineHexBuf(char *retbuf, uint32_t retbuflen, uint8_t *buf, uint32_t buflen)
62 {
63  uint32_t offset = 0;
64  uint32_t u = 0;
65 
66  for (u = 0; u < buflen; u++) {
67  PrintBufferData(retbuf, &offset, retbuflen, "%02X ", buf[u]);
68  }
69 }
70 
71 void PrintRawJsonFp(FILE *fp, uint8_t *buf, uint32_t buflen)
72 {
73 #define BUFFER_LENGTH 2048
74  char nbuf[BUFFER_LENGTH] = "";
75  uint32_t offset = 0;
76  uint32_t u = 0;
77 
78  for (u = 0; u < buflen; u++) {
79  if (buf[u] == '\\' || buf[u] == '/' || buf[u] == '\"') {
80  PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
81  "\\%c", buf[u]);
82  } else if (isprint(buf[u])) {
83  PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
84  "%c", buf[u]);
85  } else {
86  PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
87  "\\\\x%02X", buf[u]);
88  }
89  }
90  fprintf(fp, "%s", nbuf);
91 }
92 
93 void PrintRawUriFp(FILE *fp, uint8_t *buf, uint32_t buflen)
94 {
95 #define BUFFER_LENGTH 2048
96  char nbuf[BUFFER_LENGTH] = "";
97  uint32_t offset = 0;
98  uint32_t u = 0;
99 
100  for (u = 0; u < buflen; u++) {
101  if (isprint(buf[u]) && buf[u] != '\"') {
102  if (buf[u] == '\\') {
103  PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
104  "\\\\");
105  } else {
106  PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
107  "%c", buf[u]);
108  }
109  } else {
110  PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
111  "\\x%02X", buf[u]);
112  }
113  }
114 
115  fprintf(fp, "%s", nbuf);
116 }
117 
118 void PrintRawUriBuf(char *retbuf, uint32_t *offset, uint32_t retbuflen,
119  uint8_t *buf, uint32_t buflen)
120 {
121  uint32_t u = 0;
122 
123  for (u = 0; u < buflen; u++) {
124  if (isprint(buf[u]) && buf[u] != '\"') {
125  if (buf[u] == '\\') {
126  PrintBufferData(retbuf, offset, retbuflen,
127  "\\\\");
128  } else {
129  PrintBufferData(retbuf, offset, retbuflen,
130  "%c", buf[u]);
131  }
132  } else {
133  PrintBufferData(retbuf, offset, retbuflen,
134  "\\x%02X", buf[u]);
135  }
136  }
137 
138  return;
139 }
140 
141 void PrintRawDataFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
142 {
143  int ch = 0;
144  uint32_t u = 0;
145 
146  for (u = 0; u < buflen; u+=16) {
147  fprintf(fp ," %04X ", u);
148  for (ch = 0; (u+ch) < buflen && ch < 16; ch++) {
149  fprintf(fp, "%02X ", (uint8_t)buf[u+ch]);
150 
151  if (ch == 7) fprintf(fp, " ");
152  }
153  if (ch == 16) fprintf(fp, " ");
154  else if (ch < 8) {
155  int spaces = (16 - ch) * 3 + 2 + 1;
156  int s = 0;
157  for ( ; s < spaces; s++) fprintf(fp, " ");
158  } else if(ch < 16) {
159  int spaces = (16 - ch) * 3 + 2;
160  int s = 0;
161  for ( ; s < spaces; s++) fprintf(fp, " ");
162  }
163 
164  for (ch = 0; (u+ch) < buflen && ch < 16; ch++) {
165  fprintf(fp, "%c", isprint((uint8_t)buf[u+ch]) ? (uint8_t)buf[u+ch] : '.');
166 
167  if (ch == 7) fprintf(fp, " ");
168  if (ch == 15) fprintf(fp, "\n");
169  }
170  }
171  if (ch != 16)
172  fprintf(fp, "\n");
173 }
174 
175 void PrintRawDataToBuffer(uint8_t *dst_buf, uint32_t *dst_buf_offset_ptr, uint32_t dst_buf_size,
176  const uint8_t *src_buf, uint32_t src_buf_len)
177 {
178  int ch = 0;
179  uint32_t u = 0;
180 
181  for (u = 0; u < src_buf_len; u+=16) {
182  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
183  " %04X ", u);
184  for (ch = 0; (u + ch) < src_buf_len && ch < 16; ch++) {
185  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
186  "%02X ", (uint8_t)src_buf[u + ch]);
187 
188  if (ch == 7) {
189  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
190  " ");
191  }
192  }
193  if (ch == 16) {
194  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
195  } else if (ch < 8) {
196  int spaces = (16 - ch) * 3 + 2 + 1;
197  int s = 0;
198  for ( ; s < spaces; s++)
199  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
200  } else if(ch < 16) {
201  int spaces = (16 - ch) * 3 + 2;
202  int s = 0;
203  for ( ; s < spaces; s++)
204  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
205  }
206 
207  for (ch = 0; (u+ch) < src_buf_len && ch < 16; ch++) {
208  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
209  "%c",
210  isprint((uint8_t)src_buf[u + ch]) ? (uint8_t)src_buf[u + ch] : '.');
211 
212  if (ch == 7)
213  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
214  if (ch == 15)
215  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, "\n");
216  }
217  }
218  if (ch != 16)
219  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, "\n");
220 
221  return;
222 }
223 
224 void PrintStringsToBuffer(uint8_t *dst_buf, uint32_t *dst_buf_offset_ptr, uint32_t dst_buf_size,
225  const uint8_t *src_buf, const uint32_t src_buf_len)
226 {
227  uint32_t ch = 0;
228  for (ch = 0; ch < src_buf_len; ch++) {
229  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
230  "%c",
231  (isprint((uint8_t)src_buf[ch]) ||
232  src_buf[ch] == '\n' ||
233  src_buf[ch] == '\r') ? (uint8_t)src_buf[ch] : '.');
234  }
235  dst_buf[dst_buf_size - 1] = 0;
236 
237  return;
238 }
239 
240 #ifndef s6_addr16
241 # define s6_addr16 __u6_addr.__u6_addr16
242 #endif
243 
244 static const char *PrintInetIPv6(const void *src, char *dst, socklen_t size)
245 {
246  int i;
247  char s_part[6];
248  uint16_t x[8];
249  memcpy(&x, src, 16);
250 
251  /* current IPv6 format is fixed size */
252  if (size < 8 * 5) {
253  SCLogWarning(SC_ERR_ARG_LEN_LONG, "Too small buffer to write IPv6 address");
254  return NULL;
255  }
256  memset(dst, 0, size);
257  for(i = 0; i < 8; i++) {
258  snprintf(s_part, sizeof(s_part), "%04x:", htons(x[i]));
259  strlcat(dst, s_part, size);
260  }
261  /* suppress last ':' */
262  dst[strlen(dst) - 1] = 0;
263 
264  return dst;
265 }
266 
267 const char *PrintInet(int af, const void *src, char *dst, socklen_t size)
268 {
269  switch (af) {
270  case AF_INET:
271 #if defined(OS_WIN32) && NTDDI_VERSION >= NTDDI_VISTA
272 {
273  // because Windows has to provide a non-conformant inet_ntop, of
274  // course!
275  struct in_addr _src;
276  memcpy(&_src, src, sizeof(struct in_addr));
277  return inet_ntop(af, &_src, dst, size);
278 }
279 #else
280  return inet_ntop(af, src, dst, size);
281 #endif
282  case AF_INET6:
283  /* Format IPv6 without deleting zeroes */
284  return PrintInetIPv6(src, dst, size);
285  default:
286  SCLogError(SC_ERR_INVALID_VALUE, "Unsupported protocol: %d", af);
287  }
288  return NULL;
289 }
void PrintRawLineHexBuf(char *retbuf, uint32_t retbuflen, uint8_t *buf, uint32_t buflen)
print a buffer as hex on a single line in to retbuf buffer
Definition: util-print.c:61
void PrintRawJsonFp(FILE *fp, uint8_t *buf, uint32_t buflen)
Definition: util-print.c:71
void PrintRawDataToBuffer(uint8_t *dst_buf, uint32_t *dst_buf_offset_ptr, uint32_t dst_buf_size, const uint8_t *src_buf, uint32_t src_buf_len)
Definition: util-print.c:175
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
uint64_t offset
uint16_t src
#define BUFFER_LENGTH
uint16_t dst
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void PrintBufferRawLineHex(char *nbuf, int *offset, int max_size, uint8_t *buf, uint32_t buflen)
print a buffer as hex on a single line
Definition: util-print.c:42
#define PrintBufferData(buf, buf_offset_ptr, buf_size,...)
Definition: util-print.h:29
void PrintRawUriFp(FILE *fp, uint8_t *buf, uint32_t buflen)
Definition: util-print.c:93
const char * PrintInet(int af, const void *src, char *dst, socklen_t size)
Definition: util-print.c:267
void PrintStringsToBuffer(uint8_t *dst_buf, uint32_t *dst_buf_offset_ptr, uint32_t dst_buf_size, const uint8_t *src_buf, const uint32_t src_buf_len)
Definition: util-print.c:224
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:281
void PrintRawUriBuf(char *retbuf, uint32_t *offset, uint32_t retbuflen, uint8_t *buf, uint32_t buflen)
Definition: util-print.c:118
void PrintRawDataFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
Definition: util-print.c:141
uint16_t af
Definition: decode-gre.h:404