| 1 |
From: =?utf-8?b?0L3QsNCx?= <[email protected]> |
| 2 |
Date: Tue, 24 Sep 2024 17:36:39 +0200 |
| 3 |
Subject: Use the first available interface instead of always eth0 (Closes: |
| 4 |
#816411) |
| 5 |
|
| 6 |
--- |
| 7 |
ether-wake.c | 21 ++++++++++++++++++--- |
| 8 |
etherwake.8 | 6 +++--- |
| 9 |
2 files changed, 21 insertions(+), 6 deletions(-) |
| 10 |
|
| 11 |
diff --git a/ether-wake.c b/ether-wake.c |
| 12 |
index 00ea53c..5839346 100644 |
| 13 |
--- a/ether-wake.c |
| 14 |
+++ b/ether-wake.c |
| 15 |
@@ -22,7 +22,7 @@ static char usage_msg[] = |
| 16 |
" Options:\n" |
| 17 |
" -b Send wake-up packet to the broadcast address.\n" |
| 18 |
" -D Increase the debug level.\n" |
| 19 |
-" -i ifname Use interface IFNAME instead of the default 'eth0'.\n" |
| 20 |
+" -i ifname Use interface IFNAME instead of the default '%s'.\n" |
| 21 |
" -p <pw> Append the four or six byte password PW to the packet.\n" |
| 22 |
" A password is only required for a few adapter types.\n" |
| 23 |
" The password may be specified in ethernet hex format\n" |
| 24 |
@@ -66,6 +66,7 @@ static char usage_msg[] = |
| 25 |
#include <errno.h> |
| 26 |
#include <ctype.h> |
| 27 |
#include <string.h> |
| 28 |
+#include <ifaddrs.h> |
| 29 |
|
| 30 |
#if 0 /* Only exists on some versions. */ |
| 31 |
#include <ioctls.h> |
| 32 |
@@ -110,9 +111,20 @@ static int get_dest_addr(const char *arg, struct ether_addr *eaddr); |
| 33 |
static int get_fill(unsigned char *pkt, struct ether_addr *eaddr); |
| 34 |
static int get_wol_pw(const char *optarg); |
| 35 |
|
| 36 |
+static const char *default_ifname(void) { |
| 37 |
+ struct ifaddrs *itr = NULL; |
| 38 |
+ if(getifaddrs(&itr) == -1) |
| 39 |
+ return "eth0"; |
| 40 |
+ |
| 41 |
+ for(; itr; itr = itr->ifa_next) |
| 42 |
+ if((itr->ifa_flags & IFF_UP) && (itr->ifa_flags & IFF_BROADCAST)) |
| 43 |
+ return itr->ifa_name; |
| 44 |
+ return "eth0"; |
| 45 |
+} |
| 46 |
+ |
| 47 |
int main(int argc, char *argv[]) |
| 48 |
{ |
| 49 |
- char *ifname = "eth0"; |
| 50 |
+ const char *ifname = NULL; |
| 51 |
int one = 1; /* True, for socket options. */ |
| 52 |
int s; /* Raw socket */ |
| 53 |
int errflag = 0, verbose = 0, do_version = 0; |
| 54 |
@@ -131,7 +143,7 @@ int main(int argc, char *argv[]) |
| 55 |
case 'D': debug++; break; |
| 56 |
case 'i': ifname = optarg; break; |
| 57 |
case 'p': get_wol_pw(optarg); break; |
| 58 |
- case 'u': printf("%s",usage_msg); return 0; |
| 59 |
+ case 'u': printf(usage_msg, default_ifname()); return 0; |
| 60 |
case 'v': verbose++; break; |
| 61 |
case 'V': do_version++; break; |
| 62 |
case '?': |
| 63 |
@@ -149,6 +161,9 @@ int main(int argc, char *argv[]) |
| 64 |
return 3; |
| 65 |
} |
| 66 |
|
| 67 |
+ if (!ifname) |
| 68 |
+ ifname = default_ifname(); |
| 69 |
+ |
| 70 |
/* Note: PF_INET, SOCK_DGRAM, IPPROTO_UDP would allow SIOCGIFHWADDR to |
| 71 |
work as non-root, but we need SOCK_PACKET to specify the Ethernet |
| 72 |
destination address. */ |
| 73 |
diff --git a/etherwake.8 b/etherwake.8 |
| 74 |
index 5f807c5..53190b9 100644 |
| 75 |
--- a/etherwake.8 |
| 76 |
+++ b/etherwake.8 |
| 77 |
@@ -26,9 +26,9 @@ .SH DESCRIPTION |
| 78 |
command. |
| 79 |
.PP |
| 80 |
.\" TeX users may be more comfortable with the \fB<whatever>\fP and |
| 81 |
-.\" \fI<whatever>\fP escape sequences to invode bold face and italics, |
| 82 |
+.\" \fI<whatever>\fP escape sequences to invode bold face and italics, |
| 83 |
.\" respectively. |
| 84 |
-\fBetherwake\fP is a program that generates and transmits a Wake-On-LAN |
| 85 |
+\fBetherwake\fP is a program that generates and transmits a Wake-On-LAN |
| 86 |
(WOL) "Magic Packet", used for restarting machines that have been |
| 87 |
soft-powered-down (ACPI D3-warm state). It generates the standard |
| 88 |
AMD Magic Packet format, optionally with a password included. The |
| 89 |
@@ -49,7 +49,7 @@ .SH OPTIONS |
| 90 |
Increase the Debug Level. |
| 91 |
.TP |
| 92 |
.B \-i ifname |
| 93 |
-Use interface ifname instead of the default "eth0". |
| 94 |
+Use interface ifname instead of the default (first available interface or "eth0"). |
| 95 |
.TP |
| 96 |
.B \-p passwd |
| 97 |
Append a four or six byte password to the packet. Only a few adapters |