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"));
147 schedule = silc_schedule_get_global();
149 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
154 if (port < 0 || !callback) {
155 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
159 listener = silc_calloc(1, sizeof(*listener));
162 listener->schedule = schedule;
163 listener->callback = callback;
164 listener->context = context;
165 listener->require_fqdn = require_fqdn;
166 listener->lookup = lookup;
168 if (local_ip_count > 0) {
169 listener->socks = silc_calloc(local_ip_count, sizeof(*listener->socks));
170 if (!listener->socks)
173 listener->socks = silc_calloc(1, sizeof(*listener->socks));
174 if (!listener->socks)
180 /* Bind to local addresses */
181 for (i = 0; i < local_ip_count; i++) {
182 SILC_LOG_DEBUG(("Binding to local address %s:%d",
183 local_ip_addr ? local_ip_addr[i] : ipany, port));
185 /* Set sockaddr for server */
186 if (!silc_net_set_sockaddr(&server,
187 local_ip_addr ? local_ip_addr[i] : ipany,
191 /* Create the socket */
192 sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
194 silc_set_errno_posix(errno);
195 SILC_LOG_ERROR(("Cannot create socket: %s",
196 silc_errno_string(silc_errno)));
200 /* Set the socket options */
201 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
203 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
208 /* Bind the listener socket */
209 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
211 silc_set_errno_posix(errno);
212 SILC_LOG_ERROR(("Cannot bind socket: %s",
213 silc_errno_string(silc_errno)));
218 /* Specify that we are listenning */
219 rval = listen(sock, 64);
221 silc_set_errno_posix(errno);
222 SILC_LOG_ERROR(("Cannot set socket listenning: %s",
223 silc_errno_string(silc_errno)));
228 /* Set the server socket to non-blocking mode */
229 silc_net_set_socket_nonblock(sock);
231 /* Schedule for incoming connections */
232 silc_schedule_task_add_fd(schedule, sock, silc_net_accept, listener);
234 SILC_LOG_DEBUG(("TCP listener created, fd=%d", sock));
235 listener->socks[i] = sock;
236 listener->socks_count++;
243 silc_net_close_listener(listener);
247 /* Create TCP listener, multiple ports */
250 silc_net_tcp_create_listener2(const char *local_ip_addr, int *ports,
251 SilcUInt32 port_count,
252 SilcBool ignore_port_error,
253 SilcBool lookup, SilcBool require_fqdn,
254 SilcSchedule schedule,
255 SilcNetCallback callback, void *context)
257 SilcNetListener listener = NULL;
260 const char *ipany = "0.0.0.0";
262 SILC_LOG_DEBUG(("Creating TCP listener"));
265 schedule = silc_schedule_get_global();
267 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
273 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
277 listener = silc_calloc(1, sizeof(*listener));
280 listener->schedule = schedule;
281 listener->callback = callback;
282 listener->context = context;
283 listener->require_fqdn = require_fqdn;
284 listener->lookup = lookup;
286 if (port_count > 0) {
287 listener->socks = silc_calloc(port_count, sizeof(*listener->socks));
288 if (!listener->socks)
291 listener->socks = silc_calloc(1, sizeof(*listener->socks));
292 if (!listener->socks)
299 for (i = 0; i < port_count; i++) {
300 SILC_LOG_DEBUG(("Binding to local address %s:%d",
301 local_ip_addr ? local_ip_addr : ipany,
302 ports ? ports[i] : 0));
304 /* Set sockaddr for server */
305 if (!silc_net_set_sockaddr(&server,
306 local_ip_addr ? local_ip_addr : ipany,
307 ports ? ports[i] : 0)) {
308 if (ignore_port_error)
313 /* Create the socket */
314 sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
316 if (ignore_port_error)
318 silc_set_errno_posix(errno);
319 SILC_LOG_ERROR(("Cannot create socket: %s",
320 silc_errno_string(silc_errno)));
324 /* Set the socket options */
325 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
328 if (ignore_port_error)
330 SILC_LOG_ERROR(("Cannot set socket options: %s",
331 silc_errno_string(silc_errno)));
335 /* Bind the listener socket */
336 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
339 if (ignore_port_error)
341 silc_set_errno_posix(errno);
342 SILC_LOG_ERROR(("Cannot bind socket: %s",
343 silc_errno_string(silc_errno)));
347 /* Specify that we are listenning */
348 rval = listen(sock, 64);
351 if (ignore_port_error)
353 silc_set_errno_posix(errno);
354 SILC_LOG_ERROR(("Cannot set socket listenning: %s",
355 silc_errno_string(silc_errno)));
359 /* Set the server socket to non-blocking mode */
360 silc_net_set_socket_nonblock(sock);
362 /* Schedule for incoming connections */
363 silc_schedule_task_add_fd(schedule, sock, silc_net_accept, listener);
365 SILC_LOG_DEBUG(("TCP listener created, fd=%d", sock));
366 listener->socks[i] = sock;
367 listener->socks_count++;
370 if (ignore_port_error && !listener->socks_count)
377 silc_net_close_listener(listener);
381 /* Close network listener */
383 void silc_net_close_listener(SilcNetListener listener)
387 SILC_LOG_DEBUG(("Closing network listener"));
389 for (i = 0; i < listener->socks_count; i++) {
390 silc_schedule_task_del_by_fd(listener->schedule, listener->socks[i]);
391 shutdown(listener->socks[i], 2);
392 close(listener->socks[i]);
395 silc_free(listener->socks);
399 /******************************* UDP Stream *********************************/
401 /* Create UDP stream */
404 silc_net_udp_connect(const char *local_ip_addr, int local_port,
405 const char *remote_ip_addr, int remote_port,
406 SilcSchedule schedule)
411 const char *ipany = "0.0.0.0";
413 SILC_LOG_DEBUG(("Creating UDP stream"));
416 schedule = silc_schedule_get_global();
418 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
423 /* Bind to local addresses */
424 SILC_LOG_DEBUG(("Binding to local address %s",
425 local_ip_addr ? local_ip_addr : ipany));
427 /* Set sockaddr for server */
428 if (!silc_net_set_sockaddr(&server, local_ip_addr ? local_ip_addr : ipany,
432 /* Create the socket */
433 sock = socket(server.sin.sin_family, SOCK_DGRAM, 0);
435 silc_set_errno_posix(errno);
436 SILC_LOG_ERROR(("Cannot create socket: %s",
437 silc_errno_string(silc_errno)));
441 /* Set the socket options */
442 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
444 SILC_LOG_ERROR(("Cannot set socket options: %s",
445 silc_errno_string(silc_errno)));
449 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEPORT, 1);
451 SILC_LOG_ERROR(("Cannot set socket options: %s",
452 silc_errno_string(silc_errno)));
455 #endif /* SO_REUSEPORT */
457 /* Bind the listener socket */
458 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
460 silc_set_errno_posix(errno);
461 SILC_LOG_DEBUG(("Cannot bind socket: %s", silc_errno_string(silc_errno)));
465 /* Set to connected state if remote address is provided. */
466 if (remote_ip_addr && remote_port) {
467 if (!silc_net_set_sockaddr(&server, remote_ip_addr, remote_port))
470 rval = connect(sock, &server.sa, SIZEOF_SOCKADDR(server));
472 silc_set_errno_posix(errno);
473 SILC_LOG_DEBUG(("Cannot connect UDP stream: %s",
474 silc_errno_string(silc_errno)));
479 /* Set send and receive buffer size */
481 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_SNDBUF, 765535);
483 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_SNDBUF, 65535);
485 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
489 #endif /* SO_SNDBUF */
491 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_RCVBUF, 765535);
493 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_RCVBUF, 65535);
495 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
499 #endif /* SO_RCVBUF */
501 /* Encapsulate into socket stream */
503 silc_socket_udp_stream_create(sock, local_ip_addr ?
504 silc_net_is_ip6(local_ip_addr) : FALSE,
505 remote_ip_addr ? TRUE : FALSE, schedule);
509 SILC_LOG_DEBUG(("UDP stream created, fd=%d", sock));
518 /* Receive UDP packet */
520 int silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
521 SilcUInt32 remote_ip_addr_size, int *remote_port,
522 unsigned char *ret_data, SilcUInt32 data_size)
524 SilcSocketStream sock = stream;
526 struct sockaddr *from;
529 SILC_LOG_DEBUG(("Reading data from UDP socket %d", sock->sock));
531 if (remote_ip_addr && remote_port) {
534 from = (struct sockaddr *)&s.sin6;
535 flen = sizeof(s.sin6);
536 #endif /* HAVE_IPV6 */
538 from = (struct sockaddr *)&s.sin;
539 flen = sizeof(s.sin);
541 len = recvfrom(sock->sock, ret_data, data_size, 0, from, &flen);
543 len = recv(sock->sock, ret_data, data_size, 0);
546 silc_set_errno_posix(errno);
547 if (errno == EAGAIN || errno == EINTR) {
548 SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
549 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
550 SILC_TASK_READ, FALSE);
553 SILC_LOG_DEBUG(("Cannot read from UDP socket: %d:%s",
554 sock->sock, strerror(errno)));
555 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
559 SILC_LOG_DEBUG(("Read %d bytes", len));
562 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
564 /* Return remote address */
565 if (remote_ip_addr && remote_port) {
568 *remote_port = ntohs(s.sin6.sin6_port);
569 inet_ntop(AF_INET6, &s.sin6.sin6_addr, remote_ip_addr,
570 remote_ip_addr_size);
573 #endif /* HAVE_IPV6 */
575 *remote_port = ntohs(s.sin.sin_port);
576 inet_ntop(AF_INET, &s.sin.sin_addr, remote_ip_addr,
577 remote_ip_addr_size);
580 SILC_LOG_DEBUG(("UDP packet from %s:%d", remote_ip_addr, *remote_port));
586 /* Send UDP packet */
588 int silc_net_udp_send(SilcStream stream,
589 const char *remote_ip_addr, int remote_port,
590 const unsigned char *data, SilcUInt32 data_len)
592 SilcSocketStream sock = stream;
596 SILC_LOG_DEBUG(("Sending data to UDP socket %d", sock->sock));
599 if (!silc_net_set_sockaddr(&remote, remote_ip_addr, remote_port))
603 ret = sendto(sock->sock, data, data_len, 0, &remote.sa,
604 SIZEOF_SOCKADDR(remote));
606 silc_set_errno_posix(errno);
607 if (errno == EAGAIN || errno == EINTR) {
608 SILC_LOG_DEBUG(("Could not send immediately, will do it later"));
609 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
610 SILC_TASK_READ | SILC_TASK_WRITE, FALSE);
613 SILC_LOG_DEBUG(("Cannot send to UDP socket: %s", strerror(errno)));
614 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
618 SILC_LOG_DEBUG(("Sent data %d bytes", ret));
619 if (silc_schedule_get_fd_events(sock->schedule, sock->sock) &
621 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
622 SILC_TASK_READ, FALSE);
627 /******************************* TCP Stream *********************************/
629 /* Asynchronous TCP/IP connecting */
635 SilcFSMEventStruct event;
636 SilcAsyncOperation op;
637 SilcAsyncOperation sop;
642 SilcNetCallback callback;
644 unsigned int port : 24;
645 unsigned int retry : 7;
646 unsigned int aborted : 1;
649 SILC_FSM_STATE(silc_net_connect_st_start);
650 SILC_FSM_STATE(silc_net_connect_st_connected);
651 SILC_FSM_STATE(silc_net_connect_st_stream);
652 SILC_FSM_STATE(silc_net_connect_st_finish);
654 SILC_TASK_CALLBACK(silc_net_connect_wait)
656 SilcNetConnect conn = context;
657 SILC_FSM_EVENT_SIGNAL(&conn->event);
660 SILC_FSM_STATE(silc_net_connect_st_start)
662 SilcNetConnect conn = fsm_context;
664 SilcSockaddr desthost;
665 SilcBool prefer_ipv6 = TRUE;
669 silc_fsm_next(fsm, silc_net_connect_st_finish);
670 return SILC_FSM_CONTINUE;
675 if (!silc_net_gethostbyname(conn->remote, prefer_ipv6,
676 conn->ip_addr, sizeof(conn->ip_addr))) {
677 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
678 "host", conn->remote));
680 /** Network unreachable */
681 conn->status = SILC_ERR_UNREACHABLE;
682 silc_fsm_next(fsm, silc_net_connect_st_finish);
683 return SILC_FSM_CONTINUE;
686 /* Set sockaddr for this connection */
687 if (!silc_net_set_sockaddr(&desthost, conn->ip_addr, conn->port)) {
688 /** Sockaddr failed */
689 silc_fsm_next(fsm, silc_net_connect_st_finish);
690 return SILC_FSM_CONTINUE;
693 /* Create the connection socket */
694 sock = socket(desthost.sin.sin_family, SOCK_STREAM, 0);
696 /* If address is IPv6, then fallback to IPv4 and see whether we can do
697 better with that on socket creation. */
698 if (prefer_ipv6 && silc_net_is_ip6(conn->ip_addr)) {
703 /** Cannot create socket */
704 silc_set_errno_posix(errno);
705 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
706 silc_fsm_next(fsm, silc_net_connect_st_finish);
707 return SILC_FSM_CONTINUE;
710 /* Bind to the local address if provided */
711 if (conn->local_ip) {
714 /* Set sockaddr for local listener, and try to bind it. */
715 if (silc_net_set_sockaddr(&local, conn->local_ip, 0))
716 bind(sock, &local.sa, SIZEOF_SOCKADDR(local));
719 /* Set the socket to non-blocking mode */
720 silc_net_set_socket_nonblock(sock);
722 /* Connect to the host */
723 rval = connect(sock, &desthost.sa, SIZEOF_SOCKADDR(desthost));
725 if (errno != EINPROGRESS) {
726 silc_set_errno_posix(errno);
730 /* Retry using an IPv4 adress, if IPv6 didn't work */
731 if (prefer_ipv6 && silc_net_is_ip6(conn->ip_addr)) {
736 /** Cannot connect to remote host */
737 SILC_LOG_ERROR(("Cannot connect to remote host: %s",
738 silc_errno_string(silc_errno)));
739 silc_fsm_next(fsm, silc_net_connect_st_finish);
740 return SILC_FSM_CONTINUE;
744 /* Set appropriate options */
745 #if defined(TCP_NODELAY)
746 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
748 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
750 SILC_LOG_DEBUG(("Connection operation in progress"));
754 /** Wait for connection */
755 silc_fsm_next(fsm, silc_net_connect_st_connected);
756 silc_fsm_event_init(&conn->event, fsm);
757 silc_schedule_task_add_fd(silc_fsm_get_schedule(fsm), sock,
758 silc_net_connect_wait, conn);
759 silc_schedule_set_listen_fd(silc_fsm_get_schedule(fsm), sock,
760 SILC_TASK_WRITE, FALSE);
761 SILC_FSM_EVENT_WAIT(&conn->event);
762 return SILC_FSM_CONTINUE;
765 static void silc_net_connect_wait_stream(SilcResult status,
766 SilcStream stream, void *context)
768 SilcNetConnect conn = context;
770 conn->status = status;
771 conn->stream = stream;
772 SILC_FSM_CALL_CONTINUE(&conn->fsm);
775 SILC_FSM_STATE(silc_net_connect_st_connected)
777 SilcNetConnect conn = fsm_context;
778 SilcSchedule schedule = silc_fsm_get_schedule(fsm);
779 int opt = EINVAL, optlen = sizeof(opt), ret;
783 silc_schedule_unset_listen_fd(schedule, conn->sock);
784 silc_schedule_task_del_by_fd(schedule, conn->sock);
785 silc_fsm_next(fsm, silc_net_connect_st_finish);
786 return SILC_FSM_CONTINUE;
789 ret = silc_net_get_socket_opt(conn->sock, SOL_SOCKET, SO_ERROR,
792 silc_schedule_unset_listen_fd(schedule, conn->sock);
793 silc_schedule_task_del_by_fd(schedule, conn->sock);
795 if (ret != 0 || opt != 0) {
797 /** Retry connecting */
798 SILC_LOG_DEBUG(("Retry connecting"));
800 silc_net_close_connection(conn->sock);
801 silc_fsm_next(fsm, silc_net_connect_st_start);
802 return SILC_FSM_CONTINUE;
806 silc_set_errno_posix(opt);
807 conn->status = silc_errno;
809 /** Connecting failed */
810 SILC_LOG_DEBUG(("Connecting failed, error %s",
811 silc_errno_string(silc_errno)));
812 silc_fsm_next(fsm, silc_net_connect_st_finish);
813 return SILC_FSM_CONTINUE;
816 SILC_LOG_DEBUG(("TCP connection established"));
818 /** Connection created */
819 silc_fsm_next(fsm, silc_net_connect_st_stream);
820 SILC_FSM_CALL((conn->sop = silc_socket_tcp_stream_create(
821 conn->sock, TRUE, FALSE, schedule,
822 silc_net_connect_wait_stream, conn)));
825 SILC_FSM_STATE(silc_net_connect_st_stream)
827 SilcNetConnect conn = fsm_context;
831 silc_fsm_next(fsm, silc_net_connect_st_finish);
832 return SILC_FSM_CONTINUE;
835 if (conn->status != SILC_OK) {
836 /** Stream creation failed */
837 silc_fsm_next(fsm, silc_net_connect_st_finish);
838 return SILC_FSM_CONTINUE;
841 /** Stream created successfully */
842 SILC_LOG_DEBUG(("Connected successfully, sock %d", conn->sock));
843 conn->status = SILC_OK;
844 silc_fsm_next(fsm, silc_net_connect_st_finish);
845 return SILC_FSM_CONTINUE;
848 SILC_FSM_STATE(silc_net_connect_st_finish)
850 SilcNetConnect conn = fsm_context;
852 /* Deliver error or new stream */
853 if (!conn->aborted) {
854 conn->callback(conn->status, conn->stream, conn->context);
856 silc_async_free(conn->op);
859 return SILC_FSM_FINISH;
862 static void silc_net_connect_abort(SilcAsyncOperation op, void *context)
864 SilcNetConnect conn = context;
865 conn->aborted = TRUE;
867 /* Abort underlaying stream creation too */
869 silc_async_abort(conn->sop, NULL, NULL);
874 static void silc_net_connect_destructor(SilcFSM fsm, void *fsm_context,
875 void *destructor_context)
877 SilcNetConnect conn = fsm_context;
878 silc_free(conn->local_ip);
879 silc_free(conn->remote);
883 /* Create asynchronous TCP/IP connection. */
885 SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
886 const char *remote_ip_addr,
888 SilcSchedule schedule,
889 SilcNetCallback callback,
895 schedule = silc_schedule_get_global();
897 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
902 if (!remote_ip_addr || remote_port < 1 || !callback) {
903 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
907 SILC_LOG_DEBUG(("Creating connection to host %s port %d",
908 remote_ip_addr, remote_port));
910 conn = silc_calloc(1, sizeof(*conn));
912 callback(silc_errno, NULL, context);
916 /* Start async operation */
917 conn->op = silc_async_alloc(silc_net_connect_abort, NULL, conn);
920 callback(silc_errno, NULL, context);
925 conn->local_ip = silc_strdup(local_ip_addr);
926 conn->remote = silc_strdup(remote_ip_addr);
928 silc_async_free(conn->op);
929 silc_free(conn->local_ip);
931 callback(silc_errno, NULL, context);
934 conn->port = remote_port;
935 conn->callback = callback;
936 conn->context = context;
938 conn->status = SILC_ERR;
940 silc_fsm_init(&conn->fsm, conn, silc_net_connect_destructor, NULL, schedule);
941 silc_fsm_start(&conn->fsm, silc_net_connect_st_start);
946 /* Closes the connection by closing the socket connection. */
948 void silc_net_close_connection(int sock)
950 SILC_LOG_DEBUG(("Closing sock %d", sock));
954 /* Set's the socket to non-blocking mode. */
956 int silc_net_set_socket_nonblock(SilcSocket sock)
958 int ret = fcntl((int)sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
960 silc_set_errno_posix(errno);
964 /* Converts the IP number string from numbers-and-dots notation to
967 SilcBool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len)
971 if (silc_net_is_ip4(addr)) {
976 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
980 ret = inet_aton(addr, &tmp);
982 silc_set_errno_posix(errno);
986 memcpy(bin, (unsigned char *)&tmp.s_addr, 4);
989 struct addrinfo hints, *ai;
994 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
998 memset(&hints, 0, sizeof(hints));
999 hints.ai_family = AF_INET6;
1000 if (getaddrinfo(addr, NULL, &hints, &ai))
1004 s = (SilcSockaddr *)ai->ai_addr;
1005 memcpy(bin, &s->sin6.sin6_addr, sizeof(s->sin6.sin6_addr));
1010 #endif /* HAVE_IPV6 */