jpopelka / rpms / net-tools

Forked from rpms/net-tools 4 years ago
Clone

Blame SOURCES/ether-wake-interfaces.patch

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