/*************************************************************************
> File Name: wake_up.c
> Author: wz
> Created Time: Fri 15 Nov 2013 03:42:47 PM CST
************************************************************************/
#includeunistd.h>
#includearpa/inet.h>
#includenet/if.h>
#includestdio.h>
#includestring.h>
#includestdlib.h>
#includesys/ioctl.h>
void get_mac(char *argv,unsigned char *save_mac)
{
int j = 0;
int num_x = 0,num_d,first = 1;
while(*argv != 0)
{
if(*argv != ':')
{
if(isdigit(*argv) first)
{
num_x = (*argv - '0')*16;
first = 0;
argv++;
continue;
}
if(first isalpha(*argv))
{
num_x = (10+(*argv - 'a'))*16;
first = 0;
argv++;
continue;
}
if(!first isdigit(*argv))
{
num_x = num_x+(*argv - '0');
first = 1;
argv++;
continue;
}
if(!first isalpha(*argv))
{
num_x = num_x + (10 + (*argv - 'a'));
first = 1;
argv++;
continue;
}
}
else
{
save_mac[j] = num_x;
num_x = 0;
argv++;
j++;
}
}
save_mac[j] = num_x;
}
int main(int argc,char *argv[])
{
int sockfd,i,j,on = 1,n;
unsigned char packet[120];
unsigned char save_mac[120];
struct sockaddr_in to;
if(argc 2)
{
fprintf(stderr,"用法:程序名 + 目標(biāo)主機(jī)物理地址");
return 1;
}
get_mac(argv[1],save_mac);
//構(gòu)建magic數(shù)據(jù)包
for(i = 0; i 6; i++)
{
packet[i] = 0xff;
}
for(i = 1; i 17; i++)
{
memcpy(packet+i*6,save_mac,6);
}
sockfd = socket(AF_INET,SOCK_DGRAM,0);
setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,on,sizeof(on));
if(socket 0)
{
fprintf(stderr,"套接字創(chuàng)建錯(cuò)誤\n");
return 1;
}
memset((void *)to,0,sizeof(to));
to.sin_family = AF_INET;
to.sin_port = htons(30000);
to.sin_addr.s_addr = inet_addr("125.217.40.255");
n = sendto(sockfd,packet,sizeof(packet),0,(struct sockaddr *)to,sizeof(to));
if(n = 0)
{
fprintf(stderr,"發(fā)送失敗!\n");
return 1;
}
else
{
printf("發(fā)送成功.\n");
}
close(sockfd);
return 0;
}