android firmware 利用UDP socket发送Magic Packet
1 Magic Packet格式:
6个0xFF + 16个Dst Mac Address
2 代码需要设置目的MAC地址, 目的IP地址和使用的端口
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h> void generate_magic_packet(void *magic_buf, void *mac)
{
int i;
char *ptr; ptr = magic_buf;
memset(ptr, 0xFF, );
ptr += ; for(i = ; i < ; ++i) {
memcpy(ptr, mac, );
ptr += ;
}
} int main(int argc, char **argv)
{
int s;
int packet_num = ;
char c; unsigned char mac[] = {0x94, 0x39, 0xE5, 0x02, 0x5E, 0xC9};
char dstip[] = "192.168.1.106";
int port = ; struct sockaddr_in address;
char magic_buf[ + * ] = {}; daemon(,); /* run in background */ s = socket(AF_INET, SOCK_DGRAM, );
if (s == -) {
perror("Opening socket");
exit(EXIT_FAILURE);
} memset(&address, , sizeof(struct sockaddr_in));
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(dstip);
address.sin_port = htons(port); generate_magic_packet(magic_buf, mac); /* ten packets. TODO: use ping to check whether the destination is on or else. */
while (packet_num-- > ) {
if (sendto(s, magic_buf, sizeof(magic_buf), ,
(struct sockaddr *)&address, sizeof(address)) < ) {
printf("sendto\n");
exit(EXIT_FAILURE);
}
sleep();
} exit(EXIT_SUCCESS);
}