silcnet.c
- Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+ Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 1997 - 2000 Pekka Riikonen
+ Copyright (C) 1997 - 2001 Pekka Riikonen
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.
+ the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.
*/
-/*
- * $Id$
- * $Log$
- * Revision 1.2 2000/10/31 19:48:32 priikone
- * A LOT updates. Cannot separate. :)
- *
- * Revision 1.1 2000/09/13 17:45:16 priikone
- * Splitted SILC core library. Core library includes now only
- * SILC protocol specific stuff. New utility library includes the
- * old stuff from core library that is more generic purpose stuff.
- *
- * Revision 1.3 2000/07/05 06:06:35 priikone
- * Global cosmetic change.
- *
- * Revision 1.2 2000/06/30 10:49:48 priikone
- * Added SOCKS4 and SOCKS5 support for SILC client.
- *
- * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
- * Imported from internal CVS/Added Log headers.
- *
- *
- */
+/* $Id$ */
#include "silcincludes.h"
#include "silcnet.h"
-/* This function creates server or daemon or listener or what ever. This
- does not fork a new process, it must be done by the caller if caller
- wants to create a child process. This is used by the SILC server.
- If argument `ip_addr' is NULL `any' address will be used. Returns
- the created socket or -1 on error. */
+/* Accepts a connection from a particular socket */
-int silc_net_create_server(int port, char *ip_addr)
+int silc_net_accept_connection(int sock)
{
- int sock, rval;
- struct sockaddr_in server;
+ return accept(sock, 0, 0);
+}
- SILC_LOG_DEBUG(("Creating a new server listener"));
+/* Sets a option for a socket. */
- /* Create the socket */
- sock = socket(PF_INET, SOCK_STREAM, 0);
- if (sock < 0) {
- SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
- return -1;
- }
+int silc_net_set_socket_opt(int sock, int level, int option, int on)
+{
+ return setsockopt(sock, level, option, (void *)&on, sizeof(on));
+}
- /* Set the socket options */
- rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
- if (rval < 0) {
- SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
- return -1;
- }
+/* Get socket options */
- /* Set the socket information for bind() */
- memset(&server, 0, sizeof(server));
- server.sin_family = PF_INET;
- server.sin_port = htons(port);
-
- /* Convert IP address to network byte order */
- if (ip_addr)
- inet_aton(ip_addr, &server.sin_addr);
- else
- server.sin_addr.s_addr = INADDR_ANY;
-
- /* Bind the server socket */
- rval = bind(sock, (struct sockaddr *)&server, sizeof(server));
- if (rval < 0) {
- SILC_LOG_ERROR(("Cannot bind socket: %s", strerror(errno)));
- return -1;
- }
+int silc_net_get_socket_opt(int sock, int level, int option,
+ void *optval, int *opt_len)
+{
+ return getsockopt(sock, level, option, optval, opt_len);
+}
- /* Specify that we are listenning */
- rval = listen(sock, 5);
- if (rval < 0) {
- SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
- return -1;
+/* Checks whether IP address sent as argument is valid IPv4 address. */
+
+bool silc_net_is_ip4(const char *addr)
+{
+ int count = 0;
+
+ while (*addr) {
+ if (*addr != '.' && !isdigit(*addr))
+ return FALSE;
+ if (*addr == '.')
+ count++;
+ addr++;
}
- /* Set the server socket to non-blocking mode */
- silc_net_set_socket_nonblock(sock);
+ if (count != 3)
+ return FALSE;
+
+ return TRUE;
+}
- SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
+/* Checks whether IP address sent as argument is valid IPv6 address. */
- return sock;
+bool silc_net_is_ip6(const char *addr)
+{
+ /* XXX does this work with all kinds of IPv6 addresses? */
+ while (*addr) {
+ if (*addr != ':' && !isxdigit(*addr))
+ return FALSE;
+ addr++;
+ }
+
+ return TRUE;
}
-void silc_net_close_server(int sock)
+/* Checks whether IP address sent as argument is valid IP address. */
+
+bool silc_net_is_ip(const char *addr)
{
- shutdown(sock, 2);
- close(sock);
+ if (silc_net_is_ip4(addr))
+ return TRUE;
+ return silc_net_is_ip6(addr);
+}
- SILC_LOG_DEBUG(("Server socket closed"));
+/* Internal context for async resolving */
+typedef struct {
+ SilcNetResolveCallback completion;
+ void *context;
+ bool prefer_ipv6;
+ SilcSchedule schedule;
+ char *input;
+ char *result;
+} *SilcNetResolveContext;
+
+SILC_TASK_CALLBACK(silc_net_resolve_completion)
+{
+ SilcNetResolveContext r = (SilcNetResolveContext)context;
+
+ /* Call the completion callback */
+ if (r->completion)
+ (*r->completion)(r->result, r->context);
+
+ silc_free(r->input);
+ silc_free(r->result);
+ silc_free(r);
}
-/* Creates a connection (TCP/IP) to a remote host. Returns the connection
- socket or -1 on error. This blocks the process while trying to create
- the connection. */
+/* Thread function to resolve the address for hostname. */
-int silc_net_create_connection(int port, char *host)
+static void *silc_net_gethostbyname_thread(void *context)
{
- int sock, rval;
- struct hostent *dest;
- struct sockaddr_in desthost;
+ SilcNetResolveContext r = (SilcNetResolveContext)context;
+ SilcSchedule schedule = r->schedule;
+ char tmp[64];
- SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
-
- /* Do host lookup */
- dest = gethostbyname(host);
- if (!dest) {
- SILC_LOG_ERROR(("Network (%s) unreachable", host));
- return -1;
- }
+ if (silc_net_gethostbyname(r->input, r->prefer_ipv6, tmp, sizeof(tmp)))
+ r->result = strdup(tmp);
- /* Set socket information */
- memset(&desthost, 0, sizeof(desthost));
- desthost.sin_port = htons(port);
- desthost.sin_family = PF_INET;
- memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
-
- /* Create the connection socket */
- sock = socket(PF_INET, SOCK_STREAM, 0);
- if (sock < 0) {
- SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
- return -1;
- }
+ silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1,
+ SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
+ silc_schedule_wakeup(schedule);
+ return NULL;
+}
- /* Connect to the host */
- rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
- if (rval < 0) {
- SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
- shutdown(sock, 2);
- close(sock);
- return -1;
- }
+/* Thread function to resolve the hostname for address. */
- /* Set appropriate options */
- silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
- silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
+static void *silc_net_gethostbyaddr_thread(void *context)
+{
+ SilcNetResolveContext r = (SilcNetResolveContext)context;
+ SilcSchedule schedule = r->schedule;
+ char tmp[256];
- SILC_LOG_DEBUG(("Connection created"));
+ if (silc_net_gethostbyaddr(r->input, tmp, sizeof(tmp)))
+ r->result = strdup(tmp);
- return sock;
+ silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1,
+ SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
+ silc_schedule_wakeup(schedule);
+ return NULL;
}
-/* Creates a connection (TCP/IP) to a remote host. Returns the connection
- socket or -1 on error. This creates non-blocking socket hence the
- connection returns directly. To get the result of the connect() one
- must select() the socket and read the result after it's ready. */
+/* Resolves IP address for hostname. */
-int silc_net_create_connection_async(int port, char *host)
+bool silc_net_gethostbyname(const char *name, bool prefer_ipv6, char *address,
+ SilcUInt32 address_len)
{
- int sock, rval;
- struct hostent *dest;
- struct sockaddr_in desthost;
-
- SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
- host, port));
-
- /* Do host lookup */
- dest = gethostbyname(host);
- if (!dest) {
- SILC_LOG_ERROR(("Network (%s) unreachable", host));
- return -1;
+#ifdef HAVE_IPV6
+ struct addrinfo hints, *ai, *tmp, *ip4 = NULL, *ip6 = NULL;
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_socktype = SOCK_STREAM;
+ if (getaddrinfo(name, NULL, &hints, &ai))
+ return FALSE;
+
+ for (tmp = ai; tmp; tmp = tmp->ai_next) {
+ if (tmp->ai_family == AF_INET6) {
+ ip6 = tmp;
+ if (ip4)
+ break;
+ continue;
+ }
+ if (tmp->ai_family == AF_INET) {
+ ip4 = tmp;
+ if (ip6)
+ break;
+ continue;
+ }
}
- /* Set socket information */
- memset(&desthost, 0, sizeof(desthost));
- desthost.sin_port = htons(port);
- desthost.sin_family = PF_INET;
- memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
-
- /* Create the connection socket */
- sock = socket(PF_INET, SOCK_STREAM, 0);
- if (sock < 0) {
- SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
- return -1;
+ tmp = (prefer_ipv6 ? (ip6 ? ip6 : ip4) : (ip4 ? ip4 : ip6));
+ if (!tmp) {
+ freeaddrinfo(ai);
+ return FALSE;
}
- /* Set the socket to non-blocking mode */
- silc_net_set_socket_nonblock(sock);
-
- /* Connect to the host */
- rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
- if (rval < 0) {
- if (errno != EINPROGRESS) {
- SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
- shutdown(sock, 2);
- close(sock);
- return -1;
- }
+ if (getnameinfo(tmp->ai_addr, tmp->ai_addrlen, address,
+ address_len, NULL, 0, NI_NUMERICHOST)) {
+ freeaddrinfo(ai);
+ return FALSE;
}
- /* Set appropriate options */
- silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
- silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
-
- SILC_LOG_DEBUG(("Connection operation in progress"));
-
- return sock;
+ freeaddrinfo(ai);
+#else
+ struct hostent *hp;
+ struct in_addr ip;
+ char *tmp;
+
+ hp = gethostbyname(name);
+ if (!hp)
+ return FALSE;
+
+ memcpy(&ip.s_addr, hp->h_addr_list[0], 4);
+ tmp = inet_ntoa(ip);
+ if (!tmp)
+ return FALSE;
+ if (address_len < strlen(tmp))
+ return FALSE;
+ memset(address, 0, address_len);
+ strncpy(address, tmp, strlen(tmp));
+#endif
+
+ return TRUE;
}
-/* Closes the connection */
+/* Resolves IP address for hostname async. */
-void silc_net_close_connection(int sock)
+void silc_net_gethostbyname_async(const char *name,
+ bool prefer_ipv6,
+ SilcSchedule schedule,
+ SilcNetResolveCallback completion,
+ void *context)
{
- close(sock);
-}
+ SilcNetResolveContext r = silc_calloc(1, sizeof(*r));
-/* Accepts a connection from a particular socket */
+ r->completion = completion;
+ r->context = context;
+ r->prefer_ipv6 = prefer_ipv6;
+ r->schedule = schedule;
+ r->input = strdup(name);
-int silc_net_accept_connection(int sock)
-{
- return accept(sock, 0, 0);
+ silc_thread_create(silc_net_gethostbyname_thread, r, FALSE);
}
-/* Set's the socket to non-blocking mode. */
+/* Resolves hostname by IP address. */
-int silc_net_set_socket_nonblock(int sock)
+bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len)
{
- return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
+#ifdef HAVE_IPV6
+ struct addrinfo req, *ai;
+
+ memset(&req, 0, sizeof(req));
+ req.ai_socktype = SOCK_STREAM;
+ req.ai_flags = AI_CANONNAME;
+
+ if (getaddrinfo(addr, NULL, &req, &ai))
+ return FALSE;
+ if (getnameinfo(ai->ai_addr, ai->ai_addrlen, name, name_len, NULL, 0, 0)) {
+ freeaddrinfo(ai);
+ return FALSE;
+ }
+ freeaddrinfo(ai);
+#else
+ struct hostent *hp;
+ unsigned char a[16];
+
+ if (!silc_net_addr2bin(addr, a, sizeof(a)))
+ return FALSE;
+
+ hp = gethostbyaddr(a, 4, AF_INET);
+ if (!hp)
+ return FALSE;
+ if (name_len < strlen(hp->h_name))
+ return FALSE;
+ memset(name, 0, name_len);
+ strncpy(name, hp->h_name, strlen(hp->h_name));
+#endif
+
+ return TRUE;
}
-/* Sets a option for a socket. */
+/* Resolves hostname by IP address async. */
-int silc_net_set_socket_opt(int sock, int level, int option, int on)
+void silc_net_gethostbyaddr_async(const char *addr,
+ SilcSchedule schedule,
+ SilcNetResolveCallback completion,
+ void *context)
{
- return setsockopt(sock, level, option, (void *)&on, sizeof(on));
-}
+ SilcNetResolveContext r = silc_calloc(1, sizeof(*r));
-/* Checks whether IP address sent as argument is valid IP address. */
+ r->completion = completion;
+ r->context = context;
+ r->schedule = schedule;
+ r->input = strdup(addr);
-int silc_net_is_ip(const char *addr)
-{
- struct in_addr tmp;
- return inet_aton(addr, &tmp);
+ silc_thread_create(silc_net_gethostbyaddr_thread, r, FALSE);
}
-/* Performs lookups for remote name and IP address. */
+/* Performs lookups for remote name and IP address. This peforms reverse
+ lookup as well to verify that the IP has FQDN. */
-void silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
+bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
{
- struct sockaddr_in remote;
- struct hostent *dest;
- char *host_ip = NULL;
- char host_name[1024];
+ char host[1024];
int rval, len;
- int i;
- *hostname = NULL;
+#ifdef HAVE_IPV6
+ struct sockaddr_storage remote;
+ char s[NI_MAXHOST];
+
+ if (hostname)
+ *hostname = NULL;
*ip = NULL;
SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
memset(&remote, 0, sizeof(remote));
+ memset(&s, 0, sizeof(s));
len = sizeof(remote);
rval = getpeername(sock, (struct sockaddr *)&remote, &len);
if (rval < 0)
- return;
-
- /* Get host by address */
- dest = gethostbyaddr((char *)&remote.sin_addr,
- sizeof(struct in_addr), AF_INET);
- if (!dest)
- return;
-
- /* Get same hsot by name to see that the remote host really is
- the who it says it is */
- memset(host_name, 0, sizeof(host_name));
- memcpy(host_name, dest->h_name, strlen(dest->h_name));
- dest = gethostbyname(host_name);
- if (!dest)
- return;
-
- /* Find the address from list */
- for (i = 0; dest->h_addr_list[i]; i++)
- if (!memcmp(dest->h_addr_list[i], &remote.sin_addr,
- sizeof(struct in_addr)))
- break;
- if (!dest->h_addr_list[i])
- return;
+ return FALSE;
+
+ if (getnameinfo((struct sockaddr *)&remote, 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 remote;
+ char *host_ip;
+
+ if (hostname)
+ *hostname = NULL;
+ *ip = NULL;
+
+ SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
+
+ memset(&remote, 0, sizeof(remote));
+ len = sizeof(remote);
+ rval = getpeername(sock, (struct sockaddr *)&remote, &len);
+ if (rval < 0)
+ return FALSE;
host_ip = inet_ntoa(remote.sin_addr);
if (!host_ip)
- return;
+ 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;
+ }
- *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
- memcpy(*hostname, host_name, strlen(host_name));
- SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
- *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
- memcpy(*ip, host_ip, strlen(host_ip));
SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
+ 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;
+}
+
+/* Return remote port by socket. */
+
+SilcUInt16 silc_net_get_remote_port(int sock)
+{
+#ifdef HAVE_IPV6
+ struct sockaddr_storage remote;
+ int len;
+ char s[NI_MAXSERV];
+
+ memset(&remote, 0, sizeof(remote));
+ len = sizeof(remote);
+ if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
+ return 0;
+
+ if (getnameinfo((struct sockaddr *)&remote, len, NULL, 0, s, sizeof(s),
+ NI_NUMERICSERV))
+ return 0;
+
+ return atoi(s);
+#else
+ struct sockaddr_in remote;
+ int len;
+
+ memset(&remote, 0, sizeof(remote));
+ len = sizeof(remote);
+ if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
+ return 0;
+
+ return ntohs(remote.sin_port);
+#endif
+}
+
+/* Return local port by socket. */
+
+SilcUInt16 silc_net_get_local_port(int sock)
+{
+#ifdef HAVE_IPV6
+ struct sockaddr_storage local;
+ int len;
+ char s[NI_MAXSERV];
+
+ memset(&local, 0, sizeof(local));
+ len = sizeof(local);
+ if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
+ return 0;
+
+ if (getnameinfo((struct sockaddr *)&local, len, NULL, 0, s, sizeof(s),
+ NI_NUMERICSERV))
+ return 0;
+
+ return atoi(s);
+#else
+ struct sockaddr_in local;
+ int len;
+
+ memset(&local, 0, sizeof(local));
+ len = sizeof(local);
+ if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
+ return 0;
+
+ return ntohs(local.sin_port);
+#endif
}
/* Return name of localhost. */
-char *silc_net_localhost()
+char *silc_net_localhost(void)
{
- char hostname[256];
- if (!gethostname(hostname, sizeof(hostname)))
+ char hostname[256], ip_addr[64];
+
+ if (gethostname(hostname, sizeof(hostname)))
+ return NULL;
+
+ if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr)))
return strdup(hostname);
- return NULL;
+
+ silc_net_gethostbyaddr(ip_addr, hostname, sizeof(hostname));
+ return strdup(hostname);
+}
+
+/* Returns local IP address */
+
+char *silc_net_localip(void)
+{
+ char hostname[256], ip_addr[64];
+
+ if (gethostname(hostname, sizeof(hostname)))
+ return NULL;
+
+ if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr)))
+ return NULL;
+
+ return strdup(ip_addr);
}