Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <winsock2.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <openssl/rand.h>
- #include <openssl/ssl.h>
- #include <openssl/err.h>
- #define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
- #define SERVER "imap.gmail.com"
- #define PORT 993
- typedef struct
- {
- int socket;
- SSL *sslHandle;
- SSL_CTX *sslContext;
- } connection;
- int tcpConnect ()
- {
- int error;
- SOCKET handle;
- struct hostent *host;
- SOCKADDR_IN server;
- host = gethostbyname (SERVER);
- if(host==NULL)
- {
- printf("gethostbyname failed: %d\n", WSAGetLastError());
- return -1;
- }
- handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (handle == -1)
- {
- perror ("Socket");
- return -1;
- }
- else
- {
- server.sin_family = AF_INET;
- server.sin_port = htons (PORT);
- server.sin_addr.s_addr=*((unsigned long*)host->h_addr);
- memset( &( server.sin_zero ), '\0', 8 );
- error=connect(handle, (SOCKADDR*)&server,
- sizeof (server));
- if (error == -1)
- {
- perror ("Connect");
- return -1;
- }
- }
- return handle;
- }
- // Establish a connection using an SSL layer
- connection *sslConnect (void)
- {
- connection *c;
- c = malloc (sizeof (connection));
- c->sslHandle = NULL;
- c->sslContext = NULL;
- c->socket = tcpConnect ();
- if(c->socket==-1)
- return NULL;
- if (c->socket)
- {
- // Register the error strings for libcrypto & libssl
- SSL_load_error_strings ();
- // Register the available ciphers and digests
- SSL_library_init ();
- // New context saying we are a client, and using SSL 2 or 3
- c->sslContext = SSL_CTX_new (SSLv23_client_method ());
- if (c->sslContext == NULL)
- ERR_print_errors_fp (stderr);
- // Create an SSL struct for the connection
- c->sslHandle = SSL_new (c->sslContext);
- if (c->sslHandle == NULL)
- ERR_print_errors_fp (stderr);
- // Connect the SSL struct to our connection
- if (!SSL_set_fd (c->sslHandle, c->socket))
- ERR_print_errors_fp (stderr);
- // Initiate SSL handshake
- if (SSL_connect (c->sslHandle) != 1)
- ERR_print_errors_fp (stderr);
- }
- else
- {
- perror ("Connect failed");
- }
- return c;
- }
- // Disconnect & free connection struct
- void sslDisconnect (connection *c)
- {
- if (c->socket)
- close (c->socket);
- if (c->sslHandle)
- {
- SSL_shutdown (c->sslHandle);
- SSL_free (c->sslHandle);
- }
- if (c->sslContext)
- SSL_CTX_free (c->sslContext);
- free (c);
- }
- // Read all available text from the connection
- char *sslRead (connection *c)
- {
- const int readSize = 1024;
- char *rc = NULL;
- int received, count = 0;
- char buffer[1024];
- if (c)
- {
- while (1)
- {
- if (!rc)
- rc = malloc (readSize * sizeof (char) + 1);
- else
- rc = realloc (rc, (count + 1) *
- readSize * sizeof (char) + 1);
- received = SSL_read (c->sslHandle, buffer, readSize);
- buffer[received] = '\0';
- if (received > 0)
- strcat (rc, buffer);
- if (received < readSize)
- break;
- count++;
- }
- }
- return rc;
- }
- // Write text to the connection
- void sslWrite (connection *c, char *text)
- {
- if (c)
- SSL_write (c->sslHandle, text, strlen (text));
- }
- // Very basic main: we send GET / and print the response.
- int main (int argc, char **argv)
- {
- connection *c;
- char *response;
- WSADATA WsaDat;
- if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
- {
- fprintf(stderr, "WSAStartup failed: ");
- return 1;
- }
- c = sslConnect ();
- if(c==NULL)
- {
- printf("Connection Failed..\n");
- WSACleanup();
- return 1;
- }
- response = sslRead (c);
- sslWrite (c, "a LOGIN adres@email haslo\r\n");
- response = sslRead (c);
- sslWrite (c, "b SELECT INBOX\r\n");
- response = sslRead (c);
- sslWrite (c, "d SEARCH UNSEEN\r\n");
- response = sslRead (c);
- printf ("%s\n", response);
- sslWrite (c, "a LOGOUT\r\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement