ecore_con_client_example.c

Shows how to write a simple client (dialer that connects to the example server using ecore_con_server_connect().

Shows how to write a simple client (dialer that connects to the example server using ecore_con_server_connect().

//Compile with:
// gcc -o ecore_con_client_example ecore_con_client_example.c `pkg-config --libs --cflags ecore ecore-con eina`
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <Ecore.h>
#include <Ecore_Con.h>
#include <Ecore_Getopt.h>
/* Ecore_Con client example
* 2010 Mike Blumenkrantz
*/
static Ecore_Con_Server *svr;
static int retval = EXIT_SUCCESS;
static Eina_Bool do_flush = EINA_FALSE;
static Eina_Bool single_message = EINA_FALSE;
static Eina_Bool verify = EINA_TRUE;
static Eina_Bool hostname_verify = EINA_TRUE;
static Eina_Bool do_ssl_upgrade = EINA_FALSE;
static char *starttls_local_command = NULL;
static Eina_Bool
_setup_ssl(void)
{
const char *ca;
if (!(it = eina_file_ls("/etc/ssl/certs")))
{
retval = EXIT_FAILURE;
return EINA_FALSE;
}
/* add all the CAs */
{
fprintf(stderr, "WARNING: could not load CA: %s!\n", ca);
}
if (verify)
if (hostname_verify)
return EINA_TRUE;
}
static Eina_Bool
_on_stdin(void *data EINA_UNUSED, Ecore_Fd_Handler *fdh EINA_UNUSED)
{
char *line = NULL;
#ifdef _WIN32
char lbuf[4096] = "";
ssize_t r;
if (fgets(lbuf, sizeof(lbuf), stdin) == NULL)
r = -1;
else
{
line = strdup(lbuf);
r = strlen(line);
}
#else
size_t len = 0;
ssize_t r = getline(&line, &len, stdin);
#endif
if (r < 0)
{
fprintf(stderr, "ERROR: could not read from stdin: %s\n", strerror(errno));
}
if (!svr)
fputs("WARNING: not connected to server, ignored input.\n", stderr);
else
{
size_t clen = strlen(starttls_local_command);
if (do_ssl_upgrade && ((size_t)r > clen) &&
(strncmp(line, starttls_local_command, clen) == 0) &&
(line[clen] == '\n' || line[clen] == '\r'))
{
printf("INFO: starting SSL communication...\n");
if (!ecore_con_ssl_server_upgrade(svr, ECORE_CON_USE_MIXED | ECORE_CON_LOAD_CERT))
{
printf("ERROR: failed to upgrade to SSL!\n");
retval = EXIT_FAILURE;
}
#if 1
/* This just works since EFL v 1.19. Prior to this,
* upgrade couldn't get any extra setup, such as
* certificate or verification mode as OpenSSL would
* complain.
*/
else if (!_setup_ssl())
{
retval = EXIT_FAILURE;
}
#endif
svr = NULL; /* it's considered dead until ECORE_CON_EVENT_SERVER_UPGRADE */
goto end;
}
ecore_con_server_send(svr, line, r);
printf("INFO: sent %zd bytes to server.\n", r);
if (do_flush) ecore_con_server_flush(svr);
if (single_message)
{
svr = NULL;
}
}
end:
free(line);
}
_add(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server_Add *ev)
{
printf("Server with ip %s connected!\n", ecore_con_server_ip_get(ev->server));
if (do_ssl_upgrade)
printf("INFO: Not sending 'hello!' in tcp+ssl mode. Use: %s to upgrade the connection\n", starttls_local_command);
else
ecore_con_server_send(ev->server, "hello!", strlen("hello!"));
if (do_flush) ecore_con_server_flush(ev->server);
}
_del(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server_Del *ev)
{
printf("Lost server with ip %s!\n", ecore_con_server_ip_get(ev->server));
svr = NULL;
}
_data(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server_Data *ev)
{
printf("Received %i bytes from server:\n"
">>>>>\n"
"%.*s\n"
">>>>>\n",
ev->size,
ev->size, (const char *)ev->data);
}
_write_(void *data EINA_UNUSED, int type EINA_UNUSED, Ecore_Con_Event_Server_Write *ev)
{
print