X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcnet.c;h=d2121556ef91c77372556f591d329279794ead7f;hb=20bb4767d7d7f5a9f3147c5cb47291932320dfff;hp=a6c0f9271456171013bc36b0aac679bae7a2b03c;hpb=9905799a86c606304fd7df2cd401de1740a272a1;p=crypto.git diff --git a/lib/silcutil/silcnet.c b/lib/silcutil/silcnet.c index a6c0f927..d2121556 100644 --- a/lib/silcutil/silcnet.c +++ b/lib/silcutil/silcnet.c @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 1997 - 2006 Pekka Riikonen + Copyright (C) 1997 - 2007 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 @@ -41,18 +41,70 @@ SilcUInt16 *silc_net_listener_get_port(SilcNetListener listener, return ports; } +/* Return bound IP from listener */ + +char **silc_net_listener_get_ip(SilcNetListener listener, + SilcUInt32 *ip_count) +{ + char **ips = NULL, *ip; + int i, k; + + ips = silc_calloc(listener->socks_count, sizeof(*ips)); + if (!ips) + return NULL; + + for (i = 0, k = 0; i < listener->socks_count; i++) { + if (silc_net_check_local_by_sock(listener->socks[i], NULL, &ip)) + ips[k++] = ip; + } + + if (ip_count) + *ip_count = k; + + return ips; +} + +/* Return bound hostname from listener */ + +char **silc_net_listener_get_hostname(SilcNetListener listener, + SilcUInt32 *hostname_count) +{ + char **hs = NULL, *h; + int i, k; + + hs = silc_calloc(listener->socks_count, sizeof(*hs)); + if (!hs) + return NULL; + + for (i = 0, k = 0; i < listener->socks_count; i++) { + if (silc_net_check_local_by_sock(listener->socks[i], &h, NULL)) + hs[k++] = h; + } + + if (hostname_count) + *hostname_count = k; + + return hs; +} + /* Accepts a connection from a particular socket */ int silc_net_accept_connection(int sock) { - return accept(sock, 0, 0); + int ret = accept(sock, 0, 0); + if (ret < 0) + silc_set_errno_posix(errno); + return ret; } /* Sets a option for a socket. */ int silc_net_set_socket_opt(int sock, int level, int option, int on) { - return setsockopt(sock, level, option, (void *)&on, sizeof(on)); + int ret = setsockopt(sock, level, option, (void *)&on, sizeof(on)); + if (ret < 0) + silc_set_errno_posix(errno); + return ret; } /* Get socket options */ @@ -60,7 +112,10 @@ int silc_net_set_socket_opt(int sock, int level, int option, int on) 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); + int ret = getsockopt(sock, level, option, optval, opt_len); + if (ret < 0) + silc_set_errno_posix(errno); + return ret; } /* Checks whether IP address sent as argument is valid IPv4 address. */ @@ -138,7 +193,7 @@ static void *silc_net_gethostbyname_thread(void *context) char tmp[64]; if (silc_net_gethostbyname(r->input, r->prefer_ipv6, tmp, sizeof(tmp))) - r->result = strdup(tmp); + r->result = silc_strdup(tmp); silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1, SILC_TASK_TIMEOUT); @@ -155,7 +210,7 @@ static void *silc_net_gethostbyaddr_thread(void *context) char tmp[256]; if (silc_net_gethostbyaddr(r->input, tmp, sizeof(tmp))) - r->result = strdup(tmp); + r->result = silc_strdup(tmp); silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1, SILC_TASK_TIMEOUT); @@ -210,6 +265,14 @@ SilcBool silc_net_gethostbyname(const char *name, struct in_addr ip; char *tmp; + if (silc_net_is_ip4(name)) { + memset(address, 0, address_len); + if (address_len < strlen(name)) + return FALSE; + strncpy(address, name, strlen(name)); + return TRUE; + } + hp = gethostbyname(name); if (!hp) return FALSE; @@ -241,7 +304,7 @@ void silc_net_gethostbyname_async(const char *name, r->context = context; r->prefer_ipv6 = prefer_ipv6; r->schedule = schedule; - r->input = strdup(name); + r->input = silc_strdup(name); silc_thread_create(silc_net_gethostbyname_thread, r, FALSE); } @@ -267,7 +330,7 @@ SilcBool silc_net_gethostbyaddr(const char *addr, char *name, freeaddrinfo(ai); #else struct hostent *hp; - unsigned char a[16]; + unsigned char a[4]; if (!silc_net_addr2bin(addr, a, sizeof(a))) return FALSE; @@ -296,7 +359,7 @@ void silc_net_gethostbyaddr_async(const char *addr, r->completion = completion; r->context = context; r->schedule = schedule; - r->input = strdup(addr); + r->input = silc_strdup(addr); silc_thread_create(silc_net_gethostbyaddr_thread, r, FALSE); } @@ -536,10 +599,10 @@ char *silc_net_localhost(void) return NULL; if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr))) - return strdup(hostname); + return silc_strdup(hostname); silc_net_gethostbyaddr(ip_addr, hostname, sizeof(hostname)); - return strdup(hostname); + return silc_strdup(hostname); } /* Returns local IP address */ @@ -554,5 +617,5 @@ char *silc_net_localip(void) if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr))) return NULL; - return strdup(ip_addr); + return silc_strdup(ip_addr); }