aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ecore/src/lib/ecore_con/ecore_con_info.c
diff options
context:
space:
mode:
authorDavid Walter Seikel2013-01-13 17:29:19 +1000
committerDavid Walter Seikel2013-01-13 17:29:19 +1000
commit07274513e984f0b5544586c74508ccd16e7dcafa (patch)
treeb32ff2a9136fbc1a4a6a0ed1e4d79cde0f5f16d9 /libraries/ecore/src/lib/ecore_con/ecore_con_info.c
parentAdded Irrlicht 1.8, but without all the Windows binaries. (diff)
downloadSledjHamr-07274513e984f0b5544586c74508ccd16e7dcafa.zip
SledjHamr-07274513e984f0b5544586c74508ccd16e7dcafa.tar.gz
SledjHamr-07274513e984f0b5544586c74508ccd16e7dcafa.tar.bz2
SledjHamr-07274513e984f0b5544586c74508ccd16e7dcafa.tar.xz
Remove EFL, since it's been released now.
Diffstat (limited to '')
-rw-r--r--libraries/ecore/src/lib/ecore_con/ecore_con_info.c449
1 files changed, 0 insertions, 449 deletions
diff --git a/libraries/ecore/src/lib/ecore_con/ecore_con_info.c b/libraries/ecore/src/lib/ecore_con/ecore_con_info.c
deleted file mode 100644
index fdcf0b9..0000000
--- a/libraries/ecore/src/lib/ecore_con/ecore_con_info.c
+++ /dev/null
@@ -1,449 +0,0 @@
1/*
2 * getaddrinfo with callback
3 *
4 * man getaddrinfo
5 *
6 */
7
8#ifdef HAVE_CONFIG_H
9# include <config.h>
10#endif
11
12#ifdef HAVE_ALLOCA_H
13# include <alloca.h>
14#elif defined __GNUC__
15# define alloca __builtin_alloca
16#elif defined _AIX
17# define alloca __alloca
18#elif defined _MSC_VER
19# include <malloc.h>
20# define alloca _alloca
21#else
22# include <stddef.h>
23# ifdef __cplusplus
24extern "C"
25# endif
26void *alloca(size_t);
27#endif
28
29#include <string.h>
30#include <sys/types.h>
31#include <unistd.h>
32#include <ctype.h>
33#ifdef __OpenBSD__
34# include <sys/types.h>
35#endif
36
37#ifdef HAVE_NETINET_IN_H
38# include <netinet/in.h>
39#endif
40
41#ifdef HAVE_ARPA_INET_H
42# include <arpa/inet.h>
43#endif
44
45#ifdef HAVE_ARPA_NAMESER_H
46# include <arpa/nameser.h>
47#endif
48
49#ifdef HAVE_SYS_SOCKET_H
50# include <sys/socket.h>
51#endif
52
53#ifdef HAVE_NETDB_H
54# include <netdb.h>
55#endif
56
57#include <errno.h>
58
59#include "Ecore.h"
60#include "ecore_private.h"
61#include "ecore_con_private.h"
62
63typedef struct _CB_Data CB_Data;
64
65struct _CB_Data
66{
67 EINA_INLIST;
68 Ecore_Con_Info_Cb cb_done;
69 void *data;
70 Ecore_Fd_Handler *fdh;
71 pid_t pid;
72 Ecore_Event_Handler *handler;
73 int fd2;
74};
75
76static void _ecore_con_info_readdata(CB_Data *cbdata);
77static void _ecore_con_info_slave_free(CB_Data *cbdata);
78static Eina_Bool _ecore_con_info_data_handler(void *data,
79 Ecore_Fd_Handler *fd_handler);
80static Eina_Bool _ecore_con_info_exit_handler(void *data,
81 int type __UNUSED__,
82 void *event);
83
84static int info_init = 0;
85static CB_Data *info_slaves = NULL;
86
87int
88ecore_con_info_init(void)
89{
90 info_init++;
91 return info_init;
92}
93
94int
95ecore_con_info_shutdown(void)
96{
97 info_init--;
98 if (info_init == 0)
99 while (info_slaves) _ecore_con_info_slave_free(info_slaves);
100
101 return info_init;
102}
103
104int
105ecore_con_info_tcp_connect(Ecore_Con_Server *svr,
106 Ecore_Con_Info_Cb done_cb,
107 void *data)
108{
109 struct addrinfo hints;
110
111 memset(&hints, 0, sizeof(struct addrinfo));
112 hints.ai_family = AF_UNSPEC;
113 hints.ai_socktype = SOCK_STREAM;
114 hints.ai_flags = AI_CANONNAME;
115 hints.ai_protocol = IPPROTO_TCP;
116 hints.ai_canonname = NULL;
117 hints.ai_next = NULL;
118 hints.ai_addr = NULL;
119
120 return ecore_con_info_get(svr, done_cb, data, &hints);
121}
122
123int
124ecore_con_info_tcp_listen(Ecore_Con_Server *svr,
125 Ecore_Con_Info_Cb done_cb,
126 void *data)
127{
128 struct addrinfo hints;
129
130 memset(&hints, 0, sizeof(struct addrinfo));
131 hints.ai_family = AF_UNSPEC;
132 hints.ai_socktype = SOCK_STREAM;
133 hints.ai_flags = AI_PASSIVE;
134 hints.ai_protocol = IPPROTO_TCP;
135 hints.ai_canonname = NULL;
136 hints.ai_next = NULL;
137 hints.ai_addr = NULL;
138
139 return ecore_con_info_get(svr, done_cb, data, &hints);
140}
141
142int
143ecore_con_info_udp_connect(Ecore_Con_Server *svr,
144 Ecore_Con_Info_Cb done_cb,
145 void *data)
146{
147 struct addrinfo hints;
148
149 memset(&hints, 0, sizeof(struct addrinfo));
150 hints.ai_family = AF_UNSPEC;
151 hints.ai_socktype = SOCK_DGRAM;
152 hints.ai_flags = AI_CANONNAME;
153 hints.ai_protocol = IPPROTO_UDP;
154 hints.ai_canonname = NULL;
155 hints.ai_next = NULL;
156 hints.ai_addr = NULL;
157
158 return ecore_con_info_get(svr, done_cb, data, &hints);
159}
160
161int
162ecore_con_info_udp_listen(Ecore_Con_Server *svr,
163 Ecore_Con_Info_Cb done_cb,
164 void *data)
165{
166 struct addrinfo hints;
167
168 memset(&hints, 0, sizeof(struct addrinfo));
169 hints.ai_family = AF_UNSPEC;
170 hints.ai_socktype = SOCK_DGRAM;
171 hints.ai_flags = AI_PASSIVE;
172 hints.ai_protocol = IPPROTO_UDP;
173 hints.ai_canonname = NULL;
174 hints.ai_next = NULL;
175 hints.ai_addr = NULL;
176
177 return ecore_con_info_get(svr, done_cb, data, &hints);
178}
179
180int
181ecore_con_info_mcast_listen(Ecore_Con_Server *svr,
182 Ecore_Con_Info_Cb done_cb,
183 void *data)
184{
185 struct addrinfo hints;
186
187 memset(&hints, 0, sizeof(struct addrinfo));
188 hints.ai_family = AF_UNSPEC;
189 hints.ai_socktype = SOCK_DGRAM;
190 hints.ai_flags = 0;
191 hints.ai_protocol = IPPROTO_UDP;
192 hints.ai_canonname = NULL;
193 hints.ai_next = NULL;
194 hints.ai_addr = NULL;
195
196 return ecore_con_info_get(svr, done_cb, data, &hints);
197}
198
199EAPI int
200ecore_con_info_get(Ecore_Con_Server *svr,
201 Ecore_Con_Info_Cb done_cb,
202 void *data,
203 struct addrinfo *hints)
204{
205 CB_Data *cbdata;
206 int fd[2];
207
208 if (pipe(fd) < 0)
209 {
210 ecore_con_event_server_error(svr, strerror(errno));
211 return 0;
212 }
213
214 cbdata = calloc(1, sizeof(CB_Data));
215 if (!cbdata)
216 {
217 close(fd[0]);
218 close(fd[1]);
219 return 0;
220 }
221
222 cbdata->cb_done = done_cb;
223 cbdata->data = data;
224 cbdata->fd2 = fd[1];
225 if (!(cbdata->fdh = ecore_main_fd_handler_add(fd[0], ECORE_FD_READ,
226 _ecore_con_info_data_handler,
227 cbdata,
228 NULL, NULL)))
229 {
230 ecore_con_event_server_error(svr, "Memory allocation failure");
231 free(cbdata);
232 close(fd[0]);
233 close(fd[1]);
234 return 0;
235 }
236
237 if ((cbdata->pid = fork()) == 0)
238 {
239 Ecore_Con_Info *container;
240 struct addrinfo *result = NULL;
241 char service[NI_MAXSERV] = {0};
242 char hbuf[NI_MAXHOST] = {0};
243 char sbuf[NI_MAXSERV] = {0};
244 unsigned char *tosend = NULL;
245 int tosend_len;
246 int canonname_len = 0;
247
248 eina_convert_itoa(svr->ecs ? svr->ecs->port : svr->port, service);
249 /* CHILD */
250 if (!getaddrinfo(svr->ecs ? svr->ecs->ip : svr->name, service, hints, &result) && result)
251 {
252 if (result->ai_canonname)
253 canonname_len = strlen(result->ai_canonname) + 1;
254
255 tosend_len = sizeof(Ecore_Con_Info) + result->ai_addrlen +
256 canonname_len;
257
258 tosend = alloca(tosend_len);
259 memset(tosend, 0, tosend_len);
260
261 container = (Ecore_Con_Info *)tosend;
262 container->size = tosend_len;
263
264 memcpy(&container->info,
265 result,
266 sizeof(struct addrinfo));
267 memcpy(tosend + sizeof(Ecore_Con_Info),
268 result->ai_addr,
269 result->ai_addrlen);
270 if (result->ai_canonname) /* FIXME: else... */
271 memcpy(tosend + sizeof(Ecore_Con_Info) + result->ai_addrlen,
272 result->ai_canonname,
273 canonname_len);
274
275 if (!getnameinfo(result->ai_addr, result->ai_addrlen,
276 hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
277 NI_NUMERICHOST | NI_NUMERICSERV))
278 {
279 memcpy(container->ip, hbuf, sizeof(container->ip));
280 memcpy(container->service, sbuf, sizeof(container->service));
281 }
282
283 if (write(fd[1], tosend, tosend_len) < 0) perror("write");
284 }
285
286 if (result)
287 freeaddrinfo(result);
288
289 if (write(fd[1], "", 1) < 0) perror("write");
290 close(fd[1]);
291#if defined(__USE_ISOC99) && !defined(__UCLIBC__)
292 _Exit(0);
293#else
294 _exit(0);
295#endif
296 }
297
298 /* PARENT */
299 cbdata->handler =
300 ecore_event_handler_add(ECORE_EXE_EVENT_DEL, _ecore_con_info_exit_handler,
301 cbdata);
302 close(fd[1]);
303 if (!cbdata->handler)
304 {
305 ecore_main_fd_handler_del(cbdata->fdh);
306 free(cbdata);
307 close(fd[0]);
308 return 0;
309 }
310
311 info_slaves = (CB_Data *)eina_inlist_append(EINA_INLIST_GET(
312 info_slaves),
313 EINA_INLIST_GET(cbdata));
314 svr->infos = eina_list_append(svr->infos, cbdata);
315 return 1;
316}
317
318void
319ecore_con_info_data_clear(void *info)
320{
321 CB_Data *cbdata = info;
322 cbdata->data = NULL;
323}
324
325static void
326_ecore_con_info_readdata(CB_Data *cbdata)
327{
328 Ecore_Con_Info container;
329 Ecore_Con_Info *recv_info;
330 unsigned char *torecv;
331 int torecv_len;
332
333 ssize_t size;
334
335 size = read(ecore_main_fd_handler_fd_get(cbdata->fdh), &container,
336 sizeof(Ecore_Con_Info));
337 if (size == sizeof(Ecore_Con_Info))
338 {
339 torecv_len = container.size;
340 torecv = malloc(torecv_len);
341
342 memcpy(torecv, &container, sizeof(Ecore_Con_Info));
343
344 size = read(ecore_main_fd_handler_fd_get(cbdata->fdh),
345 torecv + sizeof(Ecore_Con_Info),
346 torecv_len - sizeof(Ecore_Con_Info));
347 if ((size > 0) &&
348 ((size_t)size == torecv_len - sizeof(Ecore_Con_Info)))
349 {
350 recv_info = (Ecore_Con_Info *)torecv;
351
352 recv_info->info.ai_addr =
353 (struct sockaddr *)(torecv + sizeof(Ecore_Con_Info));
354 if ((size_t)torecv_len !=
355 (sizeof(Ecore_Con_Info) + recv_info->info.ai_addrlen))
356 recv_info->info.ai_canonname = (char *)
357 (torecv + sizeof(Ecore_Con_Info) + recv_info->info.ai_addrlen);
358 else
359 recv_info->info.ai_canonname = NULL;
360
361 recv_info->info.ai_next = NULL;
362
363 if (cbdata->data)
364 {
365 cbdata->cb_done(cbdata->data, recv_info);
366 ecore_con_server_infos_del(cbdata->data, cbdata);
367 }
368
369 free(torecv);
370 }
371 else
372 {
373 if (cbdata->data)
374 {
375 cbdata->cb_done(cbdata->data, NULL);
376 ecore_con_server_infos_del(cbdata->data, cbdata);
377 }
378 }
379 }
380 else
381 {
382 if (cbdata->data)
383 {
384 ecore_con_event_server_error(cbdata->data, strerror(errno));
385 cbdata->cb_done(cbdata->data, NULL);
386 ecore_con_server_infos_del(cbdata->data, cbdata);
387 }
388 }
389
390 cbdata->cb_done = NULL;
391}
392
393static void
394_ecore_con_info_slave_free(CB_Data *cbdata)
395{
396 info_slaves = (CB_Data *)eina_inlist_remove(EINA_INLIST_GET(info_slaves),
397 EINA_INLIST_GET(cbdata));
398 ecore_main_fd_handler_del(cbdata->fdh);
399 ecore_event_handler_del(cbdata->handler);
400 close(ecore_main_fd_handler_fd_get(cbdata->fdh));
401 if (cbdata->data) ecore_con_server_infos_del(cbdata->data, cbdata);
402 free(cbdata);
403}
404
405static Eina_Bool
406_ecore_con_info_data_handler(void *data,
407 Ecore_Fd_Handler *fd_handler)
408{
409 CB_Data *cbdata;
410
411 cbdata = data;
412 if (cbdata->cb_done)
413 {
414 if (ecore_main_fd_handler_active_get(fd_handler,
415 ECORE_FD_READ))
416 _ecore_con_info_readdata(cbdata);
417 else
418 {
419 if (cbdata->data)
420 {
421 cbdata->cb_done(cbdata->data, NULL);
422 cbdata->cb_done = NULL;
423 ecore_con_server_infos_del(cbdata->data, cbdata);
424 }
425 }
426 }
427
428 _ecore_con_info_slave_free(cbdata);
429 return ECORE_CALLBACK_CANCEL;
430}
431
432static Eina_Bool
433_ecore_con_info_exit_handler(void *data,
434 int type __UNUSED__,
435 void *event)
436{
437 CB_Data *cbdata;
438 Ecore_Exe_Event_Del *ev;
439
440 ev = event;
441 cbdata = data;
442 if (cbdata->pid != ev->pid)
443 return ECORE_CALLBACK_RENEW;
444
445 return ECORE_CALLBACK_CANCEL; /* FIXME: Woot ??? */
446 _ecore_con_info_slave_free(cbdata);
447 return ECORE_CALLBACK_CANCEL;
448}
449