From d25ce3701e066121d2026b0dfd72a794f3bbee29 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Thu, 12 Mar 2009 18:07:32 +0000 Subject: [PATCH] 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). --- configure.in | 9 + src/charon/config/proposal.c | 62 +-- src/charon/config/proposal.h | 5 +- src/charon/config/traffic_selector.c | 57 +-- src/charon/config/traffic_selector.h | 5 +- src/charon/credentials/credential_manager.c | 16 +- src/charon/daemon.c | 10 +- src/charon/plugins/sql/pool.c | 4 +- src/charon/plugins/stroke/stroke_list.c | 38 +- src/charon/sa/ike_sa.c | 3 +- src/libstrongswan/Makefile.am | 4 + src/libstrongswan/asn1/asn1.c | 2 +- src/libstrongswan/chunk.c | 41 +- src/libstrongswan/chunk.h | 8 +- src/libstrongswan/enum.c | 36 +- src/libstrongswan/enum.h | 11 +- src/libstrongswan/library.c | 26 +- src/libstrongswan/library.h | 2 +- src/libstrongswan/plugins/x509/x509_ac.c | 2 +- src/libstrongswan/plugins/x509/x509_cert.c | 2 +- src/libstrongswan/plugins/x509/x509_crl.c | 2 +- .../plugins/x509/x509_ocsp_response.c | 4 +- src/libstrongswan/printf_hook.c | 379 +++++++++++++++++- src/libstrongswan/printf_hook.h | 109 ++++- src/libstrongswan/utils.c | 150 ++----- src/libstrongswan/utils.h | 21 +- src/libstrongswan/utils/hashtable.c | 4 +- src/libstrongswan/utils/host.c | 40 +- src/libstrongswan/utils/host.h | 9 +- src/libstrongswan/utils/identification.c | 38 +- src/libstrongswan/utils/identification.h | 11 +- src/libstrongswan/utils/leak_detective.c | 1 - src/libstrongswan/utils/linked_list.c | 10 +- 33 files changed, 678 insertions(+), 443 deletions(-) diff --git a/configure.in b/configure.in index a93fcb86e..81909c078 100644 --- a/configure.in +++ b/configure.in @@ -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 diff --git a/src/charon/config/proposal.c b/src/charon/config/proposal.c index 4ac95aaf3..da6973925 100644 --- a/src/charon/config/proposal.c +++ b/src/charon/config/proposal.c @@ -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. */ diff --git a/src/charon/config/proposal.h b/src/charon/config/proposal.h index 08e8b8c1b..89adf350c 100644 --- a/src/charon/config/proposal.h +++ b/src/charon/config/proposal.h @@ -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_ @} */ diff --git a/src/charon/config/traffic_selector.c b/src/charon/config/traffic_selector.c index f504a0033..7741a1227 100644 --- a/src/charon/config/traffic_selector.c +++ b/src/charon/config/traffic_selector.c @@ -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 #include #include -#include #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 */ diff --git a/src/charon/config/traffic_selector.h b/src/charon/config/traffic_selector.h index 073bbc17b..42d188157 100644 --- a/src/charon/config/traffic_selector.h +++ b/src/charon/config/traffic_selector.h @@ -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_ @} */ diff --git a/src/charon/credentials/credential_manager.c b/src/charon/credentials/credential_manager.c index 842088371..2721edc4a 100644 --- a/src/charon/credentials/credential_manager.c +++ b/src/charon/credentials/credential_manager.c @@ -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 && diff --git a/src/charon/daemon.c b/src/charon/daemon.c index 78cbeec83..6dcb39a89 100644 --- a/src/charon/daemon.c +++ b/src/charon/daemon.c @@ -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; diff --git a/src/charon/plugins/sql/pool.c b/src/charon/plugins/sql/pool.c index 8f5dc54dd..9761e88e9 100644 --- a/src/charon/plugins/sql/pool.c +++ b/src/charon/plugins/sql/pool.c @@ -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 { diff --git a/src/charon/plugins/stroke/stroke_list.c b/src/charon/plugins/stroke/stroke_list.c index 99fe8c468..94b3def3a 100644 --- a/src/charon/plugins/stroke/stroke_list.c +++ b/src/charon/plugins/stroke/stroke_list.c @@ -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"); } diff --git a/src/charon/sa/ike_sa.c b/src/charon/sa/ike_sa.c index 2ce7a11ef..30141f551 100644 --- a/src/charon/sa/ike_sa.c +++ b/src/charon/sa/ike_sa.c @@ -20,7 +20,6 @@ #include #include -#include #include #include #include @@ -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; } diff --git a/src/libstrongswan/Makefile.am b/src/libstrongswan/Makefile.am index 110c2ef16..1d0f837ef 100644 --- a/src/libstrongswan/Makefile.am +++ b/src/libstrongswan/Makefile.am @@ -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 diff --git a/src/libstrongswan/asn1/asn1.c b/src/libstrongswan/asn1/asn1.c index f5fcfdd05..fdb61c47f 100644 --- a/src/libstrongswan/asn1/asn1.c +++ b/src/libstrongswan/asn1/asn1.c @@ -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: diff --git a/src/libstrongswan/chunk.c b/src/libstrongswan/chunk.c index 01f6b9ed7..ce424f7d6 100644 --- a/src/libstrongswan/chunk.c +++ b/src/libstrongswan/chunk.c @@ -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 -#include /* 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; -} - diff --git a/src/libstrongswan/chunk.h b/src/libstrongswan/chunk.h index 0487fa008..613a093dc 100644 --- a/src/libstrongswan/chunk.h +++ b/src/libstrongswan/chunk.h @@ -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_ @}*/ diff --git a/src/libstrongswan/enum.c b/src/libstrongswan/enum.c index 724246e25..c68785aff 100644 --- a/src/libstrongswan/enum.c +++ b/src/libstrongswan/enum.c @@ -20,8 +20,6 @@ #include "enum.h" -#include - /** * 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; -} - diff --git a/src/libstrongswan/enum.h b/src/libstrongswan/enum.h index cbf15de73..d585a1b0c 100644 --- a/src/libstrongswan/enum.h +++ b/src/libstrongswan/enum.h @@ -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_ @}*/ diff --git a/src/libstrongswan/library.c b/src/libstrongswan/library.c index 6b9043b1a..da7b0fca7 100644 --- a/src/libstrongswan/library.c +++ b/src/libstrongswan/library.c @@ -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(); diff --git a/src/libstrongswan/library.h b/src/libstrongswan/library.h index 2ea4b624b..9824e4e3b 100644 --- a/src/libstrongswan/library.h +++ b/src/libstrongswan/library.h @@ -57,10 +57,10 @@ #ifndef LIBRARY_H_ #define LIBRARY_H_ +#include #include #include #include -#include #include #include #include diff --git a/src/libstrongswan/plugins/x509/x509_ac.c b/src/libstrongswan/plugins/x509/x509_ac.c index fbb7707a1..2168f9bc7 100644 --- a/src/libstrongswan/plugins/x509/x509_ac.c +++ b/src/libstrongswan/plugins/x509/x509_ac.c @@ -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; diff --git a/src/libstrongswan/plugins/x509/x509_cert.c b/src/libstrongswan/plugins/x509/x509_cert.c index 0c8011e8a..b09f446b2 100644 --- a/src/libstrongswan/plugins/x509/x509_cert.c +++ b/src/libstrongswan/plugins/x509/x509_cert.c @@ -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; diff --git a/src/libstrongswan/plugins/x509/x509_crl.c b/src/libstrongswan/plugins/x509/x509_crl.c index 62a4064cd..3bb097c4d 100644 --- a/src/libstrongswan/plugins/x509/x509_crl.c +++ b/src/libstrongswan/plugins/x509/x509_crl.c @@ -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"); } diff --git a/src/libstrongswan/plugins/x509/x509_ocsp_response.c b/src/libstrongswan/plugins/x509/x509_ocsp_response.c index 8214d91c3..257ae7597 100644 --- a/src/libstrongswan/plugins/x509/x509_ocsp_response.c +++ b/src/libstrongswan/plugins/x509/x509_ocsp_response.c @@ -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; diff --git a/src/libstrongswan/printf_hook.c b/src/libstrongswan/printf_hook.c index 26c0cac55..0475c3ec9 100644 --- a/src/libstrongswan/printf_hook.c +++ b/src/libstrongswan/printf_hook.c @@ -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 +#include + +#include +#include +#include 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 +#include /* 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 + +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 + printf_hooks[SPEC_TO_INDEX(spec)] = handler; + } + else + { + free(handler); + } } /** @@ -46,6 +383,30 @@ static void add_handler(private_printf_hook_t *this, char spec, */ static void destroy(private_printf_hook_t *this) { + int i; +#ifndef HAVE_PRINTF_HOOKS + Vstr_conf *conf = get_vstr_conf(); +#endif + + for (i = 0; i < NUM_HANDLERS; ++i) + { + printf_hook_handler_t *handler = printf_hooks[i]; + if (handler) + { +#ifndef HAVE_PRINTF_HOOKS + vstr_fmt_del(conf, handler->name); + free(handler->name); +#endif + free(handler); + } + } + +#ifndef HAVE_PRINTF_HOOKS + /* freeing the Vstr_conf of the main thread */ + pthread_key_delete(vstr_conf_key); + vstr_free_conf(conf); + vstr_exit(); +#endif free(this); } @@ -56,9 +417,19 @@ printf_hook_t *printf_hook_create() { private_printf_hook_t *this = malloc_thing(private_printf_hook_t); - this->public.add_handler = (void(*)(printf_hook_t*, char, printf_hook_functions_t))add_handler; + this->public.add_handler = (void(*)(printf_hook_t*, char, printf_hook_function_t, ...))add_handler; this->public.destroy = (void(*)(printf_hook_t*))destroy; + memset(printf_hooks, 0, sizeof(printf_hooks)); + +#ifndef HAVE_PRINTF_HOOKS + if (!vstr_init()) + { + DBG1("failed to initialize Vstr library!"); + free(this); + return NULL; + } +#endif return &this->public; } diff --git a/src/libstrongswan/printf_hook.h b/src/libstrongswan/printf_hook.h index 69b8b3507..86eaa1c43 100644 --- a/src/libstrongswan/printf_hook.h +++ b/src/libstrongswan/printf_hook.h @@ -1,4 +1,5 @@ /* + * Copyright (C) 2009 Tobias Brunner * Copyright (C) 2006-2008 Martin Willi * Hochschule fuer Technik Rapperswil * @@ -24,30 +25,103 @@ #define PRINTF_HOOK_H_ typedef struct printf_hook_t printf_hook_t; -typedef struct printf_hook_functions_t printf_hook_functions_t; +typedef struct printf_hook_spec_t printf_hook_spec_t; +typedef enum printf_hook_argtype_t printf_hook_argtype_t; + +#ifdef HAVE_PRINTF_HOOKS #include -/** - * Printf hook function set. - * - * A printf hook has two functions, one to print the string, one to read - * in the number of arguments. See . - */ -struct printf_hook_functions_t { +enum printf_hook_argtype_t { + PRINTF_HOOK_ARGTYPE_END = PA_LAST, + PRINTF_HOOK_ARGTYPE_INT = PA_INT, + PRINTF_HOOK_ARGTYPE_POINTER = PA_POINTER, +}; +#else + +#include + +enum printf_hook_argtype_t { + PRINTF_HOOK_ARGTYPE_END = VSTR_TYPE_FMT_END, + PRINTF_HOOK_ARGTYPE_INT = VSTR_TYPE_FMT_INT, + PRINTF_HOOK_ARGTYPE_POINTER = VSTR_TYPE_FMT_PTR_VOID, +}; + +/** + * Redefining printf and alike + */ +#include +#include + +int vstr_wrapper_printf(const char *format, ...); +int vstr_wrapper_fprintf(FILE *stream, const char *format, ...); +int vstr_wrapper_sprintf(char *str, const char *format, ...); +int vstr_wrapper_snprintf(char *str, size_t size, const char *format, ...); + +int vstr_wrapper_vprintf(const char *format, va_list ap); +int vstr_wrapper_vfprintf(FILE *stream, const char *format, va_list ap); +int vstr_wrapper_vsprintf(char *str, const char *format, va_list ap); +int vstr_wrapper_vsnprintf(char *str, size_t size, const char *format, va_list ap); + +#define printf vstr_wrapper_printf +#define fprintf vstr_wrapper_fprintf +#define sprintf vstr_wrapper_sprintf +#define snprintf vstr_wrapper_snprintf + +#define vprintf vstr_wrapper_vprintf +#define vfprintf vstr_wrapper_vfprintf +#define vsprintf vstr_wrapper_vsprintf +#define vsnprintf vstr_wrapper_vsnprintf + +#endif + +/** + * Callback function type for printf hooks. + * + * @param dst destination buffer + * @param len length of the buffer + * @param spec format specifier + * @param args arguments array + * @return number of characters written + */ +typedef int (*printf_hook_function_t)(char *dst, size_t len, + 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)\ + {\ + _written = buflen - 1;\ + }\ + buf += _written;\ + buflen -= _written;\ + _written;\ +}) + +/** + * Properties of the format specifier + */ +struct printf_hook_spec_t { /** - * 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. + * TRUE if a '#' was used in the format specifier */ - int (*print)(FILE *, const struct printf_info *info, const void *const *args); + int hash; /** - * Printf hook arginfo function, which is actually of type - * "printf_arginfo_function". + * TRUE if a '-' was used in the format specifier */ - int (*arginfo)(const struct printf_info *info, size_t n, int *argtypes); + int minus; + + /** + * The width as given in the format specifier. + */ + int width; }; /** @@ -59,10 +133,11 @@ struct printf_hook_t { * Register a printf handler. * * @param spec printf hook format character - * @param hook hook functions + * @param hook hook function + * @param ... list of PRINTF_HOOK_ARGTYPE_*, MUST end with PRINTF_HOOK_ARGTYPE_END */ void (*add_handler)(printf_hook_t *this, char spec, - printf_hook_functions_t hook); + printf_hook_function_t hook, ...); /** * Destroy a printf_hook instance. diff --git a/src/libstrongswan/utils.c b/src/libstrongswan/utils.c index b1974cf97..dc5b16a30 100644 --- a/src/libstrongswan/utils.c +++ b/src/libstrongswan/utils.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Tobias Brunner + * Copyright (C) 2008-2009 Tobias Brunner * Copyright (C) 2005-2008 Martin Willi * Hochschule fuer Technik Rapperswil * @@ -187,27 +187,23 @@ bool ref_put(refcount_t *ref) #endif /* HAVE_GCC_ATOMIC_OPERATIONS */ /** - * output handler in printf() for time_t + * Described in header. */ -static int time_print(FILE *stream, const struct printf_info *info, - const void *const *args) +int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, + const void *const *args) { static const char* months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; time_t *time = *((time_t**)(args[0])); - bool utc = TRUE; + bool utc = *((bool*)(args[1]));; struct tm t; - if (info->alt) - { - utc = *((bool*)(args[1])); - } if (time == UNDEFINED_TIME) { - return fprintf(stream, "--- -- --:--:--%s----", - info->alt ? " UTC " : " "); + return print_in_hook(dst, len, "--- -- --:--:--%s----", + utc ? " UTC " : " "); } if (utc) { @@ -217,54 +213,22 @@ static int time_print(FILE *stream, const struct printf_info *info, { localtime_r(time, &t); } - return fprintf(stream, "%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); + return print_in_hook(dst, len, "%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); } /** - * arginfo handler for printf() time + * Described in header. */ -static int time_arginfo(const struct printf_info *info, size_t n, int *argtypes) -{ - if (info->alt) - { - if (n > 1) - { - argtypes[0] = PA_POINTER; - argtypes[1] = PA_INT; - } - return 2; - } - - if (n > 0) - { - argtypes[0] = PA_POINTER; - } - return 1; -} - -/** - * output handler in printf() for time deltas - */ -static int time_delta_print(FILE *stream, const struct printf_info *info, - const void *const *args) +int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, + const void *const *args) { char* unit = "second"; - time_t *arg1, *arg2; - time_t delta; + time_t *arg1 = *((time_t**)(args[0])); + time_t *arg2 = *((time_t**)(args[1])); + time_t delta = abs(*arg1 - *arg2); - arg1 = *((time_t**)(args[0])); - if (info->alt) - { - arg2 = *((time_t**)(args[1])); - delta = abs(*arg1 - *arg2); - } - else - { - delta = *arg1; - } - if (delta > 2 * 60 * 60 * 24) { delta /= 60 * 60 * 24; @@ -280,29 +244,7 @@ static int time_delta_print(FILE *stream, const struct printf_info *info, delta /= 60; unit = "minute"; } - return fprintf(stream, "%d %s%s", delta, unit, (delta == 1)? "":"s"); -} - -/** - * arginfo handler for printf() time deltas - */ -int time_delta_arginfo(const struct printf_info *info, size_t n, int *argtypes) -{ - if (info->alt) - { - if (n > 1) - { - argtypes[0] = PA_POINTER; - argtypes[1] = PA_POINTER; - } - return 2; - } - - if (n > 0) - { - argtypes[0] = PA_POINTER; - } - return 1; + return print_in_hook(dst, len, "%d %s%s", delta, unit, (delta == 1)? "":"s"); } /** @@ -313,10 +255,10 @@ int time_delta_arginfo(const struct printf_info *info, size_t n, int *argtypes) static char hexdig_upper[] = "0123456789ABCDEF"; /** - * output handler in printf() for mem ranges + * Described in header. */ -static int mem_print(FILE *stream, const struct printf_info *info, - const void *const *args) +int mem_printf_hook(char *dst, size_t dstlen, + printf_hook_spec_t *spec, const void *const *args) { char *bytes = *((void**)(args[0])); int len = *((size_t*)(args[1])); @@ -330,7 +272,7 @@ static int mem_print(FILE *stream, const struct printf_info *info, int i = 0; int written = 0; - written += fprintf(stream, "=> %d bytes @ %p", len, bytes); + written += print_in_hook(dst, dstlen, "=> %d bytes @ %p", len, bytes); while (bytes_pos < bytes_roof) { @@ -343,7 +285,6 @@ static int mem_print(FILE *stream, const struct printf_info *info, if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE) { int padding = 3 * (BYTES_PER_LINE - i); - int written; while (padding--) { @@ -352,9 +293,8 @@ static int mem_print(FILE *stream, const struct printf_info *info, *buffer_pos++ = '\0'; ascii_buffer[i] = '\0'; - written += fprintf(stream, "\n%4d: %s %s", - line_start, buffer, ascii_buffer); - + written += print_in_hook(dst, dstlen, "\n%4d: %s %s", + line_start, buffer, ascii_buffer); buffer_pos = buffer; line_start += BYTES_PER_LINE; @@ -367,47 +307,3 @@ static int mem_print(FILE *stream, const struct printf_info *info, } return written; } - -/** - * arginfo handler for printf() mem ranges - */ -int mem_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 for a time - */ -printf_hook_functions_t time_get_printf_hooks() -{ - printf_hook_functions_t hooks = {time_print, time_arginfo}; - - return hooks; -} - -/** - * return printf hook functions for a time delta - */ -printf_hook_functions_t time_delta_get_printf_hooks() -{ - printf_hook_functions_t hooks = {time_delta_print, time_delta_arginfo}; - - return hooks; -} - -/** - * return printf hook functions for mem ranges - */ -printf_hook_functions_t mem_get_printf_hooks() -{ - printf_hook_functions_t hooks = {mem_print, mem_arginfo}; - - return hooks; -} - diff --git a/src/libstrongswan/utils.h b/src/libstrongswan/utils.h index c794b0797..947a488fe 100644 --- a/src/libstrongswan/utils.h +++ b/src/libstrongswan/utils.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Tobias Brunner + * Copyright (C) 2008-2009 Tobias Brunner * Copyright (C) 2008 Martin Willi * Hochschule fuer Technik Rapperswil * @@ -285,31 +285,30 @@ bool ref_put(refcount_t *ref); #endif /* HAVE_GCC_ATOMIC_OPERATIONS */ /** - * Get printf hooks for time. + * printf hook for time_t. * * Arguments are: - * time_t* time - * Arguments using #-specificer * time_t* time, bool utc */ -printf_hook_functions_t time_get_printf_hooks(); +int time_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, + const void *const *args); /** - * Get printf hooks for time deltas. + * printf hook for time_t deltas. * * Arguments are: - * time_t* delta - * Arguments using #-specificer * time_t* begin, time_t* end */ -printf_hook_functions_t time_delta_get_printf_hooks(); +int time_delta_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, + const void *const *args); /** - * Get printf hooks for time deltas. + * printf hook for memory areas. * * Arguments are: * u_char *ptr, int len */ -printf_hook_functions_t mem_get_printf_hooks(); +int mem_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, + const void *const *args); #endif /* UTILS_H_ @}*/ diff --git a/src/libstrongswan/utils/hashtable.c b/src/libstrongswan/utils/hashtable.c index 9c364dddf..295f28565 100644 --- a/src/libstrongswan/utils/hashtable.c +++ b/src/libstrongswan/utils/hashtable.c @@ -282,7 +282,7 @@ static void *get(private_hashtable_t *this, void *key) /** * Implementation of hashtable_t.remove */ -static void *remove(private_hashtable_t *this, void *key) +static void *remove_(private_hashtable_t *this, void *key) { void *value = NULL; linked_list_t *list; @@ -414,7 +414,7 @@ hashtable_t *hashtable_create(hashtable_hash_t hash, hashtable_equals_t equals, this->public.put = (void*(*)(hashtable_t*,void*,void*))put; this->public.get = (void*(*)(hashtable_t*,void*))get; - this->public.remove = (void*(*)(hashtable_t*,void*))remove; + this->public.remove = (void*(*)(hashtable_t*,void*))remove_; this->public.get_count = (u_int(*)(hashtable_t*))get_count; this->public.create_enumerator = (enumerator_t*(*)(hashtable_t*))create_enumerator; this->public.destroy = (void(*)(hashtable_t*))destroy; diff --git a/src/libstrongswan/utils/host.c b/src/libstrongswan/utils/host.c index eddd6f163..cb2b6a10f 100644 --- a/src/libstrongswan/utils/host.c +++ b/src/libstrongswan/utils/host.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2007 Tobias Brunner + * Copyright (C) 2006-2009 Tobias Brunner * Copyright (C) 2006 Daniel Roethlisberger * Copyright (C) 2005-2006 Martin Willi * Copyright (C) 2005 Jan Hutter @@ -21,7 +21,6 @@ #define _GNU_SOURCE #include #include -#include #include "host.h" @@ -106,10 +105,10 @@ static bool is_anyaddr(private_host_t *this) } /** - * output handler in printf() + * Described in header. */ -static int print(FILE *stream, const struct printf_info *info, - const void *const *args) +int host_printf_hook(char *dst, size_t dstlen, printf_hook_spec_t *spec, + const void *const *args) { private_host_t *this = *((private_host_t**)(args[0])); char buffer[INET6_ADDRSTRLEN + 16]; @@ -145,7 +144,7 @@ static int print(FILE *stream, const struct printf_info *info, snprintf(buffer, sizeof(buffer), "(address conversion failed)"); } - else if (info->alt) + else if (spec->hash) { len = strlen(buffer); snprintf(buffer + len, sizeof(buffer) - len, @@ -157,34 +156,11 @@ static int print(FILE *stream, const struct printf_info *info, break; } } - if (info->left) + if (spec->minus) { - return fprintf(stream, "%-*s", info->width, buffer); + return print_in_hook(dst, dstlen, "%-*s", spec->width, buffer); } - return fprintf(stream, "%*s", info->width, buffer); -} - - -/** - * arginfo handler for printf() hosts - */ -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 host - */ -printf_hook_functions_t host_get_printf_hooks() -{ - printf_hook_functions_t hooks = {print, arginfo}; - - return hooks; + return print_in_hook(dst, dstlen, "%*s", spec->width, buffer); } /** diff --git a/src/libstrongswan/utils/host.h b/src/libstrongswan/utils/host.h index 667cc6bcc..5f00872b8 100644 --- a/src/libstrongswan/utils/host.h +++ b/src/libstrongswan/utils/host.h @@ -1,7 +1,7 @@ /* - * Copyright (C) 2005-2008 Martin Willi - * 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 * Hochschule fuer Technik Rapperswil * @@ -198,12 +198,13 @@ host_t *host_create_from_sockaddr(sockaddr_t *sockaddr); host_t *host_create_any(int family); /** - * Get printf hooks for a host. + * printf hook function for host_t. * * Arguments are: * host_t *host * Use #-modifier to include port number */ -printf_hook_functions_t host_get_printf_hooks(); +int host_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, + const void *const *args); #endif /* HOST_H_ @}*/ diff --git a/src/libstrongswan/utils/identification.c b/src/libstrongswan/utils/identification.c index fad7e6acc..44e63b626 100644 --- a/src/libstrongswan/utils/identification.c +++ b/src/libstrongswan/utils/identification.c @@ -1,4 +1,5 @@ /* + * Copyright (C) 2009 Tobias Brunner * Copyright (C) 2005-2008 Martin Willi * Copyright (C) 2005 Jan Hutter * Hochschule fuer Technik Rapperswil @@ -23,7 +24,6 @@ #include #include #include -#include #include "identification.h" @@ -879,10 +879,10 @@ static id_match_t matches_dn(private_identification_t *this, } /** - * output handler in printf() + * Described in header. */ -static int print(FILE *stream, const struct printf_info *info, - const void *const *args) +int identification_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, + const void *const *args) { private_identification_t *this = *((private_identification_t**)(args[0])); char buf[BUF_LEN]; @@ -890,7 +890,7 @@ static int print(FILE *stream, const struct printf_info *info, if (this == NULL) { - return fprintf(stream, "%*s", info->width, "(null)"); + return print_in_hook(dst, len, "%*s", spec->width, "(null)"); } switch (this->type) @@ -940,33 +940,11 @@ static int print(FILE *stream, const struct printf_info *info, snprintf(buf, sizeof(buf), "(unknown ID type: %d)", this->type); break; } - if (info->left) + if (spec->minus) { - return fprintf(stream, "%-*s", info->width, buf); + return print_in_hook(dst, len, "%-*s", spec->width, buf); } - return fprintf(stream, "%*s", info->width, buf); -} - -/** - * arginfo handler - */ -static int arginfo(const struct printf_info *info, size_t n, int *argtypes) -{ - if (n > 0) - { - argtypes[0] = PA_POINTER; - } - return 1; -} - -/** - * Get printf hook functions - */ -printf_hook_functions_t identification_get_printf_hooks() -{ - printf_hook_functions_t hook = {print, arginfo}; - - return hook; + return print_in_hook(dst, len, "%*s", spec->width, buf); } /** diff --git a/src/libstrongswan/utils/identification.h b/src/libstrongswan/utils/identification.h index c83db3aee..1b5427bca 100644 --- a/src/libstrongswan/utils/identification.h +++ b/src/libstrongswan/utils/identification.h @@ -1,4 +1,5 @@ /* + * Copyright (C) 2009 Tobias Brunner * Copyright (C) 2005-2006 Martin Willi * Copyright (C) 2005 Jan Hutter * Hochschule fuer Technik Rapperswil @@ -274,10 +275,12 @@ identification_t * identification_create_from_string(char *string); identification_t * identification_create_from_encoding(id_type_t type, chunk_t encoded); /** - * Get the printf hook functions. - * - * @return printf hook functions + * printf hook function for identification_t. + * + * Arguments are: + * identification_t *identification */ -printf_hook_functions_t identification_get_printf_hooks(); +int identification_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec, + const void *const *args); #endif /* IDENTIFICATION_H_ @} */ diff --git a/src/libstrongswan/utils/leak_detective.c b/src/libstrongswan/utils/leak_detective.c index 63d1635f6..7983940b5 100644 --- a/src/libstrongswan/utils/leak_detective.c +++ b/src/libstrongswan/utils/leak_detective.c @@ -29,7 +29,6 @@ #include #include #include -#include #include #include "leak_detective.h" diff --git a/src/libstrongswan/utils/linked_list.c b/src/libstrongswan/utils/linked_list.c index 45d17c80f..ce34985bf 100644 --- a/src/libstrongswan/utils/linked_list.c +++ b/src/libstrongswan/utils/linked_list.c @@ -224,7 +224,7 @@ static void iterator_reset(private_iterator_t *this) /** * Implementation of iterator_t.remove. */ -static status_t remove_(private_iterator_t *this) +static status_t iterator_remove(private_iterator_t *this) { element_t *new_current; @@ -514,8 +514,8 @@ static status_t remove_last(private_linked_list_t *this, void **item) /** * Implementation of linked_list_t.remove. */ -static int remove(private_linked_list_t *this, void *item, - bool (*compare)(void *,void*)) +static int remove_(private_linked_list_t *this, void *item, + bool (*compare)(void *,void*)) { element_t *current = this->first; int removed = 0; @@ -727,7 +727,7 @@ static iterator_t *create_iterator(private_linked_list_t *linked_list, bool forw this->public.insert_before = (void (*) (iterator_t*, void *item)) insert_before; this->public.insert_after = (void (*) (iterator_t*, void *item)) insert_after; this->public.replace = (status_t (*) (iterator_t*, void **, void *)) replace; - this->public.remove = (status_t (*) (iterator_t*)) remove_; + this->public.remove = (status_t (*) (iterator_t*)) iterator_remove; this->public.reset = (void (*) (iterator_t*)) iterator_reset; this->public.destroy = (void (*) (iterator_t*)) iterator_destroy; @@ -756,7 +756,7 @@ linked_list_t *linked_list_create() this->public.insert_last = (void (*) (linked_list_t *, void *item))insert_last; this->public.remove_first = (status_t (*) (linked_list_t *, void **item))remove_first; this->public.remove_last = (status_t (*) (linked_list_t *, void **item))remove_last; - this->public.remove = (int(*)(linked_list_t*, void *item, bool (*compare)(void *,void*)))remove; + this->public.remove = (int(*)(linked_list_t*, void *item, bool (*compare)(void *,void*)))remove_; this->public.remove_at = (void(*)(linked_list_t*, enumerator_t *enumerator))remove_at; this->public.invoke_offset = (void (*)(linked_list_t*,size_t,...))invoke_offset; this->public.invoke_function = (void (*)(linked_list_t*,linked_list_invoke_t,...))invoke_function;