Module Name:    src
Committed By:   plunky
Date:           Tue May 12 21:08:30 UTC 2009

Modified Files:
        src/usr.sbin/btpand: bnep.c btpand.c btpand.h channel.c client.c
            server.c tap.c

Log Message:
rework the channel shutdown code path, each channel now has its own
action to process which separates the server and client code.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/usr.sbin/btpand/bnep.c
cvs rdiff -u -r1.2 -r1.3 src/usr.sbin/btpand/btpand.c \
    src/usr.sbin/btpand/channel.c src/usr.sbin/btpand/client.c \
    src/usr.sbin/btpand/server.c
cvs rdiff -u -r1.1 -r1.2 src/usr.sbin/btpand/btpand.h
cvs rdiff -u -r1.3 -r1.4 src/usr.sbin/btpand/tap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/btpand/bnep.c
diff -u src/usr.sbin/btpand/bnep.c:1.7 src/usr.sbin/btpand/bnep.c:1.8
--- src/usr.sbin/btpand/bnep.c:1.7	Tue May 12 19:57:59 2009
+++ src/usr.sbin/btpand/bnep.c	Tue May 12 21:08:30 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: bnep.c,v 1.7 2009/05/12 19:57:59 plunky Exp $	*/
+/*	$NetBSD: bnep.c,v 1.8 2009/05/12 21:08:30 plunky Exp $	*/
 
 /*-
  * Copyright (c) 2008 Iain Hibbert
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: bnep.c,v 1.7 2009/05/12 19:57:59 plunky Exp $");
+__RCSID("$NetBSD: bnep.c,v 1.8 2009/05/12 21:08:30 plunky Exp $");
 
 #include <bluetooth.h>
 #include <sdp.h>
@@ -293,8 +293,8 @@
 	type = *ptr++;
 	log_err("received Control Command Not Understood (0x%2.2x)", type);
 
-	/* we didn't send any reserved commands, just cut them off */
-	channel_close(chan);
+	/* we didn't send any reserved commands, just shut them down */
+	chan->down(chan);
 
 	return 1;
 }
@@ -395,7 +395,7 @@
 		chan->state = CHANNEL_OPEN;
 		channel_timeout(chan, 0);
 	} else {
-		channel_close(chan);
+		chan->down(chan);
 	}
 
 	return 2;

Index: src/usr.sbin/btpand/btpand.c
diff -u src/usr.sbin/btpand/btpand.c:1.2 src/usr.sbin/btpand/btpand.c:1.3
--- src/usr.sbin/btpand/btpand.c:1.2	Wed Apr 15 00:38:07 2009
+++ src/usr.sbin/btpand/btpand.c	Tue May 12 21:08:30 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: btpand.c,v 1.2 2009/04/15 00:38:07 lukem Exp $	*/
+/*	$NetBSD: btpand.c,v 1.3 2009/05/12 21:08:30 plunky Exp $	*/
 
 /*-
  * Copyright (c) 2008 Iain Hibbert
@@ -27,7 +27,7 @@
 
 #include <sys/cdefs.h>
 __COPYRIGHT("@(#) Copyright (c) 2008 Iain Hibbert. All rights reserved.");
-__RCSID("$NetBSD: btpand.c,v 1.2 2009/04/15 00:38:07 lukem Exp $");
+__RCSID("$NetBSD: btpand.c,v 1.3 2009/05/12 21:08:30 plunky Exp $");
 
 #include <sys/wait.h>
 
@@ -204,8 +204,8 @@
 		openlog(getprogname(), LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_DAEMON);
 
 		channel_init();
-		server_init();
 		event_init();
+		server_init();
 		client_init();
 		tap_init();
 
Index: src/usr.sbin/btpand/channel.c
diff -u src/usr.sbin/btpand/channel.c:1.2 src/usr.sbin/btpand/channel.c:1.3
--- src/usr.sbin/btpand/channel.c:1.2	Sat May  2 20:07:51 2009
+++ src/usr.sbin/btpand/channel.c	Tue May 12 21:08:30 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: channel.c,v 1.2 2009/05/02 20:07:51 plunky Exp $	*/
+/*	$NetBSD: channel.c,v 1.3 2009/05/12 21:08:30 plunky Exp $	*/
 
 /*-
  * Copyright (c) 2008 Iain Hibbert
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: channel.c,v 1.2 2009/05/02 20:07:51 plunky Exp $");
+__RCSID("$NetBSD: channel.c,v 1.3 2009/05/12 21:08:30 plunky Exp $");
 
 #include <sys/ioctl.h>
 
@@ -35,7 +35,6 @@
 #include "btpand.h"
 
 static struct chlist	channel_list;
-static int		channel_count;
 static int		channel_tick;
 
 static void channel_start(int, short, void *);
@@ -66,8 +65,6 @@
 	chan->state = CHANNEL_CLOSED;
 	LIST_INSERT_HEAD(&channel_list, chan, next);
 
-	server_update(++channel_count);
-
 	return chan;
 }
 
@@ -78,6 +75,9 @@
 
 	assert(chan->refcnt == 0);
 	assert(chan->state != CHANNEL_CLOSED);
+	assert(chan->send != NULL);
+	assert(chan->recv != NULL);
+	assert(chan->down != NULL);
 
 	if (chan->mtu > 0) {
 		chan->sendbuf = malloc(chan->mtu);
@@ -149,13 +149,6 @@
 	free(chan->mfilter);
 	free(chan->sendbuf);
 	free(chan);
-
-	server_update(--channel_count);
-
-	if (server_limit == 0) {
-		log_info("connection closed, exiting");
-		exit(EXIT_SUCCESS);
-	}
 }
 
 static void
@@ -173,7 +166,7 @@
 		if (chan->send(chan, ph->data) == false) {
 			if (event_add(&chan->wr_ev, NULL) == -1) {
 				log_err("Could not add channel write event: %m");
-				channel_close(chan);
+				chan->down(chan);
 			}
 			return;
 		}
@@ -196,7 +189,7 @@
 
 	pkt = packet_alloc(chan);
 	if (pkt == NULL) {
-		channel_close(chan);
+		chan->down(chan);
 		return;
 	}
 
@@ -204,13 +197,13 @@
 	if (nr == -1) {
 		log_err("channel read error: %m");
 		packet_free(pkt);
-		channel_close(chan);
+		chan->down(chan);
 		return;
 	}
 	if (nr == 0) {	/* EOF */
 		log_debug("(fd#%d) EOF", fd);
 		packet_free(pkt);
