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, const 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, const 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] == '\"') {
81  "\\%c", buf[u]);
82  } else if (isprint(buf[u])) {
84  "%c", buf[u]);
85  } else {
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] == '\\') {
104  "\\\\");
105  } else {
107  "%c", buf[u]);
108  }
109  } else {
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  if (buf == NULL) {
147  fprintf(fp, " (null)\n");
148  return;
149  }
150  for (u = 0; u < buflen; u+=16) {
151  fprintf(fp ," %04X ", u);
152  for (ch = 0; (u+ch) < buflen && ch < 16; ch++) {
153  fprintf(fp, "%02X ", (uint8_t)buf[u+ch]);
154 
155  if (ch == 7) fprintf(fp, " ");
156  }
157  if (ch == 16) fprintf(fp, " ");
158  else if (ch < 8) {
159  int spaces = (16 - ch) * 3 + 2 + 1;
160  int s = 0;
161  for ( ; s < spaces; s++) fprintf(fp, " ");
162  } else if(ch < 16) {
163  int spaces = (16 - ch) * 3 + 2;
164  int s = 0;
165  for ( ; s < spaces; s++) fprintf(fp, " ");
166  }
167 
168  for (ch = 0; (u+ch) < buflen && ch < 16; ch++) {
169  fprintf(fp, "%c", isprint((uint8_t)buf[u+ch]) ? (uint8_t)buf[u+ch] : '.');
170 
171  if (ch == 7) fprintf(fp, " ");
172  if (ch == 15) fprintf(fp, "\n");
173  }
174  }
175  if (ch != 16)
176  fprintf(fp, "\n");
177 }
178 
179 void PrintRawDataToBuffer(uint8_t *dst_buf, uint32_t *dst_buf_offset_ptr, uint32_t dst_buf_size,
180  const uint8_t *src_buf, uint32_t src_buf_len)
181 {
182  int ch = 0;
183  uint32_t u = 0;
184 
185  for (u = 0; u < src_buf_len; u+=16) {
186  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
187  " %04X ", u);
188  for (ch = 0; (u + ch) < src_buf_len && ch < 16; ch++) {
189  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
190  "%02X ", (uint8_t)src_buf[u + ch]);
191 
192  if (ch == 7) {
193  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
194  " ");
195  }
196  }
197  if (ch == 16) {
198  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
199  } else if (ch < 8) {
200  int spaces = (16 - ch) * 3 + 2 + 1;
201  int s = 0;
202  for ( ; s < spaces; s++)
203  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
204  } else if(ch < 16) {
205  int spaces = (16 - ch) * 3 + 2;
206  int s = 0;
207  for ( ; s < spaces; s++)
208  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
209  }
210 
211  for (ch = 0; (u+ch) < src_buf_len && ch < 16; ch++) {
212  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
213  "%c",
214  isprint((uint8_t)src_buf[u + ch]) ? (uint8_t)src_buf[u + ch] : '.');
215 
216  if (ch == 7)
217  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
218  if (ch == 15)
219  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, "\n");
220  }
221  }
222  if (ch != 16)
223  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, "\n");
224 
225  return;
226 }
227 
228 void PrintStringsToBuffer(uint8_t *dst_buf, uint32_t *dst_buf_offset_ptr, uint32_t dst_buf_size,
229  const uint8_t *src_buf, const uint32_t src_buf_len)
230 {
231  uint32_t ch = 0;
232  for (ch = 0; ch < src_buf_len; ch++) {
233  PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
234  "%c",
235  (isprint((uint8_t)src_buf[ch]) ||
236  src_buf[ch] == '\n' ||
237  src_buf[ch] == '\r') ? (uint8_t)src_buf[ch] : '.');
238  }
239  dst_buf[dst_buf_size - 1] = 0;
240 
241  return;
242 }
243 
244 #ifndef s6_addr16
245 # define s6_addr16 __u6_addr.__u6_addr16
246 #endif
247 
248 static const char *PrintInetIPv6(const void *src, char *dst, socklen_t size)
249 {
250  int i;
251  char s_part[6];
252  uint16_t x[8];
253  memcpy(&x, src, 16);
254 
255  /* current IPv6 format is fixed size */
256  if (size < 8 * 5) {
257  SCLogWarning(SC_ERR_ARG_LEN_LONG, "Too small buffer to write IPv6 address");
258  return NULL;
259  }
260  memset(dst, 0, size);
261  for(i = 0; i < 8; i++) {
262  snprintf(s_part, sizeof(s_part), "%04x:", htons(x[i]));
263  strlcat(dst, s_part, size);
264  }
265  /* suppress last ':' */
266  dst[strlen(dst) - 1] = 0;
267 
268  return dst;
269 }
270 
271 const char *PrintInet(int af, const void *src, char *dst, socklen_t size)
272 {
273  switch (af) {
274  case AF_INET:
275 #if defined(OS_WIN32) && NTDDI_VERSION >= NTDDI_VISTA
276 {
277  // because Windows has to provide a non-conformant inet_ntop, of
278  // course!
279  struct in_addr _src;
280  memcpy(&_src, src, sizeof(struct in_addr));
281  return inet_ntop(af, &_src, dst, size);
282 }
283 #else
284  return inet_ntop(af, src, dst, size);
285 #endif
286  case AF_INET6:
287  /* Format IPv6 without deleting zeroes */
288  return PrintInetIPv6(src, dst, size);
289  default:
290  SCLogError(SC_ERR_INVALID_VALUE, "Unsupported protocol: %d", af);
291  }
292  return NULL;
293 }
BUFFER_LENGTH
#define BUFFER_LENGTH
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
SC_ERR_INVALID_VALUE
@ SC_ERR_INVALID_VALUE
Definition: util-error.h:160
PrintStringsToBuffer
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:228
util-debug.h
util-error.h
PrintRawDataToBuffer
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:179
PrintBufferRawLineHex
void PrintBufferRawLineHex(char *nbuf, int *offset, int max_size, const uint8_t *buf, uint32_t buflen)
print a buffer as hex on a single line
Definition: util-print.c:42
strlcat
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
util-print.h
PrintInet
const char * PrintInet(int af, const void *src, char *dst, socklen_t size)
Definition: util-print.c:271
PrintRawDataFp
void PrintRawDataFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
Definition: util-print.c:141
PrintBufferData
#define PrintBufferData(buf, buf_offset_ptr, buf_size,...)
Definition: util-print.h:29
suricata-common.h
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
PrintRawUriFp
void PrintRawUriFp(FILE *fp, uint8_t *buf, uint32_t buflen)
Definition: util-print.c:93
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:244
src
uint16_t src
Definition: app-layer-dnp3.h:5
PrintRawJsonFp
void PrintRawJsonFp(FILE *fp, uint8_t *buf, uint32_t buflen)
Definition: util-print.c:71
PrintRawUriBuf
void PrintRawUriBuf(char *retbuf, uint32_t *offset, uint32_t retbuflen, uint8_t *buf, uint32_t buflen)
Definition: util-print.c:118
af
uint16_t af
Definition: decode-gre.h:0
SC_ERR_ARG_LEN_LONG
@ SC_ERR_ARG_LEN_LONG
Definition: util-error.h:93
dst
uint16_t dst
Definition: app-layer-dnp3.h:4
PrintRawLineHexBuf
void PrintRawLineHexBuf(char *retbuf, uint32_t retbuflen, const uint8_t *buf, uint32_t buflen)
print a buffer as hex on a single line in to retbuf buffer
Definition: util-print.c:61