Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 1997 - 2007 Pekka Riikonen
+ Copyright (C) 1997 - 2008 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
GNU General Public License for more details.
*/
-/* $Id$ */
-#include "silc.h"
-#include "silcnet.h"
+#include "silcruntime.h"
+
+/************************** Types and definitions ***************************/
#ifdef HAVE_IPV6
#define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \
#endif
} SilcSockaddr;
+/************************ Static utility functions **************************/
+
static SilcBool silc_net_set_sockaddr(SilcSockaddr *addr, const char *ip_addr,
int port)
{
if (ip_addr) {
if (!silc_net_is_ip(ip_addr)) {
SILC_LOG_ERROR(("%s is not IP address", ip_addr));
+ silc_set_errno_reason(SILC_ERR_BAD_IP, "%s is not an IP address",
+ ip_addr);
return FALSE;
}
if (silc_net_is_ip4(ip_addr)) {
/* IPv4 address */
len = sizeof(addr->sin.sin_addr);
- silc_net_addr2bin(ip_addr,
- (unsigned char *)&addr->sin.sin_addr.s_addr, len);
+ if (!silc_net_addr2bin(ip_addr,
+ (unsigned char *)&addr->sin.sin_addr.s_addr, len))
+ return FALSE;
addr->sin.sin_family = AF_INET;
addr->sin.sin_port = port ? htons(port) : 0;
} else {
#ifdef HAVE_IPV6
/* IPv6 address */
len = sizeof(addr->sin6.sin6_addr);
- silc_net_addr2bin(ip_addr,
- (unsigned char *)&addr->sin6.sin6_addr, len);
+ if (!silc_net_addr2bin(ip_addr,
+ (unsigned char *)&addr->sin6.sin6_addr, len))
+ return FALSE;
addr->sin6.sin6_family = AF_INET6;
addr->sin6.sin6_port = port ? htons(port) : 0;
#else
SILC_LOG_ERROR(("IPv6 support is not compiled in"));
+ silc_set_errno(SILC_ERR_NOT_SUPPORTED);
return FALSE;
#endif
}
return TRUE;
}
+/****************************** TCP Listener ********************************/
+
/* Deliver new stream to upper layer */
-static void silc_net_accept_stream(SilcSocketStreamStatus status,
+static void silc_net_accept_stream(SilcResult status,
SilcStream stream, void *context)
{
SilcNetListener listener = context;
- if (status != SILC_SOCKET_OK)
+ if (status != SILC_OK)
return;
- listener->callback(SILC_NET_OK, stream, listener->context);
+ listener->callback(SILC_OK, stream, listener->context);
}
/* Accept incoming connection and notify upper layer */
return;
/* Set socket options */
- silc_net_set_socket_nonblock(sock);
silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
/* Create socket stream */
SILC_LOG_DEBUG(("Creating TCP listener"));
- if (port < 0 || !schedule || !callback)
+ if (!schedule) {
+ schedule = silc_schedule_get_global();
+ if (!schedule) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+ goto err;
+ }
+ }
+
+ if (port < 0 || !callback) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
goto err;
+ }
listener = silc_calloc(1, sizeof(*listener));
- if (!listener) {
- callback(SILC_NET_NO_MEMORY, NULL, context);
+ if (!listener)
return NULL;
- }
listener->schedule = schedule;
listener->callback = callback;
listener->context = context;
if (local_ip_count > 0) {
listener->socks = silc_calloc(local_ip_count, sizeof(*listener->socks));
- if (!listener->socks) {
- callback(SILC_NET_NO_MEMORY, NULL, context);
+ if (!listener->socks)
return NULL;
- }
} else {
listener->socks = silc_calloc(1, sizeof(*listener->socks));
- if (!listener->socks) {
- callback(SILC_NET_NO_MEMORY, NULL, context);
+ if (!listener->socks)
return NULL;
- }
local_ip_count = 1;
}
/* Bind to local addresses */
for (i = 0; i < local_ip_count; i++) {
- SILC_LOG_DEBUG(("Binding to local address %s",
- local_ip_addr ? local_ip_addr[i] : ipany));
+ SILC_LOG_DEBUG(("Binding to local address %s:%d",
+ local_ip_addr ? local_ip_addr[i] : ipany, port));
/* Set sockaddr for server */
if (!silc_net_set_sockaddr(&server,
/* Create the socket */
sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
if (sock < 0) {
- SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
+ silc_set_errno_posix(errno);
+ SILC_LOG_ERROR(("Cannot create socket: %s",
+ silc_errno_string(silc_errno)));
goto err;
}
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)));
+ close(sock);
+ goto err;
+ }
+
+ /* Bind the listener socket */
+ rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
+ if (rval < 0) {
+ silc_set_errno_posix(errno);
+ SILC_LOG_ERROR(("Cannot bind socket: %s",
+ silc_errno_string(silc_errno)));
+ close(sock);
+ goto err;
+ }
+
+ /* Specify that we are listenning */
+ rval = listen(sock, 64);
+ if (rval < 0) {
+ silc_set_errno_posix(errno);
+ SILC_LOG_ERROR(("Cannot set socket listenning: %s",
+ silc_errno_string(silc_errno)));
+ close(sock);
+ goto err;
+ }
+
+ /* Set the server socket to non-blocking mode */
+ silc_net_set_socket_nonblock(sock);
+
+ /* Schedule for incoming connections */
+ silc_schedule_task_add_fd(schedule, sock, silc_net_accept, listener);
+
+ SILC_LOG_DEBUG(("TCP listener created, fd=%d", sock));
+ listener->socks[i] = sock;
+ listener->socks_count++;
+ }
+
+ return listener;
+
+ err:
+ if (listener)
+ silc_net_close_listener(listener);
+ return NULL;
+}
+
+/* Create TCP listener, multiple ports */
+
+SilcNetListener
+silc_net_tcp_create_listener2(const char *local_ip_addr, int *ports,
+ SilcUInt32 port_count,
+ SilcBool ignore_port_error,
+ SilcBool lookup, SilcBool require_fqdn,
+ SilcSchedule schedule,
+ SilcNetCallback callback, void *context)
+{
+ SilcNetListener listener = NULL;
+ SilcSockaddr server;
+ int i, sock, rval;
+ const char *ipany = "0.0.0.0";
+
+ SILC_LOG_DEBUG(("Creating TCP listener"));
+
+ if (!schedule) {
+ schedule = silc_schedule_get_global();
+ if (!schedule) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+ goto err;
+ }
+ }
+
+ if (!callback) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+ goto err;
+ }
+
+ listener = silc_calloc(1, sizeof(*listener));
+ if (!listener)
+ return NULL;
+ listener->schedule = schedule;
+ listener->callback = callback;
+ listener->context = context;
+ listener->require_fqdn = require_fqdn;
+ listener->lookup = lookup;
+
+ if (port_count > 0) {
+ listener->socks = silc_calloc(port_count, sizeof(*listener->socks));
+ if (!listener->socks)
+ return NULL;
+ } else {
+ listener->socks = silc_calloc(1, sizeof(*listener->socks));
+ if (!listener->socks)
+ return NULL;
+
+ port_count = 1;
+ }
+
+ /* Bind to ports */
+ for (i = 0; i < port_count; i++) {
+ SILC_LOG_DEBUG(("Binding to local address %s:%d",
+ local_ip_addr ? local_ip_addr : ipany,
+ ports ? ports[i] : 0));
+
+ /* Set sockaddr for server */
+ if (!silc_net_set_sockaddr(&server,
+ local_ip_addr ? local_ip_addr : ipany,
+ ports ? ports[i] : 0)) {
+ if (ignore_port_error)
+ continue;
+ goto err;
+ }
+
+ /* Create the socket */
+ sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
+ if (sock < 0) {
+ if (ignore_port_error)
+ continue;
+ silc_set_errno_posix(errno);
+ SILC_LOG_ERROR(("Cannot create socket: %s",
+ silc_errno_string(silc_errno)));
+ goto err;
+ }
+
+ /* Set the socket options */
+ rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
+ if (rval < 0) {
+ close(sock);
+ if (ignore_port_error)
+ continue;
+ SILC_LOG_ERROR(("Cannot set socket options: %s",
+ silc_errno_string(silc_errno)));
goto err;
}
/* Bind the listener socket */
rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
if (rval < 0) {
- SILC_LOG_DEBUG(("Cannot bind socket: %s", strerror(errno)));
+ close(sock);
+ if (ignore_port_error)
+ continue;
+ silc_set_errno_posix(errno);
+ SILC_LOG_ERROR(("Cannot bind socket: %s",
+ silc_errno_string(silc_errno)));
goto err;
}
/* Specify that we are listenning */
rval = listen(sock, 64);
if (rval < 0) {
- SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
+ close(sock);
+ if (ignore_port_error)
+ continue;
+ silc_set_errno_posix(errno);
+ SILC_LOG_ERROR(("Cannot set socket listenning: %s",
+ silc_errno_string(silc_errno)));
goto err;
}
listener->socks_count++;
}
+ if (ignore_port_error && !listener->socks_count)
+ goto err;
+
return listener;
err:
- if (callback)
- callback(SILC_NET_ERROR, NULL, context);
if (listener)
silc_net_close_listener(listener);
return NULL;
silc_free(listener);
}
+/******************************* UDP Stream *********************************/
+
/* Create UDP stream */
SilcStream
SILC_LOG_DEBUG(("Creating UDP stream"));
- if (!schedule)
- goto err;
+ if (!schedule) {
+ schedule = silc_schedule_get_global();
+ if (!schedule) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+ goto err;
+ }
+ }
/* Bind to local addresses */
SILC_LOG_DEBUG(("Binding to local address %s",
/* Create the socket */
sock = socket(server.sin.sin_family, SOCK_DGRAM, 0);
if (sock < 0) {
- SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
+ silc_set_errno_posix(errno);
+ SILC_LOG_ERROR(("Cannot create socket: %s",
+ silc_errno_string(silc_errno)));
goto err;
}
/* 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)));
+ SILC_LOG_ERROR(("Cannot set socket options: %s",
+ silc_errno_string(silc_errno)));
goto err;
}
#ifdef SO_REUSEPORT
rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEPORT, 1);
if (rval < 0) {
- SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
+ SILC_LOG_ERROR(("Cannot set socket options: %s",
+ silc_errno_string(silc_errno)));
goto err;
}
#endif /* SO_REUSEPORT */
/* Bind the listener socket */
rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
if (rval < 0) {
- SILC_LOG_DEBUG(("Cannot bind socket: %s", strerror(errno)));
+ silc_set_errno_posix(errno);
+ SILC_LOG_DEBUG(("Cannot bind socket: %s", silc_errno_string(silc_errno)));
goto err;
}
- /* Set socket to non-blocking mode */
- silc_net_set_socket_nonblock(sock);
-
/* Set to connected state if remote address is provided. */
if (remote_ip_addr && remote_port) {
if (!silc_net_set_sockaddr(&server, remote_ip_addr, remote_port))
rval = connect(sock, &server.sa, SIZEOF_SOCKADDR(server));
if (rval < 0) {
- SILC_LOG_DEBUG(("Cannot connect UDP stream: %s", strerror(errno)));
+ silc_set_errno_posix(errno);
+ SILC_LOG_DEBUG(("Cannot connect UDP stream: %s",
+ silc_errno_string(silc_errno)));
goto err;
}
}
/* Set send and receive buffer size */
#ifdef SO_SNDBUF
- rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_SNDBUF, 65535);
+ rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_SNDBUF, 765535);
if (rval < 0) {
- SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
- goto err;
+ rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_SNDBUF, 65535);
+ if (rval < 0) {
+ SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
+ goto err;
+ }
}
#endif /* SO_SNDBUF */
#ifdef SO_RCVBUF
- rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_RCVBUF, 65535);
+ rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_RCVBUF, 765535);
if (rval < 0) {
- SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
- goto err;
+ rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_RCVBUF, 65535);
+ if (rval < 0) {
+ SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
+ goto err;
+ }
}
#endif /* SO_RCVBUF */
SilcSocketStream sock = stream;
SilcSockaddr s;
struct sockaddr *from;
- int len, flen;
+ int len;
+ unsigned int flen;
SILC_LOG_DEBUG(("Reading data from UDP socket %d", sock->sock));
if (remote_ip_addr && remote_port) {
if (sock->ipv6) {
+#ifdef HAVE_IPV6
from = (struct sockaddr *)&s.sin6;
flen = sizeof(s.sin6);
+#endif /* HAVE_IPV6 */
} else {
from = (struct sockaddr *)&s.sin;
flen = sizeof(s.sin);
len = recv(sock->sock, ret_data, data_size, 0);
if (len < 0) {
+ silc_set_errno_posix(errno);
if (errno == EAGAIN || errno == EINTR) {
SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
silc_schedule_set_listen_fd(sock->schedule, sock->sock,
SILC_LOG_DEBUG(("Cannot read from UDP socket: %d:%s",
sock->sock, strerror(errno)));
silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
- sock->sock_error = errno;
return -2;
}
/* Return remote address */
if (remote_ip_addr && remote_port) {
if (sock->ipv6) {
+#ifdef HAVE_IPV6
*remote_port = ntohs(s.sin6.sin6_port);
inet_ntop(AF_INET6, &s.sin6.sin6_addr, remote_ip_addr,
remote_ip_addr_size);
+#else
+ *remote_port = 0;
+#endif /* HAVE_IPV6 */
} else {
*remote_port = ntohs(s.sin.sin_port);
inet_ntop(AF_INET, &s.sin.sin_addr, remote_ip_addr,
ret = sendto(sock->sock, data, data_len, 0, &remote.sa,
SIZEOF_SOCKADDR(remote));
if (ret < 0) {
+ silc_set_errno_posix(errno);
if (errno == EAGAIN || errno == EINTR) {
SILC_LOG_DEBUG(("Could not send immediately, will do it later"));
silc_schedule_set_listen_fd(sock->schedule, sock->sock,
}
SILC_LOG_DEBUG(("Cannot send to UDP socket: %s", strerror(errno)));
silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
- sock->sock_error = errno;
return -2;
}
return ret;
}
+/******************************* TCP Stream *********************************/
+
/* Asynchronous TCP/IP connecting */
typedef struct {
- SilcNetStatus status;
- SilcSocketStreamStatus stream_status;
+ SilcResult status;
SilcStream stream;
SilcFSMStruct fsm;
SilcFSMEventStruct event;
"host", conn->remote));
/** Network unreachable */
- conn->status = SILC_NET_HOST_UNREACHABLE;
+ conn->status = SILC_ERR_UNREACHABLE;
silc_fsm_next(fsm, silc_net_connect_st_finish);
return SILC_FSM_CONTINUE;
}
}
/** Cannot create socket */
+ silc_set_errno_posix(errno);
SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
silc_fsm_next(fsm, silc_net_connect_st_finish);
return SILC_FSM_CONTINUE;
rval = connect(sock, &desthost.sa, SIZEOF_SOCKADDR(desthost));
if (rval < 0) {
if (errno != EINPROGRESS) {
- /* retry using an IPv4 adress, if IPv6 didn't work */
- if (prefer_ipv6 && silc_net_is_ip6(conn->ip_addr)) {
- shutdown(sock, 2);
- close(sock);
+ silc_set_errno_posix(errno);
+ shutdown(sock, 2);
+ close(sock);
+ /* Retry using an IPv4 adress, if IPv6 didn't work */
+ if (prefer_ipv6 && silc_net_is_ip6(conn->ip_addr)) {
prefer_ipv6 = FALSE;
goto retry;
}
- shutdown(sock, 2);
- close(sock);
-
/** Cannot connect to remote host */
- SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
+ SILC_LOG_ERROR(("Cannot connect to remote host: %s",
+ silc_errno_string(silc_errno)));
silc_fsm_next(fsm, silc_net_connect_st_finish);
return SILC_FSM_CONTINUE;
}
return SILC_FSM_CONTINUE;
}
-static void silc_net_connect_wait_stream(SilcSocketStreamStatus status,
+static void silc_net_connect_wait_stream(SilcResult status,
SilcStream stream, void *context)
{
SilcNetConnect conn = context;
- conn->stream_status = status;
+ conn->sop = NULL;
+ conn->status = status;
conn->stream = stream;
SILC_FSM_CALL_CONTINUE(&conn->fsm);
}
return SILC_FSM_CONTINUE;
}
-#if defined(ECONNREFUSED)
- if (errno == ECONNREFUSED)
- conn->status = SILC_NET_CONNECTION_REFUSED;
-#endif /* ECONNREFUSED */
-#if defined(ETIMEDOUT)
- if (errno == ETIMEDOUT)
- conn->status = SILC_NET_CONNECTION_TIMEOUT;
-#endif /* ETIMEDOUT */
-#if defined(ENETUNREACH)
- if (errno == ENETUNREACH)
- conn->status = SILC_NET_HOST_UNREACHABLE;
-#endif /* ENETUNREACH */
+ /* Set error */
+ silc_set_errno_posix(opt);
+ conn->status = silc_errno;
/** Connecting failed */
- SILC_LOG_DEBUG(("Connecting failed"));
+ SILC_LOG_DEBUG(("Connecting failed, error %s",
+ silc_errno_string(silc_errno)));
silc_fsm_next(fsm, silc_net_connect_st_finish);
return SILC_FSM_CONTINUE;
}
/** Connection created */
silc_fsm_next(fsm, silc_net_connect_st_stream);
SILC_FSM_CALL((conn->sop = silc_socket_tcp_stream_create(
- conn->sock, FALSE, FALSE,
- schedule,
+ conn->sock, TRUE, FALSE, schedule,
silc_net_connect_wait_stream, conn)));
}
return SILC_FSM_CONTINUE;
}
- if (conn->stream_status != SILC_SOCKET_OK) {
+ if (conn->status != SILC_OK) {
/** Stream creation failed */
- if (conn->stream_status == SILC_SOCKET_UNKNOWN_IP)
- conn->status = SILC_NET_UNKNOWN_IP;
- else if (conn->stream_status == SILC_SOCKET_UNKNOWN_HOST)
- conn->status = SILC_NET_UNKNOWN_HOST;
- else
- conn->status = SILC_NET_ERROR;
silc_fsm_next(fsm, silc_net_connect_st_finish);
return SILC_FSM_CONTINUE;
}
- /* Set stream information */
- silc_socket_stream_set_info(conn->stream,
- !silc_net_is_ip(conn->remote) ? conn->remote :
- conn->ip_addr, conn->ip_addr, conn->port);
-
/** Stream created successfully */
SILC_LOG_DEBUG(("Connected successfully, sock %d", conn->sock));
- conn->status = SILC_NET_OK;
+ conn->status = SILC_OK;
silc_fsm_next(fsm, silc_net_connect_st_finish);
return SILC_FSM_CONTINUE;
}
conn->callback(conn->status, conn->stream, conn->context);
if (conn->op)
silc_async_free(conn->op);
- if (conn->sop)
- silc_async_free(conn->sop);
}
+ if (conn->sock && conn->status != SILC_NET_OK)
+ silc_net_close_connection(conn->sock);
+
return SILC_FSM_FINISH;
}
conn->aborted = TRUE;
/* Abort underlaying stream creation too */
- if (conn->sop)
- silc_async_abort(conn->op, NULL, NULL);
+ if (conn->sop) {
+ silc_async_abort(conn->sop, NULL, NULL);
+ conn->sop = NULL;
+ }
}
static void silc_net_connect_destructor(SilcFSM fsm, void *fsm_context,
{
SilcNetConnect conn;
- if (!remote_ip_addr || remote_port < 1 || !schedule || !callback)
+ if (!schedule) {
+ schedule = silc_schedule_get_global();
+ if (!schedule) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+ return NULL;
+ }
+ }
+
+ if (!remote_ip_addr || remote_port < 1 || !callback) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
return NULL;
+ }
SILC_LOG_DEBUG(("Creating connection to host %s port %d",
remote_ip_addr, remote_port));
conn = silc_calloc(1, sizeof(*conn));
if (!conn) {
- callback(SILC_NET_NO_MEMORY, NULL, context);
+ callback(silc_errno, NULL, context);
return NULL;
}
conn->op = silc_async_alloc(silc_net_connect_abort, NULL, conn);
if (!conn->op) {
silc_free(conn);
- callback(SILC_NET_NO_MEMORY, NULL, context);
+ callback(silc_errno, NULL, context);
return NULL;
}
if (local_ip_addr)
- conn->local_ip = strdup(local_ip_addr);
- conn->remote = strdup(remote_ip_addr);
+ conn->local_ip = silc_strdup(local_ip_addr);
+ conn->remote = silc_strdup(remote_ip_addr);
if (!conn->remote) {
silc_async_free(conn->op);
silc_free(conn->local_ip);
silc_free(conn);
- callback(SILC_NET_NO_MEMORY, NULL, context);
+ callback(silc_errno, NULL, context);
return NULL;
}
conn->port = remote_port;
conn->callback = callback;
conn->context = context;
conn->retry = 1;
- conn->status = SILC_NET_ERROR;
+ conn->status = SILC_ERR;
silc_fsm_init(&conn->fsm, conn, silc_net_connect_destructor, NULL, schedule);
silc_fsm_start(&conn->fsm, silc_net_connect_st_start);
int silc_net_set_socket_nonblock(SilcSocket sock)
{
- return fcntl((int)sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
+ int ret = fcntl((int)sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
+ if (ret)
+ silc_set_errno_posix(errno);
+ return ret;
}
/* Converts the IP number string from numbers-and-dots notation to
if (silc_net_is_ip4(addr)) {
/* IPv4 address */
struct in_addr tmp;
+
+ if (bin_len < 4) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+ return FALSE;
+ }
+
ret = inet_aton(addr, &tmp);
- if (bin_len < 4)
+ if (!ret) {
+ silc_set_errno_posix(errno);
return FALSE;
+ }
memcpy(bin, (unsigned char *)&tmp.s_addr, 4);
#ifdef HAVE_IPV6
SilcSockaddr *s;
/* IPv6 address */
- if (bin_len < 16)
+ if (bin_len < 16) {
+ silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
return FALSE;
+ }
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;