-		channel_close(chan);
+		chan->down(chan);
 		return;
 	}
 	pkt->len = nr;
@@ -314,7 +307,7 @@
 		next = LIST_NEXT(chan, next);
 
 		if (chan->tick == tick)
-			channel_close(chan);
+			chan->down(chan);
 		else if (chan->tick != 0)
 			channel_tick = tick;
 	}
Index: src/usr.sbin/btpand/client.c
diff -u src/usr.sbin/btpand/client.c:1.2 src/usr.sbin/btpand/client.c:1.3
--- src/usr.sbin/btpand/client.c:1.2	Sat Dec  6 20:01:14 2008
+++ src/usr.sbin/btpand/client.c	Tue May 12 21:08:30 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: client.c,v 1.2 2008/12/06 20:01:14 plunky Exp $	*/
+/*	$NetBSD: client.c,v 1.3 2009/05/12 21:08:30 plunky Exp $	*/
 
 /*-
  * Copyright (c) 2008 Iain Hibbert
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: client.c,v 1.2 2008/12/06 20:01:14 plunky Exp $");
+__RCSID("$NetBSD: client.c,v 1.3 2009/05/12 21:08:30 plunky Exp $");
 
 #include <bluetooth.h>
 #include <errno.h>
@@ -37,6 +37,7 @@
 #include "bnep.h"
 #include "sdp.h"
 
+static void client_down(channel_t *);
 static void client_query(void);
 
 void
@@ -118,6 +119,7 @@
 
 	chan->send = bnep_send;
 	chan->recv = bnep_recv;
+	chan->down = client_down;
 	chan->mru = mru;
 	chan->mtu = mtu;
 	b2eaddr(chan->raddr, &remote_bdaddr);
@@ -132,6 +134,14 @@
 }
 
 static void
+client_down(channel_t *chan)
+{
+
+	log_err("Client connection shut down, exiting");
+	exit(EXIT_FAILURE);
+}
+
+static void
 client_query(void)
 {
 	uint8_t buffer[512];
Index: src/usr.sbin/btpand/server.c
diff -u src/usr.sbin/btpand/server.c:1.2 src/usr.sbin/btpand/server.c:1.3
--- src/usr.sbin/btpand/server.c:1.2	Sat Jan 24 17:29:28 2009
+++ src/usr.sbin/btpand/server.c	Tue May 12 21:08:30 2009
@@ -1,7 +1,7 @@
-/*	$NetBSD: server.c,v 1.2 2009/01/24 17:29:28 plunky Exp $	*/
+/*	$NetBSD: server.c,v 1.3 2009/05/12 21:08:30 plunky Exp $	*/
 
 /*-
- * Copyright (c) 2008 Iain Hibbert
+ * Copyright (c) 2008-2009 Iain Hibbert
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: server.c,v 1.2 2009/01/24 17:29:28 plunky Exp $");
+__RCSID("$NetBSD: server.c,v 1.3 2009/05/12 21:08:30 plunky Exp $");
 
 #include <sys/ioctl.h>
 
@@ -39,59 +39,39 @@
 #include "bnep.h"
 
 static struct event	server_ev;
-static int		server_fd;
-static int		server_avail;
+static int		server_count;
 
 static void *		server_ss;
 static uint32_t		server_handle;
 
 static void server_open(void);
-static void server_close(void);
 static void server_read(int, short, void *);
-static void server_register(void);
+static void server_down(channel_t *);
+static void server_update(void);
 
 void
 server_init(void)
 {
 
-	server_fd = -1;
-}
-
-/*
- * The server_update() function is called whenever the channel count is
- * changed. We maintain the SDP record and open or close the server socket
- * as required.
- */
-void
-server_update(int count)
-{
-
 	if (server_limit == 0)
 		return;
 
-	log_debug("count %d", count);
-
-	server_avail = UINT8_MAX - (count - 1) * UINT8_MAX / server_limit;
-	log_info("Service Availability: %d/%d", server_avail, UINT8_MAX);
-
-	if (server_avail == 0 && server_fd != -1)
-		server_close();
-
-	if (server_avail > 0 && server_fd == -1)
-		server_open();
-
-	if (service_name)
-		server_register();
+	server_open();
+	server_update();
 }
 
