2004-02-17 Paul Jakma <paul@dishone.st>

        * bgpd.h: (bgp_peer) add fd_local and fd_accept
          file descriptor's, fd becomes a pointer to one of these.
        * bgpd.c: (global) adjust for fact that fd is now a pointer.
          (peer_create_accept) removed.
        * bgp_route.c: (global) adjust for change of peer fd to pointer
        * bgp_packet.c: (bgp_collision_detect) adjust and remove the
          "replace with other peer" hack.
        * bgp_network.c: (bgp_accept) Remove the dummy peer hack.
          Update peer->fd_accept instead.
          (global) Adjust fd references - now a pointer.
        * bgp_fsm.c: (global) adjust peer fd to pointer.
          (bgp_connection_stop) new function, to stop connection.
          (global) adjust everything which closed peer fd to use
          bgp_connection_stop().
diff --git a/bgpd/ChangeLog b/bgpd/ChangeLog
index 428c2b9..f28e6a0 100644
--- a/bgpd/ChangeLog
+++ b/bgpd/ChangeLog
@@ -1,8 +1,25 @@
+2004-02-17 Paul Jakma <paul@dishone.st>
+
+	* bgpd.h: (bgp_peer) add fd_local and fd_accept 
+	  file descriptor's, fd becomes a pointer to one of these.
+	* bgpd.c: (global) adjust for fact that fd is now a pointer.
+	  (peer_create_accept) removed.
+	* bgp_route.c: (global) adjust for change of peer fd to pointer
+	* bgp_packet.c: (bgp_collision_detect) adjust and remove the 
+	  "replace with other peer" hack. 
+	* bgp_network.c: (bgp_accept) Remove the dummy peer hack. 
+	  Update peer->fd_accept instead. 
+	  (global) Adjust fd references - now a pointer.
+	* bgp_fsm.c: (global) adjust peer fd to pointer.
+	  (bgp_connection_stop) new function, to stop connection.
+	  (global) adjust everything which closed peer fd to use
+	  bgp_connection_stop().
+	  
 2003-12-23 Krzysztof Oledzki <oleq@ans.pl>
 
 	* bgp_network.c: drop privs on error cases
 
-2003-08-11 kunihiro <kunihiro@zebra.org
+2003-08-11 kunihiro <kunihiro@zebra.org>
 
 	* bgp_route{,map}.c: Extend 'set ip next-hop' in route-maps with
 	  ability to specify 'peer-address' rather than IP. 
diff --git a/bgpd/bgp_fsm.c b/bgpd/bgp_fsm.c
index 3d8e957..ac25f15 100644
--- a/bgpd/bgp_fsm.c
+++ b/bgpd/bgp_fsm.c
@@ -292,7 +292,7 @@
 
   peer->synctime = time (NULL);
 
-  BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
+  BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
 
   BGP_TIMER_ON (peer->t_routeadv, bgp_routeadv_timer,
 		peer->v_routeadv);
@@ -307,6 +307,21 @@
   peer->uptime = time (NULL);
 }
 
+void
+bgp_connection_stop (struct peer *peer)
+{
+  if (peer->fd_local >= 0)
+    {
+      close (peer->fd_local);
+      peer->fd_local = -1;
+    }
+  if (peer->fd_accept >= 0)
+    {
+      close (peer->fd_accept);
+      peer->fd_accept = -1;
+    }
+}
+
 /* Administrative BGP peer stop event. */
 int
 bgp_stop (struct peer *peer)
@@ -367,12 +382,8 @@
     stream_reset (peer->work);
   stream_fifo_clean (peer->obuf);
 
-  /* Close of file descriptor. */
-  if (peer->fd >= 0)
-    {
-      close (peer->fd);
-      peer->fd = -1;
-    }
+  /* Close of connections. */
+  bgp_connection_stop (peer);
 
   /* Connection information. */
   if (peer->su_local)
@@ -386,7 +397,7 @@
       XFREE (MTYPE_SOCKUNION, peer->su_remote);
       peer->su_remote = NULL;
     }
