\hypertarget{ecore_fd_handler_gnutls_example_8c-example}{}\doxysection{ecore\+\_\+fd\+\_\+handler\+\_\+gnutls\+\_\+example.\+c}
Shows how to use fd handlers.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{comment}{//Compile with:}}
\DoxyCodeLine{\textcolor{comment}{// gcc -\/o ecore\_fd\_handler\_gnutls\_example ecore\_fd\_handler\_gnutls\_example.c `pkg-\/config -\/-\/cflags -\/-\/libs ecore gnutls`}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#ifdef HAVE\_CONFIG\_H}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include "config.h"}}
\DoxyCodeLine{\textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <fcntl.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#ifdef HAVE\_NETINET\_TCP\_H}}
\DoxyCodeLine{\textcolor{preprocessor}{\# include <netinet/tcp.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\textcolor{preprocessor}{\#ifdef HAVE\_NETINET\_IN\_H}}
\DoxyCodeLine{\textcolor{preprocessor}{\# include <netinet/in.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\textcolor{preprocessor}{\#ifdef HAVE\_SYS\_SOCKET\_H}}
\DoxyCodeLine{\textcolor{preprocessor}{\# include <sys/socket.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\textcolor{preprocessor}{\#ifdef HAVE\_ARPA\_INET\_H}}
\DoxyCodeLine{\textcolor{preprocessor}{\# include <arpa/inet.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <errno.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <unistd.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <gnutls/gnutls.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <Ecore.h>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{/* Ecore\_Fd\_Handler example}}
\DoxyCodeLine{\textcolor{comment}{ * 2010 Mike Blumenkrantz}}
\DoxyCodeLine{\textcolor{comment}{ * compile with gcc \$(pkgconfig -\/-\/cflags -\/-\/libs gnutls ecore)}}
\DoxyCodeLine{\textcolor{comment}{ */}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#define print(...) \(\backslash\)}}
\DoxyCodeLine{\textcolor{preprocessor}{do \{ \(\backslash\)}}
\DoxyCodeLine{\textcolor{preprocessor}{  fprintf(stderr, "line \%i: ", \_\_LINE\_\_); \(\backslash\)}}
\DoxyCodeLine{\textcolor{preprocessor}{  fprintf(stderr, \_\_VA\_ARGS\_\_); \(\backslash\)}}
\DoxyCodeLine{\textcolor{preprocessor}{  fprintf(stderr, "\(\backslash\)n");\(\backslash\)}}
\DoxyCodeLine{\textcolor{preprocessor}{\} while(0)}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} \textcolor{keywordtype}{int} done = 0;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} \textcolor{keywordtype}{void}}
\DoxyCodeLine{tls\_log\_func(\textcolor{keywordtype}{int} level, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *str)}
\DoxyCodeLine{\{}
\DoxyCodeLine{   fprintf(stderr, \textcolor{stringliteral}{"|<\%d>| \%s"}, level, str);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} \textcolor{keyword}{const} \textcolor{keywordtype}{char} *}
\DoxyCodeLine{SSL\_GNUTLS\_PRINT\_HANDSHAKE\_STATUS(gnutls\_handshake\_description\_t status)}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \textcolor{keywordflow}{switch} (status)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{      \textcolor{keywordflow}{case} GNUTLS\_HANDSHAKE\_HELLO\_REQUEST:}
\DoxyCodeLine{        \textcolor{keywordflow}{return} \textcolor{stringliteral}{"Hello request"};}
\DoxyCodeLine{}
\DoxyCodeLine{      \textcolor{keywordflow}{case} GNUTLS\_HANDSHAKE\_CLIENT\_HELLO:}
\DoxyCodeLine{        \textcolor{keywordflow}{return} \textcolor{stringliteral}{"Client hello"};}
\DoxyCodeLine{}
\DoxyCodeLine{      \textcolor{keywordflow}{case} GNUTLS\_HANDSHAKE\_SERVER\_HELLO:}
\DoxyCodeLine{        \textcolor{keywordflow}{return} \textcolor{stringliteral}{"Server hello"};}
\DoxyCodeLine{}
\DoxyCodeLine{      \textcolor{keywordflow}{case} GNUTLS\_HANDSHAKE\_CERTIFICATE\_PKT:}
\DoxyCodeLine{        \textcolor{keywordflow}{return} \textcolor{stringliteral}{"Certificate packet"};}
\DoxyCodeLine{}
\DoxyCodeLine{      \textcolor{keywordflow}{case} GNUTLS\_HANDSHAKE\_SERVER\_KEY\_EXCHANGE:}
\DoxyCodeLine{        \textcolor{keywordflow}{return} \textcolor{stringliteral}{"Server key exchange"};}
\DoxyCodeLine{}
\DoxyCodeLine{      \textcolor{keywordflow}{case} GNUTLS\_HANDSHAKE\_CERTIFICATE\_REQUEST:}
\DoxyCodeLine{        \textcolor{keywordflow}{return} \textcolor{stringliteral}{"Certificate request"};}
\DoxyCodeLine{}
\DoxyCodeLine{      \textcolor{keywordflow}{case} GNUTLS\_HANDSHAKE\_SERVER\_HELLO\_DONE:}
\DoxyCodeLine{        \textcolor{keywordflow}{return} \textcolor{stringliteral}{"Server hello done"};}
\DoxyCodeLine{}
\DoxyCodeLine{      \textcolor{keywordflow}{case} GNUTLS\_HANDSHAKE\_CERTIFICATE\_VERIFY:}
\DoxyCodeLine{        \textcolor{keywordflow}{return} \textcolor{stringliteral}{"Certificate verify"};}
\DoxyCodeLine{}
\DoxyCodeLine{      \textcolor{keywordflow}{case} GNUTLS\_HANDSHAKE\_CLIENT\_KEY\_EXCHANGE:}
\DoxyCodeLine{        \textcolor{keywordflow}{return} \textcolor{stringliteral}{"Client key exchange"};}
\DoxyCodeLine{}
\DoxyCodeLine{      \textcolor{keywordflow}{case} GNUTLS\_HANDSHAKE\_FINISHED:}
\DoxyCodeLine{        \textcolor{keywordflow}{return} \textcolor{stringliteral}{"Finished"};}
\DoxyCodeLine{}
\DoxyCodeLine{      \textcolor{keywordflow}{case} GNUTLS\_HANDSHAKE\_SUPPLEMENTAL:}
\DoxyCodeLine{        \textcolor{keywordflow}{return} \textcolor{stringliteral}{"Supplemental"};}
\DoxyCodeLine{      \textcolor{keywordflow}{default}:}
\DoxyCodeLine{        \textcolor{keywordflow}{return} \textcolor{stringliteral}{"Uncaught state"};}
\DoxyCodeLine{     \}}
\DoxyCodeLine{   \textcolor{keywordflow}{return} NULL;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{/* Connects to the peer and returns a socket}}
\DoxyCodeLine{\textcolor{comment}{ * descriptor.}}
\DoxyCodeLine{\textcolor{comment}{ */}}
\DoxyCodeLine{\textcolor{keyword}{static} \textcolor{keywordtype}{int}}
\DoxyCodeLine{tcp\_connect(\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *PORT = \textcolor{stringliteral}{"443"};}
\DoxyCodeLine{   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *SERVER = \textcolor{stringliteral}{"69.58.181.89"}; \textcolor{comment}{//verisign.com}}
\DoxyCodeLine{   \textcolor{keywordtype}{int} err, sd;}
\DoxyCodeLine{   \textcolor{keywordtype}{int} flag = 1, curstate = 0;}
\DoxyCodeLine{   \textcolor{keyword}{struct }sockaddr\_in sa;}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{/* sets some fd options such as nonblock */}}
\DoxyCodeLine{   sd = socket(AF\_INET, SOCK\_STREAM, 0);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Evil_gad39ebe4c4767eaa9a680777400cfaafe}{fcntl}}(sd, F\_SETFL, O\_NONBLOCK);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__File__Group_ga4b892636498d8917d995a652a8bdd6b7}{eina\_file\_close\_on\_exec}}(sd, \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}});}
\DoxyCodeLine{   setsockopt(sd, SOL\_SOCKET, SO\_REUSEADDR, (\textcolor{keyword}{const} \textcolor{keywordtype}{void} *)\&curstate, \textcolor{keyword}{sizeof}(curstate));}
\DoxyCodeLine{}
\DoxyCodeLine{   setsockopt(sd, IPPROTO\_TCP, TCP\_NODELAY, (\textcolor{keywordtype}{char} *)\&flag, \textcolor{keyword}{sizeof}(\textcolor{keywordtype}{int}));}
\DoxyCodeLine{}
\DoxyCodeLine{   memset(\&sa, \textcolor{charliteral}{'\(\backslash\)0'}, \textcolor{keyword}{sizeof} (sa));}
\DoxyCodeLine{   sa.sin\_family = AF\_INET;}
\DoxyCodeLine{   sa.sin\_port = eina\_htons(atoi(PORT));}
\DoxyCodeLine{   inet\_pton(AF\_INET, SERVER, \&sa.sin\_addr);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{/* connects to server}}
\DoxyCodeLine{\textcolor{comment}{    */}}
\DoxyCodeLine{   err = connect(sd, (\textcolor{keyword}{struct} sockaddr *)\&sa, \textcolor{keyword}{sizeof} (sa));}
\DoxyCodeLine{   \textcolor{keywordflow}{if} ((err < 0) \&\& (errno != EINPROGRESS))}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        print(\textcolor{stringliteral}{"Connect error\(\backslash\)n"});}
\DoxyCodeLine{        exit(1);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{return} sd;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{/* closes the given socket descriptor.}}
\DoxyCodeLine{\textcolor{comment}{ */}}
\DoxyCodeLine{\textcolor{keyword}{static} \textcolor{keywordtype}{void}}
\DoxyCodeLine{tcp\_close(\textcolor{keywordtype}{int} sd)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\textcolor{preprocessor}{\#ifdef \_WIN32}}
\DoxyCodeLine{   shutdown(sd, SD\_BOTH);     \textcolor{comment}{/* no more receptions */}}
\DoxyCodeLine{   closesocket(sd);}
\DoxyCodeLine{\textcolor{preprocessor}{\#else}}
\DoxyCodeLine{   shutdown(sd, SHUT\_RDWR);     \textcolor{comment}{/* no more receptions */}}
\DoxyCodeLine{   close(sd);}
\DoxyCodeLine{\textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_process\_data(gnutls\_session\_t client, \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ga225121ed2123da4b52b76aa08d78c220}{Ecore\_Fd\_Handler}} *fd\_handler)}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \textcolor{keyword}{static} \textcolor{keywordtype}{int} ret, lastret;}
\DoxyCodeLine{   \textcolor{keyword}{static} \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} count = 0;}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (!done)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        lastret = ret;}
\DoxyCodeLine{        ret = gnutls\_handshake(client);}
\DoxyCodeLine{        count++;}
\DoxyCodeLine{        \textcolor{keywordflow}{if} (gnutls\_record\_get\_direction(client))}
\DoxyCodeLine{          \mbox{\hyperlink{group__Ecore__FD__Handler__Group_gac7d30608fdd863ebde9a7870e2e7f1ff}{ecore\_main\_fd\_handler\_active\_set}}(fd\_handler, \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ggab5644f471dfa3bd182619601d9b60608a654f7e6af4875742c5349c2cc55d1bab}{ECORE\_FD\_WRITE}});}
\DoxyCodeLine{        \textcolor{keywordflow}{else}}
\DoxyCodeLine{          \mbox{\hyperlink{group__Ecore__FD__Handler__Group_gac7d30608fdd863ebde9a7870e2e7f1ff}{ecore\_main\_fd\_handler\_active\_set}}(fd\_handler, \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ggab5644f471dfa3bd182619601d9b60608a1699f804eccf88e7e169a4a20f1d44e7}{ECORE\_FD\_READ}});}
\DoxyCodeLine{        \textcolor{comment}{/* avoid printing messages infinity times */}}
\DoxyCodeLine{        \textcolor{keywordflow}{if} (lastret != ret)}
\DoxyCodeLine{          \{}
\DoxyCodeLine{             print(\textcolor{stringliteral}{"gnutls returned with: \%s -\/ \%s"}, gnutls\_strerror\_name(ret), gnutls\_strerror(ret));}
\DoxyCodeLine{             \textcolor{keywordflow}{if} ((ret == GNUTLS\_E\_WARNING\_ALERT\_RECEIVED) || (ret == GNUTLS\_E\_FATAL\_ALERT\_RECEIVED))}
\DoxyCodeLine{               print(\textcolor{stringliteral}{"Also received alert: \%s"}, gnutls\_alert\_get\_name(gnutls\_alert\_get(client)));}
\DoxyCodeLine{             print(\textcolor{stringliteral}{"last out: \%s"}, SSL\_GNUTLS\_PRINT\_HANDSHAKE\_STATUS(gnutls\_handshake\_get\_last\_out(client)));}
\DoxyCodeLine{             print(\textcolor{stringliteral}{"last in: \%s"}, SSL\_GNUTLS\_PRINT\_HANDSHAKE\_STATUS(gnutls\_handshake\_get\_last\_in(client)));}
\DoxyCodeLine{          \}}
\DoxyCodeLine{}
\DoxyCodeLine{        \textcolor{keywordflow}{if} (gnutls\_error\_is\_fatal(ret))}
\DoxyCodeLine{          \{}
\DoxyCodeLine{             print(\textcolor{stringliteral}{"yarrr this be an error!"});}
\DoxyCodeLine{             exit(1);}
\DoxyCodeLine{          \}}
\DoxyCodeLine{     \}}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (ret == GNUTLS\_E\_SUCCESS)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        done = 1;}
\DoxyCodeLine{        print(\textcolor{stringliteral}{"Handshake successful in \%u handshake calls!"}, count);}
\DoxyCodeLine{        \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga95cf8e97dff0716433c2c5474d606a98}{ecore\_main\_loop\_quit}}();}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Ecore__Main__Loop__Group_ga1ee9db07f9a46a8b20fb83e1c6dbed09}{ECORE\_CALLBACK\_RENEW}};}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \textcolor{comment}{/* credentials */}}
\DoxyCodeLine{   gnutls\_anon\_client\_credentials\_t c\_anoncred;}
\DoxyCodeLine{   gnutls\_certificate\_credentials\_t c\_certcred;}
\DoxyCodeLine{}
\DoxyCodeLine{   gnutls\_session\_t client;}
\DoxyCodeLine{   \textcolor{keywordtype}{int} sd;}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{/* General init. */}}
\DoxyCodeLine{   gnutls\_global\_init();}
\DoxyCodeLine{   \mbox{\hyperlink{group__Ecore__Init__Group_ga77757609684a2c922dc5ec398274751b}{ecore\_init}}();}
\DoxyCodeLine{   gnutls\_global\_set\_log\_function(tls\_log\_func);}
\DoxyCodeLine{   gnutls\_global\_set\_log\_level(6);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{/* Init client */}}
\DoxyCodeLine{   gnutls\_anon\_allocate\_client\_credentials(\&c\_anoncred);}
\DoxyCodeLine{   gnutls\_certificate\_allocate\_credentials(\&c\_certcred);}
\DoxyCodeLine{   gnutls\_init(\&client, GNUTLS\_CLIENT);}
\DoxyCodeLine{   \textcolor{comment}{/* set very specific priorities */}}
\DoxyCodeLine{   gnutls\_priority\_set\_direct(client, \textcolor{stringliteral}{"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);}
\DoxyCodeLine{   gnutls\_credentials\_set(client, GNUTLS\_CRD\_ANON, c\_anoncred);}
\DoxyCodeLine{   gnutls\_credentials\_set(client, GNUTLS\_CRD\_CERTIFICATE, c\_certcred);}
\DoxyCodeLine{   gnutls\_server\_name\_set(client, GNUTLS\_NAME\_DNS, \textcolor{stringliteral}{"www.verisign.com"}, strlen(\textcolor{stringliteral}{"www.verisign.com"}));}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{/* connect to the peer}}
\DoxyCodeLine{\textcolor{comment}{    */}}
\DoxyCodeLine{   sd = tcp\_connect();}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{/* associate gnutls with socket */}}
\DoxyCodeLine{   gnutls\_transport\_set\_ptr(client, (gnutls\_transport\_ptr\_t)(uintptr\_t)sd);}
\DoxyCodeLine{   \textcolor{comment}{/* add a callback for data being available for send/receive on socket */}}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (!\mbox{\hyperlink{group__Ecore__FD__Handler__Group_ga832e68eb7f15912abeadef816bb5d2f5}{ecore\_main\_fd\_handler\_add}}(sd, \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ggab5644f471dfa3bd182619601d9b60608a1699f804eccf88e7e169a4a20f1d44e7}{ECORE\_FD\_READ}} | \mbox{\hyperlink{group__Ecore__FD__Handler__Group_ggab5644f471dfa3bd182619601d9b60608a654f7e6af4875742c5349c2cc55d1bab}{ECORE\_FD\_WRITE}}, (\mbox{\hyperlink{group__Ecore__FD__Handler__Group_ga233a4c637db3fe2257c08ef64ca42b4a}{Ecore\_Fd\_Cb}})\_process\_data, client, NULL, NULL))}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        print(\textcolor{stringliteral}{"could not create fd handler!"});}
\DoxyCodeLine{        exit(1);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{   \textcolor{comment}{/* begin main loop */}}
\DoxyCodeLine{   \mbox{\hyperlink{group__Ecore__Main__Loop__Group_gaf103b9f668bb3e4fed12e52c6180132d}{ecore\_main\_loop\_begin}}();}
\DoxyCodeLine{}
\DoxyCodeLine{   gnutls\_bye(client, GNUTLS\_SHUT\_RDWR);}
\DoxyCodeLine{}
\DoxyCodeLine{   gnutls\_deinit(client);}
\DoxyCodeLine{}
\DoxyCodeLine{   tcp\_close(sd);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{return} 0;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\end{DoxyCodeInclude}
 