+ return TRUE;
+}
+
+/* Performs lookups for local name and IP address. This peforms reverse
+ lookup as well to verify that the IP has FQDN. */
+
+bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip)
+{
+ char host[1024];
+ int rval, len;
+
+#ifdef HAVE_IPV6
+ struct sockaddr_storage local;
+ char s[NI_MAXHOST];
+
+ if (hostname)
+ *hostname = NULL;
+ *ip = NULL;
+
+ SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
+
+ memset(&local, 0, sizeof(local));
+ memset(&s, 0, sizeof(s));
+ len = sizeof(local);
+ rval = getsockname(sock, (struct sockaddr *)&local, &len);
+ if (rval < 0)
+ return FALSE;
+
+ if (getnameinfo((struct sockaddr *)&local, len, s, sizeof(s), NULL, 0,
+ NI_NUMERICHOST))
+ return FALSE;
+
+ *ip = silc_memdup(s, strlen(s));
+ if (*ip == NULL)
+ return FALSE;
+#else
+ struct sockaddr_in local;
+ char *host_ip;
+
+ if (hostname)
+ *hostname = NULL;
+ *ip = NULL;
+
+ SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
+
+ memset(&local, 0, sizeof(local));
+ len = sizeof(local);
+ rval = getsockname(sock, (struct sockaddr *)&local, &len);
+ if (rval < 0)
+ return FALSE;
+
+ host_ip = inet_ntoa(local.sin_addr);
+ if (!host_ip)
+ return FALSE;
+
+ *ip = silc_memdup(host_ip, strlen(host_ip));
+ if (*ip == NULL)
+ return FALSE;
+#endif
+
+ /* Do reverse lookup if we want hostname too. */
+ if (hostname) {
+ /* Get host by address */
+ if (!silc_net_gethostbyaddr(*ip, host, sizeof(host)))
+ return FALSE;
+
+ *hostname = silc_memdup(host, strlen(host));
+ SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
+
+ /* Reverse */
+ if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host)))
+ return FALSE;
+
+ if (strcmp(*ip, host))
+ return FALSE;
+ }
+
+ SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
+ return TRUE;