-
+    
   /* Clear remote router-id. */
   peer->remote_id.s_addr = 0;
 
@@ -477,13 +488,13 @@
 int
 bgp_connect_success (struct peer *peer)
 {
-  if (peer->fd < 0)
+  if (peer->fd_local < 0)
     {
       zlog_err ("bgp_connect_success peer's fd is negative value %d",
-		peer->fd);
+                peer->fd_local);
       return -1;
     }
-  BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
+  BGP_READ_ON (peer->t_read, bgp_read, peer->fd_local);
 
   /* bgp_getsockname (peer); */
 
@@ -521,29 +532,29 @@
     {
     case connect_error:
       if (BGP_DEBUG (fsm, FSM))
-	plog_info (peer->log, "%s [FSM] Connect error", peer->host);
+        plog_info (peer->log, "%s [FSM] Connect error", peer->host);
       BGP_EVENT_ADD (peer, TCP_connection_open_failed);
       break;
     case connect_success:
       if (BGP_DEBUG (fsm, FSM))
-	plog_info (peer->log, "%s [FSM] Connect immediately success",
-		   peer->host);
+        plog_info (peer->log, "%s [FSM] Connect immediately success",
+                   peer->host);
       BGP_EVENT_ADD (peer, TCP_connection_open);
       break;
     case connect_in_progress:
       /* To check nonblocking connect, we wait until socket is
          readable or writable. */
       if (BGP_DEBUG (fsm, FSM))
-	plog_info (peer->log, "%s [FSM] Non blocking connect waiting result",
-		   peer->host);
-      if (peer->fd < 0)
+        plog_info (peer->log, "%s [FSM] Non blocking connect waiting result",
+                   peer->host);
+      if (*peer->fd < 0)
 	{
 	  zlog_err ("bgp_start peer's fd is negative value %d",
-		    peer->fd);
+		    *peer->fd);
 	  return -1;
 	}
-      BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
-      BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
+      BGP_READ_ON (peer->t_read, bgp_read, *peer->fd);
+      BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
       break;
     }
   return 0;
diff --git a/bgpd/bgp_network.c b/bgpd/bgp_network.c
index aa369bc..e03831d 100644
--- a/bgpd/bgp_network.c
+++ b/bgpd/bgp_network.c
@@ -46,7 +46,6 @@
   int accept_sock;
   union sockunion su;
   struct peer *peer;
-  struct peer *peer1;
   struct bgp *bgp;
   char buf[SU_ADDRSTRLEN];
 
@@ -73,12 +72,12 @@
     zlog_info ("[Event] BGP connection from host %s", inet_sutop (&su, buf));
   
   /* Check remote IP address */
-  peer1 = peer_lookup (bgp, &su);
-  if (! peer1 || peer1->status == Idle)
+  peer = peer_lookup (bgp, &su);
+  if (! peer || peer->status == Idle)
     {
       if (BGP_DEBUG (events, EVENTS))
 	{
-	  if (! peer1)
+	  if (! peer)
 	    zlog_info ("[Event] BGP connection IP address %s is not configured",
 		       inet_sutop (&su, buf));
 	  else
@@ -90,30 +89,13 @@
     }
 
   /* In case of peer is EBGP, we should set TTL for this connection.  */
-  if (peer_sort (peer1) == BGP_PEER_EBGP)
-    sockopt_ttl (peer1->su.sa.sa_family, bgp_sock, peer1->ttl);
+  if (peer_sort (peer) == BGP_PEER_EBGP)
+    sockopt_ttl (peer->su.sa.sa_family, bgp_sock, peer->ttl);
 
   if (! bgp)
-    bgp = peer1->bgp;
+    bgp = peer->bgp;
 
-  /* Make dummy peer until read Open packet. */
-  if (BGP_DEBUG (events, EVENTS))
-    zlog_info ("[Event] Make dummy peer structure until read Open packet");
-
-  {
-    char buf[SU_ADDRSTRLEN + 1];
-
-    peer = peer_create_accept (bgp);
-    SET_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER);
-    peer->su = su;
-    peer->fd = bgp_sock;
-    peer->status = Active;
-    peer->local_id = peer1->local_id;
-
-    /* Make peer's address string. */
-    sockunion2str (&su, buf, SU_ADDRSTRLEN);
-    peer->host = strdup (buf);
-  }
+  peer->fd_accept = bgp_sock;
 
   BGP_EVENT_ADD (peer, TCP_connection_open);
 
