Improve logging of reserved labels.
authorrenato <renato@openbsd.org>
Fri, 15 Jul 2016 17:09:25 +0000 (17:09 +0000)
committerrenato <renato@openbsd.org>
Fri, 15 Jul 2016 17:09:25 +0000 (17:09 +0000)
Print "exp-null" and "imp-null" instead of "0" and "3", for example. Also,
remove print_label() and print_pw_type() from ldpctl.c and use the
equivalent functions from ldpd's log.c.

While here, be more paranoid and use UINT32_MAX instead of UINT_MAX
for NO_LABEL.

usr.sbin/ldpctl/ldpctl.c
usr.sbin/ldpd/labelmapping.c
usr.sbin/ldpd/ldp.h
usr.sbin/ldpd/log.c
usr.sbin/ldpd/log.h

index 50568c2..9967b35 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: ldpctl.c,v 1.31 2016/05/23 19:06:03 renato Exp $
+/*     $OpenBSD: ldpctl.c,v 1.32 2016/07/15 17:09:25 renato Exp $
  *
  * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
  * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
@@ -57,8 +57,6 @@ int            show_l2vpn_pw_msg(struct imsg *);
 int             show_l2vpn_binding_msg(struct imsg *);
 const char     *get_media_descr(uint64_t);
 void            print_baudrate(uint64_t);
-char           *print_label(char **, uint32_t);
-const char     *print_pw_type(uint16_t);
 
 struct imsgbuf *ibuf;
 
@@ -402,7 +400,7 @@ int
 show_lib_msg(struct imsg *imsg, struct parse_result *res)
 {
        struct ctl_rt   *rt;
-       char            *dstnet, *local = NULL, *remote = NULL;
+       char            *dstnet;
 
        switch (imsg->hdr.type) {
        case IMSG_CTL_SHOW_LIB:
@@ -419,12 +417,9 @@ show_lib_msg(struct imsg *imsg, struct parse_result *res)
                if (strlen(dstnet) > 20)
                        printf("\n%25s", " ");
                printf(" %-15s %-11s %-13s %6s\n", inet_ntoa(rt->nexthop),
-                   print_label(&local, rt->local_label),
-                   print_label(&remote, rt->remote_label),
+                   log_label(rt->local_label), log_label(rt->remote_label),
                    rt->in_use ? "yes" : "no");
 
-               free(remote);
-               free(local);
                free(dstnet);
                break;
        case IMSG_CTL_END:
@@ -483,7 +478,6 @@ show_fib_msg(struct imsg *imsg, struct parse_result *res)
 {
        struct kroute   *k;
        char            *p;
-       char            *local = NULL, *remote = NULL;
        const char      *nexthop;
 
        switch (imsg->hdr.type) {
@@ -530,12 +524,9 @@ show_fib_msg(struct imsg *imsg, struct parse_result *res)
                } else if (k->flags & F_CONNECTED)
                        printf("link#%-13u", k->ifindex);
 
-               printf("%-18s", print_label(&local, k->local_label));
-               printf("%s", print_label(&local, k->remote_label));
+               printf("%-18s", log_label(k->local_label));
+               printf("%s", log_label(k->remote_label));
                printf("\n");
-
-               free(remote);
-               free(local);
                break;
        case IMSG_CTL_END:
                printf("\n");
@@ -621,7 +612,7 @@ show_l2vpn_binding_msg(struct imsg *imsg)
 
                printf("Neighbor: %s - PWID: %u (%s)\n",
                    inet_ntoa(pw->lsr_id), pw->pwid,
-                   print_pw_type(pw->type));
+                   pw_type_name(pw->type));
                printf("%-12s%-15s%-15s%-10s\n", "", "Label", "Group-ID",
                    "MTU");
                if (pw->local_label != NO_LABEL)
@@ -691,40 +682,3 @@ print_baudrate(uint64_t baudrate)
        else
                printf("%llu Bit/s", baudrate);
 }
-
-char *
-print_label(char **string, uint32_t label)
-{
-       if (label == NO_LABEL) {
-               if (asprintf(string, "-") == -1)
-                       err(1, NULL);
-       } else if (label == MPLS_LABEL_IMPLNULL) {
-               if (asprintf(string, "imp-null") == -1)
-                       err(1, NULL);
-       } else if (label == MPLS_LABEL_IPV4NULL ||
-           label == MPLS_LABEL_IPV6NULL) {
-               if (asprintf(string, "exp-null") == -1)
-                       err(1, NULL);
-       } else {
-               if (asprintf(string, "%u", label) == -1)
-                       err(1, NULL);
-       }
-
-       return (*string);
-}
-
-const char *
-print_pw_type(uint16_t pw_type)
-{
-       static char buf[64];
-
-       switch (pw_type) {
-       case PW_TYPE_ETHERNET_TAGGED:
-               return ("Eth Tagged");
-       case PW_TYPE_ETHERNET:
-               return ("Ethernet");
-       default:
-               snprintf(buf, sizeof(buf), "[%0x]", pw_type);
-               return (buf);
-       }
-}
index b6a736a..a1ec305 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: labelmapping.c,v 1.56 2016/07/15 17:05:50 renato Exp $ */
+/*     $OpenBSD: labelmapping.c,v 1.57 2016/07/15 17:09:25 renato Exp $ */
 
 /*
  * Copyright (c) 2014, 2015 Renato Westphal <renato@openbsd.org>
@@ -394,8 +394,8 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
                switch (type) {
                case MSG_TYPE_LABELMAPPING:
                        log_debug("label mapping from lsr-id %s, FEC %s, "
-                           "label %u", inet_ntoa(nbr->id),
-                           log_map(&me->map), me->map.label);
+                           "label %s", inet_ntoa(nbr->id),
+                           log_map(&me->map), log_label(me->map.label));
                        imsg_type = IMSG_LABEL_MAPPING;
                        break;
                case MSG_TYPE_LABELREQUEST:
index 9049db5..1a51bc4 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: ldp.h,v 1.33 2016/07/01 23:36:38 renato Exp $ */
+/*     $OpenBSD: ldp.h,v 1.34 2016/07/15 17:09:25 renato Exp $ */
 
 /*
  * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
@@ -297,6 +297,6 @@ struct pw_status_tlv {
 #define PW_PSN_RX_FAULT                (1 << 3)
 #define PW_PSN_TX_FAULT                (1 << 4)
 
-#define        NO_LABEL                UINT_MAX
+#define        NO_LABEL                UINT32_MAX
 
 #endif /* !_LDP_H_ */
