Friday, March 25, 2011

Resolve the IP Number from a Host Name

This sample translate the IP number from www.google.com. But you can even use localhost or localmachinename to gets the local or external IP number of the local machine.

#include <stdio.h>
#include <iostream>
#include <arpa/inet.h>
#include <netdb.h>
int main() {
  char hostName[] = "www.google.com";
  struct hostent *host;
  if ((host = gethostbyname(hostName)) == NULL) {
    fprintf(stderr, "(mini) nslookup failed on '%s'", hostName);
    return 1;
  }
  struct in_addr h_addr;
  h_addr.s_addr = *((unsigned long *) host->h_addr_list[0]);
  const char *ip = inet_ntoa(h_addr);
  std::cout << ip << std::endl;
  return 0;
}

Convert Integer to String without itoa

This solution is based from here.

#include <iostream>
#include <sstream>
int main() {
  int i = 100;
  std::stringstream ss;
  ss << i;
  fprintf(stderr, ss.str().c_str());
}

GNU/C++ Simple Send Mail

See the Microsoft Visual C++ Simple Send Mail here.

I did the class below for GNU C++ based in these links:

SendMail.h:

#ifndef _SENDMAILHEADER
#define _SENDMAILHEADER

#include <sys/socket.h>
#include <arpa/inet.h>
#include <string>
#include <string.h>

#define HELO "HELO\r\n"
#define DATA "DATA\r\n"
#define QUIT "QUIT\r\n"

namespace mail {
class SendMail {
private:
int sock;
char buf[BUFSIZ];
void send_socket(char *s)
{
send(sock, s, strlen(s), 0);
//fprintf(stderr, s);
}
void read_socket()
{
int len = recv(sock, buf, BUFSIZ, 0);
//fprintf(stderr, ((std::string)buf).substr(0, len).c_str());
}
public:
char* HostIp;
unsigned short HostPort;
char* To;
char* From;
char* Subject;
char* Message;
SendMail() {
HostPort = 25;
};
~SendMail() { };
void Send() {
struct sockaddr_in hostAddr;
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
fprintf(stderr, "SendMail: socket() failed");
return;
}
hostAddr.sin_family = AF_INET;
hostAddr.sin_addr.s_addr = inet_addr(HostIp);
hostAddr.sin_port = htons(HostPort);
if (connect(sock, (struct sockaddr *) &hostAddr, sizeof(struct sockaddr_in)) != 0) {
fprintf(stderr, "SendMail: connect() failed");
return;
}
read_socket();
send_socket((char*)HELO);
read_socket();
send_socket((char*)"MAIL FROM: ");
send_socket(From);
send_socket((char*)"\r\n");
read_socket();
send_socket((char*)"VRFY ");
send_socket(From);
send_socket((char*)"\r\n");
read_socket();
send_socket((char*)"RCPT TO: ");
send_socket(To);
send_socket((char*)"\r\n");
read_socket();
send_socket((char*)DATA);
send_socket((char*)"Subject: ");
send_socket(Subject);
send_socket((char*)"\r\n");
read_socket();
send_socket(Message);
send_socket((char*)"\r\n");
send_socket((char*)".\r\n");
read_socket();
send_socket((char*)QUIT);
read_socket();
close(sock);
};
};
}

#endif


Usage:

#include "SendMail.h"

int main(int argc, char** argv) {
mail::SendMail* sendmail = new mail::SendMail();
sendmail->HostIp = (char*)"192.168.1.10";
sendmail->From = (char*)"mail@address.com";
sendmail->To = (char*)"mail@address.com";
sendmail->Subject = (char*)"Test!";
sendmail->Message = (char*)"Content:\nTest 123...";
sendmail->Send();
return 0;
}

VC++ Simple Send Mail

See the GNU/C++ Simple Send Mail here.

I did the class below for Visual C++ based in these links:

SendMail.h:

#ifndef _SENDMAILHEADER
#define _SENDMAILHEADER

#include "stdafx.h"
#include <stdio.h>
#include <Winsock2.h>
#include <Windows.h>
#include <string>

#pragma comment(lib, "ws2_32.lib")

#define HELO "HELO\r\n"
#define DATA "DATA\r\n"
#define QUIT "QUIT\r\n"

namespace mail {
class SendMail {
private:
int sock;
char buf[BUFSIZ];
void send_socket(char *s)
{
send(sock, s, strlen(s), 0);
//fprintf(stderr, s);
}
void read_socket()
{
int len = recv(sock, buf, BUFSIZ, 0);
//fprintf(stderr, ((std::string)buf).substr(0, len).c_str());
}
public:
char* HostIp;
unsigned short HostPort;
char* To;
char* From;
char* Subject;
char* Message;
SendMail() {
HostPort = 25;
};
~SendMail() { };
void Send() {
struct sockaddr_in hostAddr;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
fprintf(stderr, "SendMail: WSAStartup() failed");
return;
}
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
fprintf(stderr, "SendMail: socket() failed");
return;
}
memset(&hostAddr, 0, sizeof(hostAddr));
hostAddr.sin_family = AF_INET;
hostAddr.sin_addr.s_addr = inet_addr(HostIp);
hostAddr.sin_port = htons(HostPort);
if (connect(sock, (struct sockaddr *) &hostAddr, sizeof(hostAddr)) < 0) {
fprintf(stderr, "SendMail: connect() failed");
return;
}
read_socket();
send_socket(HELO);
read_socket();
send_socket("MAIL FROM: ");
send_socket(From);
send_socket("\r\n");
read_socket();
send_socket("VRFY ");
send_socket(From);
send_socket("\r\n");
read_socket();
send_socket("RCPT TO: ");
send_socket(To);
send_socket("\r\n");
read_socket();
send_socket(DATA);
send_socket("Subject: ");
send_socket(Subject);
send_socket("\r\n");
read_socket();
send_socket(Message);
send_socket("\r\n");
send_socket(".\r\n");
read_socket();
send_socket(QUIT);
read_socket();
closesocket(sock);
WSACleanup();
};
};
}

#endif


Usage:

#include "stdafx.h"
#include "SendMail.h"

int main(int argc, char* argv[])
{
mail::SendMail* sendmail = new mail::SendMail();
sendmail->HostIp = "192.168.1.10";
sendmail->From = "mail@address.com";
sendmail->To = "mail@address.com";
sendmail->Subject = "Test!";
sendmail->Message = "Content:\nTest 123...";
sendmail->Send();
return 0;
}