5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2002 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.
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 SilcBool silc_net_set_sockaddr(SilcSockaddr *addr, const char *ip_addr,
46 memset(addr, 0, sizeof(*addr));
48 /* Check for IPv4 and IPv6 addresses */
50 if (!silc_net_is_ip(ip_addr)) {
51 SILC_LOG_ERROR(("%s is not IP address", ip_addr));
55 if (silc_net_is_ip4(ip_addr)) {
57 len = sizeof(addr->sin.sin_addr);
58 silc_net_addr2bin(ip_addr,
59 (unsigned char *)&addr->sin.sin_addr.s_addr, len);
60 addr->sin.sin_family = AF_INET;
61 addr->sin.sin_port = port ? htons(port) : 0;
65 len = sizeof(addr->sin6.sin6_addr);
66 silc_net_addr2bin(ip_addr,
67 (unsigned char *)&addr->sin6.sin6_addr, len);
68 addr->sin6.sin6_family = AF_INET6;
69 addr->sin6.sin6_port = port ? htons(port) : 0;
71 SILC_LOG_ERROR(("IPv6 support is not compiled in"));
77 addr->sin.sin_family = AF_INET;
78 addr->sin.sin_addr.s_addr = INADDR_ANY;
80 addr->sin.sin_port = htons(port);
86 /* This function creates server or daemon or listener or what ever. This
87 does not fork a new process, it must be done by the caller if caller
88 wants to create a child process. This is used by the SILC server.
89 If argument `ip_addr' is NULL `any' address will be used. Returns
90 the created socket or -1 on error. */
92 int silc_net_create_server(int port, const char *ip_addr)
97 SILC_LOG_DEBUG(("Creating a new server listener"));
99 /* Set sockaddr for server */
100 if (!silc_net_set_sockaddr(&server, ip_addr, port))
103 /* Create the socket */
104 sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
106 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
110 /* Set the socket options */
111 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
113 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
117 /* Bind the server socket */
118 rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
120 SILC_LOG_DEBUG(("Cannot bind socket: %s", strerror(errno)));
124 /* Specify that we are listenning */
125 rval = listen(sock, 5);
127 SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
131 /* Set the server socket to non-blocking mode */
132 silc_net_set_socket_nonblock(sock);
134 SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
139 /* Closes the server by closing the socket connection. */
141 void silc_net_close_server(int sock)
146 SILC_LOG_DEBUG(("Server socket closed"));
149 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
150 socket or -1 on error. This blocks the process while trying to create
153 int silc_net_create_connection(const char *local_ip, int port,
158 SilcSockaddr desthost;
160 SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
163 if (!silc_net_gethostbyname(host, 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 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
180 /* Bind to the local address if provided */
184 /* Set sockaddr for local listener, and try to bind it. */
185 if (silc_net_set_sockaddr(&local, local_ip, 0))
186 bind(sock, &local.sa, sizeof(local));
189 /* Connect to the host */
190 rval = connect(sock, &desthost.sa, sizeof(desthost));
192 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
198 /* Set appropriate options */
199 #if defined(TCP_NODELAY)
200 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
202 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
204 SILC_LOG_DEBUG(("Connection created"));
209 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
210 socket or -1 on error. This creates non-blocking socket hence the
211 connection returns directly. To get the result of the connect() one
212 must select() the socket and read the result after it's ready. */
214 int silc_net_create_connection_async(const char *local_ip, int port,
219 SilcSockaddr desthost;
221 SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
225 if (!silc_net_gethostbyname(host, ip_addr, sizeof(ip_addr))) {
226 SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
227 "IP address", host));
231 /* Set sockaddr for this connection */
232 if (!silc_net_set_sockaddr(&desthost, ip_addr, port))
235 /* Create the connection socket */
236 sock = socket(desthost.sin.sin_family, SOCK_STREAM, 0);
238 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
242 /* Bind to the local address if provided */
246 /* Set sockaddr for local listener, and try to bind it. */
247 if (silc_net_set_sockaddr(&local, local_ip, 0))
248 bind(sock, &local.sa, sizeof(local));
251 /* Set the socket to non-blocking mode */
252 silc_net_set_socket_nonblock(sock);
254 /* Connect to the host */
255 rval = connect(sock, &desthost.sa, sizeof(desthost));
257 if (errno != EINPROGRESS) {
258 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
265 /* Set appropriate options */
266 #if defined(TCP_NODELAY)
267 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
269 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
271 SILC_LOG_DEBUG(("Connection operation in progress"));
276 /* Closes the connection by closing the socket connection. */
278 void silc_net_close_connection(int sock)
283 /* Set's the socket to non-blocking mode. */
285 int silc_net_set_socket_nonblock(int sock)
287 return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
290 /* Converts the IP number string from numbers-and-dots notation to
293 SilcBool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len)
297 if (silc_net_is_ip4(addr)) {
300 ret = inet_aton(addr, &tmp);
304 memcpy(bin, (unsigned char *)&tmp.s_addr, 4);
311 ret = inet_pton(AF_INET6, addr, &bin);
312 #endif /* HAVE_IPV6 */