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.
23 /************************** Types and definitions ***************************/
26 #define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \
27 sizeof(so.sin6) : sizeof(so.sin))
29 #define SIZEOF_SOCKADDR(so) (sizeof(so.sin))
34 struct sockaddr_in sin;
36 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,
66 addr->sin.sin_family = AF_INET;
67 addr->sin.sin_port = port ? htons(port) : 0;
71 len = sizeof(addr->sin6.sin6_addr);
72 if (!silc_net_addr2bin(ip_addr,
73 (unsigned char *)&addr->sin6.sin6_addr, len))
75 addr->sin6.sin6_family = AF_INET6;
76 addr->sin6.sin6_port = port ? htons(port) : 0;
78 SILC_LOG_ERROR(("Operating System does not support IPv6"));
84 addr->sin.sin_family = AF_INET;
85 addr->sin.sin_addr.s_addr = INADDR_ANY;
87 addr->sin.sin_port = htons(port);
94 /****************************** TCP Listener ********************************/
96 /* Deliver new stream to upper layer */
98 static void silc_net_accept_stream(SilcResult status,
99 SilcStream stream, void *context)
101 SilcNetListener listener = context;
103 if (status != SILC_OK)
106 listener->callback(SILC_OK, stream, listener->context);
109 /* Accept incoming connection and notify upper layer */
111 SILC_TASK_CALLBACK(silc_net_accept)
113 SilcNetListener listener = context;
116 SILC_LOG_DEBUG(("Accepting new connection"));
118 sock = silc_net_accept_connection(fd);
119 if (sock == INVALID_SOCKET)
122 /* Set socket options */
123 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
125 /* Create socket stream */
126 silc_socket_tcp_stream_create(sock, listener->lookup,
127 listener->require_fqdn, schedule,
128 silc_net_accept_stream, listener);
131 /* Create TCP network listener */
134 silc_net_tcp_create_listener(const char **local_ip_addr,
135 SilcUInt32 local_ip_count, int port,
136 SilcBool lookup, SilcBool require_fqdn,
137 SilcSchedule schedule,
138 SilcNetCallback callback, void *context)
140 SilcNetListener listener = NULL;
144 const char *ipany = "0.0.0.0";
146 SILC_LOG_DEBUG(("Creating TCP listener"));
149 schedule = silc_schedule_get_global();
151 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
156 if (port < 0 || !callback) {
157 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
161 listener = silc_calloc(1, sizeof(*listener));
164 listener->schedule = schedule;
165 listener->callback = callback;
166 listener->context = context;
167 listener->require_fqdn = require_fqdn;
168 listener->lookup = lookup;
170 if (local_ip_count > 0) {
171 listener->socks = silc_calloc(local_ip_count, sizeof(*listener->socks));
172 if (!listener->socks)
175 listener->socks = silc_calloc(1, sizeof(*listener->socks));
176 if (!listener->socks)
182 /* Bind to local addresses */
183 for (i = 0; i < local_ip_count; i++) {
184 SILC_LOG_DEBUG(("Binding to local address %s",
185 local_ip_addr ? local_ip_addr[i] : ipany));
187 /* Set sockaddr for server */
188 if (!silc_net_set_sockaddr(&server,
189 local_ip_addr ? local_ip_addr[i] : ipany,
193 /* Create the socket */
194 sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
195 if (sock == INVALID_SOCKET) {
196 silc_set_errno_posix(WSAGetLastError());
197 SILC_LOG_ERROR(("Cannot create socket, error %s",
198 silc_errno_string(silc_errno)));
202 /* Set the socket options */
203 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
204 if (rval == SOCKET_ERROR) {
205 SILC_LOG_ERROR(("Cannot set socket options, error %s",
206 silc_errno_string(silc_errno)));
211 /* Bind the listener socket */
212 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
213 if (rval == SOCKET_ERROR) {
214 silc_set_errno_posix(WSAGetLastError());
215 SILC_LOG_ERROR(("Cannot bind socket, error %s",
216 silc_errno_string(silc_errno)));
221 /* Specify that we are listenning */
222 rval = listen(sock, SOMAXCONN);
223 if (rval == SOCKET_ERROR) {
224 silc_set_errno_posix(WSAGetLastError());
225 SILC_LOG_ERROR(("Cannot set socket listenning, error %s",
226 silc_errno_string(silc_errno)));
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 network, 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;
261 const char *ipany = "0.0.0.0";
263 SILC_LOG_DEBUG(("Creating TCP listener"));
266 schedule = silc_schedule_get_global();
268 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
274 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
278 listener = silc_calloc(1, sizeof(*listener));
281 listener->schedule = schedule;
282 listener->callback = callback;
283 listener->context = context;
284 listener->require_fqdn = require_fqdn;
285 listener->lookup = lookup;
287 if (port_count > 0) {
288 listener->socks = silc_calloc(port_count, sizeof(*listener->socks));
289 if (!listener->socks)
292 listener->socks = silc_calloc(1, sizeof(*listener->socks));
293 if (!listener->socks)
299 /* Bind to local addresses */
300 for (i = 0; i < local_ip_count; i++) {
301 SILC_LOG_DEBUG(("Binding to local address %s:%d",
302 local_ip_addr ? local_ip_addr : ipany,
303 ports ? ports[i] : 0));
305 /* Set sockaddr for server */
306 if (!silc_net_set_sockaddr(&server,
307 local_ip_addr ? local_ip_addr : ipany,
308 ports ? ports[i] : 0)) {
309 if (ignore_port_error)
314 /* Create the socket */
315 sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
316 if (sock == INVALID_SOCKET) {
317 if (ignore_port_error)
319 silc_set_errno_posix(WSAGetLastError());
320 SILC_LOG_ERROR(("Cannot create socket, error %s",
321 silc_errno_string(silc_errno)));
325 /* Set the socket options */
326 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
327 if (rval == SOCKET_ERROR) {
329 if (ignore_port_error)
331 SILC_LOG_ERROR(("Cannot set socket options, error %s",
332 silc_errno_string(silc_errno)));
336 /* Bind the listener socket */
337 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
338 if (rval == SOCKET_ERROR) {
340 if (ignore_port_error)
342 silc_set_errno_posix(WSAGetLastError());
343 SILC_LOG_ERROR(("Cannot bind socket, error %s",
344 silc_errno_string(silc_errno)));
348 /* Specify that we are listenning */
349 rval = listen(sock, SOMAXCONN);
350 if (rval == SOCKET_ERROR) {
352 if (ignore_port_error)
354 silc_set_errno_posix(WSAGetLastError());
355 SILC_LOG_ERROR(("Cannot set socket listenning, error %s",
356 silc_errno_string(silc_errno)));
360 /* Schedule for incoming connections */
361 silc_schedule_task_add_fd(schedule, sock, silc_net_accept, listener);
363 SILC_LOG_DEBUG(("TCP listener created, fd=%d", sock));
364 listener->socks[i] = sock;
365 listener->socks_count++;
368 if (ignore_port_error && !listener->socks_count)
375 silc_net_close_listener(listener);
379 /* Close network listener */
381 void silc_net_close_listener(SilcNetListener listener)
385 SILC_LOG_DEBUG(("Closing network listener"));
390 for (i = 0; i < listener->socks_count; i++) {
391 silc_schedule_task_del_by_fd(listener->schedule, listener->socks[i]);
392 shutdown(listener->socks[i], 2);
393 closesocket(listener->socks[i]);
396 silc_free(listener->socks);
400 /******************************* UDP Stream *********************************/
402 /* Create UDP stream */
405 silc_net_udp_connect(const char *local_ip_addr, int local_port,
406 const char *remote_ip_addr, int remote_port,
407 SilcSchedule schedule)
413 const char *ipany = "0.0.0.0";
415 SILC_LOG_DEBUG(("Creating UDP stream"));
418 schedule = silc_schedule_get_global();
420 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
425 /* Bind to local addresses */
426 SILC_LOG_DEBUG(("Binding to local address %s",
427 local_ip_addr ? local_ip_addr : ipany));
429 /* Set sockaddr for server */
430 if (!silc_net_set_sockaddr(&server, local_ip_addr ? local_ip_addr : ipany,
434 /* Create the socket */
435 sock = socket(server.sin.sin_family, SOCK_DGRAM, 0);
436 if (sock == INVALID_SOCKET) {
437 SILC_LOG_ERROR(("Cannot create socket"));
438 silc_set_errno_posix(WSAGetLastError());
442 /* Set the socket options */
443 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
444 if (rval == SOCKET_ERROR) {
445 SILC_LOG_ERROR(("Cannot set socket options"));
449 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEPORT, 1);
450 if (rval == SOCKET_ERROR) {
451 SILC_LOG_ERROR(("Cannot set socket options"));
454 #endif /* SO_REUSEPORT */
456 /* Bind the listener socket */
457 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
458 if (rval == SOCKET_ERROR) {
459 SILC_LOG_DEBUG(("Cannot bind socket"));
460 silc_set_errno_posix(WSAGetLastError());
464 /* Set to connected state if remote address is provided. */
465 if (remote_ip_addr && remote_port) {
466 if (!silc_net_set_sockaddr(&server, remote_ip_addr, remote_port))
469 rval = connect(sock, &server.sa, SIZEOF_SOCKADDR(server));
470 if (rval == SOCKET_ERROR) {
471 SILC_LOG_DEBUG(("Cannot connect UDP stream"));
472 silc_set_errno_posix(WSAGetLastError());
477 /* Encapsulate into socket stream */
479 silc_socket_udp_stream_create(sock, local_ip_addr ?
480 silc_net_is_ip6(local_ip_addr) : FALSE,
481 remote_ip_addr ? TRUE : FALSE, schedule);
485 SILC_LOG_DEBUG(("UDP stream created, fd=%d", sock));
494 /* Receive UDP packet */
496 int silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
497 SilcUInt32 remote_ip_addr_size, int *remote_port,
498 unsigned char *ret_data, SilcUInt32 data_size)
500 SilcSocketStream sock = stream;
502 struct sockaddr *from;
505 SILC_LOG_DEBUG(("Reading data from UDP socket %d", sock->sock));
507 if (remote_ip_addr && remote_port) {
510 from = (struct sockaddr *)&s.sin6;
511 flen = sizeof(s.sin6);
512 #endif /* HAVE_IPV6 */
514 from = (struct sockaddr *)&s.sin;
515 flen = sizeof(s.sin);
517 len = recvfrom(sock->sock, ret_data, data_size, 0, from, &flen);
519 len = recv(sock->sock, ret_data, data_size, 0);
521 if (len == SOCKET_ERROR) {
522 err = WSAGetLastError();
523 silc_set_errno_posix(err);
524 if (err == WSAEWOULDBLOCK) {
525 SILC_LOG_DEBUG(("Could not read immediately, will do it later"));
526 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
527 SILC_TASK_READ, FALSE);
530 SILC_LOG_DEBUG(("Cannot read from UDP socket: %d: %s", sock->sock,
531 silc_errno_string(silc_errno)));
532 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
536 SILC_LOG_DEBUG(("Read %d bytes", len));
539 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
541 /* Return remote address */
542 if (remote_ip_addr && remote_port) {
545 *remote_port = ntohs(s.sin6.sin6_port);
546 inet_ntop(AF_INET6, &s.sin6.sin6_addr, remote_ip_addr,
547 remote_ip_addr_size);
548 #endif /* HAVE_IPV6 */
550 const char *ip = inet_ntoa(s.sin.sin_addr);
552 silc_snprintf(remote_ip_addr, remote_ip_addr_size, ip);
553 *remote_port = ntohs(s.sin.sin_port);
556 SILC_LOG_DEBUG(("UDP packet from %s:%d", remote_ip_addr, *remote_port));
562 /* Send UDP packet */
564 int silc_net_udp_send(SilcStream stream,
565 const char *remote_ip_addr, int remote_port,
566 const unsigned char *data, SilcUInt32 data_len)
568 SilcSocketStream sock = stream;
572 SILC_LOG_DEBUG(("Sending data to UDP socket %d", sock->sock));
575 if (!silc_net_set_sockaddr(&remote, remote_ip_addr, remote_port))
579 ret = sendto(sock->sock, data, data_len, 0, &remote.sa,
580 SIZEOF_SOCKADDR(remote));
581 if (ret == SOCKET_ERROR) {
582 err = WSAGetLastError();
583 silc_set_errno_posix(err);
584 if (err == WSAEWOULDBLOCK) {
585 SILC_LOG_DEBUG(("Could not send immediately, will do it later"));
586 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
587 SILC_TASK_READ | SILC_TASK_WRITE, FALSE);
590 SILC_LOG_DEBUG(("Cannot send to UDP socket: %s",
591 silc_errno_string(silc_errno)));
592 silc_schedule_unset_listen_fd(sock->schedule, sock->sock);
596 SILC_LOG_DEBUG(("Sent data %d bytes", ret));
597 if (silc_schedule_get_fd_events(sock->schedule, sock->sock) &
599 silc_schedule_set_listen_fd(sock->schedule, sock->sock,
600 SILC_TASK_READ, FALSE);
606 /******************************* TCP Stream *********************************/
612 SilcFSMThreadStruct thread;
613 SilcAsyncOperation op;
614 SilcAsyncOperation sop;
619 SilcNetCallback callback;
621 unsigned int port : 24;
622 unsigned int retry : 7;
623 unsigned int aborted : 1;
626 SILC_FSM_STATE(silc_net_connect_st_start);
627 SILC_FSM_STATE(silc_net_connect_st_stream);
628 SILC_FSM_STATE(silc_net_connect_st_finish);
630 static void silc_net_connect_wait_stream(SilcResult status,
631 SilcStream stream, void *context)
633 SilcNetConnect conn = context;
635 conn->status = status;
636 conn->stream = stream;
637 SILC_FSM_CALL_CONTINUE(&conn->thread);
640 /* Start connecting. Create a real thread where we connect. */
642 SILC_FSM_STATE(silc_net_connect_st_thread)
644 SilcNetConnect conn = fsm_context;
646 /* Connect in real thread so as to not block the application. */
647 silc_fsm_thread_init(&conn->thread, fsm, conn, NULL, NULL, TRUE);
648 silc_fsm_start(&conn->thread, silc_net_connect_st_start);
650 /* Wait for the thread to finish */
651 silc_fsm_next(fsm, silc_net_connect_st_finish);
652 SILC_FSM_THREAD_WAIT(&conn->thread);
655 /* Connecting thread */
657 SILC_FSM_STATE(silc_net_connect_st_start)
659 SilcNetConnect conn = fsm_context;
662 SilcSockaddr desthost;
663 SilcBool prefer_ipv6 = TRUE;
666 return SILC_FSM_FINISH;
670 if (!silc_net_gethostbyname(conn->remote, prefer_ipv6,
671 conn->ip_addr, sizeof(conn->ip_addr))) {
672 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
673 "host, error %d", conn->remote, WSAGetLastError()));
675 /** Network unreachable */
676 conn->status = SILC_ERR_UNREACHABLE;
677 return SILC_FSM_FINISH;
680 /* Set sockaddr for this connection */
681 if (!silc_net_set_sockaddr(&desthost, conn->ip_addr, conn->port))
682 return SILC_FSM_FINISH;
684 /* Create the connection socket */
685 sock = socket(desthost.sin.sin_family, SOCK_STREAM, 0);
686 if (sock == INVALID_SOCKET) {
687 /* If address is IPv6, then fallback to IPv4 and see whether we can do
688 better with that on socket creation. */
689 if (prefer_ipv6 && silc_net_is_ip6(conn->ip_addr)) {
694 /** Cannot create socket */
695 silc_set_errno_posix(err);
696 SILC_LOG_ERROR(("Cannot create socket, error %d",
697 silc_errno_string(silc_errno)));
698 return SILC_FSM_FINISH;
701 /* Bind to the local address if provided */
702 if (conn->local_ip) {
705 /* Set sockaddr for local listener, and try to bind it. */
706 if (silc_net_set_sockaddr(&local, conn->local_ip, 0))
707 bind(sock, &local.sa, SIZEOF_SOCKADDR(local));
710 /* Connect to the host */
711 rval = connect(sock, &desthost.sa, SIZEOF_SOCKADDR(desthost));
712 err = WSAGetLastError();
713 if (rval == SOCKET_ERROR) {
714 if (err != WSAEWOULDBLOCK) {
718 /* Retry using an IPv4 address, if IPv6 didn't work */
719 if (prefer_ipv6 && silc_net_is_ip6(conn->ip_addr)) {
725 silc_set_errno_posix(err);
726 conn->status = silc_errno;
728 SILC_LOG_ERROR(("Cannot connect to remote host: %s",
729 silc_errno_string(silc_errno)));
730 return SILC_FSM_FINISH;
734 /* Set the socket to non-blocking mode */
735 silc_net_set_socket_nonblock(sock);
737 /* Set appropriate options */
738 #if defined(TCP_NODELAY)
739 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
741 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
743 SILC_LOG_DEBUG(("TCP connection established"));
747 /** Connection created */
748 silc_fsm_next(fsm, silc_net_connect_st_stream);
749 SILC_FSM_CALL((conn->sop = silc_socket_tcp_stream_create(
750 conn->sock, TRUE, FALSE,
751 silc_fsm_get_schedule(&conn->fsm),
752 silc_net_connect_wait_stream, conn)));
755 /* TCP socket stream created */
757 SILC_FSM_STATE(silc_net_connect_st_stream)
759 SilcNetConnect conn = fsm_context;
762 return SILC_FSM_FINISH;
764 if (conn->status != SILC_OK) {
765 /** Stream creation failed */
766 return SILC_FSM_FINISH;
769 /** Stream created successfully */
770 SILC_LOG_DEBUG(("Connected successfully, sock %d", conn->sock));
771 conn->status = SILC_OK;
772 return SILC_FSM_FINISH;
775 SILC_FSM_STATE(silc_net_connect_st_finish)
777 SilcNetConnect conn = fsm_context;
779 /* Deliver error or new stream */
780 if (!conn->aborted) {
781 conn->callback(conn->status, conn->stream, conn->context);
783 silc_async_free(conn->op);
786 return SILC_FSM_FINISH;
789 static void silc_net_connect_abort(SilcAsyncOperation op, void *context)
791 SilcNetConnect conn = context;
792 conn->aborted = TRUE;
794 /* Abort underlaying stream creation too */
796 silc_async_abort(conn->sop, NULL, NULL);
801 static void silc_net_connect_destructor(SilcFSM fsm, void *fsm_context,
802 void *destructor_context)
804 SilcNetConnect conn = fsm_context;
805 silc_free(conn->local_ip);
806 silc_free(conn->remote);
810 /* Create asynchronous TCP/IP connection. */
812 SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
813 const char *remote_ip_addr,
815 SilcSchedule schedule,
816 SilcNetCallback callback,
822 schedule = silc_schedule_get_global();
824 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
829 if (!remote_ip_addr || remote_port < 1 || !callback) {
830 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
834 SILC_LOG_DEBUG(("Creating connection to host %s port %d",
835 remote_ip_addr, remote_port));
837 conn = silc_calloc(1, sizeof(*conn));
839 callback(SILC_ERR_OUT_OF_MEMORY, NULL, context);
843 /* Start async operation */
844 conn->op = silc_async_alloc(silc_net_connect_abort, NULL, conn);
847 callback(SILC_ERR_OUT_OF_MEMORY, NULL, context);
852 conn->local_ip = silc_strdup(local_ip_addr);
853 conn->remote = silc_strdup(remote_ip_addr);
855 silc_async_free(conn->op);
856 silc_free(conn->local_ip);
858 callback(SILC_ERR_OUT_OF_MEMORY, NULL, context);
861 conn->port = remote_port;
862 conn->callback = callback;
863 conn->context = context;
865 conn->status = SILC_ERR;
867 silc_fsm_init(&conn->fsm, conn, silc_net_connect_destructor, NULL, schedule);
868 silc_fsm_start(&conn->fsm, silc_net_connect_st_thread);
873 /* Closes the connection by closing the socket connection. */
875 void silc_net_close_connection(int sock)
877 SILC_LOG_DEBUG(("Closing sock %d", sock));
881 /* Converts the IP number string from numbers-and-dots notation to
884 SilcBool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len)
886 if (silc_net_is_ip4(addr)) {
888 int i = 0, c = 0, d = 0, len = strlen(addr);
889 unsigned char ret[4];
891 memset(ret, 0, sizeof(ret));
893 if (addr[i++] == '.') {
901 if (!isdigit((int)addr[i - 1]))
904 d = 10 * d + addr[i - 1] - '0';
912 if (bin_len < sizeof(ret)) {
913 silc_set_errno(SILC_ERR_OVERFLOW);
917 memcpy(bin, ret, sizeof(ret));
924 struct addrinfo hints, *ai;
929 silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
933 memset(&hints, 0, sizeof(hints));
934 hints.ai_family = AF_INET6;
935 if (getaddrinfo(addr, NULL, &hints, &ai))
939 s = (SilcSockaddr *)ai->ai_addr;
940 memcpy(bin, &s->sin6.sin6_addr, sizeof(s->sin6.sin6_addr));
947 #endif /* HAVE_IPV6 */
951 /* Set socket to non-blocking mode. */
953 int silc_net_set_socket_nonblock(SilcSocket sock)
955 unsigned long on = 1;
956 return ioctlsocket(sock, FIONBIO, &on);