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).

This commit is contained in:
Tobias Brunner
2009-03-12 18:07:32 +00:00
parent 9086102dfd
commit d25ce3701e
33 changed files with 678 additions and 443 deletions
+20 -42
View File
@@ -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.
*/
+3 -2
View File
@@ -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_ @} */
+17 -40
View File
@@ -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
*/
+3 -2
View File
@@ -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_ @} */
+8 -8
View File
@@ -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, &not_before, &not_after))
{
DBG1(DBG_CFG, "subject certificate invalid (valid from %T to %T)",
&not_before, &not_after);
&not_before, TRUE, &not_after, TRUE);
return FALSE;
}
if (!issuer->get_validity(issuer, NULL, &not_before, &not_after))
{
DBG1(DBG_CFG, "issuer certificate invalid (valid from %T to %T)",
&not_before, &not_after);
&not_before, TRUE, &not_after, TRUE);
return FALSE;
}
if (issuer->get_type(issuer) == CERT_X509 &&
+7 -3
View File
@@ -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;
+2 -2
View File
@@ -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
{
+19 -19
View File
@@ -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, &notBefore, &notAfter);
fprintf(out, " validity: not before %#T, ", &notBefore, utc);
fprintf(out, " validity: not before %T, ", &notBefore, utc);
if (now < notBefore)
{
fprintf(out, "not valid yet (valid in %#V)\n", &now, &notBefore);
fprintf(out, "not valid yet (valid in %V)\n", &now, &notBefore);
}
else
{
fprintf(out, "ok\n");
}
fprintf(out, " not after %#T, ", &notAfter, utc);
fprintf(out, " not after %T, ", &notAfter, utc);
if (now > notAfter)
{
fprintf(out, "expired (%#V ago)\n", &now, &notAfter);
fprintf(out, "expired (%V ago)\n", &now, &notAfter);
}
else
{
fprintf(out, "ok");
if (now > notAfter - CERT_WARNING_INTERVAL * 60 * 60 * 24)
{
fprintf(out, " (expires in %#V)", &now, &notAfter);
fprintf(out, " (expires in %V)", &now, &notAfter);
}
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");
}
+1 -2
View File
@@ -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;
}