'Program/C++'에 해당되는 글 1건

  1. 2008.02.01 원격 부팅 WOL by ㉡ㅐ꼬

원격 부팅 WOL

Program/C++ 2008. 2. 1. 17:55

// http://sourceforge.net/projects/wakeonlan/
// WakeOnLan - commandline application to start computers via wake on lan enbaled
// network interface card.
// Author: erik.tornstam@home.se
// Copyright: erik.tornstam@home.se

#include "stdio.h"
#include "iostream.h"
#include "winsock2.h"

#define DEFAULT_BROADCAST "255.255.255.255"
#define DEFAULT_PORT 32767

parse_mac(unsigned char *mac, char *str)
{
    int i;
    int count;
    char c;
    unsigned char val;
    int separator_ok = 1;

    for (i = 0; i < 6; i++) {
  mac[i] = 0;
    }

    for (i = 0; i < 6; i++) {
  count = 0;
  val   = 0;
  do {
   c = toupper(*str++);
   if (c >= '0' && c <= '9')
   {
    val = (val * 16) + (c - '0');
   }
   else if (c >= 'A' && c <= 'F')
   {
    val = (val * 16) + (c - 'A') + 10;
   }
   else if (c == '-' || c == ':')
   {
   if (separator_ok || count-- != 0)
    break;
   }
   else if (c == '\0')
   {
    str--;
    break;
   }
   else
   {
    return 0;
   }
   separator_ok=1;
  } while (++count < 2);

  separator_ok=(count<2);
  *mac++ = val;
    }

    if (*str)
  return 0;

    return 1;
}

int wakeup(char * sMacAddr, char * sBroadcast, int iPort)
{
 WSADATA wsaData;
 int   s;
 struct sockaddr_in bcast;
 char msg[1024];
    int  msglen = 0;
 unsigned char macaddr[6];

 if (! parse_mac(macaddr, sMacAddr) )
 {
  cerr << "Illegal MAC address" << endl;
  return -1;
    }

 for (int i = 0; i < 6; i++) {
  msg[msglen++] = (char)0xff;
    }

    for (i = 0; i < 16; i++) {
  for (int j = 0; j < sizeof(macaddr); j++) {
   msg[msglen++] = macaddr[j];
  }
    }

 if (!WSAStartup(MAKEWORD(1, 1), &wsaData))
 {
  if ((s = socket(AF_INET, SOCK_DGRAM, 0)) != INVALID_SOCKET)
  { 
   memset((char *)&bcast,0,sizeof(bcast));
   bcast.sin_family=AF_INET;
   bcast.sin_addr.s_addr=inet_addr(sBroadcast);
   bcast.sin_port=htons(iPort);

   if ( connect(s,(struct sockaddr *)&bcast,16)== -1)
   {
    cerr << "Cannot connect socket" << endl;
    return -2;
   }

   sendto(s, (const char *)&msg, msglen, 0, (struct sockaddr *)&bcast, sizeof(bcast));
   closesocket(s);
  }
  else
  {
   cerr << "Error getting socket" << endl;
  }

  WSACleanup();
  return(0);
 }
 else
  return -3;
}

void printHelp(char * argv[])
{
 cout << "WakeOnLan (c) erik.tornstam@home.se" << endl;
 cout << "Sends a WakeOnLan packet to a network." << endl << endl;
 cout << "Usage: " << argv[0] << " -a macaddress [-b broadcast address] [-p port] " << endl << endl;
 cout << "\t-a\tMAC-address of NIC to wake.  (00-04-00-c8-85-2d)" << endl;
 cout << "\t-b\tBroadcast address, default address is " << DEFAULT_BROADCAST << endl;
 cout << "\t-p\tPort number, default port is " << DEFAULT_PORT << endl;
}

int main(int argc, char* argv[])
{
 char macaddress[20] = "";
 char broadcast[20] = DEFAULT_BROADCAST;
 int port = DEFAULT_PORT;
 int argcnt=0;

 for(int c=1; c
 {
  if(stricmp("-a",argv[c])==0 && (argc > c+1) && argv[c+1][0]!='-')
  {
   strcpy(macaddress,argv[c+1]);
   argcnt++;
  }
  if(stricmp("-b",argv[c])==0 && (argc > c+1) && argv[c+1][0]!='-')
  {
   strcpy(broadcast,argv[c+1]);
   argcnt++;
  }
  if(stricmp("-p",argv[c])==0 && (argc > c+1) && argv[c+1][0]!='-')
  {
   port=atoi(argv[c+1]);
   argcnt++;
  }
 }

 if(argcnt < 1 || strcmp(macaddress,"")==0)
 {
  printHelp(argv);
  return -1;
 }
 cout << "Wakeupcall to " << macaddress << " broadcast over " << broadcast << " on port " << port << endl;
 wakeup(macaddress, broadcast, port);

 return 0;
}


Posted by ㉡ㅐ꼬
,