wol.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* send wake-on-lan magic ethernet packet */
  2. #include <u.h>
  3. #include <libc.h>
  4. #include <ip.h>
  5. enum {
  6. Eaddrlen = 6, /* 48 bits */
  7. };
  8. typedef struct Wolpack Wolpack;
  9. struct Wolpack{
  10. uchar magic[6];
  11. uchar macs[16][Eaddrlen];
  12. char pass[6+1];
  13. };
  14. int verbose;
  15. void
  16. usage(void)
  17. {
  18. fprint(2, "usage: wol [-v] [-a dialstr] [-c password] macaddr\n");
  19. exits("usage");
  20. }
  21. void
  22. fillmac(Wolpack *w, uchar *mac)
  23. {
  24. int i;
  25. for(i = 0; i < nelem(w->macs); i++)
  26. memmove(w->macs[i], mac, Eaddrlen);
  27. }
  28. void
  29. dumppack(Wolpack *w)
  30. {
  31. int i;
  32. print("packet: [\n");
  33. print("\t%E\n", w->magic);
  34. for(i = 0; i < nelem(w->macs); i++)
  35. print("\t%E\n", w->macs[i]);
  36. print("\t%6s\n", w->pass);
  37. print("]\n");
  38. }
  39. void
  40. main(int argc, char* argv[])
  41. {
  42. int fd, nw;
  43. char *argmac, *pass, *address;
  44. uchar mac[Eaddrlen];
  45. static Wolpack w = {
  46. .magic { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }
  47. };
  48. address = pass = nil;
  49. fmtinstall('E', eipfmt);
  50. ARGBEGIN{
  51. case 'a':
  52. address = EARGF(usage());
  53. break;
  54. case 'c':
  55. pass = EARGF(usage());
  56. break;
  57. case 'v':
  58. verbose++;
  59. break;
  60. default:
  61. usage();
  62. }ARGEND
  63. if(argc != 1)
  64. usage();
  65. argmac = argv[0];
  66. if(verbose)
  67. print("mac is %s, pass is %s\n", argmac, pass);
  68. parseether(mac, argmac);
  69. fillmac(&w, mac);
  70. if(pass){
  71. if(strlen(pass) > 6)
  72. sysfatal("password greater than 6 bytes\n");
  73. strcpy(w.pass, pass);
  74. }
  75. if(verbose)
  76. dumppack(&w);
  77. if(!address)
  78. address = "udp!255.255.255.255!0";
  79. fd = dial(address, nil, nil, nil);
  80. if(fd < 0)
  81. sysfatal("%s: %r", address);
  82. nw = write(fd, &w, sizeof w);
  83. if(nw != sizeof w)
  84. sysfatal("error sending: %r");
  85. exits(0);
  86. }