+ SilcNetListener listener = context;
+
+ if (status != SILC_OK)
+ return;
+
+ listener->callback(SILC_OK, stream, listener->context);
+}
+
+/* Accept incoming connection and notify upper layer */
+
+SILC_TASK_CALLBACK(silc_net_accept)
+{
+ SilcNetListener listener = context;
+ int sock;
+
+ SILC_LOG_DEBUG(("Accepting new connection"));
+
+ sock = silc_net_accept_connection(fd);
+ if (sock < 0)
+ return;
+
+ /* Set socket options */
+ silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
+
+ /* Create socket stream */
+ silc_socket_tcp_stream_create(sock, listener->lookup,
+ listener->require_fqdn, schedule,
+ silc_net_accept_stream, listener);
+}
+
+/* Create TCP network listener */
+
+SilcNetListener
+silc_net_tcp_create_listener(const char **local_ip_addr,
+ SilcUInt32 local_ip_count, int port,
+ 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 (port < 0 || !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 (local_ip_count > 0) {
+ listener->socks = silc_calloc(local_ip_count, sizeof(*listener->socks));
+ if (!listener->socks)
+ return NULL;
+ } else {
+ listener->socks = silc_calloc(1, sizeof(*listener->socks));
+ 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:%d",
+ local_ip_addr ? local_ip_addr[i] : ipany, port));
+
+ /* Set sockaddr for server */
+ if (!silc_net_set_sockaddr(&server,
+ local_ip_addr ? local_ip_addr[i] : ipany,
+ port))
+ goto err;
+
+ /* Create the socket */
+ sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
+ if (sock < 0) {
+ 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)));
+ 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;