5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
24 /************************** Types and definitions ***************************/
27 #define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \
28 sizeof(so.sin6) : sizeof(so.sin))
30 #define SIZEOF_SOCKADDR(so) (sizeof(so.sin))
35 struct sockaddr_in sin;
37 struct sockaddr_in6 sin6;
41 /************************ Static utility functions **************************/
43 static SilcBool silc_net_set_sockaddr(SilcSockaddr *addr, const char *ip_addr,
48 memset(addr, 0, sizeof(*addr));
50 /* Check for IPv4 and IPv6 addresses */
52 if (!silc_net_is_ip(ip_addr)) {
53 SILC_LOG_ERROR(("%s is not IP address", ip_addr));
57 if (silc_net_is_ip4(ip_addr)) {
59 len = sizeof(addr->sin.sin_addr);
60 silc_net_addr2bin(ip_addr,
61 (unsigned char *)&addr->sin.sin_addr.s_addr, len);
62 addr->sin.sin_family = AF_INET;
63 addr->sin.sin_port = port ? htons(port) : 0;
67 len = sizeof(addr->sin6.sin6_addr);
68 silc_net_addr2bin(ip_addr,
69 (unsigned char *)&addr->sin6.sin6_addr, len);
70 addr->sin6.sin6_family = AF_INET6;
71 addr->sin6.sin6_port = port ? htons(port) : 0;
73 SILC_LOG_ERROR(("IPv6 support is not compiled in"));
79 addr->sin.sin_family = AF_INET;
80 addr->sin.sin_addr.s_addr = INADDR_ANY;
82 addr->sin.sin_port = htons(port);
88 /****************************** TCP Listener ********************************/
90 /* Deliver new stream to upper layer */
92 static void silc_net_accept_stream(SilcSocketStreamStatus status,
93 SilcStream stream, void *context)
95 SilcNetListener listener = context;
97 if (status != SILC_SOCKET_OK)
100 listener->callback(SILC_NET_OK, stream, listener->context);
103 /* Accept incoming connection and notify upper layer */
105 SILC_TASK_CALLBACK(silc_net_accept)
107 SilcNetListener listener = context;
110 SILC_LOG_DEBUG(("Accepting new connection"));
112 sock = silc_net_accept_connection(fd);
116 /* Set socket options */
117 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
119 /* Create socket stream */
120 silc_socket_tcp_stream_create(sock, listener->lookup,
121 listener->require_fqdn, schedule,
122 silc_net_accept_stream, listener);
125 /* Create TCP network listener */
128 silc_net_tcp_create_listener(const char **local_ip_addr,
129 SilcUInt32 local_ip_count, int port,
130 SilcBool lookup, SilcBool require_fqdn,
131 SilcSchedule schedule,
132 SilcNetCallback callback, void *context)
134 SilcNetListener listener = NULL;
137 const char *ipany = "0.0.0.0";
139 SILC_LOG_DEBUG(("Creating TCP listener"));
141 if (port < 0 || !schedule || !callback)
144 listener = silc_calloc(1, sizeof(*listener));
147 listener->schedule = schedule;
148 listener->callback = callback;
149 listener->context = context;
150 listener->require_fqdn = require_fqdn;
151 listener->lookup = lookup;
153 if (local_ip_count > 0) {
154 listener->socks = silc_calloc(local_ip_count, sizeof(*listener->socks));
155 if (!listener->socks)
158 listener->socks = silc_calloc(1, sizeof(*listener->socks));
159 if (!listener->socks)
165 /* Bind to local addresses */
166 for (i = 0; i < local_ip_count; i++) {
167 SILC_LOG_DEBUG(("Binding to local address %s:%d",
168 local_ip_addr ? local_ip_addr[i] : ipany, port));
170 /* Set sockaddr for server */
171 if (!silc_net_set_sockaddr(&server,
172 local_ip_addr ? local_ip_addr[i] : ipany,
176 /* Create the socket */
177 sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
179 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
183 /* Set the socket options */
184 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
186 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
191 /* Bind the listener socket */
192 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
194 SILC_LOG_ERROR(("Cannot bind socket: %s", strerror(errno)));
199 /* Specify that we are listenning */
200 rval = listen(sock, 64);
202 SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
207 /* Set the server socket to non-blocking mode */
208 silc_net_set_socket_nonblock(sock);
210 /* Schedule for incoming connections */
211 silc_schedule_task_add_fd(schedule, sock, silc_net_accept, listener);
213 SILC_LOG_DEBUG(("TCP listener created, fd=%d", sock));
214 listener->socks[i] = sock;
215 listener->socks_count++;
222 silc_net_close_listener(listener);
226 /* Create TCP listener, multiple ports */
229 silc_net_tcp_create_listener2(const char *local_ip_addr, int *ports,
230 SilcUInt32 port_count,
231 SilcBool ignore_port_error,
232 SilcBool lookup, SilcBool require_fqdn,
233 SilcSchedule schedule,
234 SilcNetCallback callback, void *context)
236 SilcNetListener listener = NULL;
239 const char *ipany = "0.0.0.0";
241 SILC_LOG_DEBUG(("Creating TCP listener"));
243 if (!schedule || !callback)
246 listener = silc_calloc(1, sizeof(*listener));
249 listener->schedule = schedule;
250 listener->callback = callback;
251 listener->context = context;
252 listener->require_fqdn = require_fqdn;
253 listener->lookup = lookup;
255 if (port_count > 0) {
256 listener->socks = silc_calloc(port_count, sizeof(*listener->socks));
257 if (!listener->socks)
260 listener->socks = silc_calloc(1, sizeof(*listener->socks));
261 if (!listener->socks)
268 for (i = 0; i < port_count; i++) {
269 SILC_LOG_DEBUG(("Binding to local address %s:%d",
270 local_ip_addr ? local_ip_addr : ipany,
271 ports ? ports[i] : 0));
273 /* Set sockaddr for server */
274 if (!silc_net_set_sockaddr(&server,
275 local_ip_addr ? local_ip_addr : ipany,
276 ports ? ports[i] : 0)) {
277 if (ignore_port_error)
282 /* Create the socket */
283 sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
285 if (ignore_port_error)
287 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
291 /* Set the socket options */
292 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
295 if (ignore_port_error)
297 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
301 /* Bind the listener socket */
302 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
305 if (ignore_port_error)
307 SILC_LOG_ERROR(("Cannot bind socket: %s", strerror(errno)));
311 /* Specify that we are listenning */
312 rval = listen(sock, 64);
315 SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
316 if (ignore_port_error)
321 /* Set the server socket to non-blocking mode */
322 silc_net_set_socket_nonblock(sock);
324 /* Schedule for incoming connections */
325 silc_schedule_task_add_fd(schedule, sock, silc_net_accept, listener);
327 SILC_LOG_DEBUG(("TCP listener created, fd=%d", sock));
328 listener->socks[i] = sock;
329 listener->socks_count++;
332 if (ignore_port_error && !listener->socks_count)
339 silc_net_close_listener(listener);
343 /* Close network listener */
345 void silc_net_close_listener(SilcNetListener listener)
349 SILC_LOG_DEBUG(("Closing network listener"));
351 for (i = 0; i < listener->socks_count; i++) {
352 silc_schedule_task_del_by_fd(listener->schedule, listener->socks[i]);
353 shutdown(listener->socks[i], 2);
354 close(listener->socks[i]);
357 silc_free(listener->socks);
361 /******************************* UDP Stream *********************************/
363 /* Create UDP stream */
366 silc_net_udp_connect(const char *local_ip_addr, int local_port,
367 const char *remote_ip_addr, int remote_port,
368 SilcSchedule schedule)
373 const char *ipany = "0.0.0.0";
375 SILC_LOG_DEBUG(("Creating UDP stream"));
380 /* Bind to local addresses */
381 SILC_LOG_DEBUG(("Binding to local address %s",
382 local_ip_addr ? local_ip_addr : ipany));
384 /* Set sockaddr for server */
385 if (!silc_net_set_sockaddr(&server, local_ip_addr ? local_ip_addr : ipany,
389 /* Create the socket */
390 sock = socket(server.sin.sin_family, SOCK_DGRAM, 0);
392 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
396 /* Set the socket options */
397 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
399 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
403 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEPORT, 1);
405 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
408 #endif /* SO_REUSEPORT */
410 /* Bind the listener socket */
411 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
413 SILC_LOG_DEBUG(("Cannot bind socket: %s", strerror(errno)));
417 /* Set to connected state if remote address is provided. */
418 if (remote_ip_addr && remote_port) {
419 if (!silc_net_set_sockaddr(&server, remote_ip_addr, remote_port))
422 rval = connect(sock, &server.sa, SIZEOF_SOCKADDR(server));
424 SILC_LOG_DEBUG(("Cannot connect UDP stream: %s", strerror(errno)));
429 /* Set send and receive buffer size */
431 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_SNDBUF, 765535);
433 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_SNDBUF, 65535);
435 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
439 #endif /* SO_SNDBUF */
441 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_RCVBUF, 765535);
443 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_RCVBUF, 65535);
445 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
449 #endif /* SO_RCVBUF */
451 /* Encapsulate into socket stream */
453 silc_socket_udp_stream_create(sock, local_ip_addr ?
454 silc_net_is_ip6(local_ip_addr) : FALSE,
455 remote_ip_addr ? TRUE : FALSE, schedule);
459 SILC_LOG_DEBUG(("UDP stream created, fd=%d", sock));
468 /* Receive UDP packet */
470 int silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
471 SilcUInt32 remote_ip_addr_size, int *remote_port,
472 unsigned char *ret_data, SilcUInt32 data_size)
474 SilcSocketStream sock = stream;
476 struct sockaddr *from;
479 SILC_LOG_DEBUG(("Reading data from UDP socket %d", sock->sock));
481 if (remote_ip_addr && remote_port) {
484 from = (struct sockaddr *)&s.sin6;
485 flen = sizeof(s.sin6);
486 #endif /* HAVE_IPV6 */
488 from = (struct sockaddr *)&s.sin;
489 flen = sizeof(s.sin);
491 len = recvfrom(sock->sock, ret_data, data_size, 0, from, &flen);
493 len = recv(sock->sock, ret_data, data_size, 0);
496 if (errno == EAGAIN || errno == EINTR) {
497 SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
498 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
499 SILC_TASK_READ, FALSE);
502 SILC_LOG_DEBUG(("Cannot read from UDP socket: %d:%s",
503 sock->sock, strerror(errno)));
504 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
505 sock->sock_error = errno;
509 SILC_LOG_DEBUG(("Read %d bytes", len));
512 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
514 /* Return remote address */
515 if (remote_ip_addr && remote_port) {
518 *remote_port = ntohs(s.sin6.sin6_port);
519 inet_ntop(AF_INET6, &s.sin6.sin6_addr, remote_ip_addr,
520 remote_ip_addr_size);
523 #endif /* HAVE_IPV6 */
525 *remote_port = ntohs(s.sin.sin_port);
526 inet_ntop(AF_INET, &s.sin.sin_addr, remote_ip_addr,
527 remote_ip_addr_size);
530 SILC_LOG_DEBUG(("UDP packet from %s:%d", remote_ip_addr, *remote_port));
536 /* Send UDP packet */
538 int silc_net_udp_send(SilcStream stream,
539 const char *remote_ip_addr, int remote_port,
540 const unsigned char *data, SilcUInt32 data_len)
542 SilcSocketStream sock = stream;
546 SILC_LOG_DEBUG(("Sending data to UDP socket %d", sock->sock));
549 if (!silc_net_set_sockaddr(&remote, remote_ip_addr, remote_port))
553 ret = sendto(sock->sock, data, data_len, 0, &remote.sa,
554 SIZEOF_SOCKADDR(remote));
556 if (errno == EAGAIN || errno == EINTR) {
557 SILC_LOG_DEBUG(("Could not send immediately, will do it later"));
558 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
559 SILC_TASK_READ | SILC_TASK_WRITE, FALSE);
562 SILC_LOG_DEBUG(("Cannot send to UDP socket: %s", strerror(errno)));
563 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
564 sock->sock_error = errno;
568 SILC_LOG_DEBUG(("Sent data %d bytes", ret));
569 if (silc_schedule_get_fd_events(sock->schedule, sock->sock) &
571 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
572 SILC_TASK_READ, FALSE);
577 /******************************* TCP Stream *********************************/
579 /* Asynchronous TCP/IP connecting */
582 SilcNetStatus status;
583 SilcSocketStreamStatus stream_status;
586 SilcFSMEventStruct event;
587 SilcAsyncOperation op;
588 SilcAsyncOperation sop;
593 SilcNetCallback callback;
595 unsigned int port : 24;
596 unsigned int retry : 7;
597 unsigned int aborted : 1;
600 SILC_FSM_STATE(silc_net_connect_st_start);
601 SILC_FSM_STATE(silc_net_connect_st_connected);
602 SILC_FSM_STATE(silc_net_connect_st_stream);
603 SILC_FSM_STATE(silc_net_connect_st_finish);
605 SILC_TASK_CALLBACK(silc_net_connect_wait)
607 SilcNetConnect conn = context;
608 SILC_FSM_EVENT_SIGNAL(&conn->event);
611 SILC_FSM_STATE(silc_net_connect_st_start)
613 SilcNetConnect conn = fsm_context;
615 SilcSockaddr desthost;
616 SilcBool prefer_ipv6 = TRUE;
620 silc_fsm_next(fsm, silc_net_connect_st_finish);
621 return SILC_FSM_CONTINUE;
626 if (!silc_net_gethostbyname(conn->remote, prefer_ipv6,
627 conn->ip_addr, sizeof(conn->ip_addr))) {
628 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
629 "host", conn->remote));
631 /** Network unreachable */
632 conn->status = SILC_NET_HOST_UNREACHABLE;
633 silc_fsm_next(fsm, silc_net_connect_st_finish);
634 return SILC_FSM_CONTINUE;
637 /* Set sockaddr for this connection */
638 if (!silc_net_set_sockaddr(&desthost, conn->ip_addr, conn->port)) {
639 /** Sockaddr failed */
640 silc_fsm_next(fsm, silc_net_connect_st_finish);
641 return SILC_FSM_CONTINUE;
644 /* Create the connection socket */
645 sock = socket(desthost.sin.sin_family, SOCK_STREAM, 0);
647 /* If address is IPv6, then fallback to IPv4 and see whether we can do
648 better with that on socket creation. */
649 if (prefer_ipv6 && silc_net_is_ip6(conn->ip_addr)) {
654 /** Cannot create socket */
655 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
656 silc_fsm_next(fsm, silc_net_connect_st_finish);
657 return SILC_FSM_CONTINUE;
660 /* Bind to the local address if provided */
661 if (conn->local_ip) {
664 /* Set sockaddr for local listener, and try to bind it. */
665 if (silc_net_set_sockaddr(&local, conn->local_ip, 0))
666 bind(sock, &local.sa, SIZEOF_SOCKADDR(local));
669 /* Set the socket to non-blocking mode */
670 silc_net_set_socket_nonblock(sock);
672 /* Connect to the host */
673 rval = connect(sock, &desthost.sa, SIZEOF_SOCKADDR(desthost));
675 if (errno != EINPROGRESS) {
679 /* Retry using an IPv4 adress, if IPv6 didn't work */
680 if (prefer_ipv6 && silc_net_is_ip6(conn->ip_addr)) {
685 /** Cannot connect to remote host */
686 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
687 silc_fsm_next(fsm, silc_net_connect_st_finish);
688 return SILC_FSM_CONTINUE;
692 /* Set appropriate options */
693 #if defined(TCP_NODELAY)
694 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
696 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
698 SILC_LOG_DEBUG(("Connection operation in progress"));
702 /** Wait for connection */
703 silc_fsm_next(fsm, silc_net_connect_st_connected);
704 silc_fsm_event_init(&conn->event, fsm);
705 silc_schedule_task_add_fd(silc_fsm_get_schedule(fsm), sock,
706 silc_net_connect_wait, conn);
707 silc_schedule_set_listen_fd(silc_fsm_get_schedule(fsm), sock,
708 SILC_TASK_WRITE, FALSE);
709 SILC_FSM_EVENT_WAIT(&conn->event);
710 return SILC_FSM_CONTINUE;
713 static void silc_net_connect_wait_stream(SilcSocketStreamStatus status,
714 SilcStream stream, void *context)
716 SilcNetConnect conn = context;
718 conn->stream_status = status;
719 conn->stream = stream;
720 SILC_FSM_CALL_CONTINUE(&conn->fsm);
723 SILC_FSM_STATE(silc_net_connect_st_connected)
725 SilcNetConnect conn = fsm_context;
726 SilcSchedule schedule = silc_fsm_get_schedule(fsm);
727 int opt = EINVAL, optlen = sizeof(opt), ret;
731 silc_schedule_unset_listen_fd(schedule, conn->sock);
732 silc_schedule_task_del_by_fd(schedule, conn->sock);
733 silc_fsm_next(fsm, silc_net_connect_st_finish);
734 return SILC_FSM_CONTINUE;
737 ret = silc_net_get_socket_opt(conn->sock, SOL_SOCKET, SO_ERROR,
740 silc_schedule_unset_listen_fd(schedule, conn->sock);
741 silc_schedule_task_del_by_fd(schedule, conn->sock);
743 if (ret != 0 || opt != 0) {
745 /** Retry connecting */
746 SILC_LOG_DEBUG(("Retry connecting"));
748 silc_net_close_connection(conn->sock);
749 silc_fsm_next(fsm, silc_net_connect_st_start);
750 return SILC_FSM_CONTINUE;
753 #if defined(ECONNREFUSED)
754 if (opt == ECONNREFUSED)
755 conn->status = SILC_NET_CONNECTION_REFUSED;
756 #endif /* ECONNREFUSED */
757 #if defined(ETIMEDOUT)
758 if (opt == ETIMEDOUT)
759 conn->status = SILC_NET_CONNECTION_TIMEOUT;
760 #endif /* ETIMEDOUT */
761 #if defined(ENETUNREACH)
762 if (opt == ENETUNREACH)
763 conn->status = SILC_NET_HOST_UNREACHABLE;
764 #endif /* ENETUNREACH */
766 /** Connecting failed */
767 SILC_LOG_DEBUG(("Connecting failed, error %s", strerror(opt)));
768 silc_fsm_next(fsm, silc_net_connect_st_finish);
769 return SILC_FSM_CONTINUE;
772 SILC_LOG_DEBUG(("TCP connection established"));
774 /** Connection created */
775 silc_fsm_next(fsm, silc_net_connect_st_stream);
776 SILC_FSM_CALL((conn->sop = silc_socket_tcp_stream_create(
777 conn->sock, TRUE, FALSE, schedule,
778 silc_net_connect_wait_stream, conn)));
781 SILC_FSM_STATE(silc_net_connect_st_stream)
783 SilcNetConnect conn = fsm_context;
787 silc_fsm_next(fsm, silc_net_connect_st_finish);
788 return SILC_FSM_CONTINUE;
791 if (conn->stream_status != SILC_SOCKET_OK) {
792 /** Stream creation failed */
793 if (conn->stream_status == SILC_SOCKET_UNKNOWN_IP)
794 conn->status = SILC_NET_UNKNOWN_IP;
795 else if (conn->stream_status == SILC_SOCKET_UNKNOWN_HOST)
796 conn->status = SILC_NET_UNKNOWN_HOST;
798 conn->status = SILC_NET_ERROR;
799 silc_fsm_next(fsm, silc_net_connect_st_finish);
800 return SILC_FSM_CONTINUE;
803 /** Stream created successfully */
804 SILC_LOG_DEBUG(("Connected successfully, sock %d", conn->sock));
805 conn->status = SILC_NET_OK;
806 silc_fsm_next(fsm, silc_net_connect_st_finish);
807 return SILC_FSM_CONTINUE;
810 SILC_FSM_STATE(silc_net_connect_st_finish)
812 SilcNetConnect conn = fsm_context;
814 /* Deliver error or new stream */
815 if (!conn->aborted) {
816 conn->callback(conn->status, conn->stream, conn->context);
818 silc_async_free(conn->op);
821 return SILC_FSM_FINISH;
824 static void silc_net_connect_abort(SilcAsyncOperation op, void *context)
826 SilcNetConnect conn = context;
827 conn->aborted = TRUE;
829 /* Abort underlaying stream creation too */
831 silc_async_abort(conn->sop, NULL, NULL);
836 static void silc_net_connect_destructor(SilcFSM fsm, void *fsm_context,
837 void *destructor_context)
839 SilcNetConnect conn = fsm_context;
840 silc_free(conn->local_ip);
841 silc_free(conn->remote);
845 /* Create asynchronous TCP/IP connection. */
847 SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
848 const char *remote_ip_addr,
850 SilcSchedule schedule,
851 SilcNetCallback callback,
856 if (!remote_ip_addr || remote_port < 1 || !schedule || !callback)
859 SILC_LOG_DEBUG(("Creating connection to host %s port %d",
860 remote_ip_addr, remote_port));
862 conn = silc_calloc(1, sizeof(*conn));
864 callback(SILC_NET_NO_MEMORY, NULL, context);
868 /* Start async operation */
869 conn->op = silc_async_alloc(silc_net_connect_abort, NULL, conn);
872 callback(SILC_NET_NO_MEMORY, NULL, context);
877 conn->local_ip = strdup(local_ip_addr);
878 conn->remote = strdup(remote_ip_addr);
880 silc_async_free(conn->op);
881 silc_free(conn->local_ip);
883 callback(SILC_NET_NO_MEMORY, NULL, context);
886 conn->port = remote_port;
887 conn->callback = callback;
888 conn->context = context;
890 conn->status = SILC_NET_ERROR;
892 silc_fsm_init(&conn->fsm, conn, silc_net_connect_destructor, NULL, schedule);
893 silc_fsm_start(&conn->fsm, silc_net_connect_st_start);
898 /* Closes the connection by closing the socket connection. */
900 void silc_net_close_connection(int sock)
902 SILC_LOG_DEBUG(("Closing sock %d", sock));
906 /* Set's the socket to non-blocking mode. */
908 int silc_net_set_socket_nonblock(SilcSocket sock)
910 return fcntl((int)sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
913 /* Converts the IP number string from numbers-and-dots notation to
916 SilcBool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len)
920 if (silc_net_is_ip4(addr)) {
923 ret = inet_aton(addr, &tmp);
927 memcpy(bin, (unsigned char *)&tmp.s_addr, 4);
930 struct addrinfo hints, *ai;
937 memset(&hints, 0, sizeof(hints));
938 hints.ai_family = AF_INET6;
939 if (getaddrinfo(addr, NULL, &hints, &ai))
943 s = (SilcSockaddr *)ai->ai_addr;
944 memcpy(bin, &s->sin6.sin6_addr, sizeof(s->sin6.sin6_addr));
949 #endif /* HAVE_IPV6 */