2007-10-30 Nick Hilliard <nick@inex.ie>

	* bgp_main.c: Add 'listenon' argument, to pass address to bind
	  to.
	* bgp_network.c: (bgp_socket) Extend to take bind address.
	* bgpd.c: (bgp_init) Pass stored address.
	* bgpd.h: (struct bgp_master) storage for bind address

2007-11-01 Paul Jakma <paul.jakma@sun.com>

	* tools/multiple-bgpd.sh: New, quick script to launch a bunch of
	  bgpds.
diff --git a/bgpd/ChangeLog b/bgpd/ChangeLog
index 5a31d9c..0fdd36a 100644
--- a/bgpd/ChangeLog
+++ b/bgpd/ChangeLog
@@ -1,3 +1,11 @@
+2007-10-30 Nick Hilliard <nick@inex.ie>
+
+	* bgp_main.c: Add 'listenon' argument, to pass address to bind
+	  to.
+	* bgp_network.c: (bgp_socket) Extend to take bind address.
+	* bgpd.c: (bgp_init) Pass stored address.
+	* bgpd.h: (struct bgp_master) storage for bind address
+
 2007-09-27 Paul Jakma <paul.jakma@sun.com>
 
 	* bgp_aspath.c: (assegment_normalise) remove duplicates from
diff --git a/bgpd/bgp_main.c b/bgpd/bgp_main.c
index ecfe62e..843b124 100644
--- a/bgpd/bgp_main.c
+++ b/bgpd/bgp_main.c
@@ -43,6 +43,7 @@
   { "config_file", required_argument, NULL, 'f'},
   { "pid_file",    required_argument, NULL, 'i'},
   { "bgp_port",    required_argument, NULL, 'p'},
+  { "listenon",    required_argument, NULL, 'l'},
   { "vty_addr",    required_argument, NULL, 'A'},
   { "vty_port",    required_argument, NULL, 'P'},
   { "retain",      no_argument,       NULL, 'r'},
