6 # ha:2345:respawn:/usr/local/sbin/harpy >>/var/log/harpy.log eth1 eth2
8 # ha:2345:respawn:/usr/local/sbin/harpy >>/var/log/harpy.log eth_intra
11 sub __KERNEL__ { 1; } # for "linux/socket.ph"
15 require "linux/if_ether.ph";
16 require "linux/socket.ph";
17 require "linux/sockios.ph";
18 require "linux/if_arp.ph";
20 sub SOCKADDR_SIZEOF { 16; }
32 my $a=unpack("H*",$n);
50 socket $sock,AF_PACKET(),SOCK_RAW(),ETH_P_ARP() or die "No ARP socket: $!";
52 local *ioctl_ifhwaddr=sub($)
56 my $buf=$ifname.("\x00"x0x1000);
57 ioctl $sock,SIOCGIFHWADDR(),$buf or die "ioctl($ifname,SIOCGIFHWADDR): $!";
58 my($trash,$sockaddr)=unpack("a16a16",$buf);
59 my($sa_len,$sa_data)=unpack("a2a6",$sockaddr);
63 local *ioctl_ifindex=sub($)
67 my $buf=$ifname.("\x00"x0x1000);
68 ioctl $sock,SIOCGIFINDEX(),$buf or die "ioctl($ifname,SIOCGIFINDEX): $!";
69 my($trash,$ifindex)=unpack("a16i",$buf);
73 my $hw=ioctl_ifhwaddr($ifname);
74 my $sockaddr_ll=pack "SniSCCa8", # struct sockaddr_ll:
75 AF_PACKET(), # unsigned short int sll_family;
76 ETH_P_ARP(), # unsigned short int sll_protocol;
77 ioctl_ifindex($ifname), # int sll_ifindex;
78 ARPHRD_ETHER(), # unsigned short int sll_hatype;
79 PACKET_BROADCAST(), # unsigned char sll_pkttype;
80 ETH_ALEN(), # unsigned char sll_halen;
81 $hw; # unsigned char sll_addr[8];
83 bind $sock,$sockaddr_ll or die "bind($ifname): $!";
85 return $sock,hw_ntoa($hw);
88 #struct ether_arp_frame {
89 # struct ether_header {
90 # u_int8_t ether_dhost[ETH_ALEN]; /* destination eth addr */
91 # u_int8_t ether_shost[ETH_ALEN]; /* source ether addr */
92 # u_int16_t ether_type; /* packet type ID field */
95 # struct arphdr { /* fixed-size header */
96 # unsigned short int ar_hrd; /* Format of hardware address. */
97 # unsigned short int ar_pro; /* Format of protocol address. */
98 # unsigned char ar_hln; /* Length of hardware address. */
99 # unsigned char ar_pln; /* Length of protocol address. */
100 # unsigned short int ar_op; /* ARP opcode (command). */
102 # u_int8_t arp_sha[ETH_ALEN]; /* sender hardware address */
103 # u_int8_t arp_spa[4]; /* sender protocol address */
104 # u_int8_t arp_tha[ETH_ALEN]; /* target hardware address */
105 # u_int8_t arp_tpa[4]; /* target protocol address */
111 my($dst_hw,$src_hw,$dst,$src)=@_;
113 my $msg=pack "a6a6nnnCCna6a4a6a4",
114 hw_aton($dst_hw), # $ether_dhost
115 hw_aton($src_hw), # $ether_shost
121 ARPOP_REPLY(), # $ar_op
122 hw_aton($src_hw), # $arp_sha
123 inet_aton($src), # $arp_spa
124 hw_aton($dst_hw), # $arp_tha
125 inet_aton($dst); # $arp_tpa
133 return if 42>length $msg;
147 )=unpack "a6a6nnnCCna6a4a6a4",$msg;
148 $V>=3 and print Data::Dumper->Dump([
175 return if $ar_op!=ARPOP_REQUEST();
176 return if $ar_hln!=6;
177 return if $ar_pln!=4;
178 my $tell=inet_ntoa $arp_spa;
179 my $who_has=inet_ntoa $arp_tpa;
180 my $tell_hw=hw_ntoa $ether_shost;
181 return $tell,$who_has,$tell_hw;
186 for my $ifname (@ARGV) {
187 my($sock,$hw)=sock($ifname);
194 $V and print localtime()." START\n";
197 vec($rfds,fileno($_->{"sock"}),1)=1 for values(%socks);
198 my $got=select $rfds,undef(),undef(),undef();
199 die "Invalid select(2): ".Dumper($got) if !defined $got || $got<0;
201 while (my($ifname,$hash)=each(%socks)) {
202 next if !vec($rfds,fileno($hash->{"sock"}),1);
203 $V>=2 and print localtime()." got packet: $ifname\n";
205 defined(my $from_addr=recv $hash->{"sock"},$msg,0x1000,0) or die "recv($ifname): $!";
206 next if !(my($tell,$who_has,$tell_hw)=arp_unpack($msg));
207 $V and print localtime()." got: tell=$tell,who_has=$who_has,tell_hw=$tell_hw\n";
208 next if $tell eq $who_has; # do not reply to self-detection queries
209 my $msg_reply=arp_pack($tell_hw,$hash->{"hw"},$tell,$who_has);
210 send $hash->{"sock"},$msg_reply,0,$from_addr or die "send($ifname): $!";