csync2/bind-to-local-hostname.patch

39 lines
1.4 KiB
Diff
Raw Normal View History

Index: csync2-1.34/conn.c
===================================================================
--- csync2-1.34.orig/conn.c
+++ csync2-1.34/conn.c
@@ -50,7 +50,9 @@ SSL *conn_ssl;
int conn_open(const char *peername)
{
struct sockaddr_in sin;
+ struct sockaddr_in sme;
struct hostent *hp;
+ struct hostent *me;
int on = 1;
hp = gethostbyname(peername);
@@ -69,6 +71,23 @@ int conn_open(const char *peername)
bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
sin.sin_port = htons(csync_port);
+ /* Try to bind to IP address of local hostname, else in cases where
+ * there's multiple local IPs on the same subnet, we might autobind
+ * to the wrong one, and the remote csync2 instance will fail because
+ * it verifies the expected hostname against the initiating IP.
+ * If the explicit bind fails for some reason though, we'll still
+ * try to connect (it might work, and it can't hurt...).
+ */
+ me = gethostbyname(myhostname);
+ if (me) {
+ sme.sin_family = me->h_addrtype;
+ bcopy(me->h_addr, &sme.sin_addr, me->h_length);
+ sme.sin_port = 0; /* random port */
+ if (bind(conn_fd_in, (struct sockaddr *)&sme, sizeof(sme)) < 0) {
+ csync_debug(1, "Can't bind local socket (attempting connect anyway).\n");
+ }
+ }
+
if (connect(conn_fd_in, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
csync_debug(1, "Can't connect to remote host.\n");
close(conn_fd_in); conn_fd_in = -1;