/*************************************************************************
> File name:wake_up.c
> Author:wz
> Created time:fri Nov 2013 03:42:47 PM CST
************************************************************************/
#include <unistd.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/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) && A
{
num_x = (*ARGV-' 0 ') *16;
i = 0;
argv++;
Continue
}
if (a && isalpha (*ARGV))
{
num_x = (10+ (*ARGV-' a ')) *16;
i = 0;
argv++;
Continue
}
if (!first && isdigit (*ARGV))
{
num_x = num_x+ (*ARGV-' 0 ');
i = 1;
argv++;
Continue
}
if (!first && isalpha (*ARGV))
{
num_x = num_x + (*ARGV-' a '));
i = 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, "Usage: Program name + target host Physical Address");
return 1;
}
Get_mac (ARGV[1],SAVE_MAC);
Building Magic Packets
for (i = 0; i < 6; i++)
{
Packet[i] = 0xFF;
}
for (i = 1; i < 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, "Socket creation error \ 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, "Send failed!") \ n ");
return 1;
}
Else
{
printf ("sent successfully. \ n");
}
Close (SOCKFD);
return 0;
}