+ 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;
+ 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) {
+ 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) {
+ 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;
+ }
+
+ /* 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++;
+ }
+
+ if (ignore_port_error && !listener->socks_count)
+ goto err;
+
+ return listener;
+
+ err:
+ if (listener)
+ silc_net_close_listener(listener);
+ return NULL;
+}
+
+/* Close network listener */
+
+void silc_net_close_listener(SilcNetListener listener)
+{
+ int i;
+
+ SILC_LOG_DEBUG(("Closing network listener"));
+
+ for (i = 0; i < listener->socks_count; i++) {
+ silc_schedule_task_del_by_fd(listener->schedule, listener->socks[i]);
+ shutdown(listener->socks[i], 2);
+ close(listener->socks[i]);
+ }
+
+ silc_free(listener->socks);
+ silc_free(listener);
+}
+
+/******************************* UDP Stream *********************************/
+
+/* Create UDP stream */
+
+SilcStream
+silc_net_udp_connect(const char *local_ip_addr, int local_port,
+ const char *remote_ip_addr, int remote_port,
+ SilcSchedule schedule)
+{
+ SilcStream stream;
+ SilcSockaddr server;
+ int sock = -1, rval;
+ const char *ipany = "0.0.0.0";
+
+ SILC_LOG_DEBUG(("Creating UDP stream"));
+
+ 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",
+ local_ip_addr ? local_ip_addr : ipany));