printf hooks refactored to increase portability (i.e. support for platforms without glibc-compatible customizable printf - the Vstr string library is currently required on such platforms).
parent
9086102dfd
commit
d25ce3701e
|
@ -740,6 +740,14 @@ AC_TRY_RUN(
|
|||
[AC_MSG_RESULT([no])],
|
||||
[AC_MSG_RESULT([no])])
|
||||
|
||||
AC_CHECK_FUNC(
|
||||
[register_printf_function],
|
||||
[AC_DEFINE(HAVE_PRINTF_HOOKS)],
|
||||
[
|
||||
AC_MSG_NOTICE([printf does not support custom format specifiers!])
|
||||
AC_HAVE_LIBRARY([vstr],[LIBS="$LIBS"]; vstr=true,[AC_MSG_ERROR([Vstr string library not found])])
|
||||
])
|
||||
|
||||
if test x$gmp = xtrue; then
|
||||
AC_HAVE_LIBRARY([gmp],[LIBS="$LIBS"],[AC_MSG_ERROR([GNU Multi Precision library gmp not found])])
|
||||
AC_MSG_CHECKING([gmp.h version >= 4.1.4])
|
||||
|
@ -981,6 +989,7 @@ AM_CONDITIONAL(USE_CHARON, test x$charon = xtrue)
|
|||
AM_CONDITIONAL(USE_TOOLS, test x$tools = xtrue)
|
||||
AM_CONDITIONAL(USE_LIBSTRONGSWAN, test x$charon = xtrue -o x$tools = xtrue)
|
||||
AM_CONDITIONAL(USE_FILE_CONFIG, test x$pluto = xtrue -o x$stroke = xtrue)
|
||||
AM_CONDITIONAL(USE_VSTR, test x$vstr = xtrue)
|
||||
|
||||
dnl ==============================
|
||||
dnl set global definitions
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2008 Tobias Brunner
|
||||
* Copyright (C) 2008-2009 Tobias Brunner
|
||||
* Copyright (C) 2006 Martin Willi
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
|
@ -803,10 +803,10 @@ static status_t add_string_algo(private_proposal_t *this, chunk_t alg)
|
|||
}
|
||||
|
||||
/**
|
||||
* print all algorithms of a kind to stream
|
||||
* print all algorithms of a kind to buffer
|
||||
*/
|
||||
static int print_alg(private_proposal_t *this, FILE *stream, u_int kind,
|
||||
void *names, bool *first)
|
||||
static int print_alg(private_proposal_t *this, char **dst, int *len,
|
||||
u_int kind, void *names, bool *first)
|
||||
{
|
||||
enumerator_t *enumerator;
|
||||
size_t written = 0;
|
||||
|
@ -817,16 +817,16 @@ static int print_alg(private_proposal_t *this, FILE *stream, u_int kind,
|
|||
{
|
||||
if (*first)
|
||||
{
|
||||
written += fprintf(stream, "%N", names, alg);
|
||||
written += print_in_hook(*dst, *len, "%N", names, alg);
|
||||
*first = FALSE;
|
||||
}
|
||||
else
|
||||
{
|
||||
written += fprintf(stream, "/%N", names, alg);
|
||||
written += print_in_hook(*dst, *len, "/%N", names, alg);
|
||||
}
|
||||
if (size)
|
||||
{
|
||||
written += fprintf(stream, "-%d", size);
|
||||
written += print_in_hook(*dst, *len, "-%d", size);
|
||||
}
|
||||
}
|
||||
enumerator->destroy(enumerator);
|
||||
|
@ -834,10 +834,10 @@ static int print_alg(private_proposal_t *this, FILE *stream, u_int kind,
|
|||
}
|
||||
|
||||
/**
|
||||
* output handler in printf()
|
||||
* Described in header.
|
||||
*/
|
||||
static int print(FILE *stream, const struct printf_info *info,
|
||||
const void *const *args)
|
||||
int proposal_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
|
||||
const void *const *args)
|
||||
{
|
||||
private_proposal_t *this = *((private_proposal_t**)(args[0]));
|
||||
linked_list_t *list = *((linked_list_t**)(args[0]));
|
||||
|
@ -847,64 +847,42 @@ static int print(FILE *stream, const struct printf_info *info,
|
|||
|
||||
if (this == NULL)
|
||||
{
|
||||
return fprintf(stream, "(null)");
|
||||
return print_in_hook(dst, len, "(null)");
|
||||
}
|
||||
|
||||
if (info->alt)
|
||||
if (spec->hash)
|
||||
{
|
||||
enumerator = list->create_enumerator(list);
|
||||
while (enumerator->enumerate(enumerator, &this))
|
||||
{ /* call recursivly */
|
||||
if (first)
|
||||
{
|
||||
written += fprintf(stream, "%P", this);
|
||||
written += print_in_hook(dst, len, "%P", this);
|
||||
first = FALSE;
|
||||
}
|
||||
else
|
||||
{
|
||||
written += fprintf(stream, ", %P", this);
|
||||
written += print_in_hook(dst, len, ", %P", this);
|
||||
}
|
||||
}
|
||||
enumerator->destroy(enumerator);
|
||||
return written;
|
||||
}
|
||||
|
||||
written = fprintf(stream, "%N:", protocol_id_names, this->protocol);
|
||||
written += print_alg(this, stream, ENCRYPTION_ALGORITHM,
|
||||
written = print_in_hook(dst, len, "%N:", protocol_id_names, this->protocol);
|
||||
written += print_alg(this, &dst, &len, ENCRYPTION_ALGORITHM,
|
||||
encryption_algorithm_names, &first);
|
||||
written += print_alg(this, stream, INTEGRITY_ALGORITHM,
|
||||
written += print_alg(this, &dst, &len, INTEGRITY_ALGORITHM,
|
||||
integrity_algorithm_names, &first);
|
||||
written += print_alg(this, stream, PSEUDO_RANDOM_FUNCTION,
|
||||
written += print_alg(this, &dst, &len, PSEUDO_RANDOM_FUNCTION,
|
||||
pseudo_random_function_names, &first);
|
||||
written += print_alg(this, stream, DIFFIE_HELLMAN_GROUP,
|
||||
written += print_alg(this, &dst, &len, DIFFIE_HELLMAN_GROUP,
|
||||
diffie_hellman_group_names, &first);
|
||||
written += print_alg(this, stream, EXTENDED_SEQUENCE_NUMBERS,
|
||||
written += print_alg(this, &dst, &len, EXTENDED_SEQUENCE_NUMBERS,
|
||||
extended_sequence_numbers_names, &first);
|
||||
return written;
|
||||
}
|
||||
|
||||
/**
|
||||
* arginfo handler for printf() proposal
|
||||
*/
|
||||
static int arginfo(const struct printf_info *info, size_t n, int *argtypes)
|
||||
{
|
||||
if (n > 0)
|
||||
{
|
||||
argtypes[0] = PA_POINTER;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* return printf hook functions for a proposal
|
||||
*/
|
||||
printf_hook_functions_t proposal_get_printf_hooks()
|
||||
{
|
||||
printf_hook_functions_t hooks = {print, arginfo};
|
||||
|
||||
return hooks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements proposal_t.destroy.
|
||||
*/
|
||||
|
|
|
@ -233,13 +233,14 @@ proposal_t *proposal_create_default(protocol_id_t protocol);
|
|||
proposal_t *proposal_create_from_string(protocol_id_t protocol, const char *algs);
|
||||
|
||||
/**
|
||||
* Get printf hooks for a proposal.
|
||||
* printf hook function for proposal_t.
|
||||
*
|
||||
* Arguments are:
|
||||
* proposal_t *proposal
|
||||
* With the #-specifier, arguments are:
|
||||
* linked_list_t *list containing proposal_t*
|
||||
*/
|
||||
printf_hook_functions_t proposal_get_printf_hooks();
|
||||
int proposal_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
|
||||
const void *const *args);
|
||||
|
||||
#endif /* PROPOSAL_H_ @} */
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2007 Tobias Brunner
|
||||
* Copyright (C) 2007-2009 Tobias Brunner
|
||||
* Copyright (C) 2005-2007 Martin Willi
|
||||
* Copyright (C) 2005 Jan Hutter
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
|
@ -21,7 +21,6 @@
|
|||
#include <string.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <printf.h>
|
||||
|
||||
#include "traffic_selector.h"
|
||||
|
||||
|
@ -157,10 +156,10 @@ static u_int8_t calc_netbits(private_traffic_selector_t *this)
|
|||
static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol, ts_type_t type, u_int16_t from_port, u_int16_t to_port);
|
||||
|
||||
/**
|
||||
* output handler in printf()
|
||||
* Described in header.
|
||||
*/
|
||||
static int print(FILE *stream, const struct printf_info *info,
|
||||
const void *const *args)
|
||||
int traffic_selector_printf_hook(char *dst, size_t len, 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]));
|
||||
|
@ -175,16 +174,16 @@ static int print(FILE *stream, const struct printf_info *info,
|
|||
|
||||
if (this == NULL)
|
||||
{
|
||||
return fprintf(stream, "(null)");
|
||||
return print_in_hook(dst, len, "(null)");
|
||||
}
|
||||
|
||||
if (info->alt)
|
||||
if (spec->hash)
|
||||
{
|
||||
iterator = list->create_iterator(list, TRUE);
|
||||
while (iterator->iterate(iterator, (void**)&this))
|
||||
{
|
||||
/* call recursivly */
|
||||
written += fprintf(stream, "%R ", this);
|
||||
written += print_in_hook(dst, len, "%R ", this);
|
||||
}
|
||||
iterator->destroy(iterator);
|
||||
return written;
|
||||
|
@ -196,7 +195,7 @@ static int print(FILE *stream, const struct printf_info *info,
|
|||
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 += fprintf(stream, "dynamic");
|
||||
written += print_in_hook(dst, len, "dynamic");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -209,7 +208,7 @@ static int print(FILE *stream, const struct printf_info *info,
|
|||
inet_ntop(AF_INET6, &this->from6, addr_str, sizeof(addr_str));
|
||||
}
|
||||
mask = calc_netbits(this);
|
||||
written += fprintf(stream, "%s/%d", addr_str, mask);
|
||||
written += print_in_hook(dst, len, "%s/%d", addr_str, mask);
|
||||
}
|
||||
|
||||
/* check if we have protocol and/or port selectors */
|
||||
|
@ -221,7 +220,7 @@ static int print(FILE *stream, const struct printf_info *info,
|
|||
return written;
|
||||
}
|
||||
|
||||
written += fprintf(stream, "[");
|
||||
written += print_in_hook(dst, len, "[");
|
||||
|
||||
/* build protocol string */
|
||||
if (has_proto)
|
||||
|
@ -230,18 +229,18 @@ static int print(FILE *stream, const struct printf_info *info,
|
|||
|
||||
if (proto)
|
||||
{
|
||||
written += fprintf(stream, "%s", proto->p_name);
|
||||
written += print_in_hook(dst, len, "%s", proto->p_name);
|
||||
serv_proto = proto->p_name;
|
||||
}
|
||||
else
|
||||
{
|
||||
written += fprintf(stream, "%d", this->protocol);
|
||||
written += print_in_hook(dst, len, "%d", this->protocol);
|
||||
}
|
||||
}
|
||||
|
||||
if (has_proto && has_ports)
|
||||
{
|
||||
written += fprintf(stream, "/");
|
||||
written += print_in_hook(dst, len, "/");
|
||||
}
|
||||
|
||||
/* build port string */
|
||||
|
@ -253,46 +252,24 @@ static int print(FILE *stream, const struct printf_info *info,
|
|||
|
||||
if (serv)
|
||||
{
|
||||
written += fprintf(stream, "%s", serv->s_name);
|
||||
written += print_in_hook(dst, len, "%s", serv->s_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
written += fprintf(stream, "%d", this->from_port);
|
||||
written += print_in_hook(dst, len, "%d", this->from_port);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
written += fprintf(stream, "%d-%d", this->from_port, this->to_port);
|
||||
written += print_in_hook(dst, len, "%d-%d", this->from_port, this->to_port);
|
||||
}
|
||||
}
|
||||
|
||||
written += fprintf(stream, "]");
|
||||
written += print_in_hook(dst, len, "]");
|
||||
|
||||
return written;
|
||||
}
|
||||
|
||||
/**
|
||||
* arginfo handler for printf() traffic selector
|
||||
*/
|
||||
static int arginfo(const struct printf_info *info, size_t n, int *argtypes)
|
||||
{
|
||||
if (n > 0)
|
||||
{
|
||||
argtypes[0] = PA_POINTER;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* return printf hook functions for a chunk
|
||||
*/
|
||||
printf_hook_functions_t traffic_selector_get_printf_hooks()
|
||||
{
|
||||
printf_hook_functions_t hooks = {print, arginfo};
|
||||
|
||||
return hooks;
|
||||
}
|
||||
|
||||
/**
|
||||
* implements traffic_selector_t.get_subset
|
||||
*/
|
||||
|
|
|
@ -291,13 +291,14 @@ traffic_selector_t *traffic_selector_create_dynamic(u_int8_t protocol,
|
|||
u_int16_t from_port, u_int16_t to_port);
|
||||
|
||||
/**
|
||||
* Get printf hooks for a traffic selector.
|
||||
* printf hook function for traffic_selector_t.
|
||||
*
|
||||
* Arguments are:
|
||||
* traffic_selector_t *ts
|
||||
* With the #-specifier, arguments are:
|
||||
* linked_list_t *list containing traffic_selector_t*
|
||||
*/
|
||||
printf_hook_functions_t traffic_selector_get_printf_hooks();
|
||||
int traffic_selector_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
|
||||
const void *const *args);
|
||||
|
||||
#endif /* TRAFFIC_SELECTOR_H_ @} */
|
||||
|
|
|
@ -572,7 +572,7 @@ static certificate_t *get_better_ocsp(private_credential_manager_t *this,
|
|||
case VALIDATION_REVOKED:
|
||||
/* subject has been revoked by a valid OCSP response */
|
||||
DBG1(DBG_CFG, "certificate was revoked on %T, reason: %N",
|
||||
&revocation, crl_reason_names, reason);
|
||||
&revocation, TRUE, crl_reason_names, reason);
|
||||
revoked = TRUE;
|
||||
break;
|
||||
case VALIDATION_GOOD:
|
||||
|
@ -593,7 +593,7 @@ static certificate_t *get_better_ocsp(private_credential_manager_t *this,
|
|||
best = cand;
|
||||
if (best->get_validity(best, NULL, NULL, &valid_until))
|
||||
{
|
||||
DBG1(DBG_CFG, " ocsp response is valid: until %#T",
|
||||
DBG1(DBG_CFG, " ocsp response is valid: until %T",
|
||||
&valid_until, FALSE);
|
||||
*valid = VALIDATION_GOOD;
|
||||
if (cache)
|
||||
|
@ -603,7 +603,7 @@ static certificate_t *get_better_ocsp(private_credential_manager_t *this,
|
|||
}
|
||||
else
|
||||
{
|
||||
DBG1(DBG_CFG, " ocsp response is stale: since %#T",
|
||||
DBG1(DBG_CFG, " ocsp response is stale: since %T",
|
||||
&valid_until, FALSE);
|
||||
*valid = VALIDATION_STALE;
|
||||
}
|
||||
|
@ -791,7 +791,7 @@ static certificate_t *get_better_crl(private_credential_manager_t *this,
|
|||
if (chunk_equals(serial, subject->get_serial(subject)))
|
||||
{
|
||||
DBG1(DBG_CFG, "certificate was revoked on %T, reason: %N",
|
||||
&revocation, crl_reason_names, reason);
|
||||
&revocation, TRUE, crl_reason_names, reason);
|
||||
*valid = VALIDATION_REVOKED;
|
||||
enumerator->destroy(enumerator);
|
||||
DESTROY_IF(best);
|
||||
|
@ -807,7 +807,7 @@ static certificate_t *get_better_crl(private_credential_manager_t *this,
|
|||
best = cand;
|
||||
if (best->get_validity(best, NULL, NULL, &valid_until))
|
||||
{
|
||||
DBG1(DBG_CFG, " crl is valid: until %#T", &valid_until, FALSE);
|
||||
DBG1(DBG_CFG, " crl is valid: until %T", &valid_until, FALSE);
|
||||
*valid = VALIDATION_GOOD;
|
||||
if (cache)
|
||||
{ /* we cache non-stale crls only, as a stale crls are refetched */
|
||||
|
@ -816,7 +816,7 @@ static certificate_t *get_better_crl(private_credential_manager_t *this,
|
|||
}
|
||||
else
|
||||
{
|
||||
DBG1(DBG_CFG, " crl is stale: since %#T", &valid_until, FALSE);
|
||||
DBG1(DBG_CFG, " crl is stale: since %T", &valid_until, FALSE);
|
||||
*valid = VALIDATION_STALE;
|
||||
}
|
||||
}
|
||||
|
@ -938,13 +938,13 @@ static bool check_certificate(private_credential_manager_t *this,
|
|||
if (!subject->get_validity(subject, NULL, ¬_before, ¬_after))
|
||||
{
|
||||
DBG1(DBG_CFG, "subject certificate invalid (valid from %T to %T)",
|
||||
¬_before, ¬_after);
|
||||
¬_before, TRUE, ¬_after, TRUE);
|
||||
return FALSE;
|
||||
}
|
||||
if (!issuer->get_validity(issuer, NULL, ¬_before, ¬_after))
|
||||
{
|
||||
DBG1(DBG_CFG, "issuer certificate invalid (valid from %T to %T)",
|
||||
¬_before, ¬_after);
|
||||
¬_before, TRUE, ¬_after, TRUE);
|
||||
return FALSE;
|
||||
}
|
||||
if (issuer->get_type(issuer) == CERT_X509 &&
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2006-2007 Tobias Brunner
|
||||
* Copyright (C) 2006-2009 Tobias Brunner
|
||||
* Copyright (C) 2006 Daniel Roethlisberger
|
||||
* Copyright (C) 2005-2008 Martin Willi
|
||||
* Copyright (C) 2005 Jan Hutter
|
||||
|
@ -644,9 +644,13 @@ int main(int argc, char *argv[])
|
|||
/* initialize library */
|
||||
library_init(STRONGSWAN_CONF);
|
||||
lib->printf_hook->add_handler(lib->printf_hook, 'R',
|
||||
traffic_selector_get_printf_hooks());
|
||||
traffic_selector_printf_hook,
|
||||
PRINTF_HOOK_ARGTYPE_POINTER,
|
||||
PRINTF_HOOK_ARGTYPE_END);
|
||||
lib->printf_hook->add_handler(lib->printf_hook, 'P',
|
||||
proposal_get_printf_hooks());
|
||||
proposal_printf_hook,
|
||||
PRINTF_HOOK_ARGTYPE_POINTER,
|
||||
PRINTF_HOOK_ARGTYPE_END);
|
||||
private_charon = daemon_create();
|
||||
charon = (daemon_t*)private_charon;
|
||||
|
||||
|
|
|
@ -554,10 +554,10 @@ static void leases(char *filter, bool utc)
|
|||
printf("%-7s ", "expired");
|
||||
}
|
||||
|
||||
printf(" %#T ", &acquired, utc);
|
||||
printf(" %T ", &acquired, utc);
|
||||
if (released)
|
||||
{
|
||||
printf("%#T ", &released, utc);
|
||||
printf("%T ", &released, utc);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -88,7 +88,7 @@ static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
|
|||
time_t established;
|
||||
|
||||
established = ike_sa->get_statistic(ike_sa, STAT_ESTABLISHED);
|
||||
fprintf(out, " %#V ago", &now, &established);
|
||||
fprintf(out, " %V ago", &now, &established);
|
||||
}
|
||||
|
||||
fprintf(out, ", %H[%D]...%H[%D]\n",
|
||||
|
@ -116,11 +116,11 @@ static void log_ike_sa(FILE *out, ike_sa_t *ike_sa, bool all)
|
|||
|
||||
if (rekey)
|
||||
{
|
||||
fprintf(out, ", rekeying in %#V", &rekey, &now);
|
||||
fprintf(out, ", rekeying in %V", &rekey, &now);
|
||||
}
|
||||
if (reauth)
|
||||
{
|
||||
fprintf(out, ", %N reauthentication in %#V", auth_class_names,
|
||||
fprintf(out, ", %N reauthentication in %V", auth_class_names,
|
||||
get_auth_class(ike_sa->get_peer_cfg(ike_sa)),
|
||||
&reauth, &now);
|
||||
}
|
||||
|
@ -212,7 +212,7 @@ static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
|
|||
rekey = child_sa->get_lifetime(child_sa, FALSE);
|
||||
if (rekey)
|
||||
{
|
||||
fprintf(out, "in %#V", &now, &rekey);
|
||||
fprintf(out, "in %V", &now, &rekey);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -265,12 +265,12 @@ static void status(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out, bo
|
|||
char *plugin, *pool;
|
||||
host_t *host;
|
||||
u_int32_t dpd;
|
||||
time_t uptime = time(NULL) - this->uptime;
|
||||
time_t now = time(NULL);
|
||||
bool first = TRUE;
|
||||
u_int size, online, offline;
|
||||
|
||||
fprintf(out, "Performance:\n");
|
||||
fprintf(out, " uptime: %V, since %#T\n", &uptime, &this->uptime, FALSE);
|
||||
fprintf(out, " uptime: %V, since %T\n", &now, &this->uptime, &this->uptime, FALSE);
|
||||
fprintf(out, " worker threads: %d idle of %d,",
|
||||
charon->processor->get_idle_threads(charon->processor),
|
||||
charon->processor->get_total_threads(charon->processor));
|
||||
|
@ -659,26 +659,26 @@ static void stroke_list_certs(linked_list_t *list, char *label,
|
|||
|
||||
/* list validity */
|
||||
cert->get_validity(cert, &now, ¬Before, ¬After);
|
||||
fprintf(out, " validity: not before %#T, ", ¬Before, utc);
|
||||
fprintf(out, " validity: not before %T, ", ¬Before, utc);
|
||||
if (now < notBefore)
|
||||
{
|
||||
fprintf(out, "not valid yet (valid in %#V)\n", &now, ¬Before);
|
||||
fprintf(out, "not valid yet (valid in %V)\n", &now, ¬Before);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(out, "ok\n");
|
||||
}
|
||||
fprintf(out, " not after %#T, ", ¬After, utc);
|
||||
fprintf(out, " not after %T, ", ¬After, utc);
|
||||
if (now > notAfter)
|
||||
{
|
||||
fprintf(out, "expired (%#V ago)\n", &now, ¬After);
|
||||
fprintf(out, "expired (%V ago)\n", &now, ¬After);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(out, "ok");
|
||||
if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
|
||||
{
|
||||
fprintf(out, " (expires in %#V)", &now, ¬After);
|
||||
fprintf(out, " (expires in %V)", &now, ¬After);
|
||||
}
|
||||
fprintf(out, " \n");
|
||||
}
|
||||
|
@ -759,18 +759,18 @@ static void stroke_list_acerts(linked_list_t *list, bool utc, FILE *out)
|
|||
|
||||
/* list validity */
|
||||
cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
|
||||
fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
|
||||
fprintf(out, " next %#T, ", &nextUpdate, utc);
|
||||
fprintf(out, " updates: this %T\n", &thisUpdate, utc);
|
||||
fprintf(out, " next %T, ", &nextUpdate, utc);
|
||||
if (now > nextUpdate)
|
||||
{
|
||||
fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
|
||||
fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(out, "ok");
|
||||
if (now > nextUpdate - AC_WARNING_INTERVAL * 60 * 60 * 24)
|
||||
{
|
||||
fprintf(out, " (expires in %#V)", &now, &nextUpdate);
|
||||
fprintf(out, " (expires in %V)", &now, &nextUpdate);
|
||||
}
|
||||
fprintf(out, " \n");
|
||||
}
|
||||
|
@ -832,18 +832,18 @@ static void stroke_list_crls(linked_list_t *list, bool utc, FILE *out)
|
|||
|
||||
/* list validity */
|
||||
cert->get_validity(cert, &now, &thisUpdate, &nextUpdate);
|
||||
fprintf(out, " updates: this %#T\n", &thisUpdate, utc);
|
||||
fprintf(out, " next %#T, ", &nextUpdate, utc);
|
||||
fprintf(out, " updates: this %T\n", &thisUpdate, utc);
|
||||
fprintf(out, " next %T, ", &nextUpdate, utc);
|
||||
if (now > nextUpdate)
|
||||
{
|
||||
fprintf(out, "expired (%#V ago)\n", &now, &nextUpdate);
|
||||
fprintf(out, "expired (%V ago)\n", &now, &nextUpdate);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(out, "ok");
|
||||
if (now > nextUpdate - CRL_WARNING_INTERVAL * 60 * 60 * 24)
|
||||
{
|
||||
fprintf(out, " (expires in %#V)", &now, &nextUpdate);
|
||||
fprintf(out, " (expires in %V)", &now, &nextUpdate);
|
||||
}
|
||||
fprintf(out, " \n");
|
||||
}
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
|
||||
#include <sys/time.h>
|
||||
#include <string.h>
|
||||
#include <printf.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
@ -1743,7 +1742,7 @@ static status_t reauth(private_ike_sa_t *this)
|
|||
{
|
||||
time_t now = time(NULL);
|
||||
|
||||
DBG1(DBG_IKE, "IKE_SA will timeout in %#V",
|
||||
DBG1(DBG_IKE, "IKE_SA will timeout in %V",
|
||||
&now, &this->stats[STAT_DELETE]);
|
||||
return FAILED;
|
||||
}
|
||||
|
|
|
@ -74,6 +74,10 @@ if USE_INTEGRITY_TEST
|
|||
fips/fips_canister_end.c
|
||||
endif
|
||||
|
||||
if USE_VSTR
|
||||
libstrongswan_la_LIBADD += -lvstr
|
||||
endif
|
||||
|
||||
EXTRA_DIST = asn1/oid.txt asn1/oid.pl
|
||||
BUILT_SOURCES = asn1/oid.c asn1/oid.h
|
||||
MAINTAINERCLEANFILES = asn1/oid.c asn1/oid.h
|
||||
|
|
|
@ -397,7 +397,7 @@ void asn1_debug_simple_object(chunk_t object, asn1_t type, bool private)
|
|||
{
|
||||
time_t time = asn1_to_time(&object, type);
|
||||
|
||||
DBG2(" '%T'", &time);
|
||||
DBG2(" '%T'", &time, TRUE);
|
||||
}
|
||||
return;
|
||||
default:
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2008 Tobias Brunner
|
||||
* Copyright (C) 2008-2009 Tobias Brunner
|
||||
* Copyright (C) 2005-2006 Martin Willi
|
||||
* Copyright (C) 2005 Jan Hutter
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
|
@ -25,7 +25,6 @@
|
|||
#include "chunk.h"
|
||||
|
||||
#include <debug.h>
|
||||
#include <printf_hook.h>
|
||||
|
||||
/* required for chunk_hash */
|
||||
#undef get16bits
|
||||
|
@ -520,21 +519,20 @@ u_int32_t chunk_hash(chunk_t chunk)
|
|||
}
|
||||
|
||||
/**
|
||||
* output handler in printf() for chunks
|
||||
* Described in header.
|
||||
*/
|
||||
static int chunk_print(FILE *stream, const struct printf_info *info,
|
||||
const void *const *args)
|
||||
int chunk_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
|
||||
const void *const *args)
|
||||
{
|
||||
chunk_t *chunk = *((chunk_t**)(args[0]));
|
||||
bool first = TRUE;
|
||||
chunk_t copy = *chunk;
|
||||
int written = 0;
|
||||
printf_hook_functions_t mem = mem_get_printf_hooks();
|
||||
|
||||
if (!info->alt)
|
||||
if (!spec->hash)
|
||||
{
|
||||
const void *new_args[] = {&chunk->ptr, &chunk->len};
|
||||
return mem.print(stream, info, new_args);
|
||||
return mem_printf_hook(dst, len, spec, new_args);
|
||||
}
|
||||
|
||||
while (copy.len > 0)
|
||||
|
@ -545,33 +543,10 @@ static int chunk_print(FILE *stream, const struct printf_info *info,
|
|||
}
|
||||
else
|
||||
{
|
||||
written += fprintf(stream, ":");
|
||||
written += print_in_hook(dst, len, ":");
|
||||
}
|
||||
written += fprintf(stream, "%02x", *copy.ptr++);
|
||||
written += print_in_hook(dst, len, "%02x", *copy.ptr++);
|
||||
copy.len--;
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
/**
|
||||
* arginfo handler for printf() mem ranges
|
||||
*/
|
||||
static int chunk_arginfo(const struct printf_info *info, size_t n, int *argtypes)
|
||||
{
|
||||
if (n > 0)
|
||||
{
|
||||
argtypes[0] = PA_POINTER;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* return printf hook functions for a chunk
|
||||
*/
|
||||
printf_hook_functions_t chunk_get_printf_hooks()
|
||||
{
|
||||
printf_hook_functions_t hooks = {chunk_print, chunk_arginfo};
|
||||
|
||||
return hooks;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2008 Tobias Brunner
|
||||
* Copyright (C) 2008-2009 Tobias Brunner
|
||||
* Copyright (C) 2005-2008 Martin Willi
|
||||
* Copyright (C) 2005 Jan Hutter
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
|
@ -240,11 +240,13 @@ u_int32_t chunk_hash(chunk_t chunk);
|
|||
u_int32_t chunk_hash_inc(chunk_t chunk, u_int32_t hash);
|
||||
|
||||
/**
|
||||
* Get printf hooks for a chunk.
|
||||
* printf hook function for chunk_t.
|
||||
*
|
||||
* Arguments are:
|
||||
* chunk_t *chunk
|
||||
* Use #-modifier to print a compact version
|
||||
*/
|
||||
printf_hook_functions_t chunk_get_printf_hooks();
|
||||
int chunk_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
|
||||
const void *const *args);
|
||||
|
||||
#endif /* CHUNK_H_ @}*/
|
||||
|
|
|
@ -20,8 +20,6 @@
|
|||
|
||||
#include "enum.h"
|
||||
|
||||
#include <printf_hook.h>
|
||||
|
||||
/**
|
||||
* get the name of an enum value in a enum_name_t list
|
||||
*/
|
||||
|
@ -39,10 +37,10 @@ static char *enum_name(enum_name_t *e, int val)
|
|||
}
|
||||
|
||||
/**
|
||||
* output handler in printf() for enum names
|
||||
* Described in header.
|
||||
*/
|
||||
static int print(FILE *stream, const struct printf_info *info,
|
||||
const void *const *args)
|
||||
int enum_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
|
||||
const void *const *args)
|
||||
{
|
||||
enum_name_t *ed = *((enum_name_t**)(args[0]));
|
||||
int val = *((int*)(args[1]));
|
||||
|
@ -51,34 +49,10 @@ static int print(FILE *stream, const struct printf_info *info,
|
|||
|
||||
if (name == NULL)
|
||||
{
|
||||
return fprintf(stream, "(%d)", val);
|
||||
return print_in_hook(dst, len, "(%d)", val);
|
||||
}
|
||||
else
|
||||
{
|
||||
return fprintf(stream, "%s", name);
|
||||
return print_in_hook(dst, len, "%s", name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* arginfo handler for printf() hook
|
||||
*/
|
||||
static int arginfo(const struct printf_info *info, size_t n, int *argtypes)
|
||||
{
|
||||
if (n > 1)
|
||||
{
|
||||
argtypes[0] = PA_POINTER;
|
||||
argtypes[1] = PA_INT;
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* return printf hook functions
|
||||
*/
|
||||
printf_hook_functions_t enum_get_printf_hooks()
|
||||
{
|
||||
printf_hook_functions_t hooks = {print, arginfo};
|
||||
|
||||
return hooks;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2009 Tobias Brunner
|
||||
* Copyright (C) 2006-2008 Martin Willi
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
|
@ -108,12 +109,12 @@ struct enum_name_t {
|
|||
#define ENUM(name, first, last, ...) ENUM_BEGIN(name, first, last, __VA_ARGS__); ENUM_END(name, last)
|
||||
|
||||
/**
|
||||
* Get printf hook functions for enum_names_t.
|
||||
* printf hook function for enum_names_t.
|
||||
*
|
||||
* The handler takes the arguments: enum_names_t *names, int value
|
||||
*
|
||||
* @return printf hook functions
|
||||
* Arguments are:
|
||||
* enum_names_t *names, int value
|
||||
*/
|
||||
printf_hook_functions_t enum_get_printf_hooks();
|
||||
int enum_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
|
||||
const void *const *args);
|
||||
|
||||
#endif /* ENUM_H_ @}*/
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2009 Tobias Brunner
|
||||
* Copyright (C) 2008 Martin Willi
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
|
@ -95,13 +96,24 @@ void library_init(char *settings)
|
|||
pfh = printf_hook_create();
|
||||
this->public.printf_hook = pfh;
|
||||
|
||||
pfh->add_handler(pfh, 'b', mem_get_printf_hooks());
|
||||
pfh->add_handler(pfh, 'B', chunk_get_printf_hooks());
|
||||
pfh->add_handler(pfh, 'D', identification_get_printf_hooks());
|
||||
pfh->add_handler(pfh, 'H', host_get_printf_hooks());
|
||||
pfh->add_handler(pfh, 'N', enum_get_printf_hooks());
|
||||
pfh->add_handler(pfh, 'T', time_get_printf_hooks());
|
||||
pfh->add_handler(pfh, 'V', time_delta_get_printf_hooks());
|
||||
pfh->add_handler(pfh, 'b', mem_printf_hook,
|
||||
PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
|
||||
PRINTF_HOOK_ARGTYPE_END);
|
||||
pfh->add_handler(pfh, 'B', chunk_printf_hook,
|
||||
PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
|
||||
pfh->add_handler(pfh, 'D', identification_printf_hook,
|
||||
PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
|
||||
pfh->add_handler(pfh, 'H', host_printf_hook,
|
||||
PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
|
||||
pfh->add_handler(pfh, 'N', enum_printf_hook,
|
||||
PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
|
||||
PRINTF_HOOK_ARGTYPE_END);
|
||||
pfh->add_handler(pfh, 'T', time_printf_hook,
|
||||
PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
|
||||
PRINTF_HOOK_ARGTYPE_END);
|
||||
pfh->add_handler(pfh, 'V', time_delta_printf_hook,
|
||||
PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER,
|
||||
PRINTF_HOOK_ARGTYPE_END);
|
||||
|
||||
this->public.crypto = crypto_factory_create();
|
||||
this->public.creds = credential_factory_create();
|
||||
|
|
|
@ -57,10 +57,10 @@
|
|||
#ifndef LIBRARY_H_
|
||||
#define LIBRARY_H_
|
||||
|
||||
#include <printf_hook.h>
|
||||
#include <utils.h>
|
||||
#include <chunk.h>
|
||||
#include <settings.h>
|
||||
#include <printf_hook.h>
|
||||
#include <plugins/plugin_loader.h>
|
||||
#include <crypto/crypto_factory.h>
|
||||
#include <credentials/credential_factory.h>
|
||||
|
|
|
@ -870,7 +870,7 @@ static bool is_newer(private_x509_ac_t *this, ac_t *that)
|
|||
this_cert->get_validity(this_cert, &now, &this_update, NULL);
|
||||
that_cert->get_validity(that_cert, &now, &that_update, NULL);
|
||||
new = this_update > that_update;
|
||||
DBG1(" attr cert from %#T is %s - existing attr_cert from %#T %s",
|
||||
DBG1(" attr cert from %T is %s - existing attr_cert from %T %s",
|
||||
&this_update, FALSE, new ? "newer":"not newer",
|
||||
&that_update, FALSE, new ? "replaced":"retained");
|
||||
return new;
|
||||
|
|
|
@ -1010,7 +1010,7 @@ static bool is_newer(certificate_t *this, certificate_t *that)
|
|||
this->get_validity(this, &now, &this_update, NULL);
|
||||
that->get_validity(that, &now, &that_update, NULL);
|
||||
new = this_update > that_update;
|
||||
DBG1(" certificate from %#T is %s - existing certificate from %#T %s",
|
||||
DBG1(" certificate from %T is %s - existing certificate from %T %s",
|
||||
&this_update, FALSE, new ? "newer":"not newer",
|
||||
&that_update, FALSE, new ? "replaced":"retained");
|
||||
return new;
|
||||
|
|
|
@ -539,7 +539,7 @@ static bool is_newer(private_x509_crl_t *this, crl_t *that)
|
|||
this_cert->get_validity(this_cert, &now, &this_update, NULL);
|
||||
that_cert->get_validity(that_cert, &now, &that_update, NULL);
|
||||
new = this_update > that_update;
|
||||
DBG1(" crl from %#T is %s - existing crl from %#T %s",
|
||||
DBG1(" crl from %T is %s - existing crl from %T %s",
|
||||
&this_update, FALSE, new ? "newer":"not newer",
|
||||
&that_update, FALSE, new ? "replaced":"retained");
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* Copyright (C) 2008 Martin Willi
|
||||
* Copyright (C) 2007 Andreas Steffen
|
||||
* Hochschule für Technik Rapperswil
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
* Copyright (C) 2003 Christoph Gysin, Simon Zwahlen
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
|
@ -806,7 +806,7 @@ static bool is_newer(certificate_t *this, certificate_t *that)
|
|||
this->get_validity(this, &now, &this_update, NULL);
|
||||
that->get_validity(that, &now, &that_update, NULL);
|
||||
new = this_update > that_update;
|
||||
DBG1(" ocsp response from %#T is %s - existing ocsp response from %#T %s",
|
||||
DBG1(" ocsp response from %T is %s - existing ocsp response from %T %s",
|
||||
&this_update, FALSE, new ? "newer":"not newer",
|
||||
&that_update, FALSE, new ? "replaced":"retained");
|
||||
return new;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2009 Tobias Brunner
|
||||
* Copyright (C) 2006-2008 Martin Willi
|
||||
* Hochschule fuer Technik Rapperswil
|
||||
*
|
||||
|
@ -18,8 +19,17 @@
|
|||
#include "printf_hook.h"
|
||||
|
||||
#include <utils.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct private_printf_hook_t private_printf_hook_t;
|
||||
typedef struct printf_hook_handler_t printf_hook_handler_t;
|
||||
|
||||
#define PRINTF_BUF_LEN 8192
|
||||
#define ARGS_MAX 3
|
||||
|
||||
/**
|
||||
* private data of printf_hook
|
||||
|
@ -32,13 +42,340 @@ struct private_printf_hook_t {
|
|||
printf_hook_t public;
|
||||
};
|
||||
|
||||
/**
|
||||
* struct with information about a registered handler
|
||||
*/
|
||||
struct printf_hook_handler_t {
|
||||
|
||||
/**
|
||||
* callback function
|
||||
*/
|
||||
printf_hook_function_t hook;
|
||||
|
||||
/**
|
||||
* number of arguments
|
||||
*/
|
||||
int numargs;
|
||||
|
||||
/**
|
||||
* types of the arguments
|
||||
*/
|
||||
int argtypes[ARGS_MAX];
|
||||
|
||||
#ifndef HAVE_PRINTF_HOOKS
|
||||
/**
|
||||
* name required for Vstr
|
||||
*/
|
||||
char *name;
|
||||
#endif
|
||||
};
|
||||
|
||||
/* A-Z | 6 other chars | a-z */
|
||||
#define NUM_HANDLERS 58
|
||||
static printf_hook_handler_t *printf_hooks[NUM_HANDLERS];
|
||||
|
||||
#define SPEC_TO_INDEX(spec) ((int)(spec) - (int)'A')
|
||||
#define IS_VALID_SPEC(spec) (SPEC_TO_INDEX(spec) > -1 && SPEC_TO_INDEX(spec) < NUM_HANDLERS)
|
||||
|
||||
#ifdef HAVE_PRINTF_HOOKS
|
||||
|
||||
/**
|
||||
* Printf hook print function. This is actually of type "printf_function",
|
||||
* however glibc does it typedef to function, but uclibc to a pointer.
|
||||
* So we redefine it here.
|
||||
*/
|
||||
static int custom_print(FILE *stream, const struct printf_info *info,
|
||||
const void *const *args)
|
||||
{
|
||||
int written;
|
||||
char buf[PRINTF_BUF_LEN];
|
||||
printf_hook_spec_t spec;
|
||||
printf_hook_handler_t *handler = printf_hooks[SPEC_TO_INDEX(info->spec)];
|
||||
|
||||
spec.hash = info->alt;
|
||||
spec.minus = info->left;
|
||||
spec.width = info->width;
|
||||
|
||||
written = handler->hook(buf, sizeof(buf), &spec, args);
|
||||
if (written > 0)
|
||||
{
|
||||
fwrite(buf, 1, written, stream);
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
/**
|
||||
* Printf hook arginfo function, which is actually of type
|
||||
* "printf_arginfo_function".
|
||||
*/
|
||||
static int custom_arginfo(const struct printf_info *info, size_t n, int *argtypes)
|
||||
{
|
||||
int i;
|
||||
printf_hook_handler_t *handler = printf_hooks[SPEC_TO_INDEX(info->spec)];
|
||||
|
||||
if (handler->numargs <= n)
|
||||
{
|
||||
for (i = 0; i < handler->numargs; ++i)
|
||||
{
|
||||
argtypes[i] = handler->argtypes[i];
|
||||
}
|
||||
}
|
||||
return handler->numargs;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <errno.h>
|
||||
#include <unistd.h> /* for STDOUT_FILENO */
|
||||
|
||||
/**
|
||||
* Vstr custom format specifier callback function.
|
||||
*/
|
||||
static int custom_fmt_cb(Vstr_base *base, size_t pos, Vstr_fmt_spec *fmt_spec)
|
||||
{
|
||||
int i, written;
|
||||
char buf[PRINTF_BUF_LEN];
|
||||
const void *args[ARGS_MAX];
|
||||
printf_hook_spec_t spec;
|
||||
printf_hook_handler_t *handler = printf_hooks[SPEC_TO_INDEX(fmt_spec->name[0])];
|
||||
|
||||
for (i = 0; i < handler->numargs; i++)
|
||||
{
|
||||
switch(handler->argtypes[i])
|
||||
{
|
||||
case PRINTF_HOOK_ARGTYPE_INT:
|
||||
args[i] = VSTR_FMT_CB_ARG_PTR(fmt_spec, i);
|
||||
break;
|
||||
case PRINTF_HOOK_ARGTYPE_POINTER:
|
||||
args[i] = &VSTR_FMT_CB_ARG_PTR(fmt_spec, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
spec.hash = fmt_spec->fmt_hash;
|
||||
spec.minus = fmt_spec->fmt_minus;
|
||||
spec.width = fmt_spec->fmt_field_width;
|
||||
|
||||
written = handler->hook(buf, sizeof(buf), &spec, args);
|
||||
if (written > 0)
|
||||
{
|
||||
vstr_add_buf(base, pos, buf, written);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a custom format handler to the given Vstr_conf object
|
||||
*/
|
||||
static void vstr_fmt_add_handler(Vstr_conf *conf, printf_hook_handler_t *handler)
|
||||
{
|
||||
int *at = handler->argtypes;
|
||||
switch(handler->numargs)
|
||||
{
|
||||
case 1:
|
||||
vstr_fmt_add(conf, handler->name, custom_fmt_cb, at[0], VSTR_TYPE_FMT_END);
|
||||
break;
|
||||
case 2:
|
||||
vstr_fmt_add(conf, handler->name, custom_fmt_cb, at[0], at[1], VSTR_TYPE_FMT_END);
|
||||
break;
|
||||
case 3:
|
||||
vstr_fmt_add(conf, handler->name, custom_fmt_cb, at[0], at[1], at[2], VSTR_TYPE_FMT_END);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Management of thread-specific Vstr_conf objects
|
||||
*/
|
||||
#include <pthread.h>
|
||||
|
||||
static pthread_key_t vstr_conf_key;
|
||||
static pthread_once_t vstr_conf_key_once = PTHREAD_ONCE_INIT;
|
||||
|
||||
static void init_vstr_conf_key(void)
|
||||
{
|
||||
pthread_key_create(&vstr_conf_key, (void*)vstr_free_conf);
|
||||
}
|
||||
|
||||
static Vstr_conf *create_vstr_conf()
|
||||
{
|
||||
int i;
|
||||
Vstr_conf *conf = vstr_make_conf();
|
||||
vstr_cntl_conf(conf, VSTR_CNTL_CONF_SET_FMT_CHAR_ESC, '%');
|
||||
vstr_cntl_conf(conf, VSTR_CNTL_CONF_SET_TYPE_GRPALLOC_CACHE,
|
||||
VSTR_TYPE_CNTL_CONF_GRPALLOC_CSTR);
|
||||
vstr_cntl_conf(conf, VSTR_CNTL_CONF_SET_NUM_BUF_SZ, PRINTF_BUF_LEN);
|
||||
for (i = 0; i < NUM_HANDLERS; ++i)
|
||||
{
|
||||
printf_hook_handler_t *handler = printf_hooks[i];
|
||||
if (handler)
|
||||
{
|
||||
vstr_fmt_add_handler(conf, handler);
|
||||
}
|
||||
}
|
||||
return conf;
|
||||
}
|
||||
|
||||
static inline Vstr_conf *get_vstr_conf()
|
||||
{
|
||||
Vstr_conf *conf;
|
||||
pthread_once(&vstr_conf_key_once, init_vstr_conf_key);
|
||||
conf = (Vstr_conf*)pthread_getspecific(vstr_conf_key);
|
||||
if (!conf)
|
||||
{
|
||||
conf = create_vstr_conf();
|
||||
pthread_setspecific(vstr_conf_key, conf);
|
||||
}
|
||||
return conf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper functions for printf and alike
|
||||
*/
|
||||
int vstr_wrapper_printf(const char *format, ...)
|
||||
{
|
||||
int written;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
written = vstr_wrapper_vprintf(format, args);
|
||||
va_end(args);
|
||||
return written;
|
||||
}
|
||||
int vstr_wrapper_fprintf(FILE *stream, const char *format, ...)
|
||||
{
|
||||
int written;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
written = vstr_wrapper_vfprintf(stream, format, args);
|
||||
va_end(args);
|
||||
return written;
|
||||
}
|
||||
int vstr_wrapper_sprintf(char *str, const char *format, ...)
|
||||
{
|
||||
int written;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
written = vstr_wrapper_vsprintf(str, format, args);
|
||||
va_end(args);
|
||||
return written;
|
||||
}
|
||||
int vstr_wrapper_snprintf(char *str, size_t size, const char *format, ...)
|
||||
{
|
||||
int written;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
written = vstr_wrapper_vsnprintf(str, size, format, args);
|
||||
va_end(args);
|
||||
return written;
|
||||
}
|
||||
static inline int vstr_wrapper_vprintf_internal(int fd, const char *format,
|
||||
va_list args)
|
||||
{
|
||||
int written;
|
||||
Vstr_conf *conf = get_vstr_conf();
|
||||
Vstr_base *s = vstr_make_base(conf);
|
||||
vstr_add_vfmt(s, 0, format, args);
|
||||
written = s->len;
|
||||
while (s->len)
|
||||
{
|
||||
if (!vstr_sc_write_fd(s, 1, s->len, fd, NULL))
|
||||
{
|
||||
if (errno != EAGAIN && errno != EINTR)
|
||||
{
|
||||
written -= s->len;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
vstr_free_base(s);
|
||||
return written;
|
||||
}
|
||||
int vstr_wrapper_vprintf(const char *format, va_list args)
|
||||
{
|
||||
return vstr_wrapper_vprintf_internal(STDOUT_FILENO, format, args);
|
||||
}
|
||||
int vstr_wrapper_vfprintf(FILE *stream, const char *format, va_list args)
|
||||
{
|
||||
return vstr_wrapper_vprintf_internal(fileno(stream), format, args);
|
||||
}
|
||||
static inline int vstr_wrapper_vsnprintf_internal(char *str, size_t size,
|
||||
const char *format,
|
||||
va_list args)
|
||||
{
|
||||
int written;
|
||||
Vstr_conf *conf = get_vstr_conf();
|
||||
Vstr_base *s = vstr_make_base(conf);
|
||||
vstr_add_vfmt(s, 0, format, args);
|
||||
written = s->len;
|
||||
vstr_export_cstr_buf(s, 1, s->len, str, (size > 0) ? size : s->len + 1);
|
||||
vstr_free_base(s);
|
||||
return written;
|
||||
}
|
||||
int vstr_wrapper_vsprintf(char *str, const char *format, va_list args)
|
||||
{
|
||||
return vstr_wrapper_vsnprintf_internal(str, 0, format, args);
|
||||
}
|
||||
int vstr_wrapper_vsnprintf(char *str, size_t size, const char *format,
|
||||
va_list args)
|
||||
{
|
||||
return (size > 0) ? vstr_wrapper_vsnprintf_internal(str, size, format, args) : 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Implementation of printf_hook_t.add_handler.
|
||||
*/
|
||||
static void add_handler(private_printf_hook_t *this, char spec,
|
||||
printf_hook_functions_t hook)
|
||||
static void add_handler(private_printf_hook_t *this, char spec,
|
||||
printf_hook_function_t hook, ...)
|
||||
{
|
||||
register_printf_function(spec, hook.print, hook.arginfo);
|
||||
int i = -1;
|
||||
printf_hook_handler_t *handler;
|
||||
printf_hook_argtype_t argtype;
|
||||
va_list args;
|
||||
|
||||
if (!IS_VALID_SPEC(spec))
|
||||
{
|
||||
DBG1("'%c' is not a valid printf hook specifier, not registered!", spec);
|
||||
return;
|
||||
}
|
||||
|
||||
handler = malloc_thing(printf_hook_handler_t);
|
||||
handler->hook = hook;
|
||||
|
||||
va_start(args, hook);
|
||||
while ((argtype = va_arg(args, printf_hook_argtype_t)) != PRINTF_HOOK_ARGTYPE_END)
|
||||
{
|
||||
if (++i >= ARGS_MAX)
|
||||
{
|
||||
DBG1("Too many arguments for printf hook with specifier '%c', not registered!", spec);
|
||||
va_end(args);
|
||||
free(handler);
|
||||
return;
|
||||
}
|
||||
handler->argtypes[i] = argtype;
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
handler->numargs = i + 1;
|
||||
|
||||
if (handler->numargs > 0)
|
||||
{
|
||||
#ifdef HAVE_PRINTF_HOOKS
|
||||
register_printf_function(spec, custom_print, custom_arginfo);
|
||||
#else
|
||||
Vstr_conf *conf = get_vstr_conf();
|
||||
handler->name = malloc(2);
|
||||
handler->name[0] = spec;
|
||||
handler->name[1] = '\0';
|
||||
vstr_fmt_add_handler(conf, handler);
|
||||
#endif
|
||||