Index: debian/copyright
===================================================================
--- debian/copyright	(revision b1b9453552ad9d42f5cc552da6707e9ec3b556e4)
+++ debian/copyright	(revision 9c78519b3bdc8351488f3460d4f5387d6ceee2b6)
@@ -17,6 +17,6 @@
 * lib/json.[ch] is from <https://github.com/udp/json-parser> and licensed
   under the modified BSD license.
-* reverse_lookup() from lib/misc.c is from OpenSSH and licenced under
-  the BSD-3-clause licence.
+* lib/canohost.[ch] is from OpenSSH and licenced under the BSD-3-clause
+  licence.
 
 
Index: irc.c
===================================================================
--- irc.c	(revision b1b9453552ad9d42f5cc552da6707e9ec3b556e4)
+++ irc.c	(revision 9c78519b3bdc8351488f3460d4f5387d6ceee2b6)
@@ -25,4 +25,5 @@
 
 #include "bitlbee.h"
+#include "canohost.h"
 #include "ipc.h"
 #include "dcc.h"
Index: irc_commands.c
===================================================================
--- irc_commands.c	(revision b1b9453552ad9d42f5cc552da6707e9ec3b556e4)
+++ irc_commands.c	(revision 9c78519b3bdc8351488f3460d4f5387d6ceee2b6)
@@ -26,4 +26,5 @@
 #define BITLBEE_CORE
 #include "bitlbee.h"
+#include "canohost.h"
 #include "help.h"
 #include "ipc.h"
Index: lib/Makefile
===================================================================
--- lib/Makefile	(revision b1b9453552ad9d42f5cc552da6707e9ec3b556e4)
+++ lib/Makefile	(revision 9c78519b3bdc8351488f3460d4f5387d6ceee2b6)
@@ -13,5 +13,5 @@
 
 # [SH] Program variables
-objects = arc.o base64.o $(EVENT_HANDLER) ftutil.o http_client.o ini.o json.o json_util.o md5.o misc.o oauth.o oauth2.o proxy.o sha1.o $(SSL_CLIENT) url.o xmltree.o ns_parse.o
+objects = arc.o base64.o canohost.o $(EVENT_HANDLER) ftutil.o http_client.o ini.o json.o json_util.o md5.o misc.o oauth.o oauth2.o proxy.o sha1.o $(SSL_CLIENT) url.o xmltree.o ns_parse.o
 
 LFLAGS += -r
