summaryrefslogtreecommitdiff
path: root/src/lib/ecore_con/efl_net_dialer_udp.c
blob: 1c7eda3fd14e9bd550af405c8235e4c1222477e5 (plain) (blame)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#define EFL_NET_DIALER_UDP_PROTECTED 1
#define EFL_NET_DIALER_PROTECTED 1
#define EFL_NET_SOCKET_UDP_PROTECTED 1
#define EFL_NET_SOCKET_FD_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_UDP_H
# include <netinet/udp.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#ifdef _WIN32
# include <Evil.h>
#endif

#define MY_CLASS EFL_NET_DIALER_UDP_CLASS

typedef struct _Efl_Net_Dialer_Udp_Data
{
   struct {
      Ecore_Thread *thread;
      Eina_Future *timeout;
   } resolver;
   Eina_Stringshare *address_dial;
   Eina_Bool connected;
   Eina_Bool closed;
   double timeout_dial;
} Efl_Net_Dialer_Udp_Data;

EOLIAN static Eo*
_efl_net_dialer_udp_efl_object_constructor(Eo *o, Efl_Net_Dialer_Udp_Data *pd EINA_UNUSED)
{
   o = efl_constructor(efl_super(o, MY_CLASS));
   if (!o) return NULL;

   efl_net_dialer_timeout_dial_set(o, 30.0);
   return o;
}

EOLIAN static void
_efl_net_dialer_udp_efl_object_destructor(Eo *o, Efl_Net_Dialer_Udp_Data *pd)
{
   if (efl_io_closer_close_on_destructor_get(o) &&
       (!efl_io_closer_closed_get(o)))
     {
        efl_event_freeze(o);
        efl_io_closer_close(o);
        efl_event_thaw(o);
     }

   if (pd->resolver.thread)
     {
        ecore_thread_cancel(pd->resolver.thread);
        pd->resolver.thread = NULL;
     }

   efl_destructor(efl_super(o, MY_CLASS));

   eina_stringshare_replace(&pd->address_dial, NULL);
}

static Eina_Value
_efl_net_dialer_udp_resolver_timeout(Eo *o, const Eina_Value v)
{
   Efl_Net_Dialer_Udp_Data *pd = efl_data_scope_get(o, MY_CLASS);
   Eina_Error err = ETIMEDOUT;

   if (pd->resolver.thread)
     {
        ecore_thread_cancel(pd->resolver.thread);
        pd->resolver.thread = NULL;
     }

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

static void
_timeout_schedule(Eo *o, Efl_Net_Dialer_Udp_Data *pd)
{
   efl_future_Eina_FutureXXX_then(o, efl_loop_timeout(efl_loop_get(o), pd->timeout_dial),
                                  .success = _efl_net_dialer_udp_resolver_timeout,
                                  .storage = &pd->resolver.timeout);
}

static Eina_Error
_efl_net_dialer_udp_resolved_bind(Eo *o, Efl_Net_Dialer_Udp_Data *pd EINA_UNUSED, struct addrinfo *addr)
{
   Eina_Error err = 0;
   Eo *remote_address;
   char buf[INET6_ADDRSTRLEN + sizeof("[]:65536")];
   SOCKET fd;
   int family = addr->ai_family;

   efl_net_socket_fd_family_set(o, family);

   fd = efl_net_socket4(family, addr->ai_socktype, addr->ai_protocol,
                        efl_io_closer_close_on_exec_get(o));
   if (fd == INVALID_SOCKET)
     {
        err = efl_net_socket_error_get();
        ERR("socket(%d, %d, %d): %s",
            family, addr->ai_socktype, addr->ai_protocol,
            eina_error_msg_get(err));
        return err;
     }

   if (!efl_net_socket_udp_bind_get(o))
     {
        if (family == AF_INET)
          efl_net_socket_udp_bind_set(o, "0.0.0.0:0");
        else
          efl_net_socket_udp_bind_set(o, "[::]:0");
     }

   efl_loop_fd_set(o, fd); /* will also apply reuse_address et al */

   if (family == AF_INET)
     {
        const struct sockaddr_in *a = (const struct sockaddr_in *)addr->ai_addr;
        uint32_t ipv4 = ntohl(a->sin_addr.s_addr);
        if (ipv4 == INADDR_BROADCAST)
          {
#ifdef _WIN32
             DWORD enable = 1;
#else
             int enable = 1;
#endif
             if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (const char *)&enable, sizeof(enable)) == 0)
               DBG("enabled SO_BROADCAST for socket=" SOCKET_FMT, fd);
             else
               WRN("could not enable SO_BROADCAST for socket=" SOCKET_FMT ": %s", fd, eina_error_msg_get(efl_net_socket_error_get()));
          }
     }
   else if (family == AF_INET6)
     {
        const struct sockaddr_in6 *a = (const struct sockaddr_in6 *)addr->ai_addr;
        if (IN6_IS_ADDR_MULTICAST(&a->sin6_addr))
          {
             struct ipv6_mreq mreq = {
               .ipv6mr_multiaddr = a->sin6_addr,
             };
             if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, (const char *)&mreq, sizeof(mreq)) == 0)
               {
                  efl_net_ip_port_fmt(buf, sizeof(buf), addr->ai_addr);
                  DBG("joined multicast group %s socket=" SOCKET_FMT, buf, fd);
               }
             else
               {
                  err = efl_net_socket_error_get();
                  efl_net_ip_port_fmt(buf, sizeof(buf), addr->ai_addr);
                  ERR("could not join multicast group %s socket=" SOCKET_FMT ": %s", buf, fd, eina_error_msg_get(err));
                  goto error;
               }
          }
     }

   remote_address = efl_net_ip_address_create_sockaddr(EFL_NET_IP_ADDRESS_CLASS, addr->ai_addr);
   if (remote_address)
     {
        efl_net_socket_udp_init(o, remote_address);
        efl_event_callback_call(o, EFL_NET_DIALER_EVENT_RESOLVED, NULL);
        efl_del(remote_address);
     }
   efl_net_dialer_connected_set(o, EINA_TRUE);
   return 0;

 error:
   efl_net_socket_fd_family_set(o, AF_UNSPEC);
   efl_loop_fd_set(o, SOCKET_TO_LOOP_FD(INVALID_SOCKET));
   closesocket(fd);
   return err;
}

static void
_efl_net_dialer_udp_resolved(void *data, const char *host EINA_UNUSED, const char *port EINA_UNUSED, const struct addrinfo *hints EINA_UNUSED, struct addrinfo *result, int gai_error)
{
   Eo *o = data;
   Efl_Net_Dialer_Udp_Data *pd = efl_data_scope_get(o, MY_CLASS);
   Eina_Error err = EINA_ERROR_NO_ERROR;
   struct addrinfo *addr;

   pd->resolver.thread = NULL;

   efl_ref(o); /* we're emitting callbacks then continuing the workflow */

   if (gai_error)
     {
        err = EFL_NET_ERROR_COULDNT_RESOLVE_HOST;
        goto end;
     }

   for (addr = result; addr != NULL; addr = addr->ai_next)
     {
        err = _efl_net_dialer_udp_resolved_bind(o, pd, addr);
        if (err == 0) break;
     }

 end:
   if (err)
     {
        if (result)
          {
             char buf[INET6_ADDRSTRLEN + sizeof("[]:65536")] = "";
             if (efl_net_ip_port_fmt(buf, sizeof(buf), result->ai_addr))
               {
                  efl_net_socket_address_remote_set(o, buf);
                  efl_event_callback_call(o, EFL_NET_DIALER_EVENT_RESOLVED, NULL);
               }
          }

        efl_io_reader_eos_set(o, EINA_TRUE);
        efl_event_callback_call(o, EFL_NET_DIALER_EVENT_ERROR, &err);
     }
   freeaddrinfo(result);

   efl_unref(o);
}

EOLIAN static Eina_Error
_efl_net_dialer_udp_efl_net_dialer_dial(Eo *o, Efl_Net_Dialer_Udp_Data *pd EINA_UNUSED, const char *address)
{
   char *str;
   const char *host, *port;
   struct addrinfo hints = {
     .ai_socktype = SOCK_DGRAM,
     .ai_protocol = IPPROTO_UDP,
     .ai_family = AF_UNSPEC,
     .ai_flags = AI_ADDRCONFIG | AI_V4MAPPED,
   };

   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);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(efl_loop_fd_get(o) >= 0, EALREADY);

   if (pd->resolver.thread)
     {
        ecore_thread_cancel(pd->resolver.thread);
        pd->resolver.thread = NULL;
     }

   str = strdup(address);
   if (!efl_net_ip_port_split(str, &host, &port))
     {
        free(str);
        return EINVAL;
     }
   if (!port) port = "0";
   if (strchr(host, ':')) hints.ai_family = AF_INET6;

   pd->resolver.thread = efl_net_ip_resolve_async_new(host, port, &hints,
                                                     _efl_net_dialer_udp_resolved, o);
   free(str);
   EINA_SAFETY_ON_NULL_RETURN_VAL(pd->resolver.thread, EINVAL);

   efl_net_dialer_address_dial_set(o, address);

   if (pd->resolver.timeout) eina_future_cancel(pd->resolver.timeout);
   if (pd->timeout_dial > 0.0) _timeout_schedule(o, pd);

   return 0;
}

EOLIAN static void
_efl_net_dialer_udp_efl_net_dialer_address_dial_set(Eo *o EINA_UNUSED, Efl_Net_Dialer_Udp_Data *pd, const char *address)
{
   eina_stringshare_replace(&pd->address_dial, address);
}

EOLIAN static const char *
_efl_net_dialer_udp_efl_net_dialer_address_dial_get(const Eo *o EINA_UNUSED, Efl_Net_Dialer_Udp_Data *pd)
{
   return pd->address_dial;
}

EOLIAN static void
_efl_net_dialer_udp_efl_net_dialer_timeout_dial_set(Eo *o EINA_UNUSED, Efl_Net_Dialer_Udp_Data *pd, double seconds)
{
   pd->timeout_dial = seconds;
   if (pd->resolver.timeout) eina_future_cancel(pd->resolver.timeout);
   if ((pd->timeout_dial > 0.0) && (pd->resolver.thread)) _timeout_schedule(o, pd);
}

EOLIAN static double
_efl_net_dialer_udp_efl_net_dialer_timeout_dial_get(const Eo *o EINA_UNUSED, Efl_Net_Dialer_Udp_Data *pd)
{
   return pd->timeout_dial;
}

EOLIAN static void
_efl_net_dialer_udp_efl_net_dialer_connected_set(Eo *o, Efl_Net_Dialer_Udp_Data *pd, Eina_Bool connected)
{
   if (pd->resolver.timeout) eina_future_cancel(pd->resolver.timeout);
   if (pd->connected == connected) return;
   pd->connected = connected;
   if (connected) efl_event_callback_call(o, EFL_NET_DIALER_EVENT_CONNECTED, NULL);
}

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

EOLIAN static Eina_Error
_efl_net_dialer_udp_efl_io_closer_close(Eo *o, Efl_Net_Dialer_Udp_Data *pd)
{
   pd->closed = EINA_TRUE;
   efl_net_dialer_connected_set(o, EINA_FALSE);
   return efl_io_closer_close(efl_super(o, MY_CLASS));
}

#include "efl_net_dialer_udp.eo.c"