Click here to Skip to main content
15,891,943 members
Articles / Desktop Programming / MFC

IP Announce by Email

Rate me:
Please Sign up or sign in to vote.
4.94/5 (18 votes)
26 Apr 2003CPOL5 min read 110.1K   3.8K   47  
Sends local computer's network information to a remote computer via email.
/*
Baseheader.h
For IP announcement by email
Version 1.0.01

By Wesley Yang.
2003 Feb. 28th
*/

#include <time.h>
#include <windows.h>
#include <stdlib.h>
#include <winsock2.h>

#define BUFFER_DEFAULT_SIZE 4096

#define TIME_STAMP_YES 0
#define TIME_STAMP_NO 1
#define STR_VALUE 0
#define DW_VALUE 1
#define REG_BUFF 100

typedef struct
{
	char* ToAdd;
	char* FromAdd;
	char* Subject;
	char* MsgBody;
}SmtpMsg;

typedef struct
{
	char* address;
	int port;
}TCPServer;

static FILE *logFile;

char* getNowTime (void)
{
	struct tm *nowTime;
	time_t aclock;

	time (&aclock);
	nowTime = localtime (&aclock);

	return asctime(nowTime);
}

FILE* openLog (char* fileName)
{
	FILE *logFilePtr;
	if ((logFilePtr = fopen(fileName, "w+t")) == NULL) 
	{
		printf ("Log file error!");
		return NULL;
	}
	return logFilePtr;
}

bool writeLog (FILE *logFilePtr, char *logText, int timeStamp = TIME_STAMP_YES)
{
	size_t logLength;
	int timeLen = strlen(getNowTime()); //The getNowTime() function can be defined to output diffent style of time information
	char* nowTime = new char[timeLen];

	ZeroMemory(nowTime, timeLen);
	logLength= strlen(logText);

	//check if the log file has been opend successfully or not
	if (logFile == NULL) return false;

	strcpy(nowTime, getNowTime());

	if (timeStamp == TIME_STAMP_YES) fwrite(nowTime, sizeof(char),timeLen,logFilePtr);

	//write logdata to log file
	fwrite(logText,sizeof(char),logLength,logFilePtr);

	fwrite("\n",sizeof(char),2,logFilePtr);

//	nowTime=NULL;
	return true;
}

bool closeLog (FILE *logFilePtr)
{
	fclose(logFilePtr);
	return true;
}

void ErrorExit (LPTSTR lpszMessage, FILE* logFilePtr = logFile)
{
	printf ("%s\n",lpszMessage);
	writeLog(logFilePtr,lpszMessage);
	closeLog(logFilePtr);
	ExitProcess(0);
}

bool getRegInfo(int dataType, char* subKeyName, char* valueName, char* strValue, int *numValue)
{
	HKEY hKey;
	long lRet;
	char regData[REG_BUFF];
	DWORD dwBufLen = REG_BUFF;

	lRet = RegOpenKeyEx(HKEY_CURRENT_USER, subKeyName, 0, KEY_QUERY_VALUE, &hKey);
	if (lRet != ERROR_SUCCESS) return false;

	lRet = RegQueryValueEx(hKey, valueName, NULL, NULL, (LPBYTE) regData, &dwBufLen);
	if ((lRet != ERROR_SUCCESS) || (dwBufLen > REG_BUFF))  return false;

	if (dataType == STR_VALUE) strcpy(strValue, regData);
	if (dataType == DW_VALUE) *numValue = (DWORD)(regData[0]);
	RegCloseKey(hKey);

	return true;
}

bool initConn (char* address, int port, SOCKET* sock2use)
{
	//The InitConnToSmtpServer() is used to create socket of ssmtp and create a connection to the SMTP server also.

	WSADATA wsdata;
	struct sockaddr_in server;
	struct hostent *host=NULL;

	if (WSAStartup(MAKEWORD (2,2), &wsdata) !=0)
	{
		ErrorExit("Failed when loading Winsock library!");
		return false;
	}

	*sock2use = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);

	if (*sock2use == INVALID_SOCKET)
	{
		ErrorExit ("socket() failed with error: " + WSAGetLastError());
		return false;
	}

	server.sin_family = AF_INET;
	server.sin_port = htons(port);
	server.sin_addr.s_addr = inet_addr(address);

	if (server.sin_addr.s_addr == INADDR_NONE)
	{
		host = gethostbyname(address);
		if (host == NULL)
		{
			ErrorExit ("Server name cannot be resolved!");
			return false;
		}
		CopyMemory (&server.sin_addr, host->h_addr_list[0], host->h_length);
	}

	if (connect(*sock2use, (struct sockaddr *) &server, sizeof(server)) == SOCKET_ERROR)
	{
		ErrorExit ("Error connecting to server: " + WSAGetLastError());
		return false;
	}
    
	writeLog(logFile, "Connection to server is now OK");

	return true;
}

bool sendData (const char* msg, SOCKET sockInUse, char* rcvBuff)
{
	const char* MSG;
	int ret;

	ZeroMemory (rcvBuff, BUFFER_DEFAULT_SIZE);
	MSG = msg;

	size_t msgLength;
	msgLength = strlen(MSG);

	send (sockInUse, MSG, msgLength, 0);
    ret = recv (sockInUse, rcvBuff, BUFFER_DEFAULT_SIZE, 0);

	if (ret != 0)
		if (ret == SOCKET_ERROR)
			writeLog(logFile, "Connection to SMTP server is now OK" + WSAGetLastError());

    return true;
}

bool closeConn(SOCKET socket2end)
{
	closesocket (socket2end);
	WSACleanup();

	writeLog(logFile, "Socket closed!");
	return true;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
China China
When I was 8, I touched a computer (SHARP PC1500, as I remembered^_^) firstly. Till 10, I could only changed rule of scroll counting in a game written in BASIC...

In the year 2001, I finally graduated from CS department of Lanzhou University.

What's to say, start work then Smile | :)

I like computer; like the way it give me that allow me to design what I want; like to make idea into reality on it…

Comments and Discussions