2005-03-30 Andrew J. Schorr <ajschorr@alumni.princeton.edu>

	* irdp.h: Add prototype for irdp_sock_init, and fix protos for
	  other irdp_* functions.
	* irdp_interface.c: (irdp_if_start) If irdp_sock is negative,
	  call irdp_sock_init to create the IRDP socket.
	  (irdp_if_init) Rename to irdp_init().
	  (get_iflist_ifp) Remove function that is a duplicate of
	  if_lookup_by_index.
	  (*) Make many functions static.  And remove superfluous "\n" from
	  several zlog messages.
	* irdp_main.c: (irdp_init) Remove function that used to call
	  irdp_if_init() and irdp_sock_init(), since we will now create
	  the socket only upon first use.
	  (irdp_sock_init) Do not update global irdp_sock variable, just
	  return the fd and assume that the caller will do so.  If setsockopt
	  calls fail, close the socket before returning -1.
	  (*) Make many functions static.
	* irdp_packet.c: Initialize irdp_sock to -1.
	  (irdp_read_raw) Call standard library function if_lookup_by_index
	  instead of get_iflist_ifp.
	  (irdp_recvmsg) Should be static, not global.
diff --git a/zebra/ChangeLog b/zebra/ChangeLog
index 59c154d..a75bd61 100644
--- a/zebra/ChangeLog
+++ b/zebra/ChangeLog
@@ -1,3 +1,26 @@
+2005-03-30 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
+
+	* irdp.h: Add prototype for irdp_sock_init, and fix protos for
+	  other irdp_* functions.
+	* irdp_interface.c: (irdp_if_start) If irdp_sock is negative,
+	  call irdp_sock_init to create the IRDP socket.
+	  (irdp_if_init) Rename to irdp_init().
+	  (get_iflist_ifp) Remove function that is a duplicate of
+	  if_lookup_by_index.
+	  (*) Make many functions static.  And remove superfluous "\n" from
+	  several zlog messages.
+	* irdp_main.c: (irdp_init) Remove function that used to call
+	  irdp_if_init() and irdp_sock_init(), since we will now create
+	  the socket only upon first use.
+	  (irdp_sock_init) Do not update global irdp_sock variable, just
+	  return the fd and assume that the caller will do so.  If setsockopt
+	  calls fail, close the socket before returning -1.
+	  (*) Make many functions static.
+	* irdp_packet.c: Initialize irdp_sock to -1.
+	  (irdp_read_raw) Call standard library function if_lookup_by_index
+	  instead of get_iflist_ifp.
+	  (irdp_recvmsg) Should be static, not global.
+
 2005-03-29 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
 
 	* rt_netlink.c: (netlink_link_change) If the status of an
diff --git a/zebra/irdp.h b/zebra/irdp.h
index 88453e9..1f0c1ec 100644
--- a/zebra/irdp.h
+++ b/zebra/irdp.h
@@ -142,12 +142,9 @@
   int pref;
 };
 
-void irdp_init();
-void irdp_finish();
-void irdp_config_write (struct vty *, struct interface *);
+extern void irdp_init(void);
+extern int irdp_sock_init(void);
+extern void irdp_finish(void);
+extern void irdp_config_write (struct vty *, struct interface *);
+
 #endif /* _IRDP_H */
-
-
-
-
-
diff --git a/zebra/irdp_interface.c b/zebra/irdp_interface.c
index 983969f..5f91385 100644
--- a/zebra/irdp_interface.c
+++ b/zebra/irdp_interface.c
@@ -74,7 +74,8 @@
 
 char b1[16], b2[16], b3[16], b4[16];  /* For inet_2a */
 
-struct prefix *irdp_get_prefix(struct interface *ifp)
+static struct prefix *
+irdp_get_prefix(struct interface *ifp)
 {
   struct listnode *node;
   struct connected *ifc;
@@ -87,7 +88,8 @@
 }
 
 /* Join to the add/leave multicast group. */
