jpopelka / rpms / net-tools

Forked from rpms/net-tools 3 years ago
Clone

Blame SOURCES/ether-wake-interfaces.patch

5aa31a
--- a/ether-wake.c	2013-09-03 18:15:13.000000000 +0200
5aa31a
+++ b/ether-wake.c	2013-09-03 17:39:02.000000000 +0200
5aa31a
@@ -22,7 +22,7 @@
5aa31a
 "	Options:\n"
5aa31a
 "		-b	Send wake-up packet to the broadcast address.\n"
5aa31a
 "		-D	Increase the debug level.\n"
5aa31a
-"		-i ifname	Use interface IFNAME instead of the default 'eth0'.\n"
5aa31a
+"		-i ifname	Use interface ifname instead of sending a wake packet to all interfaces.\n"
5aa31a
 "		-p <pw>		Append the four or six byte password PW to the packet.\n"
5aa31a
 "					A password is only required for a few adapter types.\n"
5aa31a
 "					The password may be specified in ethernet hex format\n"
5aa31a
@@ -89,6 +89,9 @@
5aa31a
 #include <netdb.h>
5aa31a
 #include <netinet/ether.h>
5aa31a
 
5aa31a
+#include "interface.h"
5aa31a
+#include "sockets.h"
5aa31a
+
5aa31a
 /* Grrr, no consistency between include versions.
5aa31a
    Enable this if setsockopt() isn't declared with your library. */
5aa31a
 #if 0
5aa31a
@@ -110,20 +113,29 @@
5aa31a
 static int get_fill(unsigned char *pkt, struct ether_addr *eaddr);
5aa31a
 static int get_wol_pw(const char *optarg);
5aa31a
 
5aa31a
+typedef struct {
5aa31a
+	int s;
5aa31a
+	int verbose;
5aa31a
+	int pktsize;
5aa31a
+} if_info;
5aa31a
+
5aa31a
+static int send_wol_packet(char *ifname, int s, int verbose, int pktsize);
5aa31a
+
5aa31a
+static int do_wake(struct interface *ife, void *cookie) {
5aa31a
+	if_info *info = (if_info *)cookie;
5aa31a
+	send_wol_packet(ife->name, info->s, info->verbose, info->pktsize);
5aa31a
+	return 0;
5aa31a
+}
5aa31a
+
5aa31a
 int main(int argc, char *argv[])
