Então depois de alguma leitura e uns testes com o protocolo OAuth2, decidi partir para minha biblioteca utilizando a Google Drive API. Pretendo colocá-la em alguns projetos pessoais com o
Simple File Manager mas antes precisava fazer o básico que era a conexão SSL com o servidor, tive como base a documentação do OpenSSL que não é lá essas coisas e ficou assim:
#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 , '' , 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 /rnrn" );
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 ;
}
Tô feliz com o resultado até agora e estou começando a melhorar minha api pra o google drive que breve devo metê-la no
github ;)