Showing posts with label winsock. Show all posts
Showing posts with label winsock. Show all posts

Friday, September 6, 2013

C++ Send EMail + Win32 w/ Download

Hello fellow coders,

[Image: dm-613784255382.png]

It is coded purely C/C++.

Also, the part that consumed most of my time in this project was base64 encryption. I was initially thinking of just ripping one online but couldn't. All the sources used external libraries and if you know me, I hate doing that. Not to mention, I had no patience to read over the external library and copy/paste it and adjust to the modifications of my project.

So I went ahead and implemented my own retarded base64 encryption, which works fine. I followed this: Base64 Wikipedia

It's pretty simple and VERY basic.

Basic outline of the program:
Code:
connect to smtp.gmail.com on port 25
Client: EHLO mail.gmail.com\r\n
Client: AUTH LOGIN\r\n
Client: base64 encrypted email\r\n
Client: base64 encrypted password\r\n
Server: 250 Accepted. --This is important to check.
Client: MAIL FROM: <emailid>\r\n
Client: RCPT TO: <toemailid>\r\n
DATA\r\n
Client: From: uremailid\r\n
Client: To: toemailid\r\n
Client: Subject: somesubject\r\n
Client: \r\n
Client: somemessageyouwanttosend\r\n
Client: .\r\n by sending period, it represents end of message.

The commands above are the ones sent to GMail server. That's all that's needed to send an EMail along with base64 encoder.

oh also, this is a very messy code, with little to no error handling. So, it's not perfect. It finishes its job though: send email. I didn't think about efficiency that much; I just wanted to get it done.

Download Link: http://uploading.com/e422f2f9/Emailer-rar

Source:
main.cpp
resource.rc
resource.h

Thanks and regards,

Wednesday, September 4, 2013

Simple Port Scanner + Source

Brief Description
Checks whether the given port is either open or closed.

Instructions
There are no need for Instructions for this program. Even your Grandma can figure it out.

Source: Simple Port Scanner

Please feel free to comment and if you have any suggestions, please post those as well.

Thanks for reading,

Monday, September 2, 2013

Get Website Source + Source

Just a simple source, I would like to share. It can output the IP Addresses of as many websites as you pass through the command line.

Code:
#include <iostream>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")

int main(int argc, char *argv[])
{
    WSADATA wsData;
    if(WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
    {
        std::cout << "Cannot WSAStartup\n";
        WSACleanup();
    }
    else
    {
        for(int i = 1; i < argc; i++)
            std::cout << argv[i] << " : " << inet_ntoa(*(in_addr*)*gethostbyname(argv[i])->h_addr_list) << std::endl;
    }
    std::cin.get();
    return 0;
}

Command-line:
Code:
Directory>GetWebAddress www.hackforums.net www.google.com www.youtube.com
www.hackforums.net : 69.162.82.251
www.google.com : 72.14.204.99
www.youtube.com : 74.125.113.92

Sunday, September 1, 2013

Simple Network Chat + Source

Hello fellow programmers,

It's a very simple chat program. The only problem is that the server doesn't handle multiple clients. I'll make one as soon as I gain in depth knowledge in Winsocks. If possible, can anyone link me to a guide to learn about handling multiple clients?

Client source:
Code:
/**

Program: Simple chat program
Author: Sri Krishna

**/

#include <iostream>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")

SOCKET sock;

void recvdata()
{
    while(1)
    {
        char msg[256];
        memset(msg, 0, 256);
        int r = recv(sock, msg, 256, 0);
        if(r == SOCKET_ERROR)
        {
            std::cout << "\n\nServer is disconnected\n\n";
            std::cin.ignore();
            std::cin.get();
            closesocket(sock);
            WSACleanup();
            exit(1);
        }
        else
            std::cout << "Friend: " << msg << std::endl;
    }
}

void senddata()
{
    while(1)
    {
        char msg[256];
        memset(msg, 0, 256);
        std::cin.get(msg, 256);
        int ret = send(sock, msg, strlen(msg), 0);
        if(ret == SOCKET_ERROR)
        {
            std::cout << "\n\nServer is disconnected\n\n";
            std::cin.ignore();
            std::cin.get();
            closesocket(sock);
            WSACleanup();
            exit(1);
        }
        std::cin.ignore();
    }
}

int main()
{
    WSADATA wsData;
    if(WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
    {
        std::cout << "WSAStartup!\n";
    }
    else
    {
        sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if(sock == INVALID_SOCKET)
        {
            std::cout << "socket()\n";
            WSACleanup();
        }
        else
        {
            sockaddr_in sin;
            sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
            sin.sin_port = htons(626);
            sin.sin_family = AF_INET;

            while(connect(sock, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
                connect(sock, (sockaddr*)&sin, sizeof(sin));
            std::cout << "Connected to your friend\n\n";
            HANDLE hThread[2];
            DWORD thread1, thread2;
            hThread[0] = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)recvdata, NULL, NULL, &thread1);
            hThread[1] = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)senddata, NULL, NULL, &thread2);
            WaitForMultipleObjects(2, hThread, true, INFINITE);
        }
    }

    std::cin.ignore();
    std::cin.get();
    return 0;
}

