Shows how to use fd handlers.
Shows how to use fd handlers.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <fcntl.h>
#ifdef HAVE_NETINET_TCP_H
# include <netinet/tcp.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <gnutls/gnutls.h>
#include <Ecore.h>
#define print(...) \
do { \
fprintf(stderr, "line %i: ", __LINE__); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n");\
} while(0)
static int done = 0;
static void
tls_log_func(int level, const char *str)
{
fprintf(stderr, "|<%d>| %s", level, str);
}
static const char *
SSL_GNUTLS_PRINT_HANDSHAKE_STATUS(gnutls_handshake_description_t status)
{
switch (status)
{
case GNUTLS_HANDSHAKE_HELLO_REQUEST:
return "Hello request";
case GNUTLS_HANDSHAKE_CLIENT_HELLO:
return "Client hello";
case GNUTLS_HANDSHAKE_SERVER_HELLO:
return "Server hello";
case GNUTLS_HANDSHAKE_CERTIFICATE_PKT:
return "Certificate packet";
case GNUTLS_HANDSHAKE_SERVER_KEY_EXCHANGE:
return "Server key exchange";
case GNUTLS_HANDSHAKE_CERTIFICATE_REQUEST:
return "Certificate request";
case GNUTLS_HANDSHAKE_SERVER_HELLO_DONE:
return "Server hello done";
case GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY:
return "Certificate verify";
case GNUTLS_HANDSHAKE_CLIENT_KEY_EXCHANGE:
return "Client key exchange";
case GNUTLS_HANDSHAKE_FINISHED:
return "Finished";
case GNUTLS_HANDSHAKE_SUPPLEMENTAL:
return "Supplemental";
default:
return "Uncaught state";
}
return NULL;
}
static int
tcp_connect(void)
{
const char *PORT = "443";
const char *SERVER = "69.58.181.89";
int err, sd;
int flag = 1, curstate = 0;
struct sockaddr_in sa;
sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd < 0) abort();
if (
fcntl(sd, F_SETFL, O_NONBLOCK) < 0) perror(
"fcntl");
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate, sizeof(curstate)) < 0) perror("setsockopt");
if (setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)) < 0) perror("setsockopt");
memset(&sa, '\0', sizeof (sa));
sa.sin_family = AF_INET;
sa.sin_port = eina_htons(atoi(PORT));
if (inet_pton(AF_INET, SERVER, &sa.sin_addr)) perror("inet_pton");
err = connect(sd, (struct sockaddr *)&sa, sizeof (sa));
if ((err < 0) && (errno != EINPROGRESS))
{
print("Connect error\n");
exit(1);
}
return sd;
}
static void
tcp_close(int sd)
{
#ifdef _WIN32
shutdown(sd, SD_BOTH);
closesocket(sd);
#else
shutdown(sd, SHUT_RDWR);
close(sd);
#endif
}
{
static int ret, lastret;
static unsigned int count = 0;
if (!done)
{
lastret = ret;
ret = gnutls_handshake(client);
count++;
if (gnutls_record_get_direction(client))
else
if (lastret != ret)
{
print("gnutls returned with: %s - %s", gnutls_strerror_name(ret), gnutls_strerror(ret));
if ((ret == GNUTLS_E_WARNING_ALERT_RECEIVED) || (ret == GNUTLS_E_FATAL_ALERT_RECEIVED))
print("Also received alert: %s", gnutls_alert_get_name(gnutls_alert_get(client)));
print("last out: %s", SSL_GNUTLS_PRINT_HANDSHAKE_STATUS(gnutls_handshake_get_last_out(client)));
print("last in: %s", SSL_GNUTLS_PRINT_HANDSHAKE_STATUS(gnutls_handshake_get_last_in(client)));
}
if (gnutls_error_is_fatal(ret))
{
print("yarrr this be an error!");
exit(1);
}
}
if (ret == GNUTLS_E_SUCCESS)
{
done = 1;
print("Handshake successful in %u handshake calls!", count);
}
}
int
main(void)
{
gnutls_anon_client_credentials_t c_anoncred;
gnutls_certificate_credentials_t c_certcred;
gnutls_session_t client;
int sd;
gnutls_global_init();
gnutls_global_set_log_function(tls_log_func);
gnutls_global_set_log_level(6);
gnutls_anon_allocate_client_credentials(&c_anoncred);
gnutls_certificate_allocate_credentials(&c_certcred);
gnutls_init(&client, GNUTLS_CLIENT);
gnutls_priority_set_direct(client, "NONE:%VERIFY_ALLOW_X509_V1_CA_CRT:+RSA:+DHE-RSA:+DHE-DSS:+ANON-DH:+COMP-DEFLATE:+COMP-NULL:+CTYPE-X509:+SHA1:+SHA256:+SHA384:+SHA512:+AES-256-CBC:+AES-128-CBC:+3DES-CBC:+VERS-TLS1.2:+VERS-TLS1.1:+VERS-TLS1.0:+VERS-SSL3.0", NULL);
gnutls_credentials_set(client, GNUTLS_CRD_ANON, c_anoncred);
gnutls_credentials_set(client, GNUTLS_CRD_CERTIFICATE, c_certcred);
gnutls_server_name_set(client, GNUTLS_NAME_DNS, "www.verisign.com", strlen("www.verisign.com"));
sd = tcp_connect();
gnutls_transport_set_ptr(client, (gnutls_transport_ptr_t)(uintptr_t)sd);
{
print("could not create fd handler!");
exit(1);
}
gnutls_bye(client, GNUTLS_SHUT_RDWR);
gnutls_deinit(client);
tcp_close(sd);
return 0;
}
struct _Ecore_Fd_Handler Ecore_Fd_Handler
A handle for Fd handlers.
Definition: Ecore_Common.h:1380
Eina_Bool(* Ecore_Fd_Cb)(void *data, Ecore_Fd_Handler *fd_handler)
A callback used by an Ecore_Fd_Handler.
Definition: Ecore_Common.h:1404
void ecore_main_fd_handler_active_set(Ecore_Fd_Handler *fd_handler, Ecore_Fd_Handler_Flags flags)
Sets what active streams the given FD handler should be monitoring.
Definition: ecore_main.c:1663
Ecore_Fd_Handler * ecore_main_fd_handler_add(int fd, Ecore_Fd_Handler_Flags flags, Ecore_Fd_Cb func, const void *data, Ecore_Fd_Cb buf_func, const void *buf_data)
Adds a callback for activity on the given file descriptor.
Definition: ecore_main.c:1449
@ ECORE_FD_READ
Fd Read mask.
Definition: Ecore_Common.h:1388
@ ECORE_FD_WRITE
Fd Write mask.
Definition: Ecore_Common.h:1389
EAPI int ecore_init(void)
Sets up connections, signal handlers, sockets etc.
Definition: ecore.c:230
#define ECORE_CALLBACK_RENEW
Return value to keep a callback.
Definition: Ecore_Common.h:153
void ecore_main_loop_quit(void)
Quits the main loop once all the events currently on the queue have been processed.
Definition: ecore_main.c:1321
void ecore_main_loop_begin(void)
Runs the application main loop.
Definition: ecore_main.c:1311
EINA_API Eina_Bool eina_file_close_on_exec(int fd, Eina_Bool on)
Make sure a file descriptor will be closed on exec.
Definition: eina_file_common.c:1059
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
EVIL_API int fcntl(int fd, int cmd,...)
Provide control over file descriptors.
Definition: evil_fcntl.c:35