android firmware 利用UDP socket發送Magic Packet

來源:互聯網
上載者:User

標籤:

android firmware 利用UDP socket發送Magic Packet

 

 1 #include <stdio.h>  2 #include <string.h>  3 #include <stdlib.h>  4 #include <unistd.h>  5 #include <sys/types.h>  6 #include <sys/stat.h>  7 #include <sys/socket.h>  8 #include <netinet/in.h>  9 #include <arpa/inet.h> 10 #include <netdb.h> 11 12 void fill_magic_buf(void *magic_buf, void *mac) 13 { 14          int i; 15          char *ptr; 16 17          ptr = magic_buf; 18          memset(ptr, 0xFF, 6); 19          ptr += 6; 20 21          for(i = 0; i < 16; ++i) { 22                  memcpy(ptr, mac, 6); 23                  ptr += 6; 24          } 25 } 26 27 void usage(void) 28 { 29          printf("usage...\n"); 30  } 31 32 int main(int argc, char **argv) 33 { 34          int s; 35          int packet_num = 10; 36          char c; 37 38          unsigned char mac[6] = {0x94, 0x39, 0xE5, 0x02, 0x5E, 0xC9}; 39          char dstip[256] = "192.168.1.103"; 40          int port = 4097; 41 42          struct sockaddr_in address; 43          char magic_buf[6 + 6 * 16] = {0}; 44 45          daemon(0,0);    /* run in background */ 46 47          while((c = getopt(argc, argv, "d:m:p:")) != -1) { 48                  switch(c) { 49                  case ‘d‘: 50                          strcpy(dstip, optarg); 51                          break; 52                  case ‘m‘: 53                          sscanf(optarg, "%x:%x:%x:%x:%x:%x", 54                                  (unsigned int*)&mac[0], (unsigned int*)&mac[1], 55                                  (unsigned int*)&mac[2], (unsigned int*)&mac[3], 56                                  (unsigned int*)&mac[4], (unsigned int*)&mac[5]); 57                          break; 58                  case ‘p‘: 59                          port = atoi(optarg); 60                          break; 61                  default: 62                          usage(); 63                          return -1; 64                  } 65          } 66 67          s = socket(AF_INET, SOCK_DGRAM, 0); 68 69          if (s == -1) { 70                  perror("Opening socket"); 71                  exit(EXIT_FAILURE); 72          } 73 74          memset(&address, 0, sizeof(struct sockaddr_in)); 75          address.sin_family = AF_INET; 76          address.sin_addr.s_addr = inet_addr(dstip); 77          address.sin_port = htons(port); 78 79          fill_magic_buf(magic_buf, mac); 80 81          /* ten packets. TODO: use ping to check whether the destination is on or else. */ 82          while (packet_num-- > 0) { 83                  if (sendto(s, magic_buf, sizeof(magic_buf), 0, 84                       (struct sockaddr *)&address, sizeof(address)) < 0) { 85                          printf("sendto\n"); 86                          exit(EXIT_FAILURE); 87                  } 88                  sleep(1); 89          } 90 91          exit(EXIT_SUCCESS); 92 } 

 

android firmware 利用UDP socket發送Magic Packet

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.