@@ -133,7 +115,7 @@
 
   strncpy ((char *)&ifreq.ifr_name, peer->ifname, sizeof (ifreq.ifr_name));
 
-  ret = setsockopt (peer->fd, SOL_SOCKET, SO_BINDTODEVICE, 
+  ret = setsockopt (*peer->fd, SOL_SOCKET, SO_BINDTODEVICE, 
 		    &ifreq, sizeof (ifreq));
   if (ret < 0)
     {
@@ -207,12 +189,12 @@
       if (! addr)
 	return;
 
-      bgp_bind_address (peer->fd, addr);
+      bgp_bind_address (*peer->fd, addr);
     }
 
   /* Source is specified with IP address.  */
   if (peer->update_source)
-    sockunion_bind (peer->fd, peer->update_source, 0, peer->update_source);
+    sockunion_bind (*peer->fd, peer->update_source, 0, peer->update_source);
 }
 
 /* BGP try to connect to the peer.  */
@@ -222,16 +204,16 @@
   unsigned int ifindex = 0;
 
   /* Make socket for the peer. */
-  peer->fd = sockunion_socket (&peer->su);
-  if (peer->fd < 0)
+  *peer->fd = sockunion_socket (&peer->su);
+  if (*peer->fd < 0)
     return -1;
 
   /* If we can get socket for the peer, adjest TTL and make connection. */
   if (peer_sort (peer) == BGP_PEER_EBGP)
-    sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl);
+    sockopt_ttl (peer->su.sa.sa_family, *peer->fd, peer->ttl);
 
-  sockopt_reuseaddr (peer->fd);
-  sockopt_reuseport (peer->fd);
+  sockopt_reuseaddr (*peer->fd);
+  sockopt_reuseport (*peer->fd);
 
   /* Bind socket. */
   bgp_bind (peer);
@@ -246,10 +228,10 @@
 
   if (BGP_DEBUG (events, EVENTS))
     plog_info (peer->log, "%s [Event] Connect start to %s fd %d",
-	       peer->host, peer->host, peer->fd);
+	       peer->host, peer->host, *peer->fd);
 
   /* Connect to the remote peer. */
-  return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
+  return sockunion_connect (*peer->fd, &peer->su, htons (peer->port), ifindex);
 }
 
 /* After TCP connection is established.  Get local address and port. */
@@ -268,8 +250,8 @@
       peer->su_remote = NULL;
     }
 
-  peer->su_local = sockunion_getsockname (peer->fd);
-  peer->su_remote = sockunion_getpeername (peer->fd);
+  peer->su_local = sockunion_getsockname (*peer->fd);
+  peer->su_remote = sockunion_getpeername (*peer->fd);
 
   bgp_nexthop_set (peer->su_local, peer->su_remote, &peer->nexthop, peer);
 }
diff --git a/bgpd/bgp_packet.c b/bgpd/bgp_packet.c
index 48879f3..7d921be 100644
--- a/bgpd/bgp_packet.c
+++ b/bgpd/bgp_packet.c
@@ -133,7 +133,7 @@
 
   /* Check file descriptor. */
   slen = sizeof (status);
-  ret = getsockopt(peer->fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen);
+  ret = getsockopt(*peer->fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen);
 
   /* If getsockopt is fail, this is fatal error. */
   if (ret < 0)
@@ -238,7 +238,6 @@
       bgp_packet_set_size (s);
       packet = bgp_packet_dup (s);
       bgp_packet_add (peer, packet);