+/*
+ * Start listening on server socket
+ */
 static void
 server_open(void)
 {
 	struct sockaddr_bt sa;
 	uint16_t mru;
+	int fd;
 
-	server_fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
-	if (server_fd == -1) {
+	fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
+	if (fd == -1) {
 		log_err("Could not open L2CAP socket: %m");
 		exit(EXIT_FAILURE);
 	}
@@ -101,30 +81,30 @@
 	sa.bt_len = sizeof(sa);
 	sa.bt_psm = l2cap_psm;
 	bdaddr_copy(&sa.bt_bdaddr, &local_bdaddr);
-	if (bind(server_fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
+	if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
 		log_err("Could not bind server socket: %m");
 		exit(EXIT_FAILURE);
 	}
 
-	if (setsockopt(server_fd, BTPROTO_L2CAP,
+	if (setsockopt(fd, BTPROTO_L2CAP,
 	    SO_L2CAP_LM, &l2cap_mode, sizeof(l2cap_mode)) == -1) {
 		log_err("Could not set link mode (0x%4.4x): %m", l2cap_mode);
 		exit(EXIT_FAILURE);
 	}
 
 	mru = BNEP_MTU_MIN;
-	if (setsockopt(server_fd, BTPROTO_L2CAP,
+	if (setsockopt(fd, BTPROTO_L2CAP,
 	    SO_L2CAP_IMTU, &mru, sizeof(mru)) == -1) {
 		log_err("Could not set L2CAP IMTU (%d): %m", mru);
 		exit(EXIT_FAILURE);
 	}
 
-	if (listen(server_fd, 0) == -1) {
+	if (listen(fd, 0) == -1) {
 		log_err("Could not listen on server socket: %m");
 		exit(EXIT_FAILURE);
 	}
 
-	event_set(&server_ev, server_fd, EV_READ | EV_PERSIST, server_read, NULL);
+	event_set(&server_ev, fd, EV_READ | EV_PERSIST, server_read, NULL);
 	if (event_add(&server_ev, NULL) == -1) {
 		log_err("Could not add server event: %m");
 		exit(EXIT_FAILURE);
@@ -133,17 +113,6 @@
 	log_info("server socket open");
 }
 
-static void
-server_close(void)
-{
-
-	event_del(&server_ev);
-	close(server_fd);
-	server_fd = -1;
-
-	log_info("server socket closed");
-}
-
 /*
  * handle connection request
  */
@@ -156,6 +125,8 @@
 	int fd, n;
 	uint16_t mru, mtu;
 
+	assert(server_count < server_limit);
+
 	len = sizeof(ra);
 	fd = accept(s, (struct sockaddr *)&ra, &len);
 	if (fd == -1)
@@ -232,6 +203,7 @@
 
 	chan->send = bnep_send;
 	chan->recv = bnep_recv;
+	chan->down = server_down;
 	chan->mru = mru;
 	chan->mtu = mtu;
 	b2eaddr(chan->raddr, &ra.bt_bdaddr);
@@ -244,14 +216,43 @@
 		close(fd);
 		return;
 	}
+
+	if (++server_count == server_limit) {
+		log_info("Server limit reached, closing server socket");
+		event_del(&server_ev);
+		close(s);
+	}
+
+	server_update();
+}
+
+/*
+ * Shut down a server channel, we need to update the service record and
+ * may want to restart accepting connections on the server socket
+ */
+static void
+server_down(channel_t *chan)
+{
+
+	assert(server_count > 0);
+
+	channel_close(chan);
+
+	if (server_count-- == server_limit)
+		server_open();
+
+	server_update();
 }
 
 static void
-server_register(void)
+server_update(void)
 {
 	sdp_nap_profile_t p;
 	int rv;
 
+	if (service_name == NULL)
+		return;
+
 	if (server_ss == NULL) {
 		server_ss = sdp_open_local(control_path);
 		if (server_ss == NULL || sdp_error(server_ss) != 0) {
@@ -262,7 +263,7 @@
 
 	memset(&p, 0, sizeof(p));
 	p.psm = l2cap_psm;
-	p.load_factor = server_avail;
+	p.load_factor = (UINT8_MAX - server_count * UINT8_MAX / server_limit);
 	p.security_description = (l2cap_mode == 0 ? 0x0000 : 0x0001);
 
 	if (server_handle)

Index: src/usr.sbin/btpand/btpand.h
diff -u src/usr.sbin/btpand/btpand.h:1.1 src/usr.sbin/btpand/btpand.h:1.2
--- src/usr.sbin/btpand/btpand.h:1.1	Sun Aug 17 13:20:57 2008
+++ src/usr.sbin/btpand/btpand.h	Tue May 12 21:08:30 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: btpand.h,v 1.1 2008/08/17 13:20:57 plunky Exp $	*/
+/*	$NetBSD: btpand.h,v 1.2 2009/05/12 21:08:30 plunky Exp $	*/
 
 /*-
  * Copyright (c) 2008 Iain Hibbert
@@ -32,8 +32,8 @@
 #include <net/if_ether.h>
 
 #include <assert.h>
-#include <event.h>
 #include <bluetooth.h>
+#include <event.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <string.h>
@@ -87,6 +87,7 @@
 
 	bool			(*send)(channel_t *, packet_t *);
 	bool			(*recv)(packet_t *);
+	void			(*down)(channel_t *);
 
 	int			tick;
 
@@ -188,7 +189,6 @@
 
 /* server.c */
 void		server_init(void);
-void		server_update(int);
 
 /* tap.c */
 void		tap_init(void);

Index: src/usr.sbin/btpand/tap.c
diff -u src/usr.sbin/btpand/tap.c:1.3 src/usr.sbin/btpand/tap.c:1.4
--- src/usr.sbin/btpand/tap.c:1.3	Sat May  2 20:07:51 2009
+++ src/usr.sbin/btpand/tap.c	Tue May 12 21:08:30 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: tap.c,v 1.3 2009/05/02 20:07:51 plunky Exp $	*/
+/*	$NetBSD: tap.c,v 1.4 2009/05/12 21:08:30 plunky Exp $	*/
 
 /*-
  * Copyright (c) 2008 Iain Hibbert
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: tap.c,v 1.3 2009/05/02 20:07:51 plunky Exp $");
+__RCSID("$NetBSD: tap.c,v 1.4 2009/05/12 21:08:30 plunky Exp $");
 
 #include <sys/ioctl.h>
 #include <sys/uio.h>
@@ -42,6 +42,7 @@
 
 static bool tap_send(channel_t *, packet_t *);
 static bool tap_recv(packet_t *);
+static void tap_down(channel_t *);
 
 void
 tap_init(void)
@@ -110,6 +111,7 @@
 
 	chan->send = tap_send;
 	chan->recv = tap_recv;
+	chan->down = tap_down;
 	chan->mru = ETHER_HDR_LEN + ETHER_MAX_LEN;
 	memcpy(chan->raddr, LLADDR(sdl), ETHER_ADDR_LEN);
 	memcpy(chan->laddr, LLADDR(sdl), ETHER_ADDR_LEN);
@@ -159,3 +161,10 @@
 
 	return true;
 }
+
+static void
+tap_down(channel_t *chan)
+{
+
+	/* we never close the tap channel */
+}

Reply via email to