lowbyte1
Goto Top

WOL network tool in C mit VS-C-Cplusplus08 (wake on lan - magic packet) (remote Rechner starten)

Open source WOL Tool in C.

XOR-BASE
http://vpn23.homelinux.org

WOL (wake on lan)

Hier ein kleines wake on lan tool in C.
Als input wird eine mac Adresse und eine Broadcast ip Adresse verlangt, danach wird das UDP packet zusammengesetzt
nach http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs ... .
Danach wird das Udp-packet an die Broadcast Adresse des Subnet gesendet.
Nun sollte der gewünschte remote Rechner starten, sofern er darauf konfiguriert ist.(Bios & Netzwerkarten-treiber einstellungen).

Sample:



Wake on Lan - Network - tool by lowbyte



Example: 00:23:54:91:0a:90 or 00-23-54-91-0a-90

Input Mac Adresse:00-23-54-91-0a-90

Input Broadcast IP Adresse:172.16.0.255

WOL Magic Packet gesendet >>>


//=========================================================================
//
// File    : WOL - Wake on lan Ver.0.04
//
// Author  : Lowbyte
//                           
// Create  : 03.12.2009, 08:27,  Lon************
//
// Last Up : 13.12.2009, 22:47,  Lon*************
//
// Prog.L  : C
//
// Project : 0026N
//
// Thx     : Jonny ,Felix ,Michaela
//=========================================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <winsock2.h>
#include <windows.h>
#include "console.h" 



#define PRINT_TITLE	system("title Network tools -  by LB"); 
#define I_TIME	40
#define DEMO_INTERRUPT_TIME		_sleep(I_TIME);
#define DEMO_ROUNDS	0



void InitWsa(void);
int initDemo(void);
int setColor(int color);
int check_input(char *str);
int rem_delim(char *mac_str);
unsigned char memory_reduction(unsigned char a_ptr ,unsigned char b_ptr);
unsigned char convchartohex(unsigned char c);



char *OUTPUT={"\n----------------------------------------------------------------------------------\n"  
				"\n Wake on Lan - Network - tool						by lowbyte       \n"  
				"\n----------------------------------------------------------------------------------\n" };  

struct sockaddr_in adr_data;

char wol_mask={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};

int main(void)
{

	SOCKET sock;
	int by ,i ,j ,m ,c;
	int Len = sizeof(BOOL);
	int Val;
	char mac[19];
	char tmp_mac[6];
	char mac_buffer[96];
	char msg[103];
	char sbn[17];


	system("title WOL - network tools by lowbyte");  
	setColor(FOREGROUND_GREEN);
	initDemo();
	printf("%s",*OUTPUT);  



	// Check MAC address input #################################
	do
	{
		printf("\n\nExample: 00:23:54:91:0a:90 or 00-23-54-91-0a-90 \n");  
		printf("\nInput Mac Adresse:");  
		fgets(mac ,19 ,stdin);
		mac[strlen(mac)-1]='\0';  
		fflush(stdin);
	}
	while( (check_input(mac) != 0) );

	printf("\nInput Broadcast IP Adresse:");  
	fgets(sbn ,17 ,stdin);
	sbn[strlen(sbn)-1]='\0';  
	fflush(stdin);


	// Make WOL Magic Packet ###################################

	// Remove delim's ########  
	rem_delim(mac);

	// Convert 2 byte char to hex
	for(j=0,m=0;(j<6);j++) {
		c = memory_reduction(mac[m] ,mac[m+1]);
		tmp_mac[j]=c;
		m +=2;
	}

        //  16 mal aufeinanderfolgend die mac Adresse des Remote Rechners in den Vector kopieren
	for(i=0;i<16;i++) {
		memcpy(&mac_buffer[i*6] ,tmp_mac ,6);  
	}

        // Kopiere die WOL_MASKE (0xFF,0xFF,0xFF,0xFF,0xFF,0xFF) in den MSG Puffer
	memcpy(msg ,wol_mask ,6);
        // Kopiere den mac-Puffer der mac addresen hinter die Maske
	memcpy(&msg[6] ,&mac_buffer ,96);
	msg[102]='\0';  

	// Initialisiere WSA #######################################
	InitWsa();

	// Socket anfordern
	if( (sock = socket(AF_INET ,SOCK_DGRAM ,0)) == INVALID_SOCKET ) {
		printf("Socket error %i", WSAGetLastError());  
		WSACleanup();
		getchar();
		return 0;
	} else {
		//printf("Winsock ok ...\n");  
	}

	// Set Broadcast opt
	setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&Val, Len);

	/* Set (struct SOCKADDR*)adr_data */
	memset(&adr_data ,0 ,sizeof(SOCKADDR));
	adr_data.sin_family = AF_INET;
	adr_data.sin_port = htons(9);
	adr_data.sin_addr.s_addr = inet_addr(sbn);

	/* Send magic packet */
	by = sendto(sock ,&msg ,(int)102 ,0 ,(struct sockaddr *)&adr_data ,sizeof(adr_data));

	if ( by == -1) {
		printf("\nSendto() failed ...Error nr:%i \n",WSAGetLastError());  
	} else {
		printf("\nWOL Magic Packet gesendet >>>");  
		closesocket(sock);
		WSACleanup();
	}

	getchar();


}