-      BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
       stream_reset (s);
       return packet;
     }
@@ -395,7 +394,7 @@
   /* Add packet to the peer. */
   bgp_packet_add (peer, packet);
 
-  BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
+  BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
 }
 
 void
@@ -469,7 +468,7 @@
   /* Add packet to the peer. */
   bgp_packet_add (peer, packet);
 
-  BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
+  BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
 }
 
 /* Get next packet to be written.  */
@@ -575,7 +574,7 @@
       writenum = stream_get_endp (s) - stream_get_getp (s);
 
       /* Call write() system call.  */
-      num = write (peer->fd, STREAM_PNT (s), writenum);
+      num = write (*peer->fd, STREAM_PNT (s), writenum);
       write_errno = errno;
       if (num <= 0)
 	{
@@ -645,7 +644,7 @@
     }
   
   if (bgp_write_proceed (peer))
-    BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
+    BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
   
   return 0;
 }
@@ -665,7 +664,7 @@
   assert (stream_get_endp (s) >= BGP_HEADER_SIZE);
 
   /* I'm not sure fd is writable. */
-  ret = writen (peer->fd, STREAM_DATA (s), stream_get_endp (s));
+  ret = writen (*peer->fd, STREAM_DATA (s), stream_get_endp (s));
   if (ret <= 0)
     {
       bgp_stop (peer);
@@ -725,7 +724,7 @@
   /* Add packet to the peer. */
   bgp_packet_add (peer, s);
 
-  BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
+  BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
 }
 
 /* Make open packet and send it to the peer. */
@@ -780,7 +779,7 @@
   /* Add packet to the peer. */
   bgp_packet_add (peer, s);
 
-  BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
+  BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
 }
 
 /* Send BGP notify packet with data potion. */
@@ -981,7 +980,7 @@
   /* Add packet to the peer. */
   bgp_packet_add (peer, packet);
 
-  BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
+  BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
 }
 
 /* Send capability message to the peer. */
@@ -1048,14 +1047,13 @@
     zlog_info ("%s send message type %d, length (incl. header) %d",
 	       peer->host, BGP_MSG_CAPABILITY, length);
 
-  BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
+  BGP_WRITE_ON (peer->t_write, bgp_write, *peer->fd);
 }
 
 /* RFC1771 6.8 Connection collision detection. */
 int
-bgp_collision_detect (struct peer *new, struct in_addr remote_id)
+bgp_collision_detect (struct peer *peer, struct in_addr remote_id)
 {
-  struct peer *peer;
   struct listnode *nn;
   struct bgp *bgp;
 
@@ -1076,11 +1074,10 @@
     {
       /* Under OpenConfirm status, local peer structure already hold
          remote router ID. */
-
-      if (peer != new
-	  && (peer->status == OpenConfirm || peer->status == OpenSent)
-	  && sockunion_same (&peer->su, &new->su))
-	{
+      if ((peer->status == OpenConfirm)
+          && ( ntohl(peer->remote_id.s_addr) == ntohl(remote_id.s_addr) )
+         )
+        {
 	  /* 1. The BGP Identifier of the local system is compared to
 	     the BGP Identifier of the remote system (as specified in
 	     the OPEN message). */
@@ -1093,8 +1090,16 @@
 		 already in the OpenConfirm state), and accepts BGP
 		 connection initiated by the remote system. */
 
-	      if (peer->fd >= 0)
-		bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
+	      if (peer->fd_local >= 0)
+	        {
+	          BGP_WRITE_OFF (peer->t_write);
+	          BGP_READ_OFF (peer->t_read);
+	          close (peer->fd_local);
+	        }
+	      peer->fd_local = -1;
+	      peer->fd = &peer->fd_accept;
+
+	      BGP_READ_ON (peer->t_read, bgp_read, *peer->fd);
 	      return 1;
 	    }
 	  else
@@ -1105,12 +1110,15 @@
 		 existing one (the one that is already in the
 		 OpenConfirm state). */
 
-	      if (new->fd >= 0)
-		bgp_notify_send (new, BGP_NOTIFY_CEASE, 0);
+	      if (peer->fd_accept >= 0)
+	        {
+	          close (peer->fd_accept);
+	          peer->fd_accept = -1;
+	        }    
 	      return -1;
 	    }
-	}
-    }
+	  }
+    }    
   return 0;
 }
 
