gunknown
Hi,

in order to improve my coding skills I've coded a tool which you can use to stay in contact to a computer on a dial up range. The tool sends an email with the extern ip to your email address every time it changes. I hope this is usefull for anyone of you.
I'm sorry about my english, it isn't perfect, so maybe there are some mistakes in my explanations.

CODE
/*
#
# ip messenger v 1.0
# Copyright (C) 2005 gunknown
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#
# ##############################################################################
#
#
# ip messenger v1.0 by gunknown
#
# you will get an e-mail with the current extern ip
# after the program has been started or every time the ip changes.
#
# usage: ipmessenger.exe [domain of a valid mail server] [your e-mail addy at this domain]
#
# Some details about the mailserver:
# This tool uses the simple mail transfer protocol (smtp)
# to send e-mails. All mailservers demand either pop or
# esmtp and smtp-auth respectively for sending e-mails
# to any e-mail address. Because I didn't want you to pass
# your e-mail password I decided to use the smtp without authentification.
# You can send e-mails intern (to an other email address at this domain)
# by sending the mail over the public MX of this domain. You can find the public MX
# by the following command (linux):
# dig domain MX
# Some mailservers, however, don't accept unauthorized e-mails sended
# from a dial up ip. So, what you need is a mailserver which accepts
# intern unauthorized e-mails from dial up ips. I think there are enough such mailservers,
# I've tested yahoo.de and oleco.net. Both are working great.
#
# Recapitulating, make sure you pass the domain of a mailserver sending and accepting
# intern e-mails from a dial up ip. And, of course, pass an e-mail adress
# belonging to this domain.
#
# You can test whether your mailserver is valid or not by querying it
# with telnet on port 25 and try to send an e-mail to yourself.
#
# two examples:
# ipmessenger.exe mx1.mail.yahoo.com your_email_adress@yahoo.de //properly .com will work too
# ipmessenger.exe mail.oleco.net your_email_adress@oleco.net
#
# contact me for any feedback! gunknown@oleco.net
#
# greets go out to wuzzler and gsc
#
#                                                         July '05
#
*/

#include <iostream.h>
#include <winsock2.h>
#include <string>

using namespace std;

void help(void);
void details(void);

int main(int argc, char* argv[])
{

if(argc != 3){
 help();
 return 0;}

int s, i, j = 1;
string ip, temp = ip = "";
char buf[256] = "";
const char* lastip;
struct sockaddr_in server, email;
hostent* he;
unsigned long int mailsint;
char* mailserver = argv[1];
char* addy = argv[2];

  WSADATA wsa;
  WSAStartup( MAKEWORD(2,0), &wsa );


//main programm loop
for(;;)
{

  do{
     //sleep one hour, then compare the ips (again)
     //not the first time because we want the ip at once
     if(j != 1){Sleep(3600000);}
     ++j;
     temp = ip;

    // get ip from http://www.panten.org/ip.php3
     s = socket(AF_INET, SOCK_STREAM, 0);
     server.sin_family = AF_INET;
     server.sin_port = htons(80);
     server.sin_addr.s_addr = inet_addr("212.227.118.82");
     i = connect(s, (struct sockaddr*) &server, sizeof(sockaddr));

     if(i == -1){closesocket(s); continue;}

     const char get[256] = "GET /ip.php3 HTTP/1.1\n";
     const char host[256] = "Host: www.panten.org\n\n";

        send(s, get, strlen(get), 0);
        send(s, host, strlen(host), 0);
        Sleep(2000);
        recv(s, buf, 256, 0);
          ip = buf;
          ip.erase(0,170);
          ip.erase(ip.find("\n"));
          lastip = ip.c_str();

     closesocket(s);

  }while(temp == ip); // ip changed? Go on ...


 // send email to argv[2]
  he = gethostbyname(mailserver);
  s = socket(AF_INET, SOCK_STREAM, 0);
  email.sin_family = AF_INET;
  email.sin_port = htons(25);
  memcpy(&mailsint, he->h_addr, he->h_length);
  email.sin_addr.s_addr = mailsint;
  connect(s, (struct sockaddr*) &email, sizeof(sockaddr));

     const char helo[] = "HELO";
     const char eohelo[] = "\r\n";
     const char mailfrom[] = "MAIL FROM:<";
     const char end[] = ">\r\n";
     const char mailto[] = "RCPT TO:<";
     const char data[] = "DATA\r\n";
     const char subject[] = "Subject: newip\r\n";
     const char enter[] = "\n\r\n";
     const char message[] = "ip messenger by gunknown [gunknown@oleco.net]\n\n";
     const char dot[] = "\n.\r\n";
     const char quit[] = "QUIT\r\n";

     send(s, helo, strlen(helo), 0);
     send(s, mailserver, strlen(mailserver), 0);
     send(s, eohelo, strlen(eohelo), 0);
     send(s, mailfrom, strlen(mailfrom), 0);
     send(s, addy, strlen(addy), 0);
     send(s, end, strlen(end), 0);
     send(s, mailto, strlen(mailto), 0);
     send(s, addy, strlen(addy), 0);
     send(s, end, strlen(end), 0);
     send(s, data, strlen(data), 0);
     send(s, subject, strlen(subject), 0);
     send(s, enter, strlen(enter), 0);
     send(s, message, strlen(message), 0);
     send(s, lastip, strlen(lastip), 0);
     send(s, dot, strlen(dot), 0);
     send(s, quit, strlen(quit), 0);

  closesocket(s);

}
}


void help(void)
{
cout << endl << endl << " # ip messenger v1.0 by gunknown [gunknown@oleco.net]" << endl;
cout << " #" << endl << " # you will get an e-mail with the current extern ip" << endl;
cout << " # after the program has been started or every time the ip changes" << endl;
cout << " #" << endl << " # usage: ipmessenger.exe [domain of a mailserver] [email addy at this domain]" << endl;
cout << " #" << endl << " # read the code to see which mailservers are valid" << endl << endl;
}


I welcome any constructive feedback or suggestion for improvement.

Download the code and the compiled .exe here
apsync
[deleted after reading the faq ph34r.gif ]
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.

 
Invision Power Board © 2001-2005 Invision Power Services, Inc.