Skip to contentSkip to conversation

alfred: support for changing interfaces

PATCH In review Simon Wunderlich 1 participant 2 messages Sep 3, 2014

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 alfred: support for changing interfaces

From: Simon Wunderlich <[email protected]>

This patch adds support for changing the interface alfreds listens to on the fly without restarting alfred.

---
Changes to PATCH:
* fix const warning

---
 alfred.h     |  4 +++-
 client.c     | 54 +++++++++++++++++++++++++++++++++++++++++++++++++
 main.c       | 66 +++++++++++++++++++++++++++++++++++-------------------------
 man/alfred.8 |  3 +++
 packet.h     | 17 ++++++++++++++++
 unix_sock.c  | 38 ++++++++++++++++++++++++++++++++++
 vis/vis.c    |  2 +-
 7 files changed, 155 insertions(+), 29 deletions(-)
alfred.h +3 -1
index 35ac4dd..c83f7f0 100644
@@ -86,6 +86,7 @@ enum clientmode {
 	CLIENT_REQUEST_DATA,
 	CLIENT_SET_DATA,
 	CLIENT_MODESWITCH,
+	CLIENT_CHANGE_INTERFACE,
 };
 
 struct globals {
@@ -93,7 +94,7 @@ struct globals {
 	struct in6_addr address;
 	uint32_t scope_id;
 	struct server *best_server;	/* NULL if we are a server ourselves */
-	const char *interface;
+	char *interface;
 	const char *mesh_iface;
 	enum opmode opmode;
 	enum clientmode clientmode;
@@ -125,6 +126,7 @@ int set_best_server(struct globals *globals);
 int alfred_client_request_data(struct globals *globals);
 int alfred_client_set_data(struct globals *globals);
 int alfred_client_modeswitch(struct globals *globals);
+int alfred_client_change_interface(struct globals *globals);
 /* recv.c */
 int recv_alfred_packet(struct globals *globals);
 struct transaction_head *
client.c +54 -0

63 lines not on this page · raw message

main.c +39 -27

99 lines not on this page · raw message

man/alfred.8 +3 -0
index c90caa8..35dee45 100644
@@ -80,6 +80,9 @@ to 0 ('\fB\-V\fP 0').
 \fBslave\fP
     Switch daemon to mode slave
 .fi
+.TP
+\fB\-I\fP, \fB\-\-change\-interface\fP \fIinterface\fP
+Change the alfred server to use the new \fBinterface\fP
 .
 .SH SERVER OPTIONS
 .TP
packet.h +17 -0
index ba8c5f6..dc06d65 100644
@@ -20,6 +20,8 @@
 #ifndef _ALFRED_PACKET_H
 #define _ALFRED_PACKET_H
 
+#include <net/if.h>	/* IFNAMSIZ */
+
 #define __packed __attribute__ ((packed))
 
 /* basic blocks */
@@ -67,6 +69,7 @@ struct alfred_transaction_mgmt {
  * @ALFRED_STATUS_TXEND: Transaction was finished by sender
  * @ALFRED_STATUS_ERROR: Error was detected during the transaction
  * @ALFRED_MODESWITCH: Switch between different operation modes
+ * @ALFRED_CHANGE_INTERFACE: Change the listening interface
  */
 enum alfred_packet_type {
 	ALFRED_PUSH_DATA = 0,
@@ -75,6 +78,7 @@ enum alfred_packet_type {
 	ALFRED_STATUS_TXEND = 3,
 	ALFRED_STATUS_ERROR = 4,
 	ALFRED_MODESWITCH = 5,
+	ALFRED_CHANGE_INTERFACE = 6,
 };
 
 /* packets */
@@ -143,6 +147,19 @@ struct alfred_modeswitch_v0 {
 } __packed;
 
 /**
+ * struct alfred_change_interface_v0 - Request to change the interface
+ * @header: TLV header describing the complete packet
+ * @iface: interface name to be changed to
+ *
+ * Sent to the daemon by client
+ */
+struct alfred_change_interface_v0 {
+	struct alfred_tlv header;
+	char iface[IFNAMSIZ];
+} __packed;
+
+
+/**
  * struct alfred_status_v0 - Status info of a transaction
  * @header: TLV header describing the complete packet
  * @tx: Transaction identificator and sequence number of packet
unix_sock.c +38 -0
index cc0d25c..516c0a5 100644
@@ -298,6 +298,38 @@ err:
 	return ret;
 }
 
+static int
+unix_sock_change_iface(struct globals *globals,
+		       struct alfred_change_interface_v0 *change_iface,
+		       int client_sock)
+{
+	int len, ret = -1;
+	char *iface;
+
+	len = ntohs(change_iface->header.length);
+
+	if (len < (int)(sizeof(*change_iface) - sizeof(change_iface->header)))
+		goto err;
+
+	iface = malloc(IFNAMSIZ + 1);
+	if (!iface)
+		goto err;
+
+	memcpy(iface, change_iface->iface, IFNAMSIZ);
+	iface[IFNAMSIZ] = 0;
+
+	netsock_close(globals->netsock);
+
+	free(globals->interface);
+	globals->interface = iface;
+	netsock_open(globals);
+
+	ret = 0;
+err:
+	close(client_sock);
+	return ret;
+}
+
 int unix_sock_read(struct globals *globals)
 {
 	int client_sock;
@@ -350,6 +382,12 @@ int unix_sock_read(struct globals *globals)
 				       (struct alfred_modeswitch_v0 *)packet,
 					client_sock);
 		break;
+	case ALFRED_CHANGE_INTERFACE:
+		ret = unix_sock_change_iface(globals,
+					     (struct alfred_change_interface_v0 *)packet,
+					     client_sock);
+		break;
+
 	default:
 		/* unknown packet type */
 		ret = -1;
vis/vis.c +1 -1
index 00372ab..3dd5c8c 100644
@@ -23,7 +23,7 @@
 #include <dirent.h>
 #include <errno.h>
 #include <getopt.h>
-#include <linux/if.h>
+#include <net/if.h>
 #include <netinet/in.h>
 #include <signal.h>
 #include <stdbool.h>
Signature · 1 line
2.1.0