code cleanups in printf handlers

This commit is contained in:
Martin Willi
2006-10-20 05:57:25 +00:00
parent 490d324d81
commit e706c7f10b
2 changed files with 39 additions and 43 deletions
+12 -13
View File
@@ -165,8 +165,7 @@ static int print(FILE *stream, const struct printf_info *info,
struct servent *serv; struct servent *serv;
char *serv_proto = NULL; char *serv_proto = NULL;
bool has_proto = FALSE; bool has_proto = FALSE;
size_t written, total_written = 0; size_t written = 0;
#define fprintf_sum(...) { written = fprintf(__VA_ARGS__); if (written < 0) return written; total_written += written; }
if (this == NULL) if (this == NULL)
{ {
@@ -183,7 +182,7 @@ static int print(FILE *stream, const struct printf_info *info,
} }
mask = calc_netbits(this); mask = calc_netbits(this);
fprintf_sum(stream, "%s/%d", addr_str, mask); written += fprintf(stream, "%s/%d", addr_str, mask);
/* build protocol string */ /* build protocol string */
if (this->protocol) if (this->protocol)
@@ -191,12 +190,12 @@ static int print(FILE *stream, const struct printf_info *info,
proto = getprotobynumber(this->protocol); proto = getprotobynumber(this->protocol);
if (proto) if (proto)
{ {
fprintf_sum(stream, "[%s", proto->p_name); written += fprintf(stream, "[%s", proto->p_name);
serv_proto = proto->p_name; serv_proto = proto->p_name;
} }
else else
{ {
fprintf_sum(stream, "[%d", this->protocol); written += fprintf(stream, "[%d", this->protocol);
} }
has_proto = TRUE; has_proto = TRUE;
} }
@@ -206,36 +205,36 @@ static int print(FILE *stream, const struct printf_info *info,
{ {
if (has_proto) if (has_proto)
{ {
fprintf_sum(stream, "/"); written += fprintf(stream, "/");
} }
else else
{ {
fprintf_sum(stream, "["); written += fprintf(stream, "[");
} }
serv = getservbyport(htons(this->from_port), serv_proto); serv = getservbyport(htons(this->from_port), serv_proto);
if (serv) if (serv)
{ {
fprintf_sum(stream, "%s]", serv->s_name); written += fprintf(stream, "%s]", serv->s_name);
} }
else else
{ {
fprintf_sum(stream, "%d]", this->from_port); written += fprintf(stream, "%d]", this->from_port);
} }
} }
else if (!(this->from_port == 0 && this->to_port == 0xFFFF)) else if (!(this->from_port == 0 && this->to_port == 0xFFFF))
{ {
if (has_proto) if (has_proto)
{ {
fprintf_sum(stream, "/"); written += fprintf(stream, "/");
} }
else else
{ {
fprintf_sum(stream, "["); written += fprintf(stream, "[");
} }
fprintf_sum(stream, "%d-%d]", this->from_port, this->to_port); written += fprintf(stream, "%d-%d]", this->from_port, this->to_port);
} }
return total_written; return written;
} }
/** /**
+27 -30
View File
@@ -767,8 +767,7 @@ static int print(FILE *stream, const struct printf_info *info,
sa_policy_t *policy; sa_policy_t *policy;
u_int32_t now, rekeying, use; u_int32_t now, rekeying, use;
status_t status; status_t status;
size_t written, total_written = 0; size_t written = 0;
#define fprintf_sum(...) { written = fprintf(__VA_ARGS__); if (written < 0) return written; total_written += written; }
if (this == NULL) if (this == NULL)
{ {
@@ -777,93 +776,91 @@ static int print(FILE *stream, const struct printf_info *info,
now = (u_int32_t)time(NULL); now = (u_int32_t)time(NULL);
fprintf_sum(stream, "%10s: %N, reqid: %d", this->name, written += fprintf(stream, "%10s: %N, reqid: %d", this->name,
child_sa_state_names, this->state, this->reqid); child_sa_state_names, this->state, this->reqid);
if (this->state == CHILD_INSTALLED) if (this->state == CHILD_INSTALLED)
{ {
fprintf_sum(stream, ", %N, SPIs (in/out): 0x%x/0x%x", written += fprintf(stream, ", %N, SPIs (in/out): 0x%x/0x%x",
protocol_id_names, this->protocol, protocol_id_names, this->protocol,
htonl(this->me.spi), htonl(this->other.spi)); htonl(this->me.spi), htonl(this->other.spi));
if (info->alt) if (info->alt)
{ {
fprintf_sum(stream, "\n%10s: ", this->name); written += fprintf(stream, "\n%10s: ", this->name);
if (this->protocol == PROTO_ESP) if (this->protocol == PROTO_ESP)
{ {
fprintf_sum(stream, "%N", written += fprintf(stream, "%N", encryption_algorithm_names,
encryption_algorithm_names, this->encryption.algorithm); this->encryption.algorithm);
if (this->encryption.key_size) if (this->encryption.key_size)
{ {
fprintf_sum(stream, "-%d", this->encryption.key_size); written += fprintf(stream, "-%d", this->encryption.key_size);
} }
fprintf_sum(stream, "/"); written += fprintf(stream, "/");
} }
fprintf_sum(stream, "%N", written += fprintf(stream, "%N", integrity_algorithm_names,
integrity_algorithm_names, this->integrity.algorithm); this->integrity.algorithm);
if (this->integrity.key_size) if (this->integrity.key_size)
{ {
fprintf_sum(stream, "-%d", this->integrity.key_size); written += fprintf(stream, "-%d", this->integrity.key_size);
} }
fprintf_sum(stream, ", rekeying: "); written += fprintf(stream, ", rekeying: ");
/* calculate rekey times */ /* calculate rekey times */
if (this->soft_lifetime) if (this->soft_lifetime)
{ {
rekeying = this->soft_lifetime - (now - this->install_time); rekeying = this->soft_lifetime - (now - this->install_time);
fprintf_sum(stream, "%ds", rekeying); written += fprintf(stream, "%ds", rekeying);
} }
else else
{ {
fprintf_sum(stream, "disabled"); written += fprintf(stream, "disabled");
} }
} }
} }
#undef fprintf_sum
#define fprintf_sum(...) { written = fprintf(__VA_ARGS__); if (written < 0) { iterator->destroy(iterator); return written; } total_written += written; }
iterator = this->policies->create_iterator(this->policies, TRUE); iterator = this->policies->create_iterator(this->policies, TRUE);
while (iterator->iterate(iterator, (void**)&policy)) while (iterator->iterate(iterator, (void**)&policy))
{ {
fprintf_sum(stream, "\n%10s: %R===%R, last use (in/out/fwd): ", written += fprintf(stream, "\n%10s: %R===%R, last use (in/out/fwd): ",
this->name, policy->my_ts, policy->other_ts); this->name, policy->my_ts, policy->other_ts);
/* query policy times */ /* query policy times */
status = charon->kernel_interface->query_policy(charon->kernel_interface, status = charon->kernel_interface->query_policy(charon->kernel_interface,
policy->other_ts, policy->my_ts, POLICY_IN, &use); policy->other_ts, policy->my_ts, POLICY_IN, &use);
if (status == SUCCESS && use) if (status == SUCCESS && use)
{ {
fprintf_sum(stream, "%ds/", now - use); written += fprintf(stream, "%ds/", now - use);
} }
else else
{ {
fprintf_sum(stream, "unused/"); written += fprintf(stream, "unused/");
} }
status = charon->kernel_interface->query_policy(charon->kernel_interface, status = charon->kernel_interface->query_policy(charon->kernel_interface,
policy->my_ts, policy->other_ts, POLICY_OUT, &use); policy->my_ts, policy->other_ts, POLICY_OUT, &use);
if (status == SUCCESS && use) if (status == SUCCESS && use)
{ {
fprintf_sum(stream, "%ds/", now - use); written += fprintf(stream, "%ds/", now - use);
} }
else else
{ {
fprintf_sum(stream, "unused/"); written += fprintf(stream, "unused/");
} }
status = charon->kernel_interface->query_policy(charon->kernel_interface, status = charon->kernel_interface->query_policy(charon->kernel_interface,
policy->other_ts, policy->my_ts, POLICY_FWD, &use); policy->other_ts, policy->my_ts, POLICY_FWD, &use);
if (status == SUCCESS && use) if (status == SUCCESS && use)
{ {
fprintf_sum(stream, "%ds", now - use); written += fprintf(stream, "%ds", now - use);
} }
else else
{ {
fprintf_sum(stream, "unused"); written += fprintf(stream, "unused");
} }
} }
iterator->destroy(iterator); iterator->destroy(iterator);
return total_written; return written;
} }
/** /**