-int if_group (struct interface *ifp, 
+static int
+if_group (struct interface *ifp, 
 	  int sock, 
 	  u_int32_t group, 
 	  int add_leave)
@@ -121,7 +123,8 @@
   return ret;
 }
 
-int if_add_group (struct interface *ifp)
+static int
+if_add_group (struct interface *ifp)
 {
   struct zebra_if *zi= ifp->info;
   struct irdp_interface *irdp = &zi->irdp;
@@ -133,12 +136,14 @@
   }
 
   if(irdp->flags & IF_DEBUG_MISC )
-    zlog_debug("IRDP: Adding group %s for %s\n", 
+    zlog_debug("IRDP: Adding group %s for %s", 
 	       inet_2a(htonl(INADDR_ALLRTRS_GROUP), b1),
 	       ifp->name);
   return 0;
 }
-int if_drop_group (struct interface *ifp)
+
+static int
+if_drop_group (struct interface *ifp)
 {
   struct zebra_if *zi= ifp->info;
   struct irdp_interface *irdp = &zi->irdp;
@@ -149,25 +154,13 @@
     return ret;
 
   if(irdp->flags & IF_DEBUG_MISC)
-    zlog_debug("IRDP: Leaving group %s for %s\n", 
+    zlog_debug("IRDP: Leaving group %s for %s", 
 	       inet_2a(htonl(INADDR_ALLRTRS_GROUP), b1),
 	       ifp->name);
   return 0;
 }
 
-struct interface *get_iflist_ifp(unsigned int idx)
-{
-  struct listnode *node;
-  struct interface *ifp;
-
-  LIST_LOOP (iflist, ifp, node)
-    if(ifp->ifindex == idx) 
-      return ifp;
-
-  return NULL;
-}
-
-void
+static void
 if_set_defaults(struct interface *ifp)
 {
   struct zebra_if *zi=ifp->info;
@@ -180,7 +173,7 @@
 }
 
 
-struct Adv *Adv_new ()
+struct Adv *Adv_new (void)
 {
   struct Adv *new;
   new = XMALLOC (MTYPE_TMP, sizeof (struct Adv));
@@ -188,12 +181,14 @@
   return new;
 }
 
-void Adv_free (struct Adv *adv)
+static void
+Adv_free (struct Adv *adv)
 {
   XFREE (MTYPE_TMP, adv);
 }
 
-void irdp_if_start(struct interface *ifp, int multicast, int set_defaults)
+static void
+irdp_if_start(struct interface *ifp, int multicast, int set_defaults)
 {
   struct zebra_if *zi= ifp->info;
   struct irdp_interface *irdp = &zi->irdp;
@@ -202,7 +197,12 @@
   u_int32_t timer, seed;
 
   if (irdp->flags & IF_ACTIVE ) {
-    zlog_warn("IRDP: Interface is already active %s\n", ifp->name);
+    zlog_warn("IRDP: Interface is already active %s", ifp->name);
+    return;
+  }
+  if ((irdp_sock < 0) && ((irdp_sock = irdp_sock_init()) < 0)) {
+    zlog_warn("IRDP: Cannot activate interface %s (cannot create "
+	      "IRDP socket)", ifp->name);
     return;
   }
   irdp->flags |= IF_ACTIVE;
@@ -213,7 +213,7 @@
   if_add_update(ifp);
 
   if (! (ifp->flags & IFF_UP)) {
-    zlog_warn("IRDP: Interface is down %s\n", ifp->name);
+    zlog_warn("IRDP: Interface is down %s", ifp->name);
   }
 
   /* Shall we cancel if_start if if_add_group fails? */
@@ -222,7 +222,7 @@
     if_add_group(ifp);
     
     if (! (ifp->flags & (IFF_MULTICAST|IFF_ALLMULTI))) {
-      zlog_warn("IRDP: Interface not multicast enabled %s\n", ifp->name);
+      zlog_warn("IRDP: Interface not multicast enabled %s", ifp->name);
     }
   }
 
@@ -256,7 +256,7 @@
 
   
   if(irdp->flags & IF_DEBUG_MISC)
-    zlog_debug("IRDP: Init timer for %s set to %u\n", 
+    zlog_debug("IRDP: Init timer for %s set to %u", 
 	       ifp->name, 
 	       timer);
 
@@ -266,7 +266,8 @@
 				       timer);
 }
 
