]>
git.ipfire.org Git - people/ms/suricata.git/blob - src/util-print.c
1 /* Copyright (C) 2007-2010 Open Information Security Foundation
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
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.
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
21 * \author Victor Julien <victor@inliniac.net>
23 * Print utility functions
26 #include "suricata-common.h"
27 #include "util-print.h"
28 #include "util-error.h"
29 #include "util-debug.h"
32 * \brief print a buffer as hex on a single line
34 * Prints in the format "00 AA BB"
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
42 void PrintBufferRawLineHex(char *nbuf
, int *offset
, int max_size
, const uint8_t *buf
, uint32_t buflen
)
46 for (u
= 0; u
< buflen
; u
++) {
47 PrintBufferData(nbuf
, offset
, max_size
, "%02X ", buf
[u
]);
52 * \brief print a buffer as hex on a single line in to retbuf buffer
54 * Prints in the format "00 AA BB"
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
61 void PrintRawLineHexBuf(char *retbuf
, uint32_t retbuflen
, const uint8_t *buf
, uint32_t buflen
)
66 for (u
= 0; u
< buflen
; u
++) {
67 PrintBufferData(retbuf
, &offset
, retbuflen
, "%02X ", buf
[u
]);
71 void PrintRawJsonFp(FILE *fp
, uint8_t *buf
, uint32_t buflen
)
73 #define BUFFER_LENGTH 2048
74 char nbuf
[BUFFER_LENGTH
] = "";
78 for (u
= 0; u
< buflen
; u
++) {
79 if (buf
[u
] == '\\' || buf
[u
] == '/' || buf
[u
] == '\"') {
80 PrintBufferData(nbuf
, &offset
, BUFFER_LENGTH
,
82 } else if (isprint(buf
[u
])) {
83 PrintBufferData(nbuf
, &offset
, BUFFER_LENGTH
,
86 PrintBufferData(nbuf
, &offset
, BUFFER_LENGTH
,
90 fprintf(fp
, "%s", nbuf
);
93 void PrintRawUriFp(FILE *fp
, uint8_t *buf
, uint32_t buflen
)
95 #define BUFFER_LENGTH 2048
96 char nbuf
[BUFFER_LENGTH
] = "";
100 for (u
= 0; u
< buflen
; u
++) {
101 if (isprint(buf
[u
]) && buf
[u
] != '\"') {
102 if (buf
[u
] == '\\') {
103 PrintBufferData(nbuf
, &offset
, BUFFER_LENGTH
,
106 PrintBufferData(nbuf
, &offset
, BUFFER_LENGTH
,
110 PrintBufferData(nbuf
, &offset
, BUFFER_LENGTH
,
115 fprintf(fp
, "%s", nbuf
);
118 void PrintRawUriBuf(char *retbuf
, uint32_t *offset
, uint32_t retbuflen
,
119 uint8_t *buf
, uint32_t buflen
)
123 for (u
= 0; u
< buflen
; u
++) {
124 if (isprint(buf
[u
]) && buf
[u
] != '\"') {
125 if (buf
[u
] == '\\') {
126 PrintBufferData(retbuf
, offset
, retbuflen
,
129 PrintBufferData(retbuf
, offset
, retbuflen
,
133 PrintBufferData(retbuf
, offset
, retbuflen
,
141 void PrintRawDataFp(FILE *fp
, const uint8_t *buf
, uint32_t buflen
)
147 fprintf(fp
, " (null)\n");
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
]);
155 if (ch
== 7) fprintf(fp
, " ");
157 if (ch
== 16) fprintf(fp
, " ");
159 int spaces
= (16 - ch
) * 3 + 2 + 1;
161 for ( ; s
< spaces
; s
++) fprintf(fp
, " ");
163 int spaces
= (16 - ch
) * 3 + 2;
165 for ( ; s
< spaces
; s
++) fprintf(fp
, " ");
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
] : '.');
171 if (ch
== 7) fprintf(fp
, " ");
172 if (ch
== 15) fprintf(fp
, "\n");
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
)
185 for (u
= 0; u
< src_buf_len
; u
+=16) {
186 PrintBufferData((char *)dst_buf
, dst_buf_offset_ptr
, dst_buf_size
,
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
]);
193 PrintBufferData((char *)dst_buf
, dst_buf_offset_ptr
, dst_buf_size
,
198 PrintBufferData((char *)dst_buf
, dst_buf_offset_ptr
, dst_buf_size
, " ");
200 int spaces
= (16 - ch
) * 3 + 2 + 1;
202 for ( ; s
< spaces
; s
++)
203 PrintBufferData((char *)dst_buf
, dst_buf_offset_ptr
, dst_buf_size
, " ");
205 int spaces
= (16 - ch
) * 3 + 2;
207 for ( ; s
< spaces
; s
++)
208 PrintBufferData((char *)dst_buf
, dst_buf_offset_ptr
, dst_buf_size
, " ");
211 for (ch
= 0; (u
+ch
) < src_buf_len
&& ch
< 16; ch
++) {
212 PrintBufferData((char *)dst_buf
, dst_buf_offset_ptr
, dst_buf_size
,
214 isprint((uint8_t)src_buf
[u
+ ch
]) ? (uint8_t)src_buf
[u
+ ch
] : '.');
217 PrintBufferData((char *)dst_buf
, dst_buf_offset_ptr
, dst_buf_size
, " ");
219 PrintBufferData((char *)dst_buf
, dst_buf_offset_ptr
, dst_buf_size
, "\n");
223 PrintBufferData((char *)dst_buf
, dst_buf_offset_ptr
, dst_buf_size
, "\n");
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
)
232 for (ch
= 0; ch
< src_buf_len
&& *dst_buf_offset_ptr
< dst_buf_size
;
233 ch
++, (*dst_buf_offset_ptr
)++) {
234 if (isprint((uint8_t)src_buf
[ch
]) || src_buf
[ch
] == '\n' || src_buf
[ch
] == '\r') {
235 dst_buf
[*dst_buf_offset_ptr
] = src_buf
[ch
];
237 dst_buf
[*dst_buf_offset_ptr
] = '.';
240 dst_buf
[dst_buf_size
- 1] = 0;
246 # define s6_addr16 __u6_addr.__u6_addr16
249 static const char *PrintInetIPv6(const void *src
, char *dst
, socklen_t size
)
256 /* current IPv6 format is fixed size */
258 SCLogWarning(SC_ERR_ARG_LEN_LONG
, "Too small buffer to write IPv6 address");
261 memset(dst
, 0, size
);
262 for(i
= 0; i
< 8; i
++) {
263 snprintf(s_part
, sizeof(s_part
), "%04x:", htons(x
[i
]));
264 strlcat(dst
, s_part
, size
);
266 /* suppress last ':' */
267 dst
[strlen(dst
) - 1] = 0;
272 const char *PrintInet(int af
, const void *src
, char *dst
, socklen_t size
)
276 #if defined(OS_WIN32) && NTDDI_VERSION >= NTDDI_VISTA
278 // because Windows has to provide a non-conformant inet_ntop, of
281 memcpy(&_src
, src
, sizeof(struct in_addr
));
282 return inet_ntop(af
, &_src
, dst
, size
);
285 return inet_ntop(af
, src
, dst
, size
);
288 /* Format IPv6 without deleting zeroes */
289 return PrintInetIPv6(src
, dst
, size
);
291 SCLogError(SC_ERR_INVALID_VALUE
, "Unsupported protocol: %d", af
);