Index: lib/canohost.c
===================================================================
--- lib/canohost.c	(revision 9c78519b3bdc8351488f3460d4f5387d6ceee2b6)
+++ lib/canohost.c	(revision 9c78519b3bdc8351488f3460d4f5387d6ceee2b6)
@@ -0,0 +1,140 @@
+  /********************************************************************\
+  * BitlBee -- An IRC to other IM-networks gateway                     *
+  *                                                                    *
+  * Copyright 2002-2017 Wilmer van der Gaast and others                *
+  \********************************************************************/
+
+/*
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License with
+  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
+  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
+  Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+/* These two functions, with some modifications, taken from OpenSSH:
+ *
+ * $OpenBSD: canohost.c,v 1.73 2016/03/07 19:02:43 djm Exp
+ *
+ * Author: Tatu Ylonen <ylo@cs.hut.fi>
+ * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
+ *                    All rights reserved
+ * Functions for returning the canonical host name of the remote site.
+ *
+ * As far as I am concerned, the code I have written for this software
+ * can be used freely for any purpose.  Any derived versions of this
+ * software must be clearly marked as such, and if the derived work is
+ * incompatible with the protocol description in the RFC file, it must be
+ * called by a name other than "ssh" or "Secure Shell".
+ */
+
+#include "canohost.h"
+
+char *reverse_lookup(const struct sockaddr *from_, const socklen_t fromlen_)
+{
+	struct sockaddr_storage from;
+	socklen_t fromlen;
+	struct addrinfo hints, *ai, *aitop;
+	char name[NI_MAXHOST], ntop2[NI_MAXHOST];
+	char ntop[INET6_ADDRSTRLEN];
+
+	fromlen = sizeof(from);
+	memset(&from, 0, sizeof(from));
+	memcpy(&from, from_, fromlen_);
+	ipv64_normalise_mapped(&from, &fromlen);
+	if (from.ss_family == AF_INET6) {
+		fromlen = sizeof(struct sockaddr_in6);
+	}
+
+	if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
+	    NULL, 0, NI_NUMERICHOST) != 0) {
+		return NULL;
+	}
+
+	/* Map the IP address to a host name. */
+	if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
+	    NULL, 0, NI_NAMEREQD) != 0) {
+		/* Host name not found.  Use ip address. */
+		return g_strdup(ntop);
+	}
+
+	/*
+	 * if reverse lookup result looks like a numeric hostname,
+	 * someone is trying to trick us by PTR record like following:
+	 *	1.1.1.10.in-addr.arpa.	IN PTR	2.3.4.5
+	 */
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
+	hints.ai_flags = AI_NUMERICHOST;
+	if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
+		freeaddrinfo(ai);
+		return g_strdup(ntop);
+	}
+
+	/* Names are stored in lowercase. */
+	char *tolower = g_utf8_strdown(name, -1);
+	g_snprintf(name, sizeof(name), "%s", tolower);
+	g_free(tolower);
+
+	/*
+	 * Map it back to an IP address and check that the given
+	 * address actually is an address of this host.  This is
+	 * necessary because anyone with access to a name server can
+	 * define arbitrary names for an IP address. Mapping from
+	 * name to IP address can be trusted better (but can still be
+	 * fooled if the intruder has access to the name server of
+	 * the domain).
+	 */
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_family = from.ss_family;
+	hints.ai_socktype = SOCK_STREAM;
+	if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
+		return g_strdup(ntop);
+	}
+	/* Look for the address from the list of addresses. */
+	for (ai = aitop; ai; ai = ai->ai_next) {
+		if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
+		    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
+		    (strcmp(ntop, ntop2) == 0))
+			break;
+	}
+	freeaddrinfo(aitop);
+	/* If we reached the end of the list, the address was not there. */
+	if (ai == NULL) {
+		/* Address not found for the host name. */
+		return g_strdup(ntop);
+	}
+	return g_strdup(name);
+}
+
+void
+ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len)
+{
+	struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)addr;
+	struct sockaddr_in *a4 = (struct sockaddr_in *)addr;
+	struct in_addr inaddr;
+	u_int16_t port;
+
+	if (addr->ss_family != AF_INET6 ||
+	    !IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr))
+		return;
+
+	memcpy(&inaddr, ((char *)&a6->sin6_addr) + 12, sizeof(inaddr));
+	port = a6->sin6_port;
+
+	memset(a4, 0, sizeof(*a4));
+
+	a4->sin_family = AF_INET;
+	*len = sizeof(*a4);
+	memcpy(&a4->sin_addr, &inaddr, sizeof(inaddr));
+	a4->sin_port = port;
+}
Index: lib/canohost.h
===================================================================
--- lib/canohost.h	(revision 9c78519b3bdc8351488f3460d4f5387d6ceee2b6)
+++ lib/canohost.h	(revision 9c78519b3bdc8351488f3460d4f5387d6ceee2b6)
@@ -0,0 +1,43 @@
+  /********************************************************************\
+  * BitlBee -- An IRC to other IM-networks gateway                     *
+  *                                                                    *
+  * Copyright 2002-2017 Wilmer van der Gaast and others                *
+  \********************************************************************/
+
+/*
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License with
+  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
+  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
+  Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+/* These two functions, with some modifications, taken from OpenSSH:
+ *
+ * $OpenBSD: canohost.c,v 1.73 2016/03/07 19:02:43 djm Exp
+ *
+ * Author: Tatu Ylonen <ylo@cs.hut.fi>
+ * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
+ *                    All rights reserved
+ * Functions for returning the canonical host name of the remote site.
+ *
+ * As far as I am concerned, the code I have written for this software
+ * can be used freely for any purpose.  Any derived versions of this
+ * software must be clearly marked as such, and if the derived work is
+ * incompatible with the protocol description in the RFC file, it must be
+ * called by a name other than "ssh" or "Secure Shell".
+ */
+
+#include <bitlbee.h>
+
+G_MODULE_EXPORT char *reverse_lookup(const struct sockaddr *from_, const socklen_t fromlen_);
+G_MODULE_EXPORT void ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len);
Index: lib/misc.c
===================================================================
--- lib/misc.c	(revision b1b9453552ad9d42f5cc552da6707e9ec3b556e4)
+++ lib/misc.c	(revision 9c78519b3bdc8351488f3460d4f5387d6ceee2b6)
@@ -1,3 +1,3 @@
-/********************************************************************\
+  /********************************************************************\
   * BitlBee -- An IRC to other IM-networks gateway                     *
   *                                                                    *
@@ -504,105 +504,4 @@
 }
 
-/* From OpenSSH 7.4p1 canohost.c" */
-char *reverse_lookup(const struct sockaddr *from_, const socklen_t fromlen_)
-{
-	struct sockaddr_storage from;
-	socklen_t fromlen;
-	struct addrinfo hints, *ai, *aitop;
-	char name[NI_MAXHOST], ntop2[NI_MAXHOST];
-	char ntop[INET6_ADDRSTRLEN];
-
-	fromlen = sizeof(from);
-	memset(&from, 0, sizeof(from));
-	memcpy(&from, from_, fromlen_);
-	ipv64_normalise_mapped(&from, &fromlen);
-	if (from.ss_family == AF_INET6) {
-		fromlen = sizeof(struct sockaddr_in6);
-	}
-
-	if (getnameinfo((struct sockaddr *)&from, fromlen, ntop, sizeof(ntop),
-	    NULL, 0, NI_NUMERICHOST) != 0) {
-		return NULL;
-	}
-
-	/* Map the IP address to a host name. */
-	if (getnameinfo((struct sockaddr *)&from, fromlen, name, sizeof(name),
-	    NULL, 0, NI_NAMEREQD) != 0) {
-		/* Host name not found.  Use ip address. */
-		return g_strdup(ntop);
-	}
-
-	/*
-	 * if reverse lookup result looks like a numeric hostname,
-	 * someone is trying to trick us by PTR record like following:
-	 *	1.1.1.10.in-addr.arpa.	IN PTR	2.3.4.5
-	 */
-	memset(&hints, 0, sizeof(hints));
-	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
-	hints.ai_flags = AI_NUMERICHOST;
-	if (getaddrinfo(name, NULL, &hints, &ai) == 0) {
-		freeaddrinfo(ai);
-		return g_strdup(ntop);
-	}
-
-	/* Names are stored in lowercase. */
-	char *tolower = g_utf8_strdown(name, -1);
-	g_snprintf(name, sizeof(name), "%s", tolower);
-	g_free(tolower);
-
-	/*
-	 * Map it back to an IP address and check that the given
-	 * address actually is an address of this host.  This is
-	 * necessary because anyone with access to a name server can
-	 * define arbitrary names for an IP address. Mapping from
-	 * name to IP address can be trusted better (but can still be
-	 * fooled if the intruder has access to the name server of
-	 * the domain).
-	 */
-	memset(&hints, 0, sizeof(hints));
-	hints.ai_family = from.ss_family;
-	hints.ai_socktype = SOCK_STREAM;
-	if (getaddrinfo(name, NULL, &hints, &aitop) != 0) {
-		return g_strdup(ntop);
-	}
-	/* Look for the address from the list of addresses. */
-	for (ai = aitop; ai; ai = ai->ai_next) {
-		if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop2,
-		    sizeof(ntop2), NULL, 0, NI_NUMERICHOST) == 0 &&
-		    (strcmp(ntop, ntop2) == 0))
-			break;
-	}
-	freeaddrinfo(aitop);
-	/* If we reached the end of the list, the address was not there. */
-	if (ai == NULL) {
-		/* Address not found for the host name. */
-		return g_strdup(ntop);
-	}
-	return g_strdup(name);
-}
-
-void
-ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len)
-{
-	struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)addr;
-	struct sockaddr_in *a4 = (struct sockaddr_in *)addr;
-	struct in_addr inaddr;
-	u_int16_t port;
-
-	if (addr->ss_family != AF_INET6 ||
-	    !IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr))
-		return;
-
-	memcpy(&inaddr, ((char *)&a6->sin6_addr) + 12, sizeof(inaddr));
-	port = a6->sin6_port;
-
-	memset(a4, 0, sizeof(*a4));
-
-	a4->sin_family = AF_INET;
-	*len = sizeof(*a4);
-	memcpy(&a4->sin_addr, &inaddr, sizeof(inaddr));
-	a4->sin_port = port;
-}
-
 char *word_wrap(const char *msg, int line_len)
 {
Index: lib/misc.h
===================================================================
--- lib/misc.h	(revision b1b9453552ad9d42f5cc552da6707e9ec3b556e4)
+++ lib/misc.h	(revision 9c78519b3bdc8351488f3460d4f5387d6ceee2b6)
@@ -141,7 +141,4 @@
 G_MODULE_EXPORT void srv_free(struct ns_srv_reply **srv);
 
-G_MODULE_EXPORT char *reverse_lookup(const struct sockaddr *from_, const socklen_t fromlen_);
-G_MODULE_EXPORT void ipv64_normalise_mapped(struct sockaddr_storage *addr, socklen_t *len);
-
 G_MODULE_EXPORT char *word_wrap(const char *msg, int line_len);
 G_MODULE_EXPORT gboolean ssl_sockerr_again(void *ssl);