-void irdp_if_stop(struct interface *ifp)
+static void
+irdp_if_stop(struct interface *ifp)
 {
   struct zebra_if *zi=ifp->info;
   struct irdp_interface *irdp=&zi->irdp;
@@ -277,7 +278,7 @@
   }
 
   if (! (irdp->flags & IF_ACTIVE )) {
-    zlog_warn("Interface is not active %s\n", ifp->name);
+    zlog_warn("Interface is not active %s", ifp->name);
     return;
   }
 
@@ -293,13 +294,14 @@
 }
 
 
-void irdp_if_shutdown(struct interface *ifp)
+static void
+irdp_if_shutdown(struct interface *ifp)
 {
   struct zebra_if *zi= ifp->info;
   struct irdp_interface *irdp = &zi->irdp;
 
   if (irdp->flags & IF_SHUTDOWN ) {
-    zlog_warn("IRDP: Interface is already shutdown %s\n", ifp->name);
+    zlog_warn("IRDP: Interface is already shutdown %s", ifp->name);
     return;
   }
 
@@ -313,13 +315,14 @@
   irdp_advert_off(ifp);
 }
 
-void irdp_if_no_shutdown(struct interface *ifp)
+static void
+irdp_if_no_shutdown(struct interface *ifp)
 {
   struct zebra_if *zi= ifp->info;
   struct irdp_interface *irdp = &zi->irdp;
 
   if (! (irdp->flags & IF_SHUTDOWN )) {
-    zlog_warn("IRDP: Interface is not shutdown %s\n", ifp->name);
+    zlog_warn("IRDP: Interface is not shutdown %s", ifp->name);
     return;
   }
 
@@ -760,7 +763,7 @@
 }
 
 void
