Friday, September 5, 2014

Send Public IP address of home router to EMail account in C++ with Boost, OpenSSL and cURL libraries

I showed how to make an email client in C++ with libraries: boost and openssl.
I also showed how to get public IP address of your home router in C++ with a library, libcurl.

Now I am going to combine them.
This article shows you how to make an email client that send the public IP address of your Router to your email account in C++.

The source code above shows you three files, stdafx.h, config.hpp and send_publicip.cpp.
I couldn't show the "#include" line in the one screen shot but it shouldn't be hard to figure out for anybody.

I am not going to repeat the source code of classes: Socket, MyCURL, MyOpenSSL and MySMTP. You can see them from my previous articles linked above.

As a fan of C++ namespace, the file, config.hpp, has a little strange looking namespace structure. When variable names conflict, I will have to type-in namespace name as well but most of time this trick improves readability of source code.

If you understood previous articles, this source code shouldn't be difficult to understand.
This main code does:

  1. initialize global setup for libraries of OpenSSL and cUrl.
  2. it sets URL, User ID and User Password of my home router.
  3. once it got the HTML body, it looks for the IP address number.
  4. sometimes the function, "RequestGet", returns not valid body data so I need to carefully check each steps of searching IP address.
  5. once I got the IP address, it is printed out to the screen and
  6. it sends an email with the retrieved Public IP address of my home router.


The Makefile above shows that it is using three libraries: boost, curl and openssl.

When I compile and run the program, it retrieves the public IP address of my router and it sends to my GMail account. As I said, sometimes the cURL fails to retrieve IP address. I am not sure why it happens yet; I will need to figure out later.

1 comment:

  1. I am still not sure why the first access is always rejected. I think it has something to do with time out or something. But as a workaround, if I call "curl.RequestGet()" twice, it seems to be working better.

    ReplyDelete

About Me

My photo
Tomorrow may not come, so I want to do my best now.