implemented wrap around of registered IKEv1 algorithm names
authorAndreas Steffen <andreas.steffen@strongswan.org>
Sun, 26 Dec 2010 16:11:02 +0000 (17:11 +0100)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Sun, 26 Dec 2010 16:11:02 +0000 (17:11 +0100)
src/libcharon/plugins/stroke/stroke_list.c
src/libstrongswan/crypto/crypto_factory.h
src/pluto/ike_alg.c
src/pluto/kernel_alg.c

index cdc2a7b..b4b5130 100644 (file)
@@ -36,8 +36,6 @@
 #define CRL_WARNING_INTERVAL   7       /* days */
 #define AC_WARNING_INTERVAL            1       /* day */
 
-#define MAX_ALG_LINE          120   /* characters */
-
 typedef struct private_stroke_list_t private_stroke_list_t;
 
 /**
@@ -1069,7 +1067,7 @@ static void print_alg(FILE *out, int *len, enum_name_t *alg_names, int alg_type,
        int alg_name_len;
        
        alg_name_len = sprintf(alg_name, " %N[%s]", alg_names, alg_type, plugin_name);
-       if (*len + alg_name_len > MAX_ALG_LINE)
+       if (*len + alg_name_len > CRYPTO_MAX_ALG_LINE)
        {
                fprintf(out, "\n             ");
                *len = 13;      
index b45ffee..8e5db63 100644 (file)
@@ -33,6 +33,8 @@ typedef struct crypto_factory_t crypto_factory_t;
 #include <crypto/diffie_hellman.h>
 #include <crypto/transform.h>
 
+#define CRYPTO_MAX_ALG_LINE          120   /* characters */
+
 /**
  * Constructor function for crypters
  */
index 5297640..a36b5ce 100644 (file)
@@ -305,6 +305,26 @@ fail:
 }
 
 /**
+ * Print the name of an algorithm plus the name of the plugin that registered it
+ */
+static void print_alg(char *buf, int *len, enum_names *alg_names, int alg_type,
+                                         const char *plugin_name)
+{
+       char alg_name[BUF_LEN];
+       int alg_name_len;
+       
+       alg_name_len = sprintf(alg_name, " %s[%s]", enum_name(alg_names, alg_type),
+                                                  plugin_name);
+       if (*len + alg_name_len > CRYPTO_MAX_ALG_LINE)
+       {
+               whack_log(RC_COMMENT, "%s", buf);
+               *len = sprintf(buf, "             ");
+       }
+       sprintf(buf + *len, "%s", alg_name);
+       *len += alg_name_len;
+}
+
+/**
  * Show registered IKE algorithms
  */
 void ike_alg_list(void)
@@ -312,80 +332,44 @@ void ike_alg_list(void)
        rng_quality_t quality;
        enumerator_t *enumerator;
        const char *plugin_name;
-       char buf[1024];
-       char *pos;
-       int n, len;
+       char buf[BUF_LEN];
+       int len;
        struct ike_alg *a;
 
        whack_log(RC_COMMENT, " ");
        whack_log(RC_COMMENT, "List of registered IKEv1 Algorithms:");
        whack_log(RC_COMMENT, " ");
 
-       pos = buf;
-       *pos = '\0';
-       len = BUF_LEN;
+       len = sprintf(buf, "  encryption:");
        for (a = ike_alg_base[IKE_ALG_ENCRYPT]; a != NULL; a = a->algo_next)
        {
-           n = snprintf(pos, len, " %s[%s]", enum_name(&oakley_enc_names,
-                                        a->algo_id), a->plugin_name);
-               pos += n;
-               len -= n;
-               if (len <= 0)
-               {
-                       break;
-               }
+           print_alg(buf, &len, &oakley_enc_names, a->algo_id, a->plugin_name);
        }
-       whack_log(RC_COMMENT, "  encryption:%s", buf);
+       whack_log(RC_COMMENT, "%s", buf);
 
-       pos = buf;
-       *pos = '\0';
-       len = BUF_LEN;
+       len = sprintf(buf, "  integrity: ");
        for (a = ike_alg_base[IKE_ALG_HASH]; a != NULL; a = a->algo_next)
        {
-           n = snprintf(pos, len, " %s[%s]", enum_name(&oakley_hash_names,
-                                        a->algo_id), a->plugin_name);
-               pos += n;
-               len -= n;
-               if (len <= 0)
-               {
-                       break;
-               }
+           print_alg(buf, &len, &oakley_hash_names, a->algo_id, a->plugin_name);
        }
