/**
* print all algorithms of a kind to buffer
*/
-static int print_alg(private_proposal_t *this, char **dst, size_t *len,
+static int print_alg(private_proposal_t *this, printf_hook_data_t *data,
u_int kind, void *names, bool *first)
{
enumerator_t *enumerator;
{
if (*first)
{
- written += print_in_hook(*dst, *len, "%N", names, alg);
+ written += print_in_hook(data, "%N", names, alg);
*first = FALSE;
}
else
{
- written += print_in_hook(*dst, *len, "/%N", names, alg);
+ written += print_in_hook(data, "/%N", names, alg);
}
if (size)
{
- written += print_in_hook(*dst, *len, "_%u", size);
+ written += print_in_hook(data, "_%u", size);
}
}
enumerator->destroy(enumerator);
/**
* Described in header.
*/
-int proposal_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
+int proposal_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args)
{
private_proposal_t *this = *((private_proposal_t**)(args[0]));
if (this == NULL)
{
- return print_in_hook(dst, len, "(null)");
+ return print_in_hook(data, "(null)");
}
if (spec->hash)
{ /* call recursivly */
if (first)
{
- written += print_in_hook(dst, len, "%P", this);
+ written += print_in_hook(data, "%P", this);
first = FALSE;
}
else
{
- written += print_in_hook(dst, len, ", %P", this);
+ written += print_in_hook(data, ", %P", this);
}
}
enumerator->destroy(enumerator);
return written;
}
- written = print_in_hook(dst, len, "%N:", protocol_id_names, this->protocol);
- written += print_alg(this, &dst, &len, ENCRYPTION_ALGORITHM,
+ written = print_in_hook(data, "%N:", protocol_id_names, this->protocol);
+ written += print_alg(this, data, ENCRYPTION_ALGORITHM,
encryption_algorithm_names, &first);
- written += print_alg(this, &dst, &len, INTEGRITY_ALGORITHM,
+ written += print_alg(this, data, INTEGRITY_ALGORITHM,
integrity_algorithm_names, &first);
- written += print_alg(this, &dst, &len, PSEUDO_RANDOM_FUNCTION,
+ written += print_alg(this, data, PSEUDO_RANDOM_FUNCTION,
pseudo_random_function_names, &first);
- written += print_alg(this, &dst, &len, DIFFIE_HELLMAN_GROUP,
+ written += print_alg(this, data, DIFFIE_HELLMAN_GROUP,
diffie_hellman_group_names, &first);
- written += print_alg(this, &dst, &len, EXTENDED_SEQUENCE_NUMBERS,
+ written += print_alg(this, data, EXTENDED_SEQUENCE_NUMBERS,
extended_sequence_numbers_names, &first);
return written;
}
* With the #-specifier, arguments are:
* linked_list_t *list containing proposal_t*
*/
-int proposal_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
+int proposal_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args);
#endif /** PROPOSAL_H_ @}*/
/**
* Described in header.
*/
-int chunk_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
+int chunk_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args)
{
chunk_t *chunk = *((chunk_t**)(args[0]));
{
u_int chunk_len = chunk->len;
const void *new_args[] = {&chunk->ptr, &chunk_len};
- return mem_printf_hook(dst, len, spec, new_args);
+ return mem_printf_hook(data, spec, new_args);
}
while (copy.len > 0)
}
else
{
- written += print_in_hook(dst, len, ":");
+ written += print_in_hook(data, ":");
}
- written += print_in_hook(dst, len, "%02x", *copy.ptr++);
+ written += print_in_hook(data, "%02x", *copy.ptr++);
copy.len--;
}
return written;
* chunk_t *chunk
* Use #-modifier to print a compact version
*/
-int chunk_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
+int chunk_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args);
#endif /** CHUNK_H_ @}*/
/**
* Described in header.
*/
-int enum_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
+int enum_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args)
{
enum_name_t *ed = *((enum_name_t**)(args[0]));
if (name == NULL)
{
- return print_in_hook(dst, len, "(%d)", val);
+ return print_in_hook(data, "(%d)", val);
}
else
{
- return print_in_hook(dst, len, "%s", name);
+ return print_in_hook(data, "%s", name);
}
}
* Arguments are:
* enum_names_t *names, int value
*/
-int enum_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
+int enum_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args);
#endif /** ENUM_H_ @}*/
char buf[PRINTF_BUF_LEN];
printf_hook_spec_t spec;
printf_hook_handler_t *handler = printf_hooks[SPEC_TO_INDEX(info->spec)];
+ printf_hook_data_t data = {
+ .buf = buf,
+ .buflen = sizeof(buf),
+ };
spec.hash = info->alt;
spec.minus = info->left;
spec.width = info->width;
- written = handler->hook(buf, sizeof(buf), &spec, args);
+ written = handler->hook(&data, &spec, args);
if (written > 0)
{
ignore_result(fwrite(buf, 1, written, stream));
const void *args[ARGS_MAX];
printf_hook_spec_t spec;
printf_hook_handler_t *handler = printf_hooks[SPEC_TO_INDEX(fmt_spec->name[0])];
+ printf_hook_data_t data = {
+ .buf = buf,
+ .buflen = sizeof(buf),
+ };
for (i = 0; i < handler->numargs; i++)
{
spec.minus = fmt_spec->fmt_minus;
spec.width = fmt_spec->fmt_field_width;
- written = handler->hook(buf, sizeof(buf), &spec, args);
+ written = handler->hook(&data, &spec, args);
if (written > 0)
{
vstr_add_buf(base, pos, buf, written);
typedef struct printf_hook_t printf_hook_t;
typedef struct printf_hook_spec_t printf_hook_spec_t;
+typedef struct printf_hook_data_t printf_hook_data_t;
typedef enum printf_hook_argtype_t printf_hook_argtype_t;
#if !defined(USE_VSTR) && \
/**
* Callback function type for printf hooks.
*
- * @param dst destination buffer
- * @param len length of the buffer
+ * @param data hook data, to pass to print_in_hook()
* @param spec format specifier
* @param args arguments array
* @return number of characters written
*/
-typedef int (*printf_hook_function_t)(char *dst, size_t len,
+typedef int (*printf_hook_function_t)(printf_hook_data_t *data,
printf_hook_spec_t *spec,
const void *const *args);
* Helper macro to be used in printf hook callbacks.
* buf and buflen get modified.
*/
-#define print_in_hook(buf, buflen, fmt, ...) ({\
- int _written = snprintf(buf, buflen, fmt, ##__VA_ARGS__);\
- if (_written < 0 || _written >= buflen)\
+#define print_in_hook(data, fmt, ...) ({\
+ int _written = snprintf(data->buf, data->buflen, fmt, ##__VA_ARGS__);\
+ if (_written < 0 || _written >= data->buflen)\
{\
- _written = buflen - 1;\
+ _written = data->buflen - 1;\
}\
- buf += _written;\
- buflen -= _written;\
+ data->buf += _written;\
+ data->buflen -= _written;\
_written;\
})
+/**
+ * Data to pass to a printf hook.
+ */
+struct printf_hook_data_t {
+
+ /**
+ * Buffer to write to
+ */
+ char *buf;
+
+ /**
+ * Size of the buffer
+ */
+ size_t buflen;
+};
+
/**
* Properties of the format specifier
*/
/**
* Described in header.
*/
-int traffic_selector_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
- const void *const *args)
+int traffic_selector_printf_hook(printf_hook_data_t *data,
+ printf_hook_spec_t *spec, const void *const *args)
{
private_traffic_selector_t *this = *((private_traffic_selector_t**)(args[0]));
linked_list_t *list = *((linked_list_t**)(args[0]));
if (this == NULL)
{
- return print_in_hook(dst, len, "(null)");
+ return print_in_hook(data, "(null)");
}
if (spec->hash)
while (enumerator->enumerate(enumerator, (void**)&this))
{
/* call recursivly */
- written += print_in_hook(dst, len, "%R ", this);
+ written += print_in_hook(data, "%R ", this);
}
enumerator->destroy(enumerator);
return written;
memeq(this->from, from, this->type == TS_IPV4_ADDR_RANGE ? 4 : 16) &&
memeq(this->to, to, this->type == TS_IPV4_ADDR_RANGE ? 4 : 16))
{
- written += print_in_hook(dst, len, "dynamic");
+ written += print_in_hook(data, "dynamic");
}
else
{
{
inet_ntop(AF_INET6, &this->to6, to_str, sizeof(to_str));
}
- written += print_in_hook(dst, len, "%s..%s", from_str, to_str);
+ written += print_in_hook(data, "%s..%s", from_str, to_str);
}
else
{
- written += print_in_hook(dst, len, "%s/%d", from_str, this->netbits);
+ written += print_in_hook(data, "%s/%d", from_str, this->netbits);
}
}
return written;
}
- written += print_in_hook(dst, len, "[");
+ written += print_in_hook(data, "[");
/* build protocol string */
if (has_proto)
if (proto)
{
- written += print_in_hook(dst, len, "%s", proto->p_name);
+ written += print_in_hook(data, "%s", proto->p_name);
serv_proto = proto->p_name;
}
else
{
- written += print_in_hook(dst, len, "%d", this->protocol);
+ written += print_in_hook(data, "%d", this->protocol);
}
}
if (has_proto && has_ports)
{
- written += print_in_hook(dst, len, "/");
+ written += print_in_hook(data, "/");
}
/* build port string */
if (serv)
{
- written += print_in_hook(dst, len, "%s", serv->s_name);
+ written += print_in_hook(data, "%s", serv->s_name);
}
else
{
- written += print_in_hook(dst, len, "%d", this->from_port);
+ written += print_in_hook(data, "%d", this->from_port);
}
}
else
{
- written += print_in_hook(dst, len, "%d-%d", this->from_port, this->to_port);
+ written += print_in_hook(data, "%d-%d", this->from_port, this->to_port);
}
}
- written += print_in_hook(dst, len, "]");
+ written += print_in_hook(data, "]");
return written;
}
* With the #-specifier, arguments are:
* linked_list_t *list containing traffic_selector_t*
*/
-int traffic_selector_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
- const void *const *args);
+int traffic_selector_printf_hook(printf_hook_data_t *data,
+ printf_hook_spec_t *spec, const void *const *args);
#endif /** TRAFFIC_SELECTOR_H_ @}*/
/**
* Described in header.
*/
-int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
+int time_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args)
{
static const char* months[] = {
if (time == UNDEFINED_TIME)
{
- return print_in_hook(dst, len, "--- -- --:--:--%s----",
+ return print_in_hook(data, "--- -- --:--:--%s----",
utc ? " UTC " : " ");
}
if (utc)
{
localtime_r(time, &t);
}
- return print_in_hook(dst, len, "%s %02d %02d:%02d:%02d%s%04d",
+ return print_in_hook(data, "%s %02d %02d:%02d:%02d%s%04d",
months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min,
t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900);
}
/**
* Described in header.
*/
-int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
+int time_delta_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args)
{
char* unit = "second";
delta /= 60;
unit = "minute";
}
- return print_in_hook(dst, len, "%" PRIu64 " %s%s", delta, unit,
+ return print_in_hook(data, "%" PRIu64 " %s%s", delta, unit,
(delta == 1) ? "" : "s");
}
/**
* Described in header.
*/
-int mem_printf_hook(char *dst, size_t dstlen,
+int mem_printf_hook(printf_hook_data_t *data,
printf_hook_spec_t *spec, const void *const *args)
{
char *bytes = *((void**)(args[0]));
int i = 0;
int written = 0;
- written += print_in_hook(dst, dstlen, "=> %u bytes @ %p", len, bytes);
+ written += print_in_hook(data, "=> %u bytes @ %p", len, bytes);
while (bytes_pos < bytes_roof)
{
*buffer_pos++ = '\0';
ascii_buffer[i] = '\0';
- written += print_in_hook(dst, dstlen, "\n%4d: %s %s",
+ written += print_in_hook(data, "\n%4d: %s %s",
line_start, buffer, ascii_buffer);
buffer_pos = buffer;
* Arguments are:
* time_t* time, bool utc
*/
-int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
+int time_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args);
/**
* Arguments are:
* time_t* begin, time_t* end
*/
-int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
+int time_delta_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args);
/**
* Arguments are:
* u_char *ptr, u_int len
*/
-int mem_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
+int mem_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args);
#endif /** UTILS_H_ @}*/
/**
* Described in header.
*/
-int host_printf_hook(char *dst, size_t dstlen, printf_hook_spec_t *spec,
+int host_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args)
{
private_host_t *this = *((private_host_t**)(args[0]));
}
if (spec->minus)
{
- return print_in_hook(dst, dstlen, "%-*s", spec->width, buffer);
+ return print_in_hook(data, "%-*s", spec->width, buffer);
}
- return print_in_hook(dst, dstlen, "%*s", spec->width, buffer);
+ return print_in_hook(data, "%*s", spec->width, buffer);
}
METHOD(host_t, get_address, chunk_t,
* host_t *host
* Use #-modifier to include port number
*/
-int host_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
+int host_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args);
#endif /** HOST_H_ @}*/
/**
* Described in header.
*/
-int identification_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
- const void *const *args)
+int identification_printf_hook(printf_hook_data_t *data,
+ printf_hook_spec_t *spec, const void *const *args)
{
private_identification_t *this = *((private_identification_t**)(args[0]));
chunk_t proper;
if (this == NULL)
{
- return print_in_hook(dst, len, "%*s", spec->width, "(null)");
+ return print_in_hook(data, "%*s", spec->width, "(null)");
}
switch (this->type)
}
if (spec->minus)
{
- return print_in_hook(dst, len, "%-*s", spec->width, buf);
+ return print_in_hook(data, "%-*s", spec->width, buf);
}
- return print_in_hook(dst, len, "%*s", spec->width, buf);
+ return print_in_hook(data, "%*s", spec->width, buf);
}
METHOD(identification_t, clone_, identification_t*,
* Arguments are:
* identification_t *identification
*/
-int identification_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
- const void *const *args);
+int identification_printf_hook(printf_hook_data_t *data,
+ printf_hook_spec_t *spec, const void *const *args);
#endif /** IDENTIFICATION_H_ @}*/