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/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 */
-
-
-
-