]> git.proxmox.com Git - mirror_frr.git/commitdiff
lib: nuke the if_*_by_name_len() functions
authorRenato Westphal <renato@opensourcerouting.org>
Tue, 3 Oct 2017 01:05:57 +0000 (22:05 -0300)
committerRenato Westphal <renato@opensourcerouting.org>
Tue, 10 Oct 2017 12:01:24 +0000 (09:01 -0300)
Make use of strnlen() and strlcpy() so we can get rid of these
convoluted if_*_by_name_len() functions.

Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
13 files changed:
eigrpd/eigrp_zebra.c
lib/if.c
lib/if.h
lib/zclient.c
ospf6d/ospf6_top.c
ospfd/ospf_interface.c
ospfd/ospf_vty.c
ospfd/ospf_zebra.c
pimd/pim_iface.c
zebra/if_ioctl.c
zebra/if_ioctl_solaris.c
zebra/if_netlink.c
zebra/kernel_socket.c

index 66f03b776e16e50d9ec5d833df0b36e1c7f21469..b70c55104c0ea08111fd208f763f3f9930407b96 100644 (file)
@@ -334,8 +334,7 @@ static struct interface *zebra_interface_if_lookup(struct stream *s)
        stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
 
        /* And look it up. */
-       return if_lookup_by_name_len(
-               ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ), VRF_DEFAULT);
+       return if_lookup_by_name(ifname_tmp, VRF_DEFAULT);
 }
 
 void eigrp_zebra_route_add(struct prefix *p, struct list *successors)
index 34de2c8d8b0fea29dab25903d2b7887f9399a88b..1a44931fec0f5b5e4b7209f20b71b033ea80a00d 100644 (file)
--- a/lib/if.c
+++ b/lib/if.c
@@ -115,7 +115,7 @@ static int if_cmp_func(struct interface *ifp1, struct interface *ifp2)
 }
 
 /* Create new interface structure. */
-struct interface *if_create(const char *name, int namelen, vrf_id_t vrf_id)
+struct interface *if_create(const char *name, vrf_id_t vrf_id)
 {
        struct interface *ifp;
        struct list *intf_list = vrf_iflist_get(vrf_id);
@@ -124,9 +124,7 @@ struct interface *if_create(const char *name, int namelen, vrf_id_t vrf_id)
        ifp->ifindex = IFINDEX_INTERNAL;
 
        assert(name);
-       assert(namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */
-       strncpy(ifp->name, name, namelen);
-       ifp->name[namelen] = '\0';
+       strlcpy(ifp->name, name, sizeof(ifp->name));
        ifp->vrf_id = vrf_id;
        if (if_lookup_by_name(ifp->name, vrf_id) == NULL)
                listnode_add_sort(intf_list, ifp);
@@ -163,8 +161,8 @@ void if_update_to_new_vrf(struct interface *ifp, vrf_id_t vrf_id)
                listnode_add_sort(intf_list, ifp);
        else
                zlog_err(
-                       "if_create(%s): corruption detected -- interface with this "
-                       "name exists already in VRF %u!",
+                       "%s(%s): corruption detected -- interface with this "
+                       "name exists already in VRF %u!", __func__,
                        ifp->name, vrf_id);
 
        return;
@@ -236,42 +234,31 @@ struct interface *if_lookup_by_name(const char *name, vrf_id_t vrf_id)
        struct listnode *node;
        struct interface *ifp;
 
-       if (name)
-               for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id), node, ifp)) {
-                       if (strcmp(name, ifp->name) == 0)
-                               return ifp;
-               }
-       return NULL;
-}
-
-struct interface *if_lookup_by_name_all_vrf(const char *name)
-{
-       struct vrf *vrf;
-       struct interface *ifp;
+       if (!name || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ)
+               return NULL;
 
-       RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
-               ifp = if_lookup_by_name(name, vrf->vrf_id);
-               if (ifp)
+       for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id), node, ifp)) {
+               if (strcmp(name, ifp->name) == 0)
                        return ifp;
        }
 
        return NULL;
 }
 
-struct interface *if_lookup_by_name_len(const char *name, size_t namelen,
-                                       vrf_id_t vrf_id)
+struct interface *if_lookup_by_name_all_vrf(const char *name)
 {
-       struct listnode *node;
+       struct vrf *vrf;
        struct interface *ifp;
 
-       if (namelen > INTERFACE_NAMSIZ)
+       if (!name || strnlen(name, INTERFACE_NAMSIZ) == INTERFACE_NAMSIZ)
                return NULL;
 
-       for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf_id), node, ifp)) {
-               if (!memcmp(name, ifp->name, namelen)
-                   && (ifp->name[namelen] == '\0'))
+       RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
+               ifp = if_lookup_by_name(name, vrf->vrf_id);
+               if (ifp)
                        return ifp;
        }
+
        return NULL;
 }
 
@@ -364,53 +351,33 @@ struct interface *if_lookup_prefix(struct prefix *prefix, vrf_id_t vrf_id)
 
 /* Get interface by name if given name interface doesn't exist create
    one. */
-struct interface *if_get_by_name(const char *name, vrf_id_t vrf_id)
+struct interface *if_get_by_name(const char *name, vrf_id_t vrf_id, int vty)
 {
        struct interface *ifp;
 
-       return ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
-                      ? ifp
-                      : if_create(name, strlen(name), vrf_id);
-}
-
-struct interface *if_get_by_name_len(const char *name, size_t namelen,
-                                    vrf_id_t vrf_id, int vty)
-{
-       struct interface *ifp;
-       struct vrf *vrf;
-       struct listnode *node;
-
-       ifp = if_lookup_by_name_len(name, namelen, vrf_id);
-       if (ifp)
-               return ifp;
+       ifp = if_lookup_by_name_all_vrf(name);
+       if (ifp) {
+               if (ifp->vrf_id == vrf_id)
+                       return ifp;
 
-       /* Didn't find the interface on that vrf. Defined on a different one? */
-       RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
-               for (ALL_LIST_ELEMENTS_RO(vrf_iflist(vrf->vrf_id), node, ifp)) {
-                       if (!memcmp(name, ifp->name, namelen)
-                           && (ifp->name[namelen] == '\0')) {
-                               /* Found a match.  If the interface command was
-                                * entered in vty without a
-                                * VRF (passed as VRF_DEFAULT), accept the ifp
-                                * we found.   If a vrf was
-                                * entered and there is a mismatch, reject it if
-                                * from vty. If it came
-                                * from the kernel by way of zclient,  believe
-                                * it and update
-                                * the ifp accordingly.
-                                */
-                               if (vty) {
-                                       if (vrf_id == VRF_DEFAULT)
-                                               return ifp;
-                                       return NULL;
-                               } else {
-                                       if_update_to_new_vrf(ifp, vrf_id);
-                                       return ifp;
-                               }
-                       }
+               /* Found a match on a different VRF. If the interface command
+                * was entered in vty without a VRF (passed as VRF_DEFAULT),
+                * accept the ifp we found. If a vrf was entered and there is
+                * a mismatch, reject it if from vty. If it came from the kernel
+                * or by way of zclient, believe it and update the ifp
+                * accordingly.
+                */
+               if (vty) {
+                       if (vrf_id == VRF_DEFAULT)
+                               return ifp;
+                       return NULL;
+               } else {
+                       if_update_to_new_vrf(ifp, vrf_id);
+                       return ifp;
                }
        }
-       return (if_create(name, namelen, vrf_id));
+
+       return if_create(name, vrf_id);
 }
 
 /* Does interface up ? */
@@ -597,24 +564,20 @@ DEFUN (no_interface_desc,
  *     if not:
  *     - no idea, just get the name in its entirety.
  */
-static struct interface *if_sunwzebra_get(const char *name, size_t nlen,
-                                         vrf_id_t vrf_id)
+static struct interface *if_sunwzebra_get(char *name, vrf_id_t vrf_id)
 {
        struct interface *ifp;
-       size_t seppos = 0;
+       char *cp;
 
-       if ((ifp = if_lookup_by_name_len(name, nlen, vrf_id)) != NULL)
+       if ((ifp = if_lookup_by_name(name, vrf_id)) != NULL)
                return ifp;
 
        /* hunt the primary interface name... */
-       while (seppos < nlen && name[seppos] != ':')
-               seppos++;
+       cp = strchr(name, ':');
+       if (cp)
+               *cp = '\0';
 
-       /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
-       if (seppos < nlen)
-               return if_get_by_name_len(name, seppos, vrf_id, 1);
-       else
-               return if_get_by_name_len(name, nlen, vrf_id, 1);
+       return if_get_by_name(name, vrf_id, 1);
 }
 #endif /* SUNOS_5 */
 
@@ -631,10 +594,9 @@ DEFUN (interface,
        const char *vrfname = (argc > 2) ? argv[idx_vrf]->arg : NULL;
 
        struct interface *ifp;
-       size_t sl;
        vrf_id_t vrf_id = VRF_DEFAULT;
 
-       if ((sl = strlen(ifname)) > INTERFACE_NAMSIZ) {
+       if (strlen(ifname) > INTERFACE_NAMSIZ) {
                vty_out(vty,
                        "%% Interface name %s is invalid: length exceeds "
                        "%d characters\n",
@@ -648,9 +610,9 @@ DEFUN (interface,
                VRF_GET_ID(vrf_id, vrfname);
 
 #ifdef SUNOS_5
-       ifp = if_sunwzebra_get(ifname, sl, vrf_id);
+       ifp = if_sunwzebra_get(ifname, vrf_id);
 #else
-       ifp = if_get_by_name_len(ifname, sl, vrf_id, 1);
+       ifp = if_get_by_name(ifname, vrf_id, 1);
 #endif /* SUNOS_5 */
 
        if (!ifp) {
index a592e0ff857d25ebfd34b6f8c655ecf4fe7919b0..caaf9aa1f0a09bf8d91c99ab117a713efd577ebd 100644 (file)
--- a/lib/if.h
+++ b/lib/if.h
@@ -209,7 +209,7 @@ struct interface {
           To delete, just set ifindex to IFINDEX_INTERNAL to indicate that the
           interface does not exist in the kernel.
         */
-       char name[INTERFACE_NAMSIZ + 1];
+       char name[INTERFACE_NAMSIZ];
 
        /* Interface index (should be IFINDEX_INTERNAL for non-kernel or
           deleted interfaces). */
@@ -408,8 +408,7 @@ struct nbr_connected {
 extern int if_cmp_name_func(char *, char *);
 
 extern void if_update_to_new_vrf(struct interface *, vrf_id_t vrf_id);
-extern struct interface *if_create(const char *name, int namelen,
-                                  vrf_id_t vrf_id);
+extern struct interface *if_create(const char *name,  vrf_id_t vrf_id);
 extern struct interface *if_lookup_by_index(ifindex_t, vrf_id_t vrf_id);
 extern struct interface *if_lookup_exact_address(void *matchaddr, int family,
                                                 vrf_id_t vrf_id);
@@ -422,16 +421,8 @@ extern struct interface *if_lookup_prefix(struct prefix *prefix,
    by a '\0' character: */
 extern struct interface *if_lookup_by_name_all_vrf(const char *ifname);
 extern struct interface *if_lookup_by_name(const char *ifname, vrf_id_t vrf_id);
-extern struct interface *if_get_by_name(const char *ifname, vrf_id_t vrf_id);
-
-/* For these 2 functions, the namelen argument should be the precise length
-   of the ifname string (not counting any optional trailing '\0' character).
-   In most cases, strnlen should be used to calculate the namelen value. */
-extern struct interface *if_lookup_by_name_len(const char *ifname,
-                                              size_t namelen, vrf_id_t vrf_id);
-extern struct interface *if_get_by_name_len(const char *ifname, size_t namelen,
-                                           vrf_id_t vrf_id, int vty);
-
+extern struct interface *if_get_by_name(const char *ifname, vrf_id_t vrf_id,
+                                       int vty);
 
 /* Delete the interface, but do not free the structure, and leave it in the
    interface list.  It is often advisable to leave the pseudo interface
index 43d46a180197db23bd7c240a858d559341b91637..1fc88ee6aad530a3532347470a646c1f365236ff 100644 (file)
@@ -1230,8 +1230,7 @@ struct interface *zebra_interface_add_read(struct stream *s, vrf_id_t vrf_id)
        stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
 
        /* Lookup/create interface by name. */
-       ifp = if_get_by_name_len(
-               ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ), vrf_id, 0);
+       ifp = if_get_by_name(ifname_tmp, vrf_id, 0);
 
        zebra_interface_if_set_value(s, ifp);
 
@@ -1254,8 +1253,7 @@ struct interface *zebra_interface_state_read(struct stream *s, vrf_id_t vrf_id)
        stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
 
        /* Lookup this by interface index. */
-       ifp = if_lookup_by_name_len(
-               ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ), vrf_id);
+       ifp = if_lookup_by_name(ifname_tmp, vrf_id);
        if (ifp == NULL) {
                zlog_warn("INTERFACE_STATE: Cannot find IF %s in VRF %d",
                          ifname_tmp, vrf_id);
index b0281b9e0a6e00ca2a2e2bc92b8197817ec0b662..e0844765d3e423d83cd1184b47bd38aa9c74a703 100644 (file)
@@ -595,7 +595,7 @@ DEFUN (ospf6_interface_area,
        u_int32_t area_id;
 
        /* find/create ospf6 interface */
-       ifp = if_get_by_name(argv[idx_ifname]->arg, VRF_DEFAULT);
+       ifp = if_get_by_name(argv[idx_ifname]->arg, VRF_DEFAULT, 0);
        oi = (struct ospf6_interface *)ifp->info;
        if (oi == NULL)
                oi = ospf6_interface_create(ifp);
index 67ce6f17135ec232ffbdf15c693ee354e79d4a18..34a1e6f6d6a984f7919b89552c5a062cd34300f2 100644 (file)
@@ -798,7 +798,7 @@ struct ospf_interface *ospf_vl_new(struct ospf *ospf,
 {
        struct ospf_interface *voi;
        struct interface *vi;
-       char ifname[INTERFACE_NAMSIZ + 1];
+       char ifname[INTERFACE_NAMSIZ];
        struct ospf_area *area;
        struct in_addr area_id;
        struct connected *co;
@@ -819,7 +819,7 @@ struct ospf_interface *ospf_vl_new(struct ospf *ospf,
                           ospf->vrf_id);
 
        snprintf(ifname, sizeof(ifname), "VLINK%d", vlink_count);
-       vi = if_create(ifname, strnlen(ifname, sizeof(ifname)), ospf->vrf_id);
+       vi = if_create(ifname, ospf->vrf_id);
        /*
         * if_create sets ZEBRA_INTERFACE_LINKDETECTION
         * virtual links don't need this.
index 3840bc41708ae3b2bbbe7b4d23152a71db8967a3..c12e7e035ade91bdcfbfdf225a904494d0f85e51 100644 (file)
@@ -413,7 +413,7 @@ DEFUN (ospf_passive_interface,
                return CMD_SUCCESS;
        }
 
-       ifp = if_get_by_name(argv[1]->arg, ospf->vrf_id);
+       ifp = if_get_by_name(argv[1]->arg, ospf->vrf_id, 0);
        if (ifp == NULL) {
                vty_out(vty, "interface %s not found.\n",
                        (char *)argv[1]->arg);
@@ -485,7 +485,7 @@ DEFUN (no_ospf_passive_interface,
                return CMD_SUCCESS;
        }
 
-       ifp = if_get_by_name(argv[2]->arg, ospf->vrf_id);
+       ifp = if_get_by_name(argv[2]->arg, ospf->vrf_id, 0);
        if (ifp == NULL) {
                vty_out(vty, "interface %s not found.\n",
                        (char *)argv[1]->arg);
index bd944ae748868a6778d157b414bbc2113d728332..0697c7801f51e8b12a105f88d087a8daa721cd4f 100644 (file)
@@ -179,8 +179,7 @@ static struct interface *zebra_interface_if_lookup(struct stream *s,
        stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
 
        /* And look it up. */
-       return if_lookup_by_name_len(
-               ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ), vrf_id);
+       return if_lookup_by_name(ifname_tmp, vrf_id);
 }
 
 static int ospf_interface_state_up(int command, struct zclient *zclient,
index 8787145027a3ab2e85974d3c4a9533efab7eac79..01e14bec094e6408f35db927d3ac1d0604ae59a1 100644 (file)
@@ -1480,17 +1480,16 @@ void pim_if_update_assert_tracking_desired(struct interface *ifp)
  */
 void pim_if_create_pimreg(struct pim_instance *pim)
 {
-       char pimreg_name[100];
+       char pimreg_name[INTERFACE_NAMSIZ];
 
        if (!pim->regiface) {
                if (pim->vrf_id == VRF_DEFAULT)
-                       strcpy(pimreg_name, "pimreg");
+                       strlcpy(pimreg_name, "pimreg", sizeof(pimreg_name));
                else
-                       sprintf(pimreg_name, "pimreg%d",
-                               pim->vrf->data.l.table_id);
+                       snprintf(pimreg_name, sizeof(pimreg_name), "pimreg%u",
+                                pim->vrf->data.l.table_id);
 
-               pim->regiface = if_create(pimreg_name, strlen(pimreg_name),
-                                         pim->vrf_id);
+               pim->regiface = if_create(pimreg_name, pim->vrf_id);
                pim->regiface->ifindex = PIM_OIF_PIM_REGISTER_VIF;
 
                pim_if_new(pim->regiface, 0, 0);
index 6396911e1b9c8d2db48ad27e2c36e35004c6fa7a..f31c24ada3468fb428bdb2b0f907a369a9b247a6 100644 (file)
@@ -105,10 +105,7 @@ static int interface_list_ioctl(void)
                unsigned int size;
 
                ifreq = (struct ifreq *)((caddr_t)ifconf.ifc_req + n);
-               ifp = if_get_by_name_len(
-                       ifreq->ifr_name,
-                       strnlen(ifreq->ifr_name, sizeof(ifreq->ifr_name)),
-                       VRF_DEFAULT, 0);
+               ifp = if_get_by_name(ifreq->ifr_name, VRF_DEFAULT, 0);
                if_add_update(ifp);
                size = ifreq->ifr_addr.sa_len;
                if (size < sizeof(ifreq->ifr_addr))
@@ -118,10 +115,7 @@ static int interface_list_ioctl(void)
        }
 #else
        for (n = 0; n < ifconf.ifc_len; n += sizeof(struct ifreq)) {
-               ifp = if_get_by_name_len(
-                       ifreq->ifr_name,
-                       strnlen(ifreq->ifr_name, sizeof(ifreq->ifr_name)),
-                       VRF_DEFAULT, 0);
+               ifp = if_get_by_name(ifreq->ifr_name, VRF_DEFAULT, 0);
                if_add_update(ifp);
                ifreq++;
        }
index 9ec575b5b01d7be6197250bb6c8a7c916f67a4e9..145dc0ac50da067cb241f6396bc2532dd251d7a0 100644 (file)
@@ -170,8 +170,7 @@ calculate_lifc_len: /* must hold privileges to enter here */
                       && (*(lifreq->lifr_name + normallen) != ':'))
                        normallen++;
 
-               ifp = if_get_by_name_len(lifreq->lifr_name, normallen,
-                                        VRF_DEFAULT, 0);
+               ifp = if_get_by_name(lifreq->lifr_name, VRF_DEFAULT, 0);
 
                if (lifreq->lifr_addr.ss_family == AF_INET)
                        ifp->flags |= IFF_IPV4;
index 5a42e0c8f1b0b45ed9d61d5bf2faabb02ca77b51..0b97903ce0e5f0e78a244e11e340e726068cd673 100644 (file)
@@ -666,7 +666,7 @@ static int netlink_interface(struct sockaddr_nl *snl, struct nlmsghdr *h,
                link_ifindex = *(ifindex_t *)RTA_DATA(tb[IFLA_LINK]);
 
        /* Add interface. */
-       ifp = if_get_by_name(name, vrf_id);
+       ifp = if_get_by_name(name, vrf_id, 0);
        set_ifindex(ifp, ifi->ifi_index, zns);
        ifp->flags = ifi->ifi_flags & 0x0000fffff;
        if (IS_ZEBRA_IF_VRF(ifp))
@@ -1121,7 +1121,7 @@ int netlink_link_change(struct sockaddr_nl *snl, struct nlmsghdr *h,
 
                        if (ifp == NULL) {
                                /* unknown interface */
-                               ifp = if_get_by_name(name, vrf_id);
+                               ifp = if_get_by_name(name, vrf_id, 0);
                        } else {
                                /* pre-configured interface, learnt now */
                                if (ifp->vrf_id != vrf_id)
index 9907ef5b7969a110598e15727594e3b7a35f0755..cbfc371199dc91b7c6c86bd58ddaecea44620589 100644 (file)
@@ -323,10 +323,7 @@ static int ifan_read(struct if_announcemsghdr *ifan)
                                __func__, ifan->ifan_index, ifan->ifan_name);
 
                /* Create Interface */
-               ifp = if_get_by_name_len(
-                       ifan->ifan_name,
-                       strnlen(ifan->ifan_name, sizeof(ifan->ifan_name)),
-                       VRF_DEFAULT, 0);
+               ifp = if_get_by_name(ifan->ifan_name, VRF_DEFAULT, 0);
                ifp->ifindex = ifan->ifan_index;
 
                if_get_metric(ifp);
@@ -517,7 +514,7 @@ int ifm_read(struct if_msghdr *ifm)
                if (ifp == NULL) {
                        /* Interface that zebra was not previously aware of, so
                         * create. */
-                       ifp = if_create(ifname, ifnlen, VRF_DEFAULT);
+                       ifp = if_create(ifname, VRF_DEFAULT);
                        if (IS_ZEBRA_DEBUG_KERNEL)
                                zlog_debug("%s: creating ifp for ifindex %d",
                                           __func__, ifm->ifm_index);