5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2001 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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
22 #include "silcincludes.h"
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;
40 static bool silc_net_set_sockaddr(SilcSockaddr *addr, const char *ip_addr,
45 memset(addr, 0, sizeof(*addr));
47 /* Check for IPv4 and IPv6 addresses */
49 if (!silc_net_is_ip(ip_addr)) {
50 SILC_LOG_ERROR(("%s is not IP address", ip_addr));
54 if (silc_net_is_ip4(ip_addr)) {
56 len = sizeof(addr->sin.sin_addr);
57 silc_net_addr2bin(ip_addr,
58 (unsigned char *)&addr->sin.sin_addr.s_addr, len);
59 addr->sin.sin_family = AF_INET;
60 addr->sin.sin_port = port ? htons(port) : 0;
64 len = sizeof(addr->sin6.sin6_addr);
65 silc_net_addr2bin(ip_addr,
66 (unsigned char *)&addr->sin6.sin6_addr, len);
67 addr->sin6.sin6_family = AF_INET6;
68 addr->sin6.sin6_port = port ? htons(port) : 0;
70 SILC_LOG_ERROR(("IPv6 support is not compiled in"));
76 addr->sin.sin_family = AF_INET;
77 addr->sin.sin_addr.s_addr = INADDR_ANY;
79 addr->sin.sin_port = htons(port);
85 /* This function creates server or daemon or listener or what ever. This
86 does not fork a new process, it must be done by the caller if caller
87 wants to create a child process. This is used by the SILC server.
88 If argument `ip_addr' is NULL `any' address will be used. Returns
89 the created socket or -1 on error. */
91 int silc_net_create_server(int port, const char *ip_addr)
96 SILC_LOG_DEBUG(("Creating a new server listener"));
98 /* Set sockaddr for server */
99 if (!silc_net_set_sockaddr(&server, ip_addr, port))
102 /* Create the socket */
103 sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
105 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
109 /* Set the socket options */
110 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
112 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
116 /* Bind the server socket */
117 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
119 SILC_LOG_DEBUG(("Cannot bind socket: %s", strerror(errno)));
123 /* Specify that we are listenning */
124 rval = listen(sock, 5);
126 SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
130 /* Set the server socket to non-blocking mode */
131 silc_net_set_socket_nonblock(sock);
133 SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
138 /* Closes the server by closing the socket connection. */
140 void silc_net_close_server(int sock)
145 SILC_LOG_DEBUG(("Server socket closed"));
148 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
149 socket or -1 on error. This blocks the process while trying to create
152 int silc_net_create_connection(const char *local_ip, int port,
157 SilcSockaddr desthost;
158 bool prefer_ipv6 = TRUE;
160 SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
164 if (!silc_net_gethostbyname(host, prefer_ipv6, ip_addr, sizeof(ip_addr))) {
165 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
166 "IP address", host));
170 /* Set sockaddr for this connection */
171 if (!silc_net_set_sockaddr(&desthost, ip_addr, port))
174 /* Create the connection socket */
175 sock = socket(desthost.sin.sin_family, SOCK_STREAM, 0);
177 /* If address is IPv6, then fallback to IPv4 and see whether we can do
178 better with that on socket creation. */
179 if (prefer_ipv6 && silc_net_is_ip6(ip_addr)) {
184 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
188 /* Bind to the local address if provided */
192 /* Set sockaddr for local listener, and try to bind it. */
193 if (silc_net_set_sockaddr(&local, local_ip, 0))
194 bind(sock, &local.sa, SIZEOF_SOCKADDR(local));
197 /* Connect to the host */
198 rval = connect(sock, &desthost.sa, SIZEOF_SOCKADDR(desthost));
200 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
206 /* Set appropriate options */
207 #if defined(TCP_NODELAY)
208 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
210 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
212 SILC_LOG_DEBUG(("Connection created"));
217 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
218 socket or -1 on error. This creates non-blocking socket hence the
219 connection returns directly. To get the result of the connect() one
220 must select() the socket and read the result after it's ready. */
222 int silc_net_create_connection_async(const char *local_ip, int port,
227 SilcSockaddr desthost;
228 bool prefer_ipv6 = TRUE;
230 SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
235 if (!silc_net_gethostbyname(host, prefer_ipv6, ip_addr, sizeof(ip_addr))) {
236 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
237 "IP address", host));
241 /* Set sockaddr for this connection */
242 if (!silc_net_set_sockaddr(&desthost, ip_addr, port))
245 /* Create the connection socket */
246 sock = socket(desthost.sin.sin_family, SOCK_STREAM, 0);
248 /* If address is IPv6, then fallback to IPv4 and see whether we can do
249 better with that on socket creation. */
250 if (prefer_ipv6 && silc_net_is_ip6(ip_addr)) {
255 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
259 /* Bind to the local address if provided */
263 /* Set sockaddr for local listener, and try to bind it. */
264 if (silc_net_set_sockaddr(&local, local_ip, 0))
265 bind(sock, &local.sa, SIZEOF_SOCKADDR(local));
268 /* Set the socket to non-blocking mode */
269 silc_net_set_socket_nonblock(sock);
271 /* Connect to the host */
272 rval = connect(sock, &desthost.sa, SIZEOF_SOCKADDR(desthost));
274 if (errno != EINPROGRESS) {
275 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
282 /* Set appropriate options */
283 #if defined(TCP_NODELAY)
284 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
286 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
288 SILC_LOG_DEBUG(("Connection operation in progress"));
293 /* Closes the connection by closing the socket connection. */
295 void silc_net_close_connection(int sock)
300 /* Set's the socket to non-blocking mode. */
302 int silc_net_set_socket_nonblock(int sock)
304 return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
307 /* Converts the IP number string from numbers-and-dots notation to
310 bool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len)
314 if (silc_net_is_ip4(addr)) {
317 ret = inet_aton(addr, &tmp);
321 memcpy(bin, (unsigned char *)&tmp.s_addr, 4);
324 struct addrinfo hints, *ai;
331 memset(&hints, 0, sizeof(hints));
332 hints.ai_family = AF_INET6;
333 if (getaddrinfo(addr, NULL, &hints, &ai))
337 s = (SilcSockaddr *)ai->ai_addr;
338 memcpy(bin, &s->sin6.sin6_addr, sizeof(s->sin6.sin6_addr));
343 #endif /* HAVE_IPV6 */