@@ -135,6 +136,7 @@
 -f, --config_file  Set configuration file name\n\
 -i, --pid_file     Set process identifier file name\n\
 -p, --bgp_port     Set bgp protocol's port number\n\
+-l, --listenon     Listen on specified address (implies -n)\n\
 -A, --vty_addr     Set vty's bind address\n\
 -P, --vty_port     Set vty's port number\n\
 -r, --retain       When program terminates, retain added route by bgpd.\n\
@@ -217,7 +219,7 @@
   /* Command line argument treatment. */
   while (1) 
     {
-      opt = getopt_long (argc, argv, "df:i:hp:A:P:rnu:g:vC", longopts, 0);
+      opt = getopt_long (argc, argv, "df:i:hp:l:A:P:rnu:g:vC", longopts, 0);
     
       if (opt == EOF)
 	break;
@@ -255,6 +257,9 @@
 	case 'r':
 	  retain_mode = 1;
 	  break;
+	case 'l':
+	  bm->address = optarg;
+	  /* listenon implies -n */
 	case 'n':
 	  bgp_option_set (BGP_OPT_NO_FIB);
 	  break;
@@ -315,8 +320,8 @@
   vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
 
   /* Print banner. */
-  zlog_notice ("BGPd %s starting: vty@%d, bgp@%d", QUAGGA_VERSION,
-	       vty_port, bm->port);
+  zlog_notice ("BGPd %s starting: vty@%d, bgp@%s:%d", QUAGGA_VERSION,
+	       vty_port, bm->address, bm->port);
 
   /* Start finite state machine, here we go! */
   while (thread_fetch (master, &thread))
diff --git a/bgpd/bgp_network.c b/bgpd/bgp_network.c
index 8040e47..6b8c8a4 100644
--- a/bgpd/bgp_network.c
+++ b/bgpd/bgp_network.c
@@ -282,7 +282,7 @@
 /* IPv6 supported version of BGP server socket setup.  */
 #if defined (HAVE_IPV6) && ! defined (NRL)
 int
-bgp_socket (struct bgp *bgp, unsigned short port)
+bgp_socket (struct bgp *bgp, unsigned short port, char *address)
 {
   int ret, en;
   struct addrinfo req;
@@ -299,7 +299,7 @@
   sprintf (port_str, "%d", port);
   port_str[sizeof (port_str) - 1] = '\0';
 
-  ret = getaddrinfo (NULL, port_str, &req, &ainfo);
+  ret = getaddrinfo (address, port_str, &req, &ainfo);
   if (ret != 0)
     {
       zlog_err ("getaddrinfo: %s", gai_strerror (ret));
@@ -357,7 +357,7 @@
 #else
 /* Traditional IPv4 only version.  */
 int
-bgp_socket (struct bgp *bgp, unsigned short port)
+bgp_socket (struct bgp *bgp, unsigned short port, char *address)
 {
   int sock;
   int socklen;
@@ -379,6 +379,14 @@
   sin.sin_family = AF_INET;
   sin.sin_port = htons (port);
   socklen = sizeof (struct sockaddr_in);
+
+  ret = inet_aton(address, &sin.sin_addr);
+
+  if (ret < 1)
+    {
+      zlog_err("bgp_socket: could not parse ip address %s: ", address, safe_strerror (errno));
+      return ret;
+    }
 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
   sin.sin_len = socklen;
 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
diff --git a/bgpd/bgp_network.h b/bgpd/bgp_network.h
index ddf050d..b455f57 100644
--- a/bgpd/bgp_network.h
+++ b/bgpd/bgp_network.h
@@ -21,7 +21,7 @@
 #ifndef _QUAGGA_BGP_NETWORK_H
 #define _QUAGGA_BGP_NETWORK_H
 
-extern int bgp_socket (struct bgp *, unsigned short);
+extern int bgp_socket (struct bgp *, unsigned short, char *);
 extern int bgp_connect (struct peer *);
 extern void bgp_getsockname (struct peer *);
 
diff --git a/bgpd/bgpd.c b/bgpd/bgpd.c
index 9852d65..374c4c5 100644
--- a/bgpd/bgpd.c
+++ b/bgpd/bgpd.c
@@ -4961,7 +4961,7 @@
   bgp_vty_init ();
 
   /* Create BGP server socket.  */
-  bgp_socket (NULL, bm->port);
+  bgp_socket (NULL, bm->port, bm->address);
 
   /* Init zebra. */
   bgp_zebra_init ();
diff --git a/bgpd/bgpd.h b/bgpd/bgpd.h
index e04575d..e9fc410 100644
--- a/bgpd/bgpd.h
+++ b/bgpd/bgpd.h
@@ -45,6 +45,9 @@
   /* BGP port number.  */
   u_int16_t port;
 
+  /* Listener address */
+  char *address;
+
   /* BGP start time.  */
   time_t start_time;
 
diff --git a/tools/multiple-bgpd.sh b/tools/multiple-bgpd.sh
new file mode 100644
index 0000000..4097754
--- /dev/null
+++ b/tools/multiple-bgpd.sh
@@ -0,0 +1,55 @@
+#!/bin/bash
+
+# Public domain, not copyrighted..
+
+NUM=5
+VTYBASE=2610
+ASBASE=64560
+BGPD=/path/to/bgpd
+PREFIX=192.168.145
+
+for H in `seq 1 ${NUM}` ; do
+	CONF=/etc/quagga/bgpd${H}.conf
+	ADDR=${PREFIX}.${H}
+	
+	if [ ! -e "$CONF" ] ; then
+		# This sets up a ring of bgpd peerings
+		NEXT=$(( ($H % ${NUM}) + 1 ))
+		PREV=$(( (($H + 3) % ${NUM}) + 1 ))
+		NEXTADDR="${PREFIX}.${NEXT}"
+		NEXTAS=$((${ASBASE} + $NEXT))
+		PREVADDR="${PREFIX}.${PREV}"
+		PREVAS=$((${ASBASE} + $PREV))
+		
+		# Edit config to suit.
+		cat > "$CONF" <<- EOF
+			password whatever
+			service advanced-vty
+			!
+			router bgp $((64560+${H}))
+			 bgp router-id ${ADDR}
+			 network 10.${H}.1.0/24 pathlimit 1
+			 network 10.${H}.2.0/24 pathlimit 2
+			 network 10.${H}.3.0/24 pathlimit 3
+			 neighbor default peer-group
+			 neighbor default update-source ${ADDR}
+			 neighbor default capability orf prefix-list both
+			 neighbor default soft-reconfiguration inbound
+			 neighbor ${NEXTADDR} remote-as ${NEXTAS}
+			 neighbor ${NEXTADDR} peer-group default
+			 neighbor ${PREVADDR} remote-as ${PREVAS}
+			 neighbor ${PREVADDR} peer-group default
+		EOF
+		chown quagga:quagga "$CONF"
+	fi
+	# You may want to automatically add configure a local address
+	# on a loop interface.
+	#
+	# Solaris: ifconfig vni${H} plumb ${ADDR}/32 up
+	# Linux:   ip address add ${ADDR}/32 dev lo 2> /dev/null
+	${BGPD} -i /var/run/quagga/bgpd${H}.pid \
+		-l ${ADDR} \
+		-f /etc/quagga/bgpd${H}.conf \
+		-P $((${VTYBASE}+${H})) \
+		-d
+done