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_ @} */