void InitWsa(void)
{
	WSADATA ptr_wsad;

	if( WSAStartup(MAKEWORD(2,2), &ptr_wsad) != -1) {
		//printf("\nWinsock initialisiert ...\n");  
	} else {
		printf("\nWinsock konnte nicht initialisiert werden ...\n");  
		getchar();
		exit(1);
	}
}



unsigned char memory_reduction(unsigned char a_ptr ,unsigned char b_ptr)
{
	unsigned char a ,b;

	a = convchartohex(a_ptr);
	b = convchartohex(b_ptr);

	return b|a<<4;


}


unsigned char convchartohex(unsigned char c)
{

	if(c >= 'a' && c <= 'f'){  
		return 10 + (c - 'a');  
	}
	if(c >= '0' && c <= '9'){  
		return c - '0';  
	}
	return 0;
}

	

int check_input(char *str)
{
	int i ,flag=0;

	if(strlen(str)!=17) {
		cls();
		printf("\nInccorect input !\n");  
		return 1;
	}

	for(i=0;i<17;i++) {

		// check delim
		if( (i==2) || (i==5) || (i==8) || (i==11) || (i==14) ) {
			if( (*(str+i)==':') || (*(str+i)=='-') ){  
				;
			} else {
				cls();
				printf("\nInccorect input !\n");  
				return 1;
			}

		} else {

			// check alpha
			if( (*(str+i)>='0' && *(str+i)<='9') || (*(str+i)>='a' && *(str+i)<='f') ){  
				;
			} else {
				cls();
				printf("\nInccorect input !\n");  
				return 1;
			}
		}
	}

	return 0;

}

int rem_delim(char *mac_str)
{
	char tmp_arr[13];

	memcpy(&tmp_arr ,mac_str ,2);
	memcpy(&tmp_arr[2] ,(mac_str+3) ,2);
	memcpy(&tmp_arr[4] ,(mac_str+6) ,2);
	memcpy(&tmp_arr[6] ,(mac_str+9) ,2);
	memcpy(&tmp_arr[8] ,(mac_str+12) ,2);
	memcpy(&tmp_arr[10] ,(mac_str+15) ,2);

	memcpy(&mac_str ,&tmp_arr ,12);
	*(mac_str+12)='\0';  

	return 0;

}








int initDemo(void)
{
	int i,r;
	char string[100];
	char printstring[100];

	strcpy_s(string  ,10 ,"[LOWBYTE]");  
	memset(printstring ,' ' ,15);  
	printstring[15]='\0';  
	strcat(printstring,string);

	for(r=0;(r<=DEMO_ROUNDS);r++) {

		for(i=0;(i<(int)strlen(printstring));i++) {

			puts("\n\n\n");  
			printf("%40i\n",rand());  
			printf("%40i\n",rand());  
			printf("%40i\n",rand());  
			printf("%40i\n",rand());  
			setCursor(9, 30);
			printf("%.15s\t\n\n", (printstring+i));  
			printf("%40i\n",rand());  
			printf("%40i\n",rand());  
			printf("%40i\n",rand());  
			printf("%40i\n",rand());  
			DEMO_INTERRUPT_TIME;
			cls();

		}
	}

	cls();

	setCursor(9, 30);
	printf("PRESS [ENTER]");  
	getchar();
	cls();

	return 0;
}

Content-ID: 131604

Url: https://administrator.de/contentid/131604

Ausgedruckt am: 23.11.2024 um 05:11 Uhr