fixed output of proto/port selectors
authorAndreas Steffen <andreas.steffen@strongswan.org>
Thu, 2 Nov 2006 07:51:53 +0000 (07:51 -0000)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Thu, 2 Nov 2006 07:51:53 +0000 (07:51 -0000)
src/charon/config/traffic_selector.c

index 2361f67..2b99b91 100644 (file)
@@ -160,11 +160,10 @@ static int print(FILE *stream, const struct printf_info *info,
 {
        private_traffic_selector_t *this = *((private_traffic_selector_t**)(args[0]));
        char addr_str[INET6_ADDRSTRLEN] = "";
 {
        private_traffic_selector_t *this = *((private_traffic_selector_t**)(args[0]));
        char addr_str[INET6_ADDRSTRLEN] = "";
-       u_int8_t mask;
-       struct protoent *proto;
-       struct servent *serv;
        char *serv_proto = NULL;
        char *serv_proto = NULL;
-       bool has_proto = FALSE;
+       u_int8_t mask;
+       bool has_proto;
+       bool has_ports;
        size_t written = 0;
        
        if (this == NULL)
        size_t written = 0;
        
        if (this == NULL)
@@ -184,56 +183,62 @@ static int print(FILE *stream, const struct printf_info *info,
        
        written += fprintf(stream, "%s/%d", addr_str, mask);
        
        
        written += fprintf(stream, "%s/%d", addr_str, mask);
        
+       /* check if we have protocol and/or port selectors */
+       has_proto = this->protocol != 0;
+       has_ports = !(this->from_port == 0 && this->to_port == 0xFFFF);
+
+       if (!has_proto && !has_ports)
+       {
+               return written;
+       }
+
+       written += fprintf(stream, "[");
+
        /* build protocol string */
        /* build protocol string */
-       if (this->protocol)
+       if (has_proto)
        {
        {
-               proto = getprotobynumber(this->protocol);
+               struct protoent *proto = getprotobynumber(this->protocol);
+
                if (proto)
                {
                if (proto)
                {
-                       written += fprintf(stream, "[%s", proto->p_name);
+                       written += fprintf(stream, "%s", proto->p_name);
                        serv_proto = proto->p_name;
                }
                else
                {
                        serv_proto = proto->p_name;
                }
                else
                {
-                       written += fprintf(stream, "[%d", this->protocol);
+                       written += fprintf(stream, "%d", this->protocol);
                }
                }
-               has_proto = TRUE;
        }
        
        }
        
-       /* build port string */
-       if (this->from_port == this->to_port)
+       if (has_proto && has_ports)
        {
        {
-               if (has_proto)
-               {
-                       written += fprintf(stream, "/");
-               }
-               else
-               {
-                       written += fprintf(stream, "[");
-               }
-               serv = getservbyport(htons(this->from_port), serv_proto);
-               if (serv)
-               {
-                       written += fprintf(stream, "%s]", serv->s_name);
-               }
-               else
-               {
-                       written += fprintf(stream, "%d]", this->from_port);
-               }
+               written += fprintf(stream, "/");
        }
        }
-       else if (!(this->from_port == 0 && this->to_port == 0xFFFF))
+
+       /* build port string */
+       if (has_ports)
        {
        {
-               if (has_proto)
+               if (this->from_port == this->to_port)
                {
                {
-                       written += fprintf(stream, "/");
+                       struct servent *serv = getservbyport(htons(this->from_port), serv_proto);
+
+                       if (serv)
+                       {
+                               written += fprintf(stream, "%s", serv->s_name);
+                       }
+                       else
+                       {
+                               written += fprintf(stream, "%d", this->from_port);
+                       }
                }
                else
                {
                }
                else
                {
-                       written += fprintf(stream, "[");
+                       written += fprintf(stream, "%d-%d", this->from_port, this->to_port);
                }
                }
-               written += fprintf(stream, "%d-%d]", this->from_port, this->to_port);
        }
        
        }
        
+       written += fprintf(stream, "]");
+
        return written;
 }
 
        return written;
 }