wakeonlan-0.41-ethers-lookup.patch 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. --- wakeonlan-0.41.orig/wakeonlan
  2. +++ wakeonlan-0.41/wakeonlan
  3. @@ -5,6 +5,7 @@
  4. #########################################################################
  5. use strict;
  6. +use Net::hostent;
  7. use Socket;
  8. use Getopt::Std;
  9. use vars qw($VERSION $opt_v $opt_h $opt_i $opt_p $opt_f);
  10. @@ -44,19 +45,64 @@
  11. sub wake
  12. {
  13. - my $hwaddr = shift;
  14. + my $host = shift;
  15. my $ipaddr = shift || $DEFAULT_IP;
  16. my $port = shift || $DEFAULT_PORT;
  17. my ($raddr, $them, $proto);
  18. - my ($hwaddr_re, $pkt);
  19. + my ($hwaddr, $hwaddr_re, $pkt);
  20. - # Validate hardware address (ethernet address)
  21. + # get the hardware address (ethernet address)
  22. $hwaddr_re = join(':', ('[0-9A-Fa-f]{1,2}') x 6);
  23. - if ($hwaddr !~ m/^$hwaddr_re$/) {
  24. - warn "Invalid hardware address: $hwaddr\n";
  25. - return undef;
  26. + if ($host =~ m/^$hwaddr_re$/) {
  27. + $hwaddr = $host;
  28. + } else {
  29. + # $host is not a hardware address, try to resolve it
  30. + my $ip_re = join('\.', ('([0-9]|[1-9][0-9]|1[0-9]{2}|2([0-4][0-9]|5[0-5]))') x 4);
  31. + my $ip_addr;
  32. + if ($host =~ m/^$ip_re$/) {
  33. + $ip_addr = $host;
  34. + } else {
  35. + my $h;
  36. + unless ($h = gethost($host)) {
  37. + warn "$host is not a hardware address and I could not resolve it as to an IP address.\n";
  38. + return undef;
  39. + }
  40. + $ip_addr = inet_ntoa($h->addr);
  41. + }
  42. + # look up ip in /etc/ethers
  43. + unless (open (ETHERS, '<', '/etc/ethers')) {
  44. + warn "$host is not a hardware address and I could not open /etc/ethers.\n";
  45. + return undef;
  46. + }
  47. + while (<ETHERS>) {
  48. + if (($_ !~ m/^$/) && ($_ !~ m/^#/)) { # ignore comments
  49. + my ($mac, $ip);
  50. + ($mac, $ip) = split(' ', $_, 3);
  51. + if ($ip =~ m/^$ip$/) {
  52. + if ($ip eq $ip_addr) {
  53. + $hwaddr = $mac;
  54. + last;
  55. + }
  56. + next;
  57. + } else {
  58. + my $h2;
  59. + unless ($h2 = gethost($ip)) {
  60. + next;
  61. + }
  62. + if (inet_ntoa($h2->addr) eq $ip_addr) {
  63. + $hwaddr = $mac;
  64. + last;
  65. + }
  66. + }
  67. + }
  68. + }
  69. + close (ETHERS);
  70. + unless (defined($hwaddr)) {
  71. + warn "Could not find $host in /etc/ethers\n";
  72. + return undef;
  73. + }
  74. }
  75. # Generate magic sequence
  76. @@ -68,7 +114,7 @@
  77. # Allocate socket and send packet
  78. - $raddr = gethostbyname($ipaddr);
  79. + $raddr = gethostbyname($ipaddr)->addr;
  80. $them = pack_sockaddr_in($port, $raddr);
  81. $proto = getprotobyname('udp');