X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcnet.c;h=a6c0f9271456171013bc36b0aac679bae7a2b03c;hb=9905799a86c606304fd7df2cd401de1740a272a1;hp=c25637c08021c4966430656984ec6ece6ea6406f;hpb=05e42fd7b315f9a6e195287c78d93445d25a5c1a;p=silc.git diff --git a/lib/silcutil/silcnet.c b/lib/silcutil/silcnet.c index c25637c0..a6c0f927 100644 --- a/lib/silcutil/silcnet.c +++ b/lib/silcutil/silcnet.c @@ -4,13 +4,12 @@ Author: Pekka Riikonen - Copyright (C) 1997 - 2001 Pekka Riikonen + Copyright (C) 1997 - 2006 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 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -19,8 +18,28 @@ */ /* $Id$ */ -#include "silcincludes.h" -#include "silcnet.h" +#include "silc.h" + +/* Returns bound port from listener */ + +SilcUInt16 *silc_net_listener_get_port(SilcNetListener listener, + SilcUInt32 *port_count) +{ + SilcUInt16 *ports; + int i; + + ports = silc_calloc(listener->socks_count, sizeof(*ports)); + if (!ports) + return NULL; + + for (i = 0; i < listener->socks_count; i++) + ports[i] = silc_net_get_local_port(listener->socks[i]); + + if (port_count) + *port_count = listener->socks_count; + + return ports; +} /* Accepts a connection from a particular socket */ @@ -38,7 +57,7 @@ int silc_net_set_socket_opt(int sock, int level, int option, int on) /* Get socket options */ -int silc_net_get_socket_opt(int sock, int level, int option, +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); @@ -46,12 +65,12 @@ int silc_net_get_socket_opt(int sock, int level, int option, /* Checks whether IP address sent as argument is valid IPv4 address. */ -bool silc_net_is_ip4(const char *addr) +SilcBool silc_net_is_ip4(const char *addr) { int count = 0; while (*addr) { - if (*addr != '.' && !isdigit(*addr)) + if (*addr != '.' && !isdigit((int)*addr)) return FALSE; if (*addr == '.') count++; @@ -60,27 +79,27 @@ bool silc_net_is_ip4(const char *addr) if (count != 3) return FALSE; - + return TRUE; } /* Checks whether IP address sent as argument is valid IPv6 address. */ -bool silc_net_is_ip6(const char *addr) +SilcBool silc_net_is_ip6(const char *addr) { /* XXX does this work with all kinds of IPv6 addresses? */ while (*addr) { - if (*addr != ':' && !isxdigit(*addr)) + if (*addr != ':' && !isxdigit((int)*addr)) return FALSE; addr++; } - + return TRUE; } /* Checks whether IP address sent as argument is valid IP address. */ -bool silc_net_is_ip(const char *addr) +SilcBool silc_net_is_ip(const char *addr) { if (silc_net_is_ip4(addr)) return TRUE; @@ -91,7 +110,7 @@ bool silc_net_is_ip(const char *addr) typedef struct { SilcNetResolveCallback completion; void *context; - bool prefer_ipv6; + SilcBool prefer_ipv6; SilcSchedule schedule; char *input; char *result; @@ -122,7 +141,7 @@ static void *silc_net_gethostbyname_thread(void *context) r->result = strdup(tmp); silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1, - SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL); + SILC_TASK_TIMEOUT); silc_schedule_wakeup(schedule); return NULL; } @@ -139,15 +158,16 @@ static void *silc_net_gethostbyaddr_thread(void *context) r->result = strdup(tmp); silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1, - SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL); + SILC_TASK_TIMEOUT); silc_schedule_wakeup(schedule); return NULL; } /* Resolves IP address for hostname. */ -bool silc_net_gethostbyname(const char *name, bool prefer_ipv6, char *address, - SilcUInt32 address_len) +SilcBool silc_net_gethostbyname(const char *name, + SilcBool prefer_ipv6, char *address, + SilcUInt32 address_len) { #ifdef HAVE_IPV6 struct addrinfo hints, *ai, *tmp, *ip4 = NULL, *ip6 = NULL; @@ -203,14 +223,14 @@ bool silc_net_gethostbyname(const char *name, bool prefer_ipv6, char *address, memset(address, 0, address_len); strncpy(address, tmp, strlen(tmp)); #endif - + return TRUE; } /* Resolves IP address for hostname async. */ -void silc_net_gethostbyname_async(const char *name, - bool prefer_ipv6, +void silc_net_gethostbyname_async(const char *name, + SilcBool prefer_ipv6, SilcSchedule schedule, SilcNetResolveCallback completion, void *context) @@ -228,15 +248,16 @@ void silc_net_gethostbyname_async(const char *name, /* Resolves hostname by IP address. */ -bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len) +SilcBool silc_net_gethostbyaddr(const char *addr, char *name, + SilcUInt32 name_len) { #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)) { @@ -259,13 +280,13 @@ bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len) memset(name, 0, name_len); strncpy(name, hp->h_name, strlen(hp->h_name)); #endif - + return TRUE; } /* Resolves hostname by IP address async. */ -void silc_net_gethostbyaddr_async(const char *addr, +void silc_net_gethostbyaddr_async(const char *addr, SilcSchedule schedule, SilcNetResolveCallback completion, void *context) @@ -280,10 +301,13 @@ void silc_net_gethostbyaddr_async(const char *addr, silc_thread_create(silc_net_gethostbyaddr_thread, r, FALSE); } +#ifndef SILC_SYMBIAN + /* Performs lookups for remote name and IP address. This peforms reverse lookup as well to verify that the IP has FQDN. */ -bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip) +SilcBool silc_net_check_host_by_sock(SilcSocket sock, char **hostname, + char **ip) { char host[1024]; int rval, len; @@ -292,7 +316,8 @@ bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip) struct sockaddr_storage remote; char s[NI_MAXHOST]; - *hostname = NULL; + if (hostname) + *hostname = NULL; *ip = NULL; SILC_LOG_DEBUG(("Resolving remote hostname and IP address")); @@ -315,7 +340,8 @@ bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip) struct sockaddr_in remote; char *host_ip; - *hostname = NULL; + if (hostname) + *hostname = NULL; *ip = NULL; SILC_LOG_DEBUG(("Resolving remote hostname and IP address")); @@ -335,19 +361,22 @@ bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip) return FALSE; #endif - /* Get host by address */ - if (!silc_net_gethostbyaddr(*ip, host, sizeof(host))) - return FALSE; + /* 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)); + *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; + /* Reverse */ + if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host))) + return FALSE; - if (strcmp(*ip, host)) - return FALSE; + if (strcmp(*ip, host)) + return FALSE; + } SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip)); return TRUE; @@ -356,7 +385,8 @@ bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip) /* 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) +SilcBool silc_net_check_local_by_sock(SilcSocket sock, char **hostname, + char **ip) { char host[1024]; int rval, len; @@ -365,7 +395,8 @@ bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip) struct sockaddr_storage local; char s[NI_MAXHOST]; - *hostname = NULL; + if (hostname) + *hostname = NULL; *ip = NULL; SILC_LOG_DEBUG(("Resolving local hostname and IP address")); @@ -388,7 +419,8 @@ bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip) struct sockaddr_in local; char *host_ip; - *hostname = NULL; + if (hostname) + *hostname = NULL; *ip = NULL; SILC_LOG_DEBUG(("Resolving local hostname and IP address")); @@ -408,19 +440,22 @@ bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip) return FALSE; #endif - /* Get host by address */ - if (!silc_net_gethostbyaddr(*ip, host, sizeof(host))) - return FALSE; + /* 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)); + *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; + /* Reverse */ + if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host))) + return FALSE; - if (strcmp(*ip, host)) - return FALSE; + if (strcmp(*ip, host)) + return FALSE; + } SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip)); return TRUE; @@ -428,7 +463,7 @@ bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip) /* Return remote port by socket. */ -SilcUInt16 silc_net_get_remote_port(int sock) +SilcUInt16 silc_net_get_remote_port(SilcSocket sock) { #ifdef HAVE_IPV6 struct sockaddr_storage remote; @@ -443,7 +478,7 @@ SilcUInt16 silc_net_get_remote_port(int sock) if (getnameinfo((struct sockaddr *)&remote, len, NULL, 0, s, sizeof(s), NI_NUMERICSERV)) return 0; - + return atoi(s); #else struct sockaddr_in remote; @@ -460,7 +495,7 @@ SilcUInt16 silc_net_get_remote_port(int sock) /* Return local port by socket. */ -SilcUInt16 silc_net_get_local_port(int sock) +SilcUInt16 silc_net_get_local_port(SilcSocket sock) { #ifdef HAVE_IPV6 struct sockaddr_storage local; @@ -475,7 +510,7 @@ SilcUInt16 silc_net_get_local_port(int sock) if (getnameinfo((struct sockaddr *)&local, len, NULL, 0, s, sizeof(s), NI_NUMERICSERV)) return 0; - + return atoi(s); #else struct sockaddr_in local; @@ -489,6 +524,7 @@ SilcUInt16 silc_net_get_local_port(int sock) return ntohs(local.sin_port); #endif } +#endif /* !SILC_SYMBIAN */ /* Return name of localhost. */