-irdp_if_init ()
+irdp_init ()
 {
   install_element (INTERFACE_NODE, &ip_irdp_broadcast_cmd);
   install_element (INTERFACE_NODE, &ip_irdp_multicast_cmd);
diff --git a/zebra/irdp_main.c b/zebra/irdp_main.c
index 0db2a54..af6bb80 100644
--- a/zebra/irdp_main.c
+++ b/zebra/irdp_main.c
@@ -77,15 +77,12 @@
 
 int irdp_read_raw(struct thread *r);
 int in_cksum (void *ptr, int nbytes);
-extern int irdp_sock;
 void send_packet(struct interface *ifp, 
 		 struct stream *s,
 		 u_int32_t dst,
 		 struct prefix *p,
 		 u_int32_t ttl);
 
-void irdp_if_init ();
-
 char *
 inet_2a(u_int32_t a, char *b)
 {
@@ -102,44 +99,48 @@
 {
   int ret, i;
   int save_errno;
+  int sock;
 
   if ( zserv_privs.change (ZPRIVS_RAISE) )
        zlog_err ("irdp_sock_init: could not raise privs, %s",
                   safe_strerror (errno) );
 
-  irdp_sock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
+  sock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
   save_errno = errno;
 
   if ( zserv_privs.change (ZPRIVS_LOWER) )
        zlog_err ("irdp_sock_init: could not lower privs, %s",
              safe_strerror (errno) );
 
-  if (irdp_sock < 0) {
+  if (sock < 0) {
     zlog_warn ("IRDP: can't create irdp socket %s", safe_strerror(save_errno));
-    return irdp_sock;
+    return sock;
   };
   
   i = 1;
-  ret = setsockopt (irdp_sock, IPPROTO_IP, IP_TTL, 
+  ret = setsockopt (sock, IPPROTO_IP, IP_TTL, 
                         (void *) &i, sizeof (i));
   if (ret < 0) {
     zlog_warn ("IRDP: can't do irdp sockopt %s", safe_strerror(errno));
+    close(sock);
     return ret;
   };
   
-  ret = setsockopt_ifindex (AF_INET, irdp_sock, 1);
+  ret = setsockopt_ifindex (AF_INET, sock, 1);
   if (ret < 0) {
     zlog_warn ("IRDP: can't do irdp sockopt %s", safe_strerror(errno));
+    close(sock);
     return ret;
   };
 
-  t_irdp_raw = thread_add_read (zebrad.master, irdp_read_raw, NULL, irdp_sock); 
+  t_irdp_raw = thread_add_read (zebrad.master, irdp_read_raw, NULL, sock); 
 
-  return irdp_sock;
+  return sock;
 }
 
 
-int get_pref(struct irdp_interface *irdp, struct prefix *p)
+static int
+get_pref(struct irdp_interface *irdp, struct prefix *p)
 {
   struct listnode *node;
   struct Adv *adv;
@@ -157,9 +158,10 @@
 }
 
 /* Make ICMP Router Advertisement Message. */
-int make_advertisement_packet (struct interface *ifp, 
-			       struct prefix *p,
-			       struct stream *s)
+static int
+make_advertisement_packet (struct interface *ifp, 
+			   struct prefix *p,
+			   struct stream *s)
 {
   struct zebra_if *zi=ifp->info;
   struct irdp_interface *irdp=&zi->irdp;
@@ -191,9 +193,8 @@
   return size;
 }
 
-void irdp_send(struct interface *ifp, 
-	       struct prefix *p, 
-	       struct stream *s)
+static void
+irdp_send(struct interface *ifp, struct prefix *p, struct stream *s)
 {
   struct zebra_if *zi=ifp->info;
   struct irdp_interface *irdp=&zi->irdp;
@@ -218,8 +219,7 @@
   send_packet (ifp, s, dst, p, ttl);
 }
 
-void irdp_advertisement (struct interface *ifp, 
-		   struct prefix *p)
+static void irdp_advertisement (struct interface *ifp, struct prefix *p)
 {
   struct stream *s;
   s = stream_new (128);
@@ -345,14 +345,4 @@
     }
 }
 
-void irdp_init()
-{
-  irdp_sock_init();
-  irdp_if_init ();
-}
-
 #endif /* HAVE_IRDP */
-
-
-
-
diff --git a/zebra/irdp_packet.c b/zebra/irdp_packet.c
index 2f448ec..74c75ab 100644
--- a/zebra/irdp_packet.c
+++ b/zebra/irdp_packet.c
@@ -66,16 +66,16 @@
 
 /* GLOBAL VARS */
 
-int irdp_sock;
+int irdp_sock = -1;
 char b1[16], b2[16], b3[16], b4[16];  /* For inet_2a */
 
 extern struct zebra_t zebrad;
 extern struct thread *t_irdp_raw;
-extern struct interface *get_iflist_ifp(int idx);
 int in_cksum (void *ptr, int nbytes);
 void process_solicit (struct interface *ifp);
 
-void parse_irdp_packet(char *p, 
+static void
+parse_irdp_packet(char *p, 
 		  int len, 
 		  struct interface *ifp)
 {
@@ -187,7 +187,8 @@
     }
 }
 
-int irdp_recvmsg (int sock, u_char *buf, int size, int *ifindex)
+static int
+irdp_recvmsg (int sock, u_char *buf, int size, int *ifindex)
 {
   struct msghdr msg;
   struct iovec iov;
@@ -239,7 +240,7 @@
  
   if (ret < 0) zlog_warn ("IRDP: RX Error length = %d", ret);
 
-  ifp = get_iflist_ifp(ifindex);
+  ifp = if_lookup_by_index(ifindex);
   if(! ifp ) return ret;
 
   zi= ifp->info;