@@ -1187,51 +1195,6 @@
   if (ret < 0)
     return ret;
 
-  /* Hack part. */
-  if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
-    {
-      if (ret == 0 && realpeer->status != Active
-	  && realpeer->status != OpenSent
-	  && realpeer->status != OpenConfirm)
- 	{
- 	  if (BGP_DEBUG (events, EVENTS))
- 	    zlog_info ("%s [Event] peer's status is %s close connection",
-		       realpeer->host, LOOKUP (bgp_status_msg, peer->status));
- 	  return -1;
- 	}
-
-      if (BGP_DEBUG (events, EVENTS))
-	zlog_info ("%s [Event] Transfer temporary BGP peer to existing one",
-		   peer->host);
-
-      bgp_stop (realpeer);
-      
-      /* Transfer file descriptor. */
-      realpeer->fd = peer->fd;
-      peer->fd = -1;
-
-      /* Transfer input buffer. */
-      stream_free (realpeer->ibuf);
-      realpeer->ibuf = peer->ibuf;
-      realpeer->packet_size = peer->packet_size;
-      peer->ibuf = NULL;
-
-      /* Transfer status. */
-      realpeer->status = peer->status;
-      bgp_stop (peer);
-
-      /* peer pointer change. Open packet send to neighbor. */
-      peer = realpeer;
-      bgp_open_send (peer);
-      if (peer->fd < 0)
-	{
-	  zlog_err ("bgp_open_receive peer's fd is negative value %d",
-		    peer->fd);
-	  return -1;
-	}
-      BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
-    }
-
   /* remote router-id check. */
   if (remote_id.s_addr == 0
       || ntohl (remote_id.s_addr) >= 0xe0000000
@@ -2031,7 +1994,7 @@
     return 0;
 
   /* Read packet from fd. */
-  nbytes = stream_read_unblock (peer->ibuf, peer->fd, readsize);
+  nbytes = stream_read_unblock (peer->ibuf, *peer->fd, readsize);
 
   /* If read byte is smaller than zero then error occured. */
   if (nbytes < 0) 
@@ -2050,7 +2013,7 @@
     {
       if (BGP_DEBUG (events, EVENTS))
 	plog_info (peer->log, "%s [Event] BGP connection closed fd %d",
-		   peer->host, peer->fd);
+		   peer->host, *peer->fd);
       BGP_EVENT_ADD (peer, TCP_connection_closed);
       return -1;
     }
@@ -2097,12 +2060,12 @@
     }
   else
     {
-      if (peer->fd < 0)
+      if (*peer->fd < 0)
 	{
-	  zlog_err ("bgp_read peer's fd is negative value %d", peer->fd);
+	  zlog_err ("bgp_read peer's fd is negative value %d", *peer->fd);
 	  return -1;
 	}
-      BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
+      BGP_READ_ON (peer->t_read, bgp_read, *peer->fd);
     }
 
   /* Read packet header to determine type of the packet */
diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c
index f6f0879..1d7a052 100644
--- a/bgpd/bgp_route.c
+++ b/bgpd/bgp_route.c
@@ -4202,7 +4202,8 @@
 	    vty_out (vty, " (inaccessible)"); 
 	  else if (binfo->igpmetric)
 	    vty_out (vty, " (metric %d)", binfo->igpmetric);
-	  vty_out (vty, " from %s", sockunion2str (&binfo->peer->su, buf, SU_ADDRSTRLEN));
+	  vty_out (vty, " from %s", 
+	           sockunion2str (&binfo->peer->su, buf, SU_ADDRSTRLEN));
 	  if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
 	    vty_out (vty, " (%s)", inet_ntoa (attr->originator_id));
 	  else