5aa31a
 {
5aa31a
-	char *ifname = "eth0";
5aa31a
-	int one = 1;				/* True, for socket options. */
5aa31a
+	char *ifname = NULL;
5aa31a
 	int s;						/* Raw socket */
5aa31a
 	int errflag = 0, verbose = 0, do_version = 0;
5aa31a
 	int perm_failure = 0;
5aa31a
-	int i, c, pktsize;
5aa31a
-#if defined(PF_PACKET)
5aa31a
-	struct sockaddr_ll whereto;
5aa31a
-#else
5aa31a
-	struct sockaddr whereto;	/* who to wake up */
5aa31a
-#endif
5aa31a
+	int c, pktsize;
5aa31a
 	struct ether_addr eaddr;
5aa31a
+	if_info info;
5aa31a
 
5aa31a
 	while ((c = getopt(argc, argv, "bDi:p:uvV")) != -1)
5aa31a
 		switch (c) {
5aa31a
@@ -177,13 +189,45 @@
5aa31a
 
5aa31a
 	pktsize = get_fill(outpack, &eaddr);
5aa31a
 
5aa31a
+	if (ifname == NULL) {
5aa31a
+		info.s = s;
5aa31a
+		info.verbose = verbose;
5aa31a
+		info.pktsize = pktsize;
5aa31a
+
5aa31a
+		/* Create a channel to the NET kernel. */
5aa31a
+		if ((sockets_open(0)) < 0) {
5aa31a
+			perror("socket");
5aa31a
+			exit(1);
5aa31a
+		}
5aa31a
+
5aa31a
+		return for_all_interfaces(do_wake, &info;;
5aa31a
+	}
5aa31a
+
5aa31a
+	return send_wol_packet(ifname, s, verbose, pktsize);
5aa31a
+}
5aa31a
+
5aa31a
+/* Send a Wake-On-LAN (WOL) "Magic Packet" to Interface IFNAME using
5aa31a
+   Socket S with a packet size PKTSIZE.  VERBOSE implies
5aa31a
+   verbosity.  */
5aa31a
+
5aa31a
+static int send_wol_packet(char *ifname, int s, int verbose, int pktsize)
5aa31a
+{
5aa31a
+	int i;
5aa31a
+	int one = 1;				/* True, for socket options. */
5aa31a
+#if defined(PF_PACKET)
5aa31a
+	struct sockaddr_ll whereto;
5aa31a
+#else
5aa31a
+	struct sockaddr whereto;	/* who to wake up */
5aa31a
+#endif
5aa31a
+
5aa31a
 	/* Fill in the source address, if possible.
5aa31a
 	   The code to retrieve the local station address is Linux specific. */
5aa31a
 	if (! opt_no_src_addr) {
5aa31a
 		struct ifreq if_hwaddr;
5aa31a
-		unsigned char *hwaddr = if_hwaddr.ifr_hwaddr.sa_data;
5aa31a
+		char *hwaddr = if_hwaddr.ifr_hwaddr.sa_data;
5aa31a
 
5aa31a
-		strcpy(if_hwaddr.ifr_name, ifname);
5aa31a
+		strncpy(if_hwaddr.ifr_name, ifname, IFNAMSIZ);
5aa31a
+		if_hwaddr.ifr_name[IFNAMSIZ-1] = '\0';
5aa31a
 		if (ioctl(s, SIOCGIFHWADDR, &if_hwaddr) < 0) {
5aa31a
 			fprintf(stderr, "SIOCGIFHWADDR on %s failed: %s\n", ifname,
5aa31a
 					strerror(errno));
5aa31a
@@ -220,7 +264,8 @@
5aa31a
 #if defined(PF_PACKET)
5aa31a
 	{
5aa31a
 		struct ifreq ifr;
5aa31a
-		strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
5aa31a
+		strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
5aa31a
+		ifr.ifr_name[IFNAMSIZ-1] = '\0';
5aa31a
 		if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
5aa31a
 			fprintf(stderr, "SIOCGIFINDEX on %s failed: %s\n", ifname,
5aa31a
 					strerror(errno));
5aa31a
--- a/Makefile	2013-09-03 13:15:22.531951613 +0100
5aa31a
+++ b/Makefile	2013-09-03 13:24:29.659823455 +0100
5aa31a
@@ -188,6 +188,8 @@ 
5aa31a
 mii-tool:	$(NET_LIB) mii-tool.o
5aa31a
 		$(CC) $(CFLAGS) $(LDFLAGS) -o $@ mii-tool.o $(NLIB) $(RESLIB)
5aa31a
 
5aa31a
+ether-wake:	$(NET_LIB) ether-wake.o
5aa31a
+		$(CC) $(LDFLAGS) -o ether-wake ether-wake.o $(NLIB)
5aa31a
 installbin:
5aa31a
 	@echo
5aa31a
 	@echo "######################################################"
5aa31a
--- a/man/en_US/ether-wake.8	2013-09-03 13:15:22.576952098 +0100
5aa31a
+++ b/man/en_US/ether-wake.8	2013-09-03 13:14:55.270657575 +0100
5aa31a
@@ -49,7 +49,7 @@ 
5aa31a
 Increase the Debug Level.
5aa31a
 .TP
5aa31a
 .B \-i ifname
5aa31a
-Use interface ifname instead of the default "eth0".
5aa31a
+Use interface ifname instead of sending a wake packet to all interfaces.
5aa31a
 .TP
5aa31a
 .B \-p passwd
5aa31a
 Append a four or six byte password to the packet. Only a few adapters