123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- #include "libbb.h"
- #include <netpacket/packet.h>
- #include <netinet/ether.h>
- #include <linux/if.h>
- #ifdef PF_PACKET
- # define whereto_t sockaddr_ll
- # define make_socket() xsocket(PF_PACKET, SOCK_RAW, 0)
- #else
- # define whereto_t sockaddr
- # define make_socket() xsocket(AF_INET, SOCK_PACKET, SOCK_PACKET)
- #endif
- #ifdef DEBUG
- # define bb_debug_msg(fmt, args...) fprintf(stderr, fmt, ## args)
- void bb_debug_dump_packet(unsigned char *outpack, int pktsize)
- {
- int i;
- printf("packet dump:\n");
- for (i = 0; i < pktsize; ++i) {
- printf("%2.2x ", outpack[i]);
- if (i % 20 == 19) bb_putchar('\n');
- }
- printf("\n\n");
- }
- #else
- # define bb_debug_msg(fmt, args...) ((void)0)
- # define bb_debug_dump_packet(outpack, pktsize) ((void)0)
- #endif
- static void get_dest_addr(const char *hostid, struct ether_addr *eaddr)
- {
- struct ether_addr *eap;
- eap = ether_aton_r(hostid, eaddr);
- if (eap) {
- bb_debug_msg("The target station address is %s\n\n", ether_ntoa(eap));
- #if !defined(__UCLIBC__) || UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 30)
- } else if (ether_hostton(hostid, eaddr) == 0) {
- bb_debug_msg("Station address for hostname %s is %s\n\n", hostid, ether_ntoa(eaddr));
- #endif
- } else {
- bb_show_usage();
- }
- }
- #define PKT_HEADER_SIZE (20 + 16*6)
- static int fill_pkt_header(unsigned char *pkt, struct ether_addr *eaddr, int broadcast)
- {
- int i;
- unsigned char *station_addr = eaddr->ether_addr_octet;
- memset(pkt, 0xff, 6);
- if (!broadcast)
- memcpy(pkt, station_addr, 6);
- pkt += 6;
- memcpy(pkt, station_addr, 6);
- pkt += 6;
- *pkt++ = 0x08;
- *pkt++ = 0x42;
- memset(pkt, 0xff, 6);
- for (i = 0; i < 16; ++i) {
- pkt += 6;
- memcpy(pkt, station_addr, 6);
- }
- return PKT_HEADER_SIZE;
- }
- static int get_wol_pw(const char *ethoptarg, unsigned char *wol_passwd)
- {
- unsigned passwd[6];
- int byte_cnt, i;
-
- byte_cnt = sscanf(ethoptarg, "%2x:%2x:%2x:%2x:%2x:%2x",
- &passwd[0], &passwd[1], &passwd[2],
- &passwd[3], &passwd[4], &passwd[5]);
-
- if (byte_cnt < 4)
- byte_cnt = sscanf(ethoptarg, "%u.%u.%u.%u",
- &passwd[0], &passwd[1], &passwd[2], &passwd[3]);
- if (byte_cnt < 4) {
- bb_error_msg("can't read Wake-On-LAN pass");
- return 0;
- }
- for (i = 0; i < byte_cnt; ++i)
- wol_passwd[i] = passwd[i];
- bb_debug_msg("password: %2.2x %2.2x %2.2x %2.2x (%d)\n\n",
- wol_passwd[0], wol_passwd[1], wol_passwd[2], wol_passwd[3],
- byte_cnt);
- return byte_cnt;
- }
- int ether_wake_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int ether_wake_main(int argc UNUSED_PARAM, char **argv)
- {
- const char *ifname = "eth0";
- char *pass;
- unsigned flags;
- unsigned char wol_passwd[6];
- int wol_passwd_sz = 0;
- int s;
- int pktsize;
- unsigned char outpack[PKT_HEADER_SIZE + 6 + 16 ];
- struct ether_addr eaddr;
- struct whereto_t whereto;
-
- flags = getopt32(argv, "^" "bi:p:" "\0" "=1", &ifname, &pass);
- if (flags & 4)
- wol_passwd_sz = get_wol_pw(pass, wol_passwd);
- flags &= 1;
-
- s = make_socket();
-
-
-
- get_dest_addr(argv[optind], &eaddr);
-
- pktsize = fill_pkt_header(outpack, &eaddr, flags );
- bb_debug_dump_packet(outpack, pktsize);
-
- #ifdef __linux__
- {
- struct ifreq if_hwaddr;
- strncpy_IFNAMSIZ(if_hwaddr.ifr_name, ifname);
- ioctl_or_perror_and_die(s, SIOCGIFHWADDR, &if_hwaddr, "SIOCGIFHWADDR on %s failed", ifname);
- memcpy(outpack+6, if_hwaddr.ifr_hwaddr.sa_data, 6);
- # ifdef DEBUG
- {
- unsigned char *hwaddr = if_hwaddr.ifr_hwaddr.sa_data;
- printf("The hardware address (SIOCGIFHWADDR) of %s is type %d "
- "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n\n", ifname,
- if_hwaddr.ifr_hwaddr.sa_family, hwaddr[0], hwaddr[1],
- hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
- }
- # endif
- }
- #endif
- bb_debug_dump_packet(outpack, pktsize);
-
- if (wol_passwd_sz > 0) {
- memcpy(outpack+pktsize, wol_passwd, wol_passwd_sz);
- pktsize += wol_passwd_sz;
- }
- bb_debug_dump_packet(outpack, pktsize);
-
- if (flags ) {
- if (setsockopt_broadcast(s) != 0)
- bb_perror_msg("SO_BROADCAST");
- }
- #if defined(PF_PACKET)
- {
- struct ifreq ifr;
- strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
- xioctl(s, SIOCGIFINDEX, &ifr);
- memset(&whereto, 0, sizeof(whereto));
- whereto.sll_family = AF_PACKET;
- whereto.sll_ifindex = ifr.ifr_ifindex;
-
- whereto.sll_halen = ETH_ALEN;
- memcpy(whereto.sll_addr, outpack, ETH_ALEN);
- }
- #else
- whereto.sa_family = 0;
- strcpy(whereto.sa_data, ifname);
- #endif
- xsendto(s, outpack, pktsize, (struct sockaddr *)&whereto, sizeof(whereto));
- if (ENABLE_FEATURE_CLEAN_UP)
- close(s);
- return EXIT_SUCCESS;
- }
|