-       whack_log(RC_COMMENT, "  integrity: %s", buf);
+       whack_log(RC_COMMENT, "%s", buf);
 
-       pos = buf;
-       *pos = '\0';
-       len = BUF_LEN;
+       len = sprintf(buf, "  dh-group:  ");
        for (a = ike_alg_base[IKE_ALG_DH_GROUP]; a != NULL; a = a->algo_next)
        {
-           n = snprintf(pos, len, " %s[%s]", enum_name(&oakley_group_names,
-                                        a->algo_id), a->plugin_name);
-               pos += n;
-               len -= n;
-               if (len <= 0)
-               {
-                       break;
-               }
+           print_alg(buf, &len, &oakley_group_names, a->algo_id, a->plugin_name);
        }
-       whack_log(RC_COMMENT, "  dh-group:  %s", buf);
+       whack_log(RC_COMMENT, "%s", buf);
 
-       pos = buf;
-       *pos = '\0';
-       len = BUF_LEN;
+       len = sprintf(buf, "  random-gen:");
        enumerator = lib->crypto->create_rng_enumerator(lib->crypto);
        while (enumerator->enumerate(enumerator, &quality, &plugin_name))
        {
-           n = snprintf(pos, len, " %N[%s]", rng_quality_names, quality, 
-                                        plugin_name);
-               pos += n;
-               len -= n;
-               if (len <= 0)
-               {
-                       break;
-               }
+           len += sprintf(buf + len, " %N[%s]", rng_quality_names, quality,
+                                                                                        plugin_name);
        }
        enumerator->destroy(enumerator);
-       whack_log(RC_COMMENT, "  random-gen:%s", buf);
+       whack_log(RC_COMMENT, "%s", buf);
 }
 
 /**
index 2a195cf..c82c376 100644 (file)
@@ -397,55 +397,55 @@ struct sadb_alg* kernel_alg_esp_sadb_alg(u_int alg_id)
        return sadb_alg;
 }
 
+/**
+ * Print the name of a kernel algorithm
+ */
+static void print_alg(char *buf, int *len, enum_names *alg_names, int alg_type)
+{
+       char alg_name[BUF_LEN];
+       int alg_name_len;
+       
+       alg_name_len = sprintf(alg_name, " %s", enum_name(alg_names, alg_type));
+       if (*len + alg_name_len > CRYPTO_MAX_ALG_LINE)
+       {
+               whack_log(RC_COMMENT, "%s", buf);
+               *len = sprintf(buf, "             ");
+       }
+       sprintf(buf + *len, "%s", alg_name);
+       *len += alg_name_len;
+}
+
 void kernel_alg_list(void)
 {
        char buf[BUF_LEN];
-       char *pos;
-       int n, len;
+       int len;
        u_int sadb_id;
 
        whack_log(RC_COMMENT, " ");
        whack_log(RC_COMMENT, "List of registered ESP Algorithms:");
        whack_log(RC_COMMENT, " ");
 
-       pos = buf;
-       *pos = '\0';
-       len = BUF_LEN;
+       len = sprintf(buf, "  encryption:");
        for (sadb_id = 1; sadb_id <= SADB_EALG_MAX; sadb_id++)
        {
                if (ESP_EALG_PRESENT(sadb_id))
                {
-                       n = snprintf(pos, len, " %s",
-                                                enum_name(&esp_transform_names, sadb_id));
-                       pos += n;
-                       len -= n;
-                       if (len <= 0)
-                       {
-                               break;
-                       }
+                       print_alg(buf, &len, &esp_transform_names, sadb_id);
                }
        }
-       whack_log(RC_COMMENT, "  encryption:%s", buf);
+       whack_log(RC_COMMENT, "%s", buf);
 
-       pos = buf;
-       *pos = '\0';
-       len = BUF_LEN;
+       len = sprintf(buf, "  integrity: ");
        for (sadb_id = 1; sadb_id <= SADB_AALG_MAX; sadb_id++)
        {
                if (ESP_AALG_PRESENT(sadb_id))
                {
                        u_int aaid = alg_info_esp_sadb2aa(sadb_id);
 
-                       n = snprintf(pos, len, " %s", enum_name(&auth_alg_names, aaid));
-                       pos += n;
-                       len -= n;
-                       if (len <= 0)
-                       {
-                               break;
-                       }
+                       print_alg(buf, &len, &auth_alg_names, aaid);
                }
        }
-       whack_log(RC_COMMENT, "  integrity: %s", buf);
+       whack_log(RC_COMMENT, "%s", buf);
 }
 
 void kernel_alg_show_connection(connection_t *c, const char *instance)