index 46224a5..c9312c0 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: log.c,v 1.28 2016/07/01 23:36:38 renato Exp $ */
+/*     $OpenBSD: log.c,v 1.29 2016/07/15 17:09:25 renato Exp $ */
 
 /*
  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <sys/types.h>
+#include <sys/socket.h>
 #include <arpa/inet.h>
+#include <netmpls/mpls.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -24,6 +27,7 @@
 #include <syslog.h>
 #include <unistd.h>
 #include <netdb.h>
+#include <limits.h>
 
 #include "ldpd.h"
 #include "ldpe.h"
@@ -241,6 +245,132 @@ log_addr(int af, const union ldpd_addr *addr)
        return ("???");
 }
 
+#define        TF_BUFS 4
+#define        TF_LEN  32
+
+char *
+log_label(uint32_t label)
+{
+       char            *buf;
+       static char      tfbuf[TF_BUFS][TF_LEN];        /* ring buffer */
+       static int       idx = 0;
+
+       buf = tfbuf[idx++];
+       if (idx == TF_BUFS)
+               idx = 0;
+
+       switch (label) {
+       case NO_LABEL:
+               snprintf(buf, TF_LEN, "-");
+               break;
+       case MPLS_LABEL_IMPLNULL:
+               snprintf(buf, TF_LEN, "imp-null");
+               break;
+       case MPLS_LABEL_IPV4NULL:
+       case MPLS_LABEL_IPV6NULL:
+               snprintf(buf, TF_LEN, "exp-null");
+               break;
+       default:
+               snprintf(buf, TF_LEN, "%u", label);
+               break;
+       }
+
+       return (buf);
+}
+
+char *
+log_hello_src(const struct hello_source *src)
+{
+       static char buf[64];
+
+       switch (src->type) {
+       case HELLO_LINK:
+               snprintf(buf, sizeof(buf), "iface %s",
+                   src->link.ia->iface->name);
+               break;
+       case HELLO_TARGETED:
+               snprintf(buf, sizeof(buf), "source %s",
+                   log_addr(src->target->af, &src->target->addr));
+               break;
+       }
+
+       return (buf);
+}
+
+const char *
+log_map(const struct map *map)
+{
+       static char     buf[64];
+       int             af;
+
+       switch (map->type) {
+       case MAP_TYPE_WILDCARD:
+               if (snprintf(buf, sizeof(buf), "wildcard") < 0)
+                       return ("???");
+               break;
+       case MAP_TYPE_PREFIX:
+               switch (map->fec.prefix.af) {
+               case AF_IPV4:
+                       af = AF_INET;
+                       break;
+               case AF_IPV6:
+                       af = AF_INET6;
+                       break;
+               default:
+                       return ("???");
+               }
+
+               if (snprintf(buf, sizeof(buf), "%s/%u",
+                   log_addr(af, &map->fec.prefix.prefix),
+                   map->fec.prefix.prefixlen) == -1)
+                       return ("???");
+               break;
+       case MAP_TYPE_PWID:
+               if (snprintf(buf, sizeof(buf), "pwid %u (%s)",
+                   map->fec.pwid.pwid,
+                   pw_type_name(map->fec.pwid.type)) == -1)
+                       return ("???");
+               break;
+       default:
+               return ("???");
+       }
+
+       return (buf);
+}
+
+const char *
+log_fec(const struct fec *fec)
+{
+       static char     buf[64];
+       union ldpd_addr addr;
+
+       switch (fec->type) {
+       case FEC_TYPE_IPV4:
+               addr.v4 = fec->u.ipv4.prefix;
+               if (snprintf(buf, sizeof(buf), "ipv4 %s/%u",
+                   log_addr(AF_INET, &addr), fec->u.ipv4.prefixlen) == -1)
+                       return ("???");
+               break;
+       case FEC_TYPE_IPV6:
+               addr.v6 = fec->u.ipv6.prefix;
+               if (snprintf(buf, sizeof(buf), "ipv6 %s/%u",
+                   log_addr(AF_INET6, &addr), fec->u.ipv6.prefixlen) == -1)
+                       return ("???");
+               break;
+       case FEC_TYPE_PWID:
+               if (snprintf(buf, sizeof(buf),
+                   "pwid %u (%s) - %s",
+                   fec->u.pwid.pwid, pw_type_name(fec->u.pwid.type),
+                   inet_ntoa(fec->u.pwid.lsr_id)) == -1)
+                       return ("???");
+               break;
+       default:
+               return ("???");
+       }
+
+       return (buf);
+}
+
 /* names */
 const char *
 af_name(int af)
