android firmware 利用UDP socket发送Magic Packet
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android firmware 利用UDP socket发送Magic Packet相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章
BeagleBone Black–ESP8266 UDP 服务
eclipse里配置Android ndk环境,用eclipse编译.so文件