Skip to content

Instantly share code, notes, and snippets.

@paulopatto
Created February 10, 2014 16:37
Show Gist options
  • Save paulopatto/8919354 to your computer and use it in GitHub Desktop.
Save paulopatto/8919354 to your computer and use it in GitHub Desktop.
Uma amostra de uma conexão SSL com o Google, lendo e escrevendo dados. Desenvolvido por Alexandre Mulatinho <[email protected]> em http://goo.gl/Jvj8cv
/*
* ssl-mlt.c
* Alexandre Mulatinho <[email protected]>
* Simple framework to HTTPs
* */
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.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 BUF_MAX 4096
typedef struct {
int socket;
char *hostname;
SSL *ssl;
SSL_CTX *ssl_ctx;
} conn;
int mlt_handle(char *servname)
{
int ret, soq;
struct hostent *host;
struct sockaddr_in server;
host = gethostbyname (servname);
if ( (soq = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) {
perror ("Socket");
return -1;
}
server.sin_family = AF_INET;
server.sin_port = htons (443);
server.sin_addr = *((struct in_addr *) host->h_addr);
if ( ( ret = connect(soq, (struct sockaddr *)&server,
sizeof (struct sockaddr)) ) == -1 ) {
perror ("Connect");
return -1;
}
return soq;
}
conn *mlt_ssl_connect(char *servname)
{
conn *p;
p = (conn *)malloc(sizeof(conn));
p->ssl = NULL;
p->ssl_ctx = NULL;
p->hostname = servname;
SSL_load_error_strings ();
SSL_library_init ();
if ( (p->socket = mlt_handle(p->hostname)) == -1)
return NULL;
if ( (p->ssl_ctx = SSL_CTX_new(SSLv23_client_method ())) == NULL)
ERR_print_errors_fp (stderr);
if ( (p->ssl = SSL_new(p->ssl_ctx)) == NULL)
ERR_print_errors_fp (stderr);
if (!SSL_set_fd(p->ssl, p->socket))
ERR_print_errors_fp (stderr);
if (SSL_connect(p->ssl) != 1)
ERR_print_errors_fp (stderr);
return p;
}
void mlt_disconnect(conn *p)
{
SSL_shutdown(p->ssl);
close(p->socket);
SSL_free(p->ssl);
SSL_CTX_free(p->ssl_ctx);
free(p);
}
int mlt_ssl_read(conn *p, char *buffer)
{
int bytes;
memset(buffer, '{FONTE}', BUF_MAX);
bytes = SSL_read (p->ssl, buffer, BUF_MAX-1);
return bytes;
}
int mlt_ssl_write(conn *p, char *cmd)
{
int ret;
ret = SSL_write(p->ssl, cmd, strlen(cmd));
return ret;
}
int main (int argc, char **argv)
{
conn *server;
char buffer[BUF_MAX];
int bytes;
server = mlt_ssl_connect("www.google.com");
mlt_ssl_write(server, "GET /\r\n\r\n");
while (1)
{
bytes = mlt_ssl_read(server, buffer);
if (!bytes) {
fprintf(stderr, ".-. server disconnected.\n");
mlt_disconnect(server);
break;
}
fprintf(stdout, "%s", buffer);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment