2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
18 static char buffer
[1024];
19 static size_t buffer_use
= 0;
20 static pthread_spinlock_t buffer_lock
;
22 static size_t lcount
= 0;
25 * We want to print our stuff terminal aligned. Since we're printing packets
26 * we're in slowpath anyways. If stdin/stdout are connected to a terminal
27 * then default size = 1024; else size = 4096.
29 void tprintf_flush(void)
32 size_t flush_len
= get_tty_size() - 5;
34 while (buffer_use
-- > 0) {
35 if (lcount
== flush_len
) {
38 while (buffer_use
> 0 && (*ptr
== ' ' ||
39 *ptr
== ',' || *ptr
== '\n')) {
46 flush_len
= get_tty_size() - 5;
50 /* Collect in stream buffer. */
59 assert(buffer_use
== 0);
62 void tprintf_init(void)
64 pthread_spin_init(&buffer_lock
, PTHREAD_PROCESS_SHARED
);
65 memset(buffer
, 0, sizeof(buffer
));
68 void tprintf_cleanup(void)
70 pthread_spin_lock(&buffer_lock
);
72 pthread_spin_unlock(&buffer_lock
);
73 pthread_spin_destroy(&buffer_lock
);
76 void tprintf(char *msg
, ...)
82 pthread_spin_lock(&buffer_lock
);
84 ret
= vsnprintf(buffer
+ buffer_use
,
85 sizeof(buffer
) - buffer_use
,
88 /* Something screwed up! Unexpected. */
91 if (ret
>= sizeof(buffer
) - buffer_use
) {
94 /* Rewrite the buffer */
95 ret
= vsnprintf(buffer
+ buffer_use
,
96 sizeof(buffer
) - buffer_use
,
98 /* Again, we've failed! This shouldn't happen! So
99 * switch to vfprintf temporarily :-( */
100 if (ret
>= sizeof(buffer
) - buffer_use
) {
101 fprintf(stderr
, "BUG (buffer too large) -->\n");
102 vfprintf(stdout
, msg
, vl
);
103 fprintf(stderr
, " <--\n");
108 if (ret
< sizeof(buffer
) - buffer_use
)
112 pthread_spin_unlock(&buffer_lock
);