@@ -417,99 +547,6 @@ pw_type_name(uint16_t pw_type)
        }
 }
 
-char *
-log_hello_src(const struct hello_source *src)
-{
-       static char buffer[64];
-
-       switch (src->type) {
-       case HELLO_LINK:
-               snprintf(buffer, sizeof(buffer), "iface %s",
-                   src->link.ia->iface->name);
-               break;
-       case HELLO_TARGETED:
-               snprintf(buffer, sizeof(buffer), "source %s",
-                   log_addr(src->target->af, &src->target->addr));
-               break;
-       }
-
-       return (buffer);
-}
-
-const char *
-log_map(const struct map *map)
-{
-       static char     buf[64];
-       int             af;
-
-       switch (map->type) {
-       case MAP_TYPE_WILDCARD:
-               if (snprintf(buf, sizeof(buf), "wildcard") < 0)
-                       return ("???");
-               break;
-       case MAP_TYPE_PREFIX:
-               switch (map->fec.prefix.af) {
-               case AF_IPV4:
-                       af = AF_INET;
-                       break;
-               case AF_IPV6:
-                       af = AF_INET6;
-                       break;
-               default:
-                       return ("???");
-               }
-
-               if (snprintf(buf, sizeof(buf), "%s/%u",
-                   log_addr(af, &map->fec.prefix.prefix),
-                   map->fec.prefix.prefixlen) == -1)
-                       return ("???");
-               break;
-       case MAP_TYPE_PWID:
-               if (snprintf(buf, sizeof(buf), "pwid %u (%s)",
-                   map->fec.pwid.pwid,
-                   pw_type_name(map->fec.pwid.type)) == -1)
-                       return ("???");
-               break;
-       default:
-               return ("???");
-       }
-
-       return (buf);
-}
-
-const char *
-log_fec(const struct fec *fec)
-{
-       static char     buf[64];
-       union ldpd_addr addr;
-
-       switch (fec->type) {
-       case FEC_TYPE_IPV4:
-               addr.v4 = fec->u.ipv4.prefix;
-               if (snprintf(buf, sizeof(buf), "ipv4 %s/%u",
-                   log_addr(AF_INET, &addr), fec->u.ipv4.prefixlen) == -1)
-                       return ("???");
-               break;
-       case FEC_TYPE_IPV6:
-               addr.v6 = fec->u.ipv6.prefix;
-               if (snprintf(buf, sizeof(buf), "ipv6 %s/%u",
-                   log_addr(AF_INET6, &addr), fec->u.ipv6.prefixlen) == -1)
-                       return ("???");
-               break;
-       case FEC_TYPE_PWID:
-               if (snprintf(buf, sizeof(buf),
-                   "pwid %u (%s) - %s",
-                   fec->u.pwid.pwid, pw_type_name(fec->u.pwid.type),
-                   inet_ntoa(fec->u.pwid.lsr_id)) == -1)
-                       return ("???");
-               break;
-       default:
-               return ("???");
-       }
-
-       return (buf);
-}
-
 static char *msgtypes[] = {
        "",
        "RTM_ADD: Add Route",
index 0118902..9d42fa6 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: log.h,v 1.12 2016/07/01 23:36:38 renato Exp $ */
+/*     $OpenBSD: log.h,v 1.13 2016/07/15 17:09:25 renato Exp $ */
 
 /*
  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -39,6 +39,10 @@ const char   *log_sockaddr(void *);
 const char     *log_in6addr(const struct in6_addr *);
 const char     *log_in6addr_scope(const struct in6_addr *, unsigned int);
 const char     *log_addr(int, const union ldpd_addr *);
+char           *log_label(uint32_t);
+char           *log_hello_src(const struct hello_source *);
+const char     *log_map(const struct map *);
+const char     *log_fec(const struct fec *);
 const char     *af_name(int);
 const char     *socket_name(int);
 const char     *nbr_state_name(int);
@@ -46,9 +50,6 @@ const char    *if_state_name(int);
 const char     *if_type_name(enum iface_type);
 const char     *status_code_name(uint32_t);
 const char     *pw_type_name(uint16_t);
-char           *log_hello_src(const struct hello_source *);
-const char     *log_map(const struct map *);
-const char     *log_fec(const struct fec *);
 void            log_rtmsg(unsigned char);
 
 #endif /* _LOG_H_ */