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;
159 SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
162 if (!silc_net_gethostbyname(host, ip_addr, sizeof(ip_addr))) {
163 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
164 "IP address", host));
168 /* Set sockaddr for this connection */
169 if (!silc_net_set_sockaddr(&desthost, ip_addr, port))
172 /* Create the connection socket */
173 sock = socket(desthost.sin.sin_family, SOCK_STREAM, 0);
175 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
179 /* Bind to the local address if provided */
183 /* Set sockaddr for local listener, and try to bind it. */
184 if (silc_net_set_sockaddr(&local, local_ip, 0))
185 bind(sock, &local.sa, sizeof(local));
188 /* Connect to the host */
189 rval = connect(sock, &desthost.sa, sizeof(desthost));
191 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
197 /* Set appropriate options */
198 #if defined(TCP_NODELAY)
199 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
201 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
203 SILC_LOG_DEBUG(("Connection created"));
208 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
209 socket or -1 on error. This creates non-blocking socket hence the
210 connection returns directly. To get the result of the connect() one
211 must select() the socket and read the result after it's ready. */
213 int silc_net_create_connection_async(const char *local_ip, int port,
218 SilcSockaddr desthost;
220 SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
224 if (!silc_net_gethostbyname(host, ip_addr, sizeof(ip_addr))) {
225 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
226 "IP address", host));
230 /* Set sockaddr for this connection */
231 if (!silc_net_set_sockaddr(&desthost, ip_addr, port))
234 /* Create the connection socket */
235 sock = socket(desthost.sin.sin_family, SOCK_STREAM, 0);
237 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
241 /* Bind to the local address if provided */
245 /* Set sockaddr for local listener, and try to bind it. */
246 if (silc_net_set_sockaddr(&local, local_ip, 0))
247 bind(sock, &local.sa, sizeof(local));
250 /* Set the socket to non-blocking mode */
251 silc_net_set_socket_nonblock(sock);
253 /* Connect to the host */
254 rval = connect(sock, &desthost.sa, sizeof(desthost));
256 if (errno != EINPROGRESS) {
257 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
264 /* Set appropriate options */
265 #if defined(TCP_NODELAY)
266 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
268 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
270 SILC_LOG_DEBUG(("Connection operation in progress"));
275 /* Closes the connection by closing the socket connection. */
277 void silc_net_close_connection(int sock)
282 /* Set's the socket to non-blocking mode. */
284 int silc_net_set_socket_nonblock(int sock)
286 return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
289 /* Converts the IP number string from numbers-and-dots notation to
292 bool silc_net_addr2bin(const char *addr, void *bin, uint32 bin_len)
296 if (silc_net_is_ip4(addr)) {
299 ret = inet_aton(addr, &tmp);
303 memcpy(bin, (unsigned char *)&tmp.s_addr, 4);
310 ret = inet_pton(AF_INET6, addr, &bin);
311 #endif /* HAVE_IPV6 */