diff --git a/bgpd/bgpd.c b/bgpd/bgpd.c
index 3a12a27..f4c8f76 100644
--- a/bgpd/bgpd.c
+++ b/bgpd/bgpd.c
@@ -504,11 +504,10 @@
   return 0;
 }
 
-/* Peer comparison function for sorting.  */
 static int
 peer_cmp (struct peer *p1, struct peer *p2)
 {
-  return sockunion_cmp (&p1->su, &p2->su);
+  return sockunion_cmp (&p1->su, &p2->su);  
 }
 
 int
@@ -684,8 +683,11 @@
   peer = XMALLOC (MTYPE_BGP_PEER, sizeof (struct peer));
   memset (peer, 0, sizeof (struct peer));
 
+  peer->fd = &peer->fd_local;
+
   /* Set default value. */
-  peer->fd = -1;
+  peer->fd_local = -1;
+  peer->fd_accept = -1;
   peer->v_start = BGP_INIT_START_TIMER;
   peer->v_connect = BGP_DEFAULT_CONNECT_RETRY;
   peer->v_asorig = BGP_DEFAULT_ASORIGINATE;
@@ -769,19 +771,6 @@
   return peer;
 }
 
-/* Make accept BGP peer.  Called from bgp_accept (). */
-struct peer *
-peer_create_accept (struct bgp *bgp)
-{
-  struct peer *peer;
-
-  peer = peer_new ();
-  peer->bgp = bgp;
-  listnode_add_sort (bgp->peer, peer);
-
-  return peer;
-}
-
 /* Change peer's AS number.  */
 void
 peer_as_change (struct peer *peer, as_t as)
@@ -1810,9 +1799,8 @@
   
   LIST_LOOP (bgp->peer, peer, nn)
     {
-      if (sockunion_same (&peer->su, su)
-	  && ! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
-	return peer;
+      if (sockunion_same (&peer->su, su))
+        return peer;
     }
   return NULL;
 }
@@ -1831,27 +1819,25 @@
 
   LIST_LOOP (bgp->peer, peer, nn)
     {
-      if (sockunion_same (&peer->su, su)
-	  && ! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
-	{
-	  if (peer->as == remote_as
-	      && peer->remote_id.s_addr == remote_id->s_addr)
-	    return peer;
-	  if (peer->as == remote_as)
-	    *as = 1;
-	}
+      if (sockunion_same (&peer->su, su))
+        {
+          if (peer->as == remote_as
+              && peer->remote_id.s_addr == remote_id->s_addr)
+            return peer;
+          if (peer->as == remote_as)
+            *as = 1;
+        }
     }
   LIST_LOOP (bgp->peer, peer, nn)
     {
-      if (sockunion_same (&peer->su, su)
-	  &&  ! CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
-	{
-	  if (peer->as == remote_as
-	      && peer->remote_id.s_addr == 0)
-	    return peer;
-	  if (peer->as == remote_as)
-	    *as = 1;
-	}
+      if (sockunion_same (&peer->su, su))
+        {
+          if (peer->as == remote_as
+              && peer->remote_id.s_addr == 0)
+            return peer;
+          if (peer->as == remote_as)
+            *as = 1;
+        }
     }
   return NULL;
 }
@@ -2278,22 +2264,24 @@
 
   if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
     {
-      if (peer->fd >= 0 && peer_sort (peer) != BGP_PEER_IBGP)
-	sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl);
+      if (*peer->fd >= 0 && peer_sort (peer) != BGP_PEER_IBGP)
+        sockopt_ttl (peer->su.sa.sa_family, *peer->fd, 
+                     peer->ttl);
     }
   else
     {
       group = peer->group;
       LIST_LOOP (group->peer, peer, nn)
-	{
-	  if (peer_sort (peer) == BGP_PEER_IBGP)
-	    continue;
+        {
+          if (peer_sort (peer) == BGP_PEER_IBGP)
+            continue;
 
-	  peer->ttl = group->conf->ttl;
+          peer->ttl = group->conf->ttl;
 
-	  if (peer->fd >= 0)
-	    sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl);
-	}
+          if (*peer->fd >= 0)
+            sockopt_ttl (peer->su.sa.sa_family,
+                         *peer->fd, peer->ttl);
+        }
     }
   return 0;
 }
@@ -2314,22 +2302,24 @@
 
   if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP))
     {
-      if (peer->fd >= 0 && peer_sort (peer) != BGP_PEER_IBGP)
-	sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl);
+      if (*peer->fd >= 0 && peer_sort (peer) != BGP_PEER_IBGP)
+        sockopt_ttl (peer->su.sa.sa_family, 
+                     *peer->fd, peer->ttl);
     }
   else
     {
       group = peer->group;
       LIST_LOOP (group->peer, peer, nn)
-	{
-	  if (peer_sort (peer) == BGP_PEER_IBGP)
-	    continue;
+        {
+          if (peer_sort (peer) == BGP_PEER_IBGP)
+            continue;
 
-	  peer->ttl = 1;
-	  
-	  if (peer->fd >= 0)
-	    sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl);
-	}
+          peer->ttl = 1;
+          
+          if (*peer->fd >= 0)
+            sockopt_ttl (peer->su.sa.sa_family,
+                         *peer->fd, peer->ttl);
+        }
     }
   return 0;
 }
diff --git a/bgpd/bgpd.h b/bgpd/bgpd.h
index 498bd07..a8bfa52 100644
--- a/bgpd/bgpd.h
+++ b/bgpd/bgpd.h
@@ -256,27 +256,29 @@
   int ostatus;
 
   /* Peer information */
-  int fd;			/* File descriptor */
-  int ttl;			/* TTL of TCP connection to the peer. */
-  char *desc;			/* Description of the peer. */
-  unsigned short port;          /* Destination port for peer */
-  char *host;			/* Printable address of the peer. */
-  union sockunion su;		/* Sockunion address of the peer. */
-  time_t uptime;		/* Last Up/Down time */
-  time_t readtime;		/* Last read time */
-  time_t resettime;		/* Last reset time */
+  int *fd;                        /* connection in use: -> local||accept */
+  int ttl;                        /* TTL of TCP connection to the peer. */
+  int fd_local;                   /* locally initiated connection */
+  int fd_accept;                  /* remote initiated/accepted connection */
+  char *desc;                     /* Description of the peer. */
+  unsigned short port;            /* Destination port for peer */
+  char *host;                     /* Printable address of the peer. */
+  union sockunion su;             /* Sockunion address of the peer. */
+  time_t uptime;                  /* Last Up/Down time */
+  time_t readtime;                /* Last read time */
+  time_t resettime;               /* Last reset time */
   
-  unsigned int ifindex;		/* ifindex of the BGP connection. */
-  char *ifname;			/* bind interface name. */
-  char *update_if;
-  union sockunion *update_source;
-  struct zlog *log;
-  u_char version;		/* Peer BGP version. */
+  unsigned int ifindex;           /* ifindex of the BGP connection. */
+  char *ifname;                   /* bind interface name. */
+  char *update_if;                /* interface to send from */
+  union sockunion *update_source; /* sockunion to send from */
+  struct zlog *log;               /* log socket */
+  u_char version;                 /* Peer BGP version. */
 
-  union sockunion *su_local;	/* Sockunion of local address.  */
-  union sockunion *su_remote;	/* Sockunion of remote address.  */
-  int shared_network;		/* Is this peer shared same network. */
-  struct bgp_nexthop nexthop;	/* Nexthop */
+  union sockunion *su_local;      /* Sockunion of local address.  */
+  union sockunion *su_remote;     /* Sockunion of remote address.  */
+  int shared_network;             /* Is this peer shared same network. */
+  struct bgp_nexthop nexthop;     /* Nexthop */
 
   /* Peer address family configuration. */
   u_char afc[AFI_MAX][SAFI_MAX];