5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 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.
23 * Revision 1.2 2000/10/31 19:48:32 priikone
24 * A LOT updates. Cannot separate. :)
26 * Revision 1.1 2000/09/13 17:45:16 priikone
27 * Splitted SILC core library. Core library includes now only
28 * SILC protocol specific stuff. New utility library includes the
29 * old stuff from core library that is more generic purpose stuff.
31 * Revision 1.3 2000/07/05 06:06:35 priikone
32 * Global cosmetic change.
34 * Revision 1.2 2000/06/30 10:49:48 priikone
35 * Added SOCKS4 and SOCKS5 support for SILC client.
37 * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
38 * Imported from internal CVS/Added Log headers.
43 #include "silcincludes.h"
46 /* This function creates server or daemon or listener or what ever. This
47 does not fork a new process, it must be done by the caller if caller
48 wants to create a child process. This is used by the SILC server.
49 If argument `ip_addr' is NULL `any' address will be used. Returns
50 the created socket or -1 on error. */
52 int silc_net_create_server(int port, char *ip_addr)
55 struct sockaddr_in server;
57 SILC_LOG_DEBUG(("Creating a new server listener"));
59 /* Create the socket */
60 sock = socket(PF_INET, SOCK_STREAM, 0);
62 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
66 /* Set the socket options */
67 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
69 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
73 /* Set the socket information for bind() */
74 memset(&server, 0, sizeof(server));
75 server.sin_family = PF_INET;
76 server.sin_port = htons(port);
78 /* Convert IP address to network byte order */
80 inet_aton(ip_addr, &server.sin_addr);
82 server.sin_addr.s_addr = INADDR_ANY;
84 /* Bind the server socket */
85 rval = bind(sock, (struct sockaddr *)&server, sizeof(server));
87 SILC_LOG_ERROR(("Cannot bind socket: %s", strerror(errno)));
91 /* Specify that we are listenning */
92 rval = listen(sock, 5);
94 SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
98 /* Set the server socket to non-blocking mode */
99 silc_net_set_socket_nonblock(sock);
101 SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
106 void silc_net_close_server(int sock)
111 SILC_LOG_DEBUG(("Server socket closed"));
114 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
115 socket or -1 on error. This blocks the process while trying to create
118 int silc_net_create_connection(int port, char *host)
121 struct hostent *dest;
122 struct sockaddr_in desthost;
124 SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
127 dest = gethostbyname(host);
129 SILC_LOG_ERROR(("Network (%s) unreachable", host));
133 /* Set socket information */
134 memset(&desthost, 0, sizeof(desthost));
135 desthost.sin_port = htons(port);
136 desthost.sin_family = PF_INET;
137 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
139 /* Create the connection socket */
140 sock = socket(PF_INET, SOCK_STREAM, 0);
142 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
146 /* Connect to the host */
147 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
149 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
155 /* Set appropriate options */
156 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
157 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
159 SILC_LOG_DEBUG(("Connection created"));
164 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
165 socket or -1 on error. This creates non-blocking socket hence the
166 connection returns directly. To get the result of the connect() one
167 must select() the socket and read the result after it's ready. */
169 int silc_net_create_connection_async(int port, char *host)
172 struct hostent *dest;
173 struct sockaddr_in desthost;
175 SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
179 dest = gethostbyname(host);
181 SILC_LOG_ERROR(("Network (%s) unreachable", host));
185 /* Set socket information */
186 memset(&desthost, 0, sizeof(desthost));
187 desthost.sin_port = htons(port);
188 desthost.sin_family = PF_INET;
189 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
191 /* Create the connection socket */
192 sock = socket(PF_INET, SOCK_STREAM, 0);
194 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
198 /* Set the socket to non-blocking mode */
199 silc_net_set_socket_nonblock(sock);
201 /* Connect to the host */
202 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
204 if (errno != EINPROGRESS) {
205 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
212 /* Set appropriate options */
213 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
214 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
216 SILC_LOG_DEBUG(("Connection operation in progress"));
221 /* Closes the connection */
223 void silc_net_close_connection(int sock)
228 /* Accepts a connection from a particular socket */
230 int silc_net_accept_connection(int sock)
232 return accept(sock, 0, 0);
235 /* Set's the socket to non-blocking mode. */
237 int silc_net_set_socket_nonblock(int sock)
239 return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
242 /* Sets a option for a socket. */
244 int silc_net_set_socket_opt(int sock, int level, int option, int on)
246 return setsockopt(sock, level, option, (void *)&on, sizeof(on));
249 /* Checks whether IP address sent as argument is valid IP address. */
251 int silc_net_is_ip(const char *addr)
254 return inet_aton(addr, &tmp);
257 /* Performs lookups for remote name and IP address. */
259 void silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
261 struct sockaddr_in remote;
262 struct hostent *dest;
263 char *host_ip = NULL;
264 char host_name[1024];
271 SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
273 memset(&remote, 0, sizeof(remote));
274 len = sizeof(remote);
275 rval = getpeername(sock, (struct sockaddr *)&remote, &len);
279 /* Get host by address */
280 dest = gethostbyaddr((char *)&remote.sin_addr,
281 sizeof(struct in_addr), AF_INET);
285 /* Get same hsot by name to see that the remote host really is
286 the who it says it is */
287 memset(host_name, 0, sizeof(host_name));
288 memcpy(host_name, dest->h_name, strlen(dest->h_name));
289 dest = gethostbyname(host_name);
293 /* Find the address from list */
294 for (i = 0; dest->h_addr_list[i]; i++)
295 if (!memcmp(dest->h_addr_list[i], &remote.sin_addr,
296 sizeof(struct in_addr)))
298 if (!dest->h_addr_list[i])
301 host_ip = inet_ntoa(remote.sin_addr);
305 *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
306 memcpy(*hostname, host_name, strlen(host_name));
307 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
308 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
309 memcpy(*ip, host_ip, strlen(host_ip));
310 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
313 /* Return name of localhost. */
315 char *silc_net_localhost()
318 if (!gethostname(hostname, sizeof(hostname)))
319 return strdup(hostname);