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));
54 silc_set_errno_reason(SILC_ERR_BAD_IP, "%s is not an IP address",
59 if (silc_net_is_ip4(ip_addr)) {
61 len = sizeof(addr->sin.sin_addr);
62 if (!silc_net_addr2bin(ip_addr,
63 (unsigned char *)&addr->sin.sin_addr.s_addr, len))
65 addr->sin.sin_family = AF_INET;
66 addr->sin.sin_port = port ? htons(port) : 0;
70 len = sizeof(addr->sin6.sin6_addr);
71 if (!silc_net_addr2bin(ip_addr,
72 (unsigned char *)&addr->sin6.sin6_addr, len))
74 addr->sin6.sin6_family = AF_INET6;
75 addr->sin6.sin6_port = port ? htons(port) : 0;
77 SILC_LOG_ERROR(("IPv6 support is not compiled in"));
78 silc_set_errno(SILC_ERR_NOT_SUPPORTED);
84 addr->sin.sin_family = AF_INET;
85 addr->sin.sin_addr.s_addr = INADDR_ANY;
87 addr->sin.sin_port = htons(port);
93 /****************************** TCP Listener ********************************/
95 /* Deliver new stream to upper layer */
97 static void silc_net_accept_stream(SilcResult status,
98 SilcStream stream, void *context)
100 SilcNetListener listener = context;
102 if (status != SILC_OK)
105 listener->callback(SILC_OK, stream, listener->context);
108 /* Accept incoming connection and notify upper layer */
110 SILC_TASK_CALLBACK(silc_net_accept)
112 SilcNetListener listener = context;
115 SILC_LOG_DEBUG(("Accepting new connection"));
117 sock = silc_net_accept_connection(fd);
121 /* Set socket options */
122 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
124 /* Create socket stream */
125 silc_socket_tcp_stream_create(sock, listener->lookup,
126 listener->require_fqdn, schedule,
127 silc_net_accept_stream, listener);
130 /* Create TCP network listener */
133 silc_net_tcp_create_listener(const char **local_ip_addr,
134 SilcUInt32 local_ip_count, int port,
135 SilcBool lookup, SilcBool require_fqdn,
136 SilcSchedule schedule,
137 SilcNetCallback callback, void *context)
139 SilcNetListener listener = NULL;
142 const char *ipany = "0.0.0.0";
144 SILC_LOG_DEBUG(("Creating TCP listener"));
146 if (port < 0 || !schedule || !callback) {
147 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
151 listener = silc_calloc(1, sizeof(*listener));
154 listener->schedule = schedule;
155 listener->callback = callback;
156 listener->context = context;
157 listener->require_fqdn = require_fqdn;
158 listener->lookup = lookup;
160 if (local_ip_count > 0) {
161 listener->socks = silc_calloc(local_ip_count, sizeof(*listener->socks));
162 if (!listener->socks)
165 listener->socks = silc_calloc(1, sizeof(*listener->socks));
166 if (!listener->socks)
172 /* Bind to local addresses */
173 for (i = 0; i < local_ip_count; i++) {
174 SILC_LOG_DEBUG(("Binding to local address %s:%d",
175 local_ip_addr ? local_ip_addr[i] : ipany, port));
177 /* Set sockaddr for server */
178 if (!silc_net_set_sockaddr(&server,
179 local_ip_addr ? local_ip_addr[i] : ipany,
183 /* Create the socket */
184 sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
186 silc_set_errno_posix(errno);
187 SILC_LOG_ERROR(("Cannot create socket: %s",
188 silc_errno_string(silc_errno)));
192 /* Set the socket options */
193 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
195 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
200 /* Bind the listener socket */
201 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
203 silc_set_errno_posix(errno);
204 SILC_LOG_ERROR(("Cannot bind socket: %s",
205 silc_errno_string(silc_errno)));
210 /* Specify that we are listenning */
211 rval = listen(sock, 64);
213 silc_set_errno_posix(errno);
214 SILC_LOG_ERROR(("Cannot set socket listenning: %s",
215 silc_errno_string(silc_errno)));
220 /* Set the server socket to non-blocking mode */
221 silc_net_set_socket_nonblock(sock);
223 /* Schedule for incoming connections */
224 silc_schedule_task_add_fd(schedule, sock, silc_net_accept, listener);
226 SILC_LOG_DEBUG(("TCP listener created, fd=%d", sock));
227 listener->socks[i] = sock;
228 listener->socks_count++;
235 silc_net_close_listener(listener);
239 /* Create TCP listener, multiple ports */
242 silc_net_tcp_create_listener2(const char *local_ip_addr, int *ports,
243 SilcUInt32 port_count,
244 SilcBool ignore_port_error,
245 SilcBool lookup, SilcBool require_fqdn,
246 SilcSchedule schedule,
247 SilcNetCallback callback, void *context)
249 SilcNetListener listener = NULL;
252 const char *ipany = "0.0.0.0";
254 SILC_LOG_DEBUG(("Creating TCP listener"));
256 if (!schedule || !callback) {
257 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
261 listener = silc_calloc(1, sizeof(*listener));
264 listener->schedule = schedule;
265 listener->callback = callback;
266 listener->context = context;
267 listener->require_fqdn = require_fqdn;
268 listener->lookup = lookup;
270 if (port_count > 0) {
271 listener->socks = silc_calloc(port_count, sizeof(*listener->socks));
272 if (!listener->socks)
275 listener->socks = silc_calloc(1, sizeof(*listener->socks));
276 if (!listener->socks)
283 for (i = 0; i < port_count; i++) {
284 SILC_LOG_DEBUG(("Binding to local address %s:%d",
285 local_ip_addr ? local_ip_addr : ipany,
286 ports ? ports[i] : 0));
288 /* Set sockaddr for server */
289 if (!silc_net_set_sockaddr(&server,
290 local_ip_addr ? local_ip_addr : ipany,
291 ports ? ports[i] : 0)) {
292 if (ignore_port_error)
297 /* Create the socket */
298 sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
300 if (ignore_port_error)
302 silc_set_errno_posix(errno);
303 SILC_LOG_ERROR(("Cannot create socket: %s",
304 silc_errno_string(silc_errno)));
308 /* Set the socket options */
309 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
312 if (ignore_port_error)
314 SILC_LOG_ERROR(("Cannot set socket options: %s",
315 silc_errno_string(silc_errno)));
319 /* Bind the listener socket */
320 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
323 if (ignore_port_error)
325 silc_set_errno_posix(errno);
326 SILC_LOG_ERROR(("Cannot bind socket: %s",
327 silc_errno_string(silc_errno)));
331 /* Specify that we are listenning */
332 rval = listen(sock, 64);
335 if (ignore_port_error)
337 silc_set_errno_posix(errno);
338 SILC_LOG_ERROR(("Cannot set socket listenning: %s",
339 silc_errno_string(silc_errno)));
343 /* Set the server socket to non-blocking mode */
344 silc_net_set_socket_nonblock(sock);
346 /* Schedule for incoming connections */
347 silc_schedule_task_add_fd(schedule, sock, silc_net_accept, listener);
349 SILC_LOG_DEBUG(("TCP listener created, fd=%d", sock));
350 listener->socks[i] = sock;
351 listener->socks_count++;
354 if (ignore_port_error && !listener->socks_count)
361 silc_net_close_listener(listener);
365 /* Close network listener */
367 void silc_net_close_listener(SilcNetListener listener)
371 SILC_LOG_DEBUG(("Closing network listener"));
373 for (i = 0; i < listener->socks_count; i++) {
374 silc_schedule_task_del_by_fd(listener->schedule, listener->socks[i]);
375 shutdown(listener->socks[i], 2);
376 close(listener->socks[i]);
379 silc_free(listener->socks);
383 /******************************* UDP Stream *********************************/
385 /* Create UDP stream */
388 silc_net_udp_connect(const char *local_ip_addr, int local_port,
389 const char *remote_ip_addr, int remote_port,
390 SilcSchedule schedule)
395 const char *ipany = "0.0.0.0";
397 SILC_LOG_DEBUG(("Creating UDP stream"));
400 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
404 /* Bind to local addresses */
405 SILC_LOG_DEBUG(("Binding to local address %s",
406 local_ip_addr ? local_ip_addr : ipany));
408 /* Set sockaddr for server */
409 if (!silc_net_set_sockaddr(&server, local_ip_addr ? local_ip_addr : ipany,
413 /* Create the socket */
414 sock = socket(server.sin.sin_family, SOCK_DGRAM, 0);
416 silc_set_errno_posix(errno);
417 SILC_LOG_ERROR(("Cannot create socket: %s",
418 silc_errno_string(silc_errno)));
422 /* Set the socket options */
423 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
425 SILC_LOG_ERROR(("Cannot set socket options: %s",
426 silc_errno_string(silc_errno)));
430 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEPORT, 1);
432 SILC_LOG_ERROR(("Cannot set socket options: %s",
433 silc_errno_string(silc_errno)));
436 #endif /* SO_REUSEPORT */
438 /* Bind the listener socket */
439 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
441 silc_set_errno_posix(errno);
442 SILC_LOG_DEBUG(("Cannot bind socket: %s", silc_errno_string(silc_errno)));
446 /* Set to connected state if remote address is provided. */
447 if (remote_ip_addr && remote_port) {
448 if (!silc_net_set_sockaddr(&server, remote_ip_addr, remote_port))
451 rval = connect(sock, &server.sa, SIZEOF_SOCKADDR(server));
453 silc_set_errno_posix(errno);
454 SILC_LOG_DEBUG(("Cannot connect UDP stream: %s",
455 silc_errno_string(silc_errno)));
460 /* Set send and receive buffer size */
462 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_SNDBUF, 765535);
464 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_SNDBUF, 65535);
466 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
470 #endif /* SO_SNDBUF */
472 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_RCVBUF, 765535);
474 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_RCVBUF, 65535);
476 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
480 #endif /* SO_RCVBUF */
482 /* Encapsulate into socket stream */
484 silc_socket_udp_stream_create(sock, local_ip_addr ?
485 silc_net_is_ip6(local_ip_addr) : FALSE,
486 remote_ip_addr ? TRUE : FALSE, schedule);
490 SILC_LOG_DEBUG(("UDP stream created, fd=%d", sock));
499 /* Receive UDP packet */
501 int silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
502 SilcUInt32 remote_ip_addr_size, int *remote_port,
503 unsigned char *ret_data, SilcUInt32 data_size)
505 SilcSocketStream sock = stream;
507 struct sockaddr *from;
510 SILC_LOG_DEBUG(("Reading data from UDP socket %d", sock->sock));
512 if (remote_ip_addr && remote_port) {
515 from = (struct sockaddr *)&s.sin6;
516 flen = sizeof(s.sin6);
517 #endif /* HAVE_IPV6 */
519 from = (struct sockaddr *)&s.sin;
520 flen = sizeof(s.sin);
522 len = recvfrom(sock->sock, ret_data, data_size, 0, from, &flen);
524 len = recv(sock->sock, ret_data, data_size, 0);
527 silc_set_errno_posix(errno);
528 if (errno == EAGAIN || errno == EINTR) {
529 SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
530 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
531 SILC_TASK_READ, FALSE);
534 SILC_LOG_DEBUG(("Cannot read from UDP socket: %d:%s",
535 sock->sock, strerror(errno)));
536 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
540 SILC_LOG_DEBUG(("Read %d bytes", len));
543 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
545 /* Return remote address */
546 if (remote_ip_addr && remote_port) {
549 *remote_port = ntohs(s.sin6.sin6_port);
550 inet_ntop(AF_INET6, &s.sin6.sin6_addr, remote_ip_addr,
551 remote_ip_addr_size);
554 #endif /* HAVE_IPV6 */
556 *remote_port = ntohs(s.sin.sin_port);
557 inet_ntop(AF_INET, &s.sin.sin_addr, remote_ip_addr,
558 remote_ip_addr_size);
561 SILC_LOG_DEBUG(("UDP packet from %s:%d", remote_ip_addr, *remote_port));
567 /* Send UDP packet */
569 int silc_net_udp_send(SilcStream stream,
570 const char *remote_ip_addr, int remote_port,
571 const unsigned char *data, SilcUInt32 data_len)
573 SilcSocketStream sock = stream;
577 SILC_LOG_DEBUG(("Sending data to UDP socket %d", sock->sock));
580 if (!silc_net_set_sockaddr(&remote, remote_ip_addr, remote_port))
584 ret = sendto(sock->sock, data, data_len, 0, &remote.sa,
585 SIZEOF_SOCKADDR(remote));
587 silc_set_errno_posix(errno);
588 if (errno == EAGAIN || errno == EINTR) {
589 SILC_LOG_DEBUG(("Could not send immediately, will do it later"));
590 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
591 SILC_TASK_READ | SILC_TASK_WRITE, FALSE);
594 SILC_LOG_DEBUG(("Cannot send to UDP socket: %s", strerror(errno)));
595 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
599 SILC_LOG_DEBUG(("Sent data %d bytes", ret));
600 if (silc_schedule_get_fd_events(sock->schedule, sock->sock) &
602 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
603 SILC_TASK_READ, FALSE);
608 /******************************* TCP Stream *********************************/
610 /* Asynchronous TCP/IP connecting */
616 SilcFSMEventStruct event;
617 SilcAsyncOperation op;
618 SilcAsyncOperation sop;
623 SilcNetCallback callback;
625 unsigned int port : 24;
626 unsigned int retry : 7;
627 unsigned int aborted : 1;
630 SILC_FSM_STATE(silc_net_connect_st_start);
631 SILC_FSM_STATE(silc_net_connect_st_connected);
632 SILC_FSM_STATE(silc_net_connect_st_stream);
633 SILC_FSM_STATE(silc_net_connect_st_finish);
635 SILC_TASK_CALLBACK(silc_net_connect_wait)
637 SilcNetConnect conn = context;
638 SILC_FSM_EVENT_SIGNAL(&conn->event);
641 SILC_FSM_STATE(silc_net_connect_st_start)
643 SilcNetConnect conn = fsm_context;
645 SilcSockaddr desthost;
646 SilcBool prefer_ipv6 = TRUE;
650 silc_fsm_next(fsm, silc_net_connect_st_finish);
651 return SILC_FSM_CONTINUE;
656 if (!silc_net_gethostbyname(conn->remote, prefer_ipv6,
657 conn->ip_addr, sizeof(conn->ip_addr))) {
658 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
659 "host", conn->remote));
661 /** Network unreachable */
662 conn->status = SILC_ERR_UNREACHABLE;
663 silc_fsm_next(fsm, silc_net_connect_st_finish);
664 return SILC_FSM_CONTINUE;
667 /* Set sockaddr for this connection */
668 if (!silc_net_set_sockaddr(&desthost, conn->ip_addr, conn->port)) {
669 /** Sockaddr failed */
670 silc_fsm_next(fsm, silc_net_connect_st_finish);
671 return SILC_FSM_CONTINUE;
674 /* Create the connection socket */
675 sock = socket(desthost.sin.sin_family, SOCK_STREAM, 0);
677 /* If address is IPv6, then fallback to IPv4 and see whether we can do
678 better with that on socket creation. */
679 if (prefer_ipv6 && silc_net_is_ip6(conn->ip_addr)) {
684 /** Cannot create socket */
685 silc_set_errno_posix(errno);
686 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
687 silc_fsm_next(fsm, silc_net_connect_st_finish);
688 return SILC_FSM_CONTINUE;
691 /* Bind to the local address if provided */
692 if (conn->local_ip) {
695 /* Set sockaddr for local listener, and try to bind it. */
696 if (silc_net_set_sockaddr(&local, conn->local_ip, 0))
697 bind(sock, &local.sa, SIZEOF_SOCKADDR(local));
700 /* Set the socket to non-blocking mode */
701 silc_net_set_socket_nonblock(sock);
703 /* Connect to the host */
704 rval = connect(sock, &desthost.sa, SIZEOF_SOCKADDR(desthost));
706 if (errno != EINPROGRESS) {
707 silc_set_errno_posix(errno);
711 /* Retry using an IPv4 adress, if IPv6 didn't work */
712 if (prefer_ipv6 && silc_net_is_ip6(conn->ip_addr)) {
717 /** Cannot connect to remote host */
718 SILC_LOG_ERROR(("Cannot connect to remote host: %s",
719 silc_errno_string(silc_errno)));
720 silc_fsm_next(fsm, silc_net_connect_st_finish);
721 return SILC_FSM_CONTINUE;
725 /* Set appropriate options */
726 #if defined(TCP_NODELAY)
727 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
729 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
731 SILC_LOG_DEBUG(("Connection operation in progress"));
735 /** Wait for connection */
736 silc_fsm_next(fsm, silc_net_connect_st_connected);
737 silc_fsm_event_init(&conn->event, fsm);
738 silc_schedule_task_add_fd(silc_fsm_get_schedule(fsm), sock,
739 silc_net_connect_wait, conn);
740 silc_schedule_set_listen_fd(silc_fsm_get_schedule(fsm), sock,
741 SILC_TASK_WRITE, FALSE);
742 SILC_FSM_EVENT_WAIT(&conn->event);
743 return SILC_FSM_CONTINUE;
746 static void silc_net_connect_wait_stream(SilcResult status,
747 SilcStream stream, void *context)
749 SilcNetConnect conn = context;
751 conn->status = status;
752 conn->stream = stream;
753 SILC_FSM_CALL_CONTINUE(&conn->fsm);
756 SILC_FSM_STATE(silc_net_connect_st_connected)
758 SilcNetConnect conn = fsm_context;
759 SilcSchedule schedule = silc_fsm_get_schedule(fsm);
760 int opt = EINVAL, optlen = sizeof(opt), ret;
764 silc_schedule_unset_listen_fd(schedule, conn->sock);
765 silc_schedule_task_del_by_fd(schedule, conn->sock);
766 silc_fsm_next(fsm, silc_net_connect_st_finish);
767 return SILC_FSM_CONTINUE;
770 ret = silc_net_get_socket_opt(conn->sock, SOL_SOCKET, SO_ERROR,
773 silc_schedule_unset_listen_fd(schedule, conn->sock);
774 silc_schedule_task_del_by_fd(schedule, conn->sock);
776 if (ret != 0 || opt != 0) {
778 /** Retry connecting */
779 SILC_LOG_DEBUG(("Retry connecting"));
781 silc_net_close_connection(conn->sock);
782 silc_fsm_next(fsm, silc_net_connect_st_start);
783 return SILC_FSM_CONTINUE;
787 silc_set_errno_posix(opt);
788 conn->status = silc_errno;
790 /** Connecting failed */
791 SILC_LOG_DEBUG(("Connecting failed, error %s",
792 silc_errno_string(silc_errno)));
793 silc_fsm_next(fsm, silc_net_connect_st_finish);
794 return SILC_FSM_CONTINUE;
797 SILC_LOG_DEBUG(("TCP connection established"));
799 /** Connection created */
800 silc_fsm_next(fsm, silc_net_connect_st_stream);
801 SILC_FSM_CALL((conn->sop = silc_socket_tcp_stream_create(
802 conn->sock, TRUE, FALSE, schedule,
803 silc_net_connect_wait_stream, conn)));
806 SILC_FSM_STATE(silc_net_connect_st_stream)
808 SilcNetConnect conn = fsm_context;
812 silc_fsm_next(fsm, silc_net_connect_st_finish);
813 return SILC_FSM_CONTINUE;
816 if (conn->status != SILC_OK) {
817 /** Stream creation failed */
818 silc_fsm_next(fsm, silc_net_connect_st_finish);
819 return SILC_FSM_CONTINUE;
822 /** Stream created successfully */
823 SILC_LOG_DEBUG(("Connected successfully, sock %d", conn->sock));
824 conn->status = SILC_OK;
825 silc_fsm_next(fsm, silc_net_connect_st_finish);
826 return SILC_FSM_CONTINUE;
829 SILC_FSM_STATE(silc_net_connect_st_finish)
831 SilcNetConnect conn = fsm_context;
833 /* Deliver error or new stream */
834 if (!conn->aborted) {
835 conn->callback(conn->status, conn->stream, conn->context);
837 silc_async_free(conn->op);
840 return SILC_FSM_FINISH;
843 static void silc_net_connect_abort(SilcAsyncOperation op, void *context)
845 SilcNetConnect conn = context;
846 conn->aborted = TRUE;
848 /* Abort underlaying stream creation too */
850 silc_async_abort(conn->sop, NULL, NULL);
855 static void silc_net_connect_destructor(SilcFSM fsm, void *fsm_context,
856 void *destructor_context)
858 SilcNetConnect conn = fsm_context;
859 silc_free(conn->local_ip);
860 silc_free(conn->remote);
864 /* Create asynchronous TCP/IP connection. */
866 SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
867 const char *remote_ip_addr,
869 SilcSchedule schedule,
870 SilcNetCallback callback,
875 if (!remote_ip_addr || remote_port < 1 || !schedule || !callback)
878 SILC_LOG_DEBUG(("Creating connection to host %s port %d",
879 remote_ip_addr, remote_port));
881 conn = silc_calloc(1, sizeof(*conn));
883 callback(silc_errno, NULL, context);
887 /* Start async operation */
888 conn->op = silc_async_alloc(silc_net_connect_abort, NULL, conn);
891 callback(silc_errno, NULL, context);
896 conn->local_ip = silc_strdup(local_ip_addr);
897 conn->remote = silc_strdup(remote_ip_addr);
899 silc_async_free(conn->op);
900 silc_free(conn->local_ip);
902 callback(silc_errno, NULL, context);
905 conn->port = remote_port;
906 conn->callback = callback;
907 conn->context = context;
909 conn->status = SILC_ERR;
911 silc_fsm_init(&conn->fsm, conn, silc_net_connect_destructor, NULL, schedule);
912 silc_fsm_start(&conn->fsm, silc_net_connect_st_start);
917 /* Closes the connection by closing the socket connection. */
919 void silc_net_close_connection(int sock)
921 SILC_LOG_DEBUG(("Closing sock %d", sock));
925 /* Set's the socket to non-blocking mode. */
927 int silc_net_set_socket_nonblock(SilcSocket sock)
929 int ret = fcntl((int)sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
931 silc_set_errno_posix(errno);
935 /* Converts the IP number string from numbers-and-dots notation to
938 SilcBool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len)
942 if (silc_net_is_ip4(addr)) {
947 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
951 ret = inet_aton(addr, &tmp);
953 silc_set_errno_posix(errno);
957 memcpy(bin, (unsigned char *)&tmp.s_addr, 4);
960 struct addrinfo hints, *ai;
965 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
969 memset(&hints, 0, sizeof(hints));
970 hints.ai_family = AF_INET6;
971 if (getaddrinfo(addr, NULL, &hints, &ai))
975 s = (SilcSockaddr *)ai->ai_addr;
976 memcpy(bin, &s->sin6.sin6_addr, sizeof(s->sin6.sin6_addr));
981 #endif /* HAVE_IPV6 */