5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2005 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.
21 #include "silcincludes.h"
25 #define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \
26 sizeof(so.sin6) : sizeof(so.sin))
28 #define SIZEOF_SOCKADDR(so) (sizeof(so.sin))
33 struct sockaddr_in sin;
35 struct sockaddr_in6 sin6;
39 static bool silc_net_set_sockaddr(SilcSockaddr *addr, const char *ip_addr,
44 memset(addr, 0, sizeof(*addr));
46 /* Check for IPv4 and IPv6 addresses */
48 if (!silc_net_is_ip(ip_addr)) {
49 SILC_LOG_ERROR(("%s is not IP address", ip_addr));
53 if (silc_net_is_ip4(ip_addr)) {
55 len = sizeof(addr->sin.sin_addr);
56 silc_net_addr2bin(ip_addr,
57 (unsigned char *)&addr->sin.sin_addr.s_addr, len);
58 addr->sin.sin_family = AF_INET;
59 addr->sin.sin_port = port ? htons(port) : 0;
63 len = sizeof(addr->sin6.sin6_addr);
64 silc_net_addr2bin(ip_addr,
65 (unsigned char *)&addr->sin6.sin6_addr, len);
66 addr->sin6.sin6_family = AF_INET6;
67 addr->sin6.sin6_port = port ? htons(port) : 0;
69 SILC_LOG_ERROR(("IPv6 support is not compiled in"));
75 addr->sin.sin_family = AF_INET;
76 addr->sin.sin_addr.s_addr = INADDR_ANY;
78 addr->sin.sin_port = htons(port);
84 /* This function creates server or daemon or listener or what ever. This
85 does not fork a new process, it must be done by the caller if caller
86 wants to create a child process. This is used by the SILC server.
87 If argument `ip_addr' is NULL `any' address will be used. Returns
88 the created socket or -1 on error. */
90 int silc_net_create_server(int port, const char *ip_addr)
95 SILC_LOG_DEBUG(("Creating a new server listener"));
97 /* Set sockaddr for server */
98 if (!silc_net_set_sockaddr(&server, ip_addr, port))
101 /* Create the socket */
102 sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
104 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
108 /* Set the socket options */
109 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
111 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
115 /* Bind the server socket */
116 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
118 SILC_LOG_DEBUG(("Cannot bind socket: %s", strerror(errno)));
122 /* Specify that we are listenning */
123 rval = listen(sock, 5);
125 SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
129 /* Set the server socket to non-blocking mode */
130 silc_net_set_socket_nonblock(sock);
132 SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
137 /* Closes the server by closing the socket connection. */
139 void silc_net_close_server(int sock)
144 SILC_LOG_DEBUG(("Server socket closed"));
147 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
148 socket or -1 on error. This blocks the process while trying to create
151 int silc_net_create_connection(const char *local_ip, int port,
156 SilcSockaddr desthost;
157 bool prefer_ipv6 = TRUE;
159 SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
163 if (!silc_net_gethostbyname(host, prefer_ipv6, ip_addr, sizeof(ip_addr))) {
164 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
165 "IP address", host));
169 /* Set sockaddr for this connection */
170 if (!silc_net_set_sockaddr(&desthost, ip_addr, port))
173 /* Create the connection socket */
174 sock = socket(desthost.sin.sin_family, SOCK_STREAM, 0);
176 /* If address is IPv6, then fallback to IPv4 and see whether we can do
177 better with that on socket creation. */
178 if (prefer_ipv6 && silc_net_is_ip6(ip_addr)) {
183 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
187 /* Bind to the local address if provided */
191 /* Set sockaddr for local listener, and try to bind it. */
192 if (silc_net_set_sockaddr(&local, local_ip, 0))
193 bind(sock, &local.sa, SIZEOF_SOCKADDR(local));
196 /* Connect to the host */
197 rval = connect(sock, &desthost.sa, SIZEOF_SOCKADDR(desthost));
199 /* retry using an IPv4 adress, if IPv6 didn't work */
200 if (prefer_ipv6 && silc_net_is_ip6(ip_addr)) {
207 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
213 /* Set appropriate options */
214 #if defined(TCP_NODELAY)
215 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
217 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
219 SILC_LOG_DEBUG(("Connection created"));
224 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
225 socket or -1 on error. This creates non-blocking socket hence the
226 connection returns directly. To get the result of the connect() one
227 must select() the socket and read the result after it's ready. */
229 int silc_net_create_connection_async(const char *local_ip, int port,
234 SilcSockaddr desthost;
235 bool prefer_ipv6 = TRUE;
237 SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
242 if (!silc_net_gethostbyname(host, prefer_ipv6, ip_addr, sizeof(ip_addr))) {
243 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
244 "IP address", host));
248 /* Set sockaddr for this connection */
249 if (!silc_net_set_sockaddr(&desthost, ip_addr, port))
252 /* Create the connection socket */
253 sock = socket(desthost.sin.sin_family, SOCK_STREAM, 0);
255 /* If address is IPv6, then fallback to IPv4 and see whether we can do
256 better with that on socket creation. */
257 if (prefer_ipv6 && silc_net_is_ip6(ip_addr)) {
262 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
266 /* Bind to the local address if provided */
270 /* Set sockaddr for local listener, and try to bind it. */
271 if (silc_net_set_sockaddr(&local, local_ip, 0))
272 bind(sock, &local.sa, SIZEOF_SOCKADDR(local));
275 /* Set the socket to non-blocking mode */
276 silc_net_set_socket_nonblock(sock);
278 /* Connect to the host */
279 rval = connect(sock, &desthost.sa, SIZEOF_SOCKADDR(desthost));
281 if (errno != EINPROGRESS) {
282 /* retry using an IPv4 adress, if IPv6 didn't work */
283 if (prefer_ipv6 && silc_net_is_ip6(ip_addr)) {
291 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
298 /* Set appropriate options */
299 #if defined(TCP_NODELAY)
300 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
302 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
304 SILC_LOG_DEBUG(("Connection operation in progress"));
309 /* Closes the connection by closing the socket connection. */
311 void silc_net_close_connection(int sock)
316 /* Set's the socket to non-blocking mode. */
318 int silc_net_set_socket_nonblock(int sock)
320 return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
323 /* Converts the IP number string from numbers-and-dots notation to
326 bool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len)
330 if (silc_net_is_ip4(addr)) {
333 ret = inet_aton(addr, &tmp);
337 memcpy(bin, (unsigned char *)&tmp.s_addr, 4);
340 struct addrinfo hints, *ai;
347 memset(&hints, 0, sizeof(hints));
348 hints.ai_family = AF_INET6;
349 if (getaddrinfo(addr, NULL, &hints, &ai))
353 s = (SilcSockaddr *)ai->ai_addr;
354 memcpy(bin, &s->sin6.sin6_addr, sizeof(s->sin6.sin6_addr));
359 #endif /* HAVE_IPV6 */