supporting width modifier in identification_t printf hook (e.g. %30D)

cleanups in host_t %H printf hook
This commit is contained in:
Martin Willi
2008-05-09 11:34:58 +00:00
parent ff2d02ed4c
commit 9f9903a3b3
2 changed files with 29 additions and 45 deletions
+7 -6
View File
@@ -107,15 +107,14 @@ static int print(FILE *stream, const struct printf_info *info,
{ {
private_host_t *this = *((private_host_t**)(args[0])); private_host_t *this = *((private_host_t**)(args[0]));
char buffer[INET6_ADDRSTRLEN + 16]; char buffer[INET6_ADDRSTRLEN + 16];
int len;
if (this == NULL) if (this == NULL)
{ {
len = sprintf(buffer, "(null)"); snprintf(buffer, sizeof(buffer), "(null)");
} }
else if (is_anyaddr(this)) else if (is_anyaddr(this))
{ {
len = sprintf(buffer, "%%any"); snprintf(buffer, sizeof(buffer), "%%any");
} }
else else
{ {
@@ -136,15 +135,17 @@ static int print(FILE *stream, const struct printf_info *info,
if (inet_ntop(this->address.sa_family, address, if (inet_ntop(this->address.sa_family, address,
buffer, sizeof(buffer)) == NULL) buffer, sizeof(buffer)) == NULL)
{ {
len = sprintf(buffer, "(address conversion failed)"); snprintf(buffer, sizeof(buffer),
"(address conversion failed)");
} }
else if (info->alt) else if (info->alt)
{ {
len = sprintf(buffer, "%s[%d]", buffer, ntohs(port)); snprintf(buffer, sizeof(buffer),
"%s[%d]", buffer, ntohs(port));
} }
break; break;
default: default:
len = sprintf(buffer, "(family not supported)"); snprintf(buffer, sizeof(buffer), "(family not supported)");
break; break;
} }
} }
+22 -39
View File
@@ -887,75 +887,58 @@ static int print(FILE *stream, const struct printf_info *info,
private_identification_t *this = *((private_identification_t**)(args[0])); private_identification_t *this = *((private_identification_t**)(args[0]));
char buf[BUF_LEN]; char buf[BUF_LEN];
chunk_t proper, buf_chunk = chunk_from_buf(buf); chunk_t proper, buf_chunk = chunk_from_buf(buf);
int written;
if (this == NULL) if (this == NULL)
{ {
return fprintf(stream, "(null)"); return fprintf(stream, "%*s", info->width, "(null)");
} }
switch (this->type) switch (this->type)
{ {
case ID_ANY: case ID_ANY:
return fprintf(stream, "%%any"); snprintf(buf, sizeof(buf), "%%any");
break;
case ID_IPV4_ADDR: case ID_IPV4_ADDR:
if (this->encoded.len < sizeof(struct in_addr) || if (this->encoded.len < sizeof(struct in_addr) ||
inet_ntop(AF_INET, this->encoded.ptr, buf, sizeof(buf)) == NULL) inet_ntop(AF_INET, this->encoded.ptr, buf, sizeof(buf)) == NULL)
{ {
return fprintf(stream, "(invalid ID_IPV4_ADDR)"); snprintf(buf, sizeof(buf), "(invalid ID_IPV4_ADDR)");
}
else
{
return fprintf(stream, "%s", buf);
} }
break;
case ID_IPV6_ADDR: case ID_IPV6_ADDR:
if (this->encoded.len < sizeof(struct in6_addr) || if (this->encoded.len < sizeof(struct in6_addr) ||
inet_ntop(AF_INET6, this->encoded.ptr, buf, INET6_ADDRSTRLEN) == NULL) inet_ntop(AF_INET6, this->encoded.ptr, buf, INET6_ADDRSTRLEN) == NULL)
{ {
return fprintf(stream, "(invalid ID_IPV6_ADDR)"); snprintf(buf, sizeof(buf), "(invalid ID_IPV6_ADDR)");
}
else
{
return fprintf(stream, "%s", buf);
} }
break;
case ID_FQDN: case ID_FQDN:
{
proper = sanitize_chunk(this->encoded);
written = fprintf(stream, "%.*s", proper.len, proper.ptr);
chunk_free(&proper);
return written;
}
case ID_RFC822_ADDR: case ID_RFC822_ADDR:
{ case ID_DER_ASN1_GN_URI:
proper = sanitize_chunk(this->encoded); proper = sanitize_chunk(this->encoded);
written = fprintf(stream, "%.*s", proper.len, proper.ptr); snprintf(buf, sizeof(buf), "%.*s", proper.len, proper.ptr);
chunk_free(&proper); chunk_free(&proper);
return written; break;
}
case ID_DER_ASN1_DN: case ID_DER_ASN1_DN:
{ if (!dntoa(this->encoded, &buf_chunk))
snprintf(buf, sizeof(buf), "%.*s", this->encoded.len, this->encoded.ptr); {
/* TODO: whats returned on failure?*/ snprintf(buf, sizeof(buf), "(invalid ID_DER_ASN1_DN)");
dntoa(this->encoded, &buf_chunk); }
return fprintf(stream, "%s", buf); break;
}
case ID_DER_ASN1_GN: case ID_DER_ASN1_GN:
return fprintf(stream, "(ASN.1 general Name"); snprintf(buf, sizeof(buf), "(ASN.1 general Name");
break;
case ID_KEY_ID: case ID_KEY_ID:
case ID_PUBKEY_INFO_SHA1: case ID_PUBKEY_INFO_SHA1:
case ID_PUBKEY_SHA1: case ID_PUBKEY_SHA1:
case ID_CERT_DER_SHA1: case ID_CERT_DER_SHA1:
return fprintf(stream, "%#B", &this->encoded); snprintf(buf, sizeof(buf), "%#B", &this->encoded);
case ID_DER_ASN1_GN_URI: break;
{
proper = sanitize_chunk(this->encoded);
written = fprintf(stream, "%.*s", proper.len, proper.ptr);
chunk_free(&proper);
return written;
}
default: default:
return fprintf(stream, "(unknown ID type: %d)", this->type); snprintf(buf, sizeof(buf), "(unknown ID type: %d)", this->type);
break;
} }
return fprintf(stream, "%*s", info->width, buf);
} }
/** /**