Server source:
Code:
/**

Program: Simple chat program
Author: Sri Krishna

**/

#include <iostream>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")

SOCKET sock, acceptsock;

void recvdata()
{
    while(1)
    {
        char msg[256];
        memset(msg, 0, 256);
        int r = recv(sock, msg, 256, 0);
        if(r == SOCKET_ERROR)
        {
            std::cout << "\n\nServer is disconnected\n\n";
            std::cin.ignore();
            std::cin.get();
            closesocket(sock);
            WSACleanup();
            exit(1);
        }
        else
            std::cout << "Friend: " << msg << std::endl;
    }
}

void senddata()
{
    while(1)
    {
        char msg[256];
        memset(msg, 0, 256);
        std::cin.get(msg, 256);
        int ret = send(sock, msg, strlen(msg), 0);
        if(ret == SOCKET_ERROR)
        {
            std::cout << "\n\nServer is disconnected\n\n";
            std::cin.ignore();
            std::cin.get();
            closesocket(sock);
            WSACleanup();
            exit(1);
        }
        std::cin.ignore();
    }
}

int main()
{
    WSADATA wsData;
    if(WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
    {
        std::cout << "WSAStartup!\n";
    }
    else
    {
        sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if(sock == INVALID_SOCKET)
        {
            std::cout << "socket()\n";
            WSACleanup();
        }
        else
        {
            sockaddr_in sin;
            sin.sin_addr.S_un.S_addr = INADDR_ANY;
            sin.sin_port = htons(626);
            sin.sin_family = AF_INET;

            while(bind(sock, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
                bind(sock, (sockaddr*)&sin, sizeof(sin));

            while(listen(sock, 1) == SOCKET_ERROR)
            {
                listen(sock, 1);
            }

            while(1)
            {
                acceptsock = SOCKET_ERROR;
                while(acceptsock == SOCKET_ERROR)
                {
        acceptsock = accept(sock, NULL, NULL);
                }
                std::cout << "Connected to your friend!\n\n";
                sock = acceptsock;
                break;
            }
        }
    }
    HANDLE hThread[2];
    DWORD thread1, thread2;
    hThread[0] = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)recvdata, NULL, NULL, &thread1);
    hThread[1] = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)senddata, NULL, NULL, &thread2);
    WaitForMultipleObjects(2, hThread, true, INFINITE);
    std::cin.ignore();
    std::cin.get();
    return 0;
}

Client code with C++ highlights: http://pastebin.com/Zbc7qwDc
Server code with C++ highlights: http://pastebin.com/R3stNU0v

Also, I need your guys' favor. The only way I can improve my knowledge is by receiving projects that require me to search for new methods and concepts. So, if possible, give me some tough projects or tough concepts to research. I need some concepts to learn....

Saturday, August 31, 2013

Simple Port Scanner + Source

Hello fellow coders,
Information:
So, I have been bored for a while and I was flipping through 'My Threads' and saw the Simple port scanner thread I made a while ago; It wasn't the best because it had limitations. Limitations such as one port at a time. So I decided to crank it up to make it multiple ports at a time. Also, I would like to mention that I am still learning Network Programming, Sockets, and all that good stuff, so please use constructive criticism instead of flaming. :)


How to use it
-You have two options.
-Option 1 gives you the ability to scan list of ports. For example: 1337 - 1600 scans all the numbers between 1337 to 1600.
-Option 2 lets you choose specific ports; This is done by using Dynamic Memory Allocation. Cool

Source: http://pastebin.com/T0FBHaxs

Thanks,
Sri Krishna

P.S. I know the code isn't that great; So, please give me some way to optimize my code.