1 /* Inter-instances internal communication socket interface */
10 #include <sys/stat.h> /* OS/2 needs this after sys/types.h */
16 #ifdef HAVE_SYS_TIME_H
23 /* Blame BSD for position of this includes. */
24 #ifdef HAVE_NETINET_IN_H
25 #include <netinet/in.h>
27 #ifdef HAVE_SYS_SELECT_H
28 #include <sys/select.h>
30 #ifdef HAVE_SYS_SOCKET_H
31 #include <sys/socket.h> /* OS/2 needs this after sys/types.h */
38 #ifdef HAVE_ARPA_INET_H
39 #include <arpa/inet.h>
44 #include "config/home.h"
45 #include "intl/gettext/libintl.h"
46 #include "main/interlink.h"
47 #include "main/select.h"
48 #include "osdep/osdep.h"
49 #include "util/conv.h"
50 #include "util/error.h"
51 #include "util/memory.h"
52 #include "util/string.h"
54 /* Testing purpose. Do not remove. */
55 /* At some point zas added experimental support for interlinking
56 * ELinks' over a TCP socket. However, it was removed due to some
57 * unsettled security related questions. More info in:
59 * src/config/options.c (1.233 -> 1.234)
60 * src/network/interlink.c (1.54 -> 1.55)
64 * Experimental remote mode is now (almost) functionnal. To test define
65 * ELINKS_REMOTE in setup.h, compile elinks, add a second ip to your
66 * machine or use a second machine, then launch elinks -listen
67 * <your_first_ip> on one side, and elinks -remote <your_first_ip> on the
68 * second side. There is many things to improve, and it may not function as
69 * you hope it does, but feel free to provide enhancements. Note this
70 * feature may disappear soon if none is interested in. Please report
71 * issues/comments/ideas/bugs on #elinks.
74 /* Set to 1 to make ELinks use a tcp socket bound to loopback address with
75 * port defined by ELINKS_PORT (setup.h) for internal communications
76 * instead of a socket file, which is the preferred way. */
78 #define CONFIG_TCP_INTERLINK
81 /* Common to both AF_UNIX and AF_INET stuff. */
83 struct sockaddr
*addr
;
88 /* Accepted socket info */
89 static struct socket_info s_info_accept
;
91 /* Listening socket info */
92 static struct socket_info s_info_listen
;
94 /* Connect socket info */
95 static struct socket_info s_info_connect
;
97 /* Type of address requested (for get_address()) */
105 #ifndef CONFIG_TCP_INTERLINK
107 /*** Unix file socket for internal communication. ***/
109 /* Compute socket file path and allocate space for it.
110 * It returns 0 on error (in this case, there's no need
112 * It returns 1 on success. */
114 get_sun_path(struct string
*sun_path
)
117 if_assert_failed
return 0;
119 if (!elinks_home
) return 0;
121 if (!init_string(sun_path
)) return 0;
123 add_to_string(sun_path
, elinks_home
);
124 add_to_string(sun_path
, ELINKS_SOCK_NAME
);
125 add_long_to_string(sun_path
,
126 get_cmd_opt_int("session-ring"));
131 /* @type is ignored here => always local. */
133 get_address(struct socket_info
*info
, enum addr_type type
)
135 struct sockaddr_un
*addr
= NULL
;
136 int sun_path_freespace
;
140 if_assert_failed
return -1;
142 if (!get_sun_path(&path
)) return -1;
144 /* Linux defines that as:
145 * #define UNIX_PATH_MAX 108
146 * struct sockaddr_un {
147 * sa_family_t sun_family;
148 * char sun_path[UNIX_PATH_MAX];
151 * UNIX_PATH_MAX is not defined on all systems, so we'll use sizeof().
154 /* Following code may need to be changed if at some time the
155 * struct sockaddr_un mess is fixed. For now, i tried to make it
158 * Extract from glibc documentation:
160 * This is the file name to use.
161 * Incomplete: Why is 108 a magic number?
162 * RMS suggests making this a zero-length array and tweaking the example
163 * following to use alloca to allocate an appropriate amount of storage
164 * based on the length of the filename.
166 * But at this day (2003/06/18) it seems there's no implementation of such
168 * If it was the case, then following code will always generate an error.
172 sun_path_freespace
= sizeof(addr
->sun_path
) - (path
.length
+ 1);
173 if (sun_path_freespace
< 0) {
174 INTERNAL("Socket path name '%s' is too long: %d >= %zu",
175 path
.source
, path
.length
, sizeof(addr
->sun_path
));
179 addr
= mem_calloc(1, sizeof(*addr
));
180 if (!addr
) goto free_and_error
;
182 memcpy(addr
->sun_path
, path
.source
, path
.length
); /* ending '\0' is done by calloc() */
185 addr
->sun_family
= AF_UNIX
;
187 info
->addr
= (struct sockaddr
*) addr
;
188 /* The size of the address is the offset of the start of the filename,
189 * plus its length, plus one for the terminating null byte (well, this
190 * last byte may or not be needed it depends of....).
191 * Alternatively we can use SUN_LEN() macro but this one is not always
192 * defined nor always defined in the same way. --Zas */
193 info
->size
= sizeof(*addr
) - sun_path_freespace
;
205 alloc_address(struct socket_info
*info
)
207 struct sockaddr_un
*sa
;
210 if_assert_failed
return 0;
212 /* calloc() is safer there. */
213 sa
= mem_calloc(1, sizeof(*sa
));
216 info
->addr
= (struct sockaddr
*) sa
;
217 info
->size
= sizeof(*sa
);
223 unlink_unix(struct sockaddr
*addr
)
226 if_assert_failed
return;
228 unlink(((struct sockaddr_un
*) addr
)->sun_path
);
231 #define setsock_reuse_addr(fd)
234 #else /* CONFIG_TCP_INTERLINK */
236 /*** TCP socket for internal communication. ***/
237 /* FIXME: IPv6 support. */
239 /* These may not be defined in netinet/in.h on some systems. */
240 #ifndef INADDR_LOOPBACK
241 #define INADDR_LOOPBACK ((struct in_addr) 0x7f000001)
244 #ifndef IPPORT_USERRESERVED
245 #define IPPORT_USERRESERVED 5000
248 /* @type is not used for now, and is ignored, it will
249 * be used in remote mode feature. */
251 get_address(struct socket_info
*info
, enum addr_type type
)
253 struct sockaddr_in
*sin
;
257 if_assert_failed
return -1;
259 /* Each ring is bind to ELINKS_PORT + ring number. */
260 port
= ELINKS_PORT
+ get_cmd_opt_int("session-ring");
261 if (port
< IPPORT_USERRESERVED
)
262 return -1; /* Just in case of... */
264 sin
= mem_calloc(1, sizeof(*sin
));
267 sin
->sin_family
= AF_INET
;
268 sin
->sin_port
= htons(port
);
269 sin
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
271 info
->addr
= (struct sockaddr
*) sin
;
272 info
->size
= sizeof(*sin
);
278 alloc_address(struct socket_info
*info
)
280 struct sockaddr_in
*sa
;
283 if_assert_failed
return 0;
285 /* calloc() is safer there. */
286 sa
= mem_calloc(1, sizeof(*sa
));
289 info
->addr
= (struct sockaddr
*) sa
;
290 info
->size
= sizeof(*sa
);
295 #if defined(SOL_SOCKET) && defined(SO_REUSEADDR)
297 setsock_reuse_addr(int fd
)
301 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
302 (void *) &reuse_addr
, sizeof(reuse_addr
));
305 #define setsock_reuse_addr(fd)
308 #define unlink_unix(s)
310 #endif /* CONFIG_TCP_INTERLINK */
312 /* Max. number of bind attempts. */
313 #define MAX_BIND_TRIES 3
314 /* Base delay in useconds between bind attempts.
315 * We will sleep that time on first attempt, then
316 * 2 * delay, then 3 * delay .... */
317 #define BIND_TRIES_DELAY 100000
318 /* Number of connections in listen backlog. */
319 #define LISTEN_BACKLOG 100
321 /* Max. number of connect attempts. */
322 #define MAX_CONNECT_TRIES 3
323 /* Base delay in useconds between connect attempts. */
324 #define CONNECT_TRIES_DELAY 50000
327 report_af_unix_error(unsigned char *function
, int error
)
329 ERROR(gettext("The call to %s failed: %d (%s)"),
330 function
, error
, (unsigned char *) strerror(error
));
333 /* Called when we receive a connection on listening socket. */
335 af_unix_connection(struct socket_info
*info
)
341 if_assert_failed
return;
345 memset(info
->addr
, 0, l
);
346 ns
= accept(info
->fd
, info
->addr
, &l
);
348 report_af_unix_error("accept()", errno
);
357 /* usleep() is not portable, so we use this replacement.
358 * TODO: move it to somewhere. */
360 elinks_usleep(unsigned long useconds
)
362 struct timeval delay
;
368 delay
.tv_usec
= useconds
;
369 select(0, &dummy
, &dummy
, &dummy
, &delay
);
372 /* Listen on socket for internal ELinks communication.
373 * Returns -1 on error
374 * or listened file descriptor on success. */
376 bind_to_af_unix(void)
378 mode_t saved_mask
= umask(S_IXUSR
| S_IRWXG
| S_IRWXO
);
380 int pf
= get_address(&s_info_listen
, ADDR_IP_SERVER
);
382 if (pf
== -1) goto free_and_error
;
385 s_info_listen
.fd
= socket(pf
, SOCK_STREAM
, 0);
386 if (s_info_listen
.fd
== -1) {
387 report_af_unix_error("socket()", errno
);
391 setsock_reuse_addr(s_info_listen
.fd
);
393 if (bind(s_info_listen
.fd
, s_info_listen
.addr
, s_info_listen
.size
) >= 0)
396 if (errno
!= EADDRINUSE
)
397 report_af_unix_error("bind()", errno
);
401 if (attempts
== MAX_BIND_TRIES
)
402 unlink_unix(s_info_listen
.addr
);
404 if (attempts
> MAX_BIND_TRIES
)
407 elinks_usleep(BIND_TRIES_DELAY
* attempts
);
408 close(s_info_listen
.fd
);
411 /* Listen and accept. */
412 if (!alloc_address(&s_info_accept
))
415 s_info_accept
.fd
= s_info_listen
.fd
;
417 if (listen(s_info_listen
.fd
, LISTEN_BACKLOG
)) {
418 report_af_unix_error("listen()", errno
);
422 set_handlers(s_info_listen
.fd
, (void (*)(void *)) af_unix_connection
,
423 NULL
, NULL
, &s_info_accept
);
426 return s_info_listen
.fd
;
435 /* Connect to an listening socket for internal ELinks communication.
436 * Returns -1 on error
437 * or file descriptor on success. */
439 connect_to_af_unix(void)
442 int pf
= get_address(&s_info_connect
, ADDR_IP_CLIENT
);
444 while (pf
!= -1 && attempts
++ < MAX_CONNECT_TRIES
) {
447 s_info_connect
.fd
= socket(pf
, SOCK_STREAM
, 0);
448 if (s_info_connect
.fd
== -1) {
449 report_af_unix_error("socket()", errno
);
453 if (connect(s_info_connect
.fd
, s_info_connect
.addr
,
454 s_info_connect
.size
) >= 0)
455 return s_info_connect
.fd
;
458 close(s_info_connect
.fd
);
460 if (saved_errno
!= ECONNREFUSED
&& saved_errno
!= ENOENT
) {
461 report_af_unix_error("connect()", errno
);
465 elinks_usleep(CONNECT_TRIES_DELAY
* attempts
);
468 mem_free_set(&s_info_connect
.addr
, NULL
);
472 static void safe_close(int *fd
) {
473 if (*fd
== -1) return;
478 /* Free all allocated memory and close all descriptors if
483 /* We test for addr != NULL since
484 * if it was not allocated then fd is not
485 * initialized and we don't want to close
487 if (s_info_listen
.addr
) {
488 safe_close(&s_info_listen
.fd
);
489 unlink_unix(s_info_listen
.addr
);
490 mem_free(s_info_listen
.addr
);
491 s_info_listen
.addr
= NULL
;
494 if (s_info_connect
.addr
) {
495 safe_close(&s_info_connect
.fd
);
496 mem_free(s_info_connect
.addr
);
497 s_info_connect
.addr
= NULL
;
500 if (s_info_accept
.addr
) {
501 safe_close(&s_info_accept
.fd
);
502 mem_free(s_info_accept
.addr
);
503 s_info_accept
.addr
= NULL
;
507 /* Initialize sockets for internal ELinks communication.
508 * If connect succeeds it returns file descriptor,
509 * else it tries to bind and listen on a socket, and
515 int fd
= connect_to_af_unix();
517 if (fd
!= -1) return fd
;
524 #undef MAX_BIND_TRIES
525 #undef BIND_TRIES_DELAY
526 #undef LISTEN_BACKLOG
527 #undef MAX_CONNECT_TRIES
528 #undef CONNECT_TRIES_DELAY