summaryrefslogtreecommitdiff
path: root/src/lib/ecore_con/efl_net_dialer_ssl.c
blob: efc9d8e34cf19c21a271e34516ebb3635a8fc0ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#define EFL_NET_DIALER_SSL_PROTECTED 1
#define EFL_NET_SOCKET_SSL_PROTECTED 1
#define EFL_NET_DIALER_PROTECTED 1
#define EFL_NET_SOCKET_PROTECTED 1
#define EFL_IO_READER_PROTECTED 1

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "Ecore.h"
#include "Ecore_Con.h"
#include "ecore_con_private.h"

#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_SSL_H
# include <netinet/ssl.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif

#define MY_CLASS EFL_NET_DIALER_SSL_CLASS

typedef struct _Efl_Net_Dialer_Ssl_Data
{
   Eo *sock;
   Eo *ssl_ctx;
   Eina_Future *connect_timeout;
   Eina_Bool connected;
} Efl_Net_Dialer_Ssl_Data;

static void
_efl_net_dialer_ssl_ready(void *data EINA_UNUSED, const Efl_Event *event)
{
   Eo *o = event->object;
   efl_net_dialer_connected_set(o, EINA_TRUE);
}

static void
_efl_net_dialer_ssl_error(void *data EINA_UNUSED, const Efl_Event *event)
{
   Eo *o = event->object;
   Eina_Error *perr = event->info;
   efl_event_callback_call(o, EFL_NET_DIALER_EVENT_DIALER_ERROR, perr);
}

EFL_CALLBACKS_ARRAY_DEFINE(_efl_net_dialer_ssl_cbs,
                           { EFL_NET_SOCKET_SSL_EVENT_SSL_READY, _efl_net_dialer_ssl_ready },
                           { EFL_NET_SOCKET_SSL_EVENT_SSL_ERROR, _efl_net_dialer_ssl_error });

EOLIAN static Eo*
_efl_net_dialer_ssl_efl_object_constructor(Eo *o, Efl_Net_Dialer_Ssl_Data *pd)
{
   o = efl_constructor(efl_super(o, MY_CLASS));
   if (!o) return NULL;

   pd->sock = efl_add_ref(EFL_NET_DIALER_TCP_CLASS, o);
   EINA_SAFETY_ON_NULL_RETURN_VAL(pd->sock, NULL);

   efl_event_callback_array_add(o, _efl_net_dialer_ssl_cbs(), o);

   return o;
}

EOLIAN static Eo*
_efl_net_dialer_ssl_efl_object_finalize(Eo *o, Efl_Net_Dialer_Ssl_Data *pd)
{
   Eo *a_sock, *a_ctx;

   if (efl_net_socket_ssl_adopted_get(o, &a_sock, &a_ctx))
     {
        efl_replace(&pd->sock, a_sock); /* stub TCP we created */
        efl_replace(&pd->ssl_ctx, a_ctx);
     }
   else
     {
        if (!pd->ssl_ctx)
          pd->ssl_ctx = efl_ref(efl_net_ssl_context_default_dialer_get());

        efl_net_socket_ssl_adopt(o, pd->sock, pd->ssl_ctx);
     }

   return efl_finalize(efl_super(o, MY_CLASS));
}

EOLIAN static void
_efl_net_dialer_ssl_efl_object_invalidate(Eo *o, Efl_Net_Dialer_Ssl_Data *pd)
{
   pd->sock = NULL;

   if (efl_io_closer_close_on_invalidate_get(o) &&
       (!efl_io_closer_closed_get(o)))
     {
        efl_event_freeze(o);
        efl_io_closer_close(o);
        efl_event_thaw(o);
     }

   efl_replace(&pd->ssl_ctx, NULL);
   efl_replace(&pd->sock, NULL);

   efl_invalidate(efl_super(o, MY_CLASS));
}

EOLIAN static void
_efl_net_dialer_ssl_ssl_context_set(Eo *o, Efl_Net_Dialer_Ssl_Data *pd, Eo *ssl_ctx)
{
   EINA_SAFETY_ON_TRUE_RETURN(efl_finalized_get(o));
   EINA_SAFETY_ON_FALSE_RETURN(efl_isa(ssl_ctx, EFL_NET_SSL_CONTEXT_CLASS));

   if (pd->ssl_ctx == ssl_ctx) return;
   efl_replace(&pd->ssl_ctx, ssl_ctx);
}

EOLIAN static Eo *
_efl_net_dialer_ssl_ssl_context_get(const Eo *o EINA_UNUSED, Efl_Net_Dialer_Ssl_Data *pd)
{
   return pd->ssl_ctx;
}

static Eina_Value
_efl_net_dialer_ssl_connect_timeout(Eo *o, void *data EINA_UNUSED, const Eina_Value v)
{
   Eina_Error err = ETIMEDOUT;

   efl_ref(o);
   efl_io_reader_eos_set(o, EINA_TRUE);
   efl_event_callback_call(o, EFL_NET_DIALER_EVENT_DIALER_ERROR, &err);
   efl_unref(o);
   return v;
}

static void
_timeout_schedule(Eo *o, Efl_Net_Dialer_Ssl_Data *pd, double timeout)
{
   efl_future_then(o, efl_loop_timeout(efl_loop_get(o), timeout),
                   .success = _efl_net_dialer_ssl_connect_timeout,
                   .storage = &pd->connect_timeout);
}

EOLIAN static Eina_Error
_efl_net_dialer_ssl_efl_net_dialer_dial(Eo *o, Efl_Net_Dialer_Ssl_Data *pd, const char *address)
{
   double timeout;

   EINA_SAFETY_ON_NULL_RETURN_VAL(address, EINVAL);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(efl_net_dialer_connected_get(o), EISCONN);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(efl_io_closer_closed_get(o), EBADF);

   if (pd->connect_timeout) eina_future_cancel(pd->connect_timeout);

   timeout = efl_net_dialer_timeout_dial_get(pd->sock);
   if (timeout > 0.0) _timeout_schedule(o, pd, timeout);

   return efl_net_dialer_dial(pd->sock, address);
}

EOLIAN static const char *
_efl_net_dialer_ssl_efl_net_dialer_address_dial_get(const Eo *o EINA_UNUSED, Efl_Net_Dialer_Ssl_Data *pd)
{
   return efl_net_dialer_address_dial_get(pd->sock);
}

EOLIAN static void
_efl_net_dialer_ssl_efl_net_dialer_proxy_set(Eo *o EINA_UNUSED, Efl_Net_Dialer_Ssl_Data *pd, const char *proxy_url)
{
   efl_net_dialer_proxy_set(pd->sock, proxy_url);
}

EOLIAN static const char *
_efl_net_dialer_ssl_efl_net_dialer_proxy_get(const Eo *o EINA_UNUSED, Efl_Net_Dialer_Ssl_Data *pd)
{
   return efl_net_dialer_proxy_get(pd->sock);
}

EOLIAN static void
_efl_net_dialer_ssl_efl_net_dialer_timeout_dial_set(Eo *o, Efl_Net_Dialer_Ssl_Data *pd, double seconds)
{
   efl_net_dialer_timeout_dial_set(pd->sock, seconds);

   if (pd->connect_timeout) eina_future_cancel(pd->connect_timeout);

   if ((seconds > 0.0) && (!pd->connected)) _timeout_schedule(o, pd, seconds);
}

EOLIAN static double
_efl_net_dialer_ssl_efl_net_dialer_timeout_dial_get(const Eo *o EINA_UNUSED, Efl_Net_Dialer_Ssl_Data *pd)
{
   return efl_net_dialer_timeout_dial_get(pd->sock);
}

EOLIAN static void
_efl_net_dialer_ssl_efl_net_dialer_connected_set(Eo *o, Efl_Net_Dialer_Ssl_Data *pd, Eina_Bool connected)
{
   if (pd->connect_timeout)
     eina_future_cancel(pd->connect_timeout);
   if (pd->connected == connected) return;
   pd->connected = connected;
   if (connected) efl_event_callback_call(o, EFL_NET_DIALER_EVENT_DIALER_CONNECTED, NULL);
}

EOLIAN static Eina_Bool
_efl_net_dialer_ssl_efl_net_dialer_connected_get(const Eo *o EINA_UNUSED, Efl_Net_Dialer_Ssl_Data *pd)
{
   return pd->connected;
}

EOLIAN static Eina_Error
_efl_net_dialer_ssl_efl_io_closer_close(Eo *o, Efl_Net_Dialer_Ssl_Data *pd EINA_UNUSED)
{
   efl_net_dialer_connected_set(o, EINA_FALSE);
   return efl_io_closer_close(efl_super(o, MY_CLASS));
}

EOLIAN static Eina_Bool
_efl_net_dialer_ssl_keep_alive_set(Eo *o EINA_UNUSED, Efl_Net_Dialer_Ssl_Data *pd, Eina_Bool keep_alive)
{
   return efl_net_socket_tcp_keep_alive_set(pd->sock, keep_alive);
}

EOLIAN static Eina_Bool
_efl_net_dialer_ssl_keep_alive_get(const Eo *o EINA_UNUSED, Efl_Net_Dialer_Ssl_Data *pd)
{
   return efl_net_socket_tcp_keep_alive_get(pd->sock);
}

EOLIAN static Eina_Bool
_efl_net_dialer_ssl_no_delay_set(Eo *o EINA_UNUSED, Efl_Net_Dialer_Ssl_Data *pd, Eina_Bool no_delay)
{
   return efl_net_socket_tcp_no_delay_set(pd->sock, no_delay);
}

EOLIAN static Eina_Bool
_efl_net_dialer_ssl_no_delay_get(const Eo *o EINA_UNUSED, Efl_Net_Dialer_Ssl_Data *pd)
{
   return efl_net_socket_tcp_no_delay_get(pd->sock);
}

#include "efl_net_dialer_ssl.eo.c"