]> git.proxmox.com Git - mirror_frr.git/blobdiff - bgpd/bgp_community.c
Key changes:
[mirror_frr.git] / bgpd / bgp_community.c
index f1997bd9ce3e5b988d4cfd56540d8b249ad8a06c..19e4954e163c828db591044bf5bbf7f631dba9dc 100644 (file)
@@ -44,6 +44,13 @@ community_free (struct community *com)
     XFREE (MTYPE_COMMUNITY_VAL, com->val);
   if (com->str)
     XFREE (MTYPE_COMMUNITY_STR, com->str);
+
+  if (com->json)
+    {
+      json_object_free(com->json);
+      com->json = NULL;
+    }
+
   XFREE (MTYPE_COMMUNITY, com);
 }
 
@@ -170,6 +177,7 @@ community_uniq_sort (struct community *com)
     return NULL;
   
   new = community_new ();;
+  new->json = NULL;
   
   for (i = 0; i < com->size; i++)
     {
@@ -194,8 +202,8 @@ community_uniq_sort (struct community *com)
    0xFFFFFF03      "local-AS"
 
    For other values, "AS:VAL" format is used.  */
-static char *
-community_com2str  (struct community *com)
+static void
+set_community_string  (struct community *com)
 {
   int i;
   char *str;
@@ -205,16 +213,25 @@ community_com2str  (struct community *com)
   u_int32_t comval;
   u_int16_t as;
   u_int16_t val;
+  json_object *json_community_list = NULL;
+  json_object *json_string = NULL;
 
   if (!com)
     return NULL;
   
+  com->json = json_object_new_object();
+  json_community_list = json_object_new_array();
+
   /* When communities attribute is empty.  */
   if (com->size == 0)
     {
       str = XMALLOC (MTYPE_COMMUNITY_STR, 1);
       str[0] = '\0';
-      return str;
+
+      json_object_string_add(com->json, "string", "");
+      json_object_object_add(com->json, "list", json_community_list);
+      com->str = str;
+      return;
     }
 
   /* Memory allocation is time consuming work.  So we calculate
@@ -266,30 +283,42 @@ community_com2str  (struct community *com)
        case COMMUNITY_INTERNET:
          strcpy (pnt, "internet");
          pnt += strlen ("internet");
+          json_string = json_object_new_string("internet");
+          json_object_array_add(json_community_list, json_string);
          break;
        case COMMUNITY_NO_EXPORT:
          strcpy (pnt, "no-export");
          pnt += strlen ("no-export");
+          json_string = json_object_new_string("no-export");
+          json_object_array_add(json_community_list, json_string);
          break;
        case COMMUNITY_NO_ADVERTISE:
          strcpy (pnt, "no-advertise");
          pnt += strlen ("no-advertise");
+          json_string = json_object_new_string("no-advertise");
+          json_object_array_add(json_community_list, json_string);
          break;
        case COMMUNITY_LOCAL_AS:
          strcpy (pnt, "local-AS");
          pnt += strlen ("local-AS");
+          json_string = json_object_new_string("local-AS");
+          json_object_array_add(json_community_list, json_string);
          break;
        default:
          as = (comval >> 16) & 0xFFFF;
          val = comval & 0xFFFF;
          sprintf (pnt, "%u:%d", as, val);
+          json_string = json_object_new_string(pnt);
+          json_object_array_add(json_community_list, json_string);
          pnt += strlen (pnt);
          break;
        }
     }
   *pnt = '\0';
 
-  return str;
+  json_object_string_add(com->json, "string", str);
+  json_object_object_add(com->json, "list", json_community_list);
+  com->str = str;
 }
 
 /* Intern communities attribute.  */
@@ -314,7 +343,7 @@ community_intern (struct community *com)
 
   /* Make string.  */
   if (! find->str)
-    find->str = community_com2str (find);
+    set_community_string (find);
 
   return find;
 }
@@ -383,9 +412,9 @@ community_str (struct community *com)
 {
   if (!com)
     return NULL;
-  
+
   if (! com->str)
-    com->str = community_com2str (com);
+    set_community_string (com);
   return com->str;
 }
 
@@ -599,7 +628,10 @@ community_str2com (const char *str)
        case community_token_no_advertise:
        case community_token_local_as:
          if (com == NULL)
-           com = community_new();
+            {
+              com = community_new();
+              com->json = NULL;
+            }
          community_add_val (com, val);
          break;
        case community_token_unknown: