]> git.puffer.fish Git - matthieu/frr.git/commitdiff
2005-04-02 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
authorajs <ajs>
Sat, 2 Apr 2005 22:50:38 +0000 (22:50 +0000)
committerajs <ajs>
Sat, 2 Apr 2005 22:50:38 +0000 (22:50 +0000)
* if.h: (if_lookup_by_name_len, if_get_by_name_len) New functions.
* if.c: (if_lookup_by_name_len, if_get_by_name_len) New functions.
  (if_get_by_name) Tighten up code.
  (interface) Use new function if_get_by_name_len.
* zclient.c: (zebra_interface_add_read) Use new if_get_by_name_len
  function.
  (zebra_interface_state_read) Use new if_lookup_by_name_len function.
* kernel_socket.c: (ifm_read) Use new if_lookup_by_name_len function
  to save a memcpy.
* if_ioctl_solaris.c: (interface_list_ioctl) Fix subtle bug with new
  if_get_by_name_len function.
* ospf_interface.c: (ospf_vl_new) Use strnlen to fix call to if_create.

lib/ChangeLog
lib/if.c
lib/if.h
lib/zclient.c
ospfd/ChangeLog
ospfd/ospf_interface.c
zebra/ChangeLog
zebra/if_ioctl_solaris.c
zebra/kernel_socket.c

index c372e66c843fd1cbc9c2d4339a1e33e9a8d87a1f..35910a6553fc77560f77ed96a71a000d22609c46 100644 (file)
@@ -1,3 +1,13 @@
+2005-04-02 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
+
+       * if.h: (if_lookup_by_name_len, if_get_by_name_len) New functions.
+       * if.c: (if_lookup_by_name_len, if_get_by_name_len) New functions.
+         (if_get_by_name) Tighten up code.
+         (interface) Use new function if_get_by_name_len.
+       * zclient.c: (zebra_interface_add_read) Use new if_get_by_name_len
+         function.
+         (zebra_interface_state_read) Use new if_lookup_by_name_len function.
+
 2005-04-02 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
 
        * str.c: Replace strlcpy and strlcat with actual working versions
index e35e3ed260e01e79e7e2d494e1ef6560304211ba..212b236c1e63f244d0b15ce503d4b9acf3541f49 100644 (file)
--- a/lib/if.c
+++ b/lib/if.c
@@ -219,12 +219,33 @@ if_lookup_by_name (const char *name)
   for (node = listhead (iflist); node; nextnode (node))
     {
       ifp = getdata (node);
+      /* Change this to strcmp once improper uses of this function
+         have been replaced with calls to if_lookup_by_name_len. */
       if (strncmp (name, ifp->name, sizeof ifp->name) == 0)
        return ifp;
     }
   return NULL;
 }
 
+struct interface *
+if_lookup_by_name_len(const char *name, size_t namelen)
+{
+  struct listnode *node;
+
+  if (namelen > INTERFACE_NAMSIZ)
+    return NULL;
+
+  for (node = listhead (iflist); node; nextnode (node))
+    {
+      struct interface *ifp;
+
+      ifp = getdata (node);
+      if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
+       return ifp;
+    }
+  return NULL;
+}
+
 /* Lookup interface by IPv4 address. */
 struct interface *
 if_lookup_exact_address (struct in_addr src)
@@ -314,10 +335,19 @@ if_get_by_name (const char *name)
 {
   struct interface *ifp;
 
-  ifp = if_lookup_by_name (name);
-  if (ifp == NULL)
-    ifp = if_create (name, INTERFACE_NAMSIZ);
-  return ifp;
+  /* Replace 2nd arg to if_create with strlen(name) once improper uses of
+     this function have been replaced with calls to if_get_by_name_len. */
+  return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp :
+        if_create(name, INTERFACE_NAMSIZ);
+}
+
+struct interface *
+if_get_by_name_len(const char *name, size_t namelen)
+{
+  struct interface *ifp;
+
+  return ((ifp = if_lookup_by_name_len(name, namelen)) != NULL) ? ifp :
+        if_create(name, namelen);
 }
 
 /* Does interface up ? */
@@ -504,10 +534,8 @@ DEFUN (interface,
       return CMD_WARNING;
     }
 
-  ifp = if_lookup_by_name (argv[0]);
+  ifp = if_get_by_name_len(argv[0], sl);
 
-  if (ifp == NULL)
-    ifp = if_create (argv[0], sl);
   vty->index = ifp;
   vty->node = INTERFACE_NODE;
 
index df9ff6052bb7ddefd053173de280f1db776fec7a..4cfc9e77b7ca7cf0aeed52f6fd3275aa88b8fa60 100644 (file)
--- a/lib/if.h
+++ b/lib/if.h
@@ -214,11 +214,25 @@ struct connected
 int if_cmp_func (struct interface *, struct interface *);
 struct interface *if_create (const char *name, int namelen);
 struct interface *if_lookup_by_index (unsigned int);
-struct interface *if_lookup_by_name (const char *);
 struct interface *if_lookup_exact_address (struct in_addr);
 struct interface *if_lookup_address (struct in_addr);
+
+/* Currently, the code assumes that the interface name arguments to these
+   functions have length <= INTERFACE_NAMSIZ, and they must be NUL-terminated
+   if they are shorter than INTERFACE_NAMSIZ.  After code cleanup, the
+   implementation will be changed to require the arguments to these functions
+   to terminate with a NUL character (no length limitation). */
+struct interface *if_lookup_by_name (const char *);
 struct interface *if_get_by_name (const char *);
 
+/* For these 2 functions, the 2nd argument should be the precise length
+   of the interface name (not counting a trailing NUL which may or may
+   not be present). */
+extern struct interface *if_lookup_by_name_len(const char *name,
+                                              size_t namelen);
+extern struct interface *if_get_by_name_len(const char *name, size_t namelen);
+
+
 /* 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 
    structure because there may be configuration information attached. */
index 453e6cdd2d434a67e34f8dd8abd50f43911457b4..efcad57f644a6eaebdbc6938bb11ec40385f13d6 100644 (file)
@@ -534,12 +534,8 @@ zebra_interface_add_read (struct stream *s)
   /* Read interface name. */
   stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
 
-  /* Lookup this by interface name. */
-  ifp = if_lookup_by_name (ifname_tmp);
-
-  /* If such interface does not exist, make new one. */
-  if (! ifp)
-    ifp = if_create (ifname_tmp, INTERFACE_NAMSIZ);
+  /* Lookup/create interface by name. */
+  ifp = if_get_by_name_len (ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ));
 
   /* Read interface's index. */
   ifp->ifindex = stream_getl (s);
@@ -579,7 +575,8 @@ zebra_interface_state_read (struct stream *s)
   stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
 
   /* Lookup this by interface index. */
-  ifp = if_lookup_by_name (ifname_tmp);
+  ifp = if_lookup_by_name_len (ifname_tmp,
+                              strnlen(ifname_tmp, INTERFACE_NAMSIZ));
 
   /* If such interface does not exist, indicate an error */
   if (! ifp)
index b1d6bbec5c269fdeafe3d8d3a8204a4ab9127813..e8eae90e6196d2018eccd63739711d8c6f3b3d44 100644 (file)
@@ -1,3 +1,7 @@
+2005-04-02 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
+
+       * ospf_interface.c: (ospf_vl_new) Use strnlen to fix call to if_create.
+
 2005-04-02 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
 
        * ospf_vty.c: (show_ip_ospf_interface_sub) Show ifindex and interface
index b76abe58a9d22c17ba3a44443a6bcf75322ada3b..027dfb9dcafb731466686c9d53c0a6ca75451351 100644 (file)
@@ -880,8 +880,8 @@ ospf_vl_new (struct ospf *ospf, struct ospf_vl_data *vl_data)
   if (IS_DEBUG_OSPF_EVENT)
     zlog_debug ("ospf_vl_new(): creating pseudo zebra interface");
 
-  snprintf (ifname, INTERFACE_NAMSIZ + 1, "VLINK%d", vlink_count);
-  vi = if_create (ifname, INTERFACE_NAMSIZ);
+  snprintf (ifname, sizeof(ifname), "VLINK%d", vlink_count);
+  vi = if_create (ifname, strnlen(ifname, sizeof(ifname)));
   co = connected_new ();
   co->ifp = vi;
   listnode_add (vi->connected, co);
index 572d2687fd573bb81476faa474f45649dfb9c27e..924f0f0a6638f430abb9b9a5a156e8a5f0365bc8 100644 (file)
@@ -1,3 +1,10 @@
+2005-04-02 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
+
+       * kernel_socket.c: (ifm_read) Use new if_lookup_by_name_len function
+         to save a memcpy.
+       * if_ioctl_solaris.c: (interface_list_ioctl) Fix subtle bug with new
+         if_get_by_name_len function.
+
 2005-04-02 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
 
        * interface.c: (if_new_intern_ifindex) Remove obsolete function.
index 38ac5a71d6d64f96c03b71dd86eae26a50f4ec38..aa01b0733c68f3ebe9710e38ed9e6ca16ae8c9e1 100644 (file)
@@ -138,7 +138,9 @@ calculate_lifc_len:     /* must hold privileges to enter here */
 
   for (n = 0; n < lifconf.lifc_len; n += sizeof (struct lifreq))
     {
-      ifp = if_get_by_name (lifreq->lifr_name);
+      ifp = if_get_by_name_len(lifreq->lifr_name,
+                              strnlen(lifreq->lifr_name,
+                                      sizeof(lifreq->lifr_name)));
 
       if (lifreq->lifr_addr.ss_family == AF_INET)
         ifp->flags |= IFF_IPV4;
index cdc6822c5a51a2ebc52707092b2526542d19f91d..121256302ce6763db208b27eadfbc3519b44dbd1 100644 (file)
@@ -235,7 +235,6 @@ ifm_read (struct if_msghdr *ifm)
   struct sockaddr_dl *sdl = NULL;
   void *cp;
   unsigned int i;
-  char ifname[IFNAMSIZ];
 
   /* paranoia: sanity check structure */
   if (ifm->ifm_msglen < sizeof(struct if_msghdr))
@@ -309,6 +308,9 @@ ifm_read (struct if_msghdr *ifm)
       /*
        * paranoia: sanity check name length.  nlen does not include
        * trailing zero, but IFNAMSIZ max length does.
+       *
+       * XXX Is this test correct?  Should it be '>=' or '>'?  And is it even
+       * necessary now that we are using if_lookup_by_name_len?
        */
       if (sdl->sdl_nlen >= IFNAMSIZ)
        {
@@ -316,9 +318,7 @@ ifm_read (struct if_msghdr *ifm)
          return -1;
        }
 
-      memcpy (ifname, sdl->sdl_data, sdl->sdl_nlen);
-      ifname[sdl->sdl_nlen] = '\0';
-      ifp = if_lookup_by_name (ifname);
+      ifp = if_lookup_by_name_len (sdl->sdl_data, sdl->sdl_nlen);
     }
 
   /*