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.3 2000/12/17 13:07:35 priikone
24 * Added require_reverse_mapping for ServerParams.
26 * Revision 1.2 2000/10/31 19:48:32 priikone
27 * A LOT updates. Cannot separate. :)
29 * Revision 1.1 2000/09/13 17:45:16 priikone
30 * Splitted SILC core library. Core library includes now only
31 * SILC protocol specific stuff. New utility library includes the
32 * old stuff from core library that is more generic purpose stuff.
34 * Revision 1.3 2000/07/05 06:06:35 priikone
35 * Global cosmetic change.
37 * Revision 1.2 2000/06/30 10:49:48 priikone
38 * Added SOCKS4 and SOCKS5 support for SILC client.
40 * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
41 * Imported from internal CVS/Added Log headers.
46 #include "silcincludes.h"
49 /* This function creates server or daemon or listener or what ever. This
50 does not fork a new process, it must be done by the caller if caller
51 wants to create a child process. This is used by the SILC server.
52 If argument `ip_addr' is NULL `any' address will be used. Returns
53 the created socket or -1 on error. */
55 int silc_net_create_server(int port, char *ip_addr)
58 struct sockaddr_in server;
60 SILC_LOG_DEBUG(("Creating a new server listener"));
62 /* Create the socket */
63 sock = socket(PF_INET, SOCK_STREAM, 0);
65 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
69 /* Set the socket options */
70 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
72 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
76 /* Set the socket information for bind() */
77 memset(&server, 0, sizeof(server));
78 server.sin_family = PF_INET;
79 server.sin_port = htons(port);
81 /* Convert IP address to network byte order */
83 inet_aton(ip_addr, &server.sin_addr);
85 server.sin_addr.s_addr = INADDR_ANY;
87 /* Bind the server socket */
88 rval = bind(sock, (struct sockaddr *)&server, sizeof(server));
90 SILC_LOG_ERROR(("Cannot bind socket: %s", strerror(errno)));
94 /* Specify that we are listenning */
95 rval = listen(sock, 5);
97 SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
101 /* Set the server socket to non-blocking mode */
102 silc_net_set_socket_nonblock(sock);
104 SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
109 void silc_net_close_server(int sock)
114 SILC_LOG_DEBUG(("Server socket closed"));
117 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
118 socket or -1 on error. This blocks the process while trying to create
121 int silc_net_create_connection(int port, char *host)
124 struct hostent *dest;
125 struct sockaddr_in desthost;
127 SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
130 dest = gethostbyname(host);
132 SILC_LOG_ERROR(("Network (%s) unreachable", host));
136 /* Set socket information */
137 memset(&desthost, 0, sizeof(desthost));
138 desthost.sin_port = htons(port);
139 desthost.sin_family = PF_INET;
140 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
142 /* Create the connection socket */
143 sock = socket(PF_INET, SOCK_STREAM, 0);
145 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
149 /* Connect to the host */
150 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
152 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
158 /* Set appropriate options */
159 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
160 silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
162 SILC_LOG_DEBUG(("Connection created"));
167 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
168 socket or -1 on error. This creates non-blocking socket hence the
169 connection returns directly. To get the result of the connect() one
170 must select() the socket and read the result after it's ready. */
172 int silc_net_create_connection_async(int port, char *host)
175 struct hostent *dest;
176 struct sockaddr_in desthost;
178 SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
182 dest = gethostbyname(host);
184 SILC_LOG_ERROR(("Network (%s) unreachable", host));
188 /* Set socket information */
189 memset(&desthost, 0, sizeof(desthost));
190 desthost.sin_port = htons(port);
191 desthost.sin_family = PF_INET;
192 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
194 /* Create the connection socket */
195 sock = socket(PF_INET, SOCK_STREAM, 0);
197 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
201 /* Set the socket to non-blocking mode */
202 silc_net_set_socket_nonblock(sock);
204 /* Connect to the host */
205 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
207 if (errno != EINPROGRESS) {
208 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
215 /* Set appropriate options */
216 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 operation in progress"));
224 /* Closes the connection */
226 void silc_net_close_connection(int sock)
231 /* Accepts a connection from a particular socket */
233 int silc_net_accept_connection(int sock)
235 return accept(sock, 0, 0);
238 /* Set's the socket to non-blocking mode. */
240 int silc_net_set_socket_nonblock(int sock)
242 return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
245 /* Sets a option for a socket. */
247 int silc_net_set_socket_opt(int sock, int level, int option, int on)
249 return setsockopt(sock, level, option, (void *)&on, sizeof(on));
252 /* Checks whether IP address sent as argument is valid IP address. */
254 int silc_net_is_ip(const char *addr)
257 return inet_aton(addr, &tmp);
260 /* Performs lookups for remote name and IP address. This peforms reverse
261 lookup as well to verify that the IP has FQDN. */
263 void silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
265 struct sockaddr_in remote;
266 struct hostent *dest;
267 char *host_ip = NULL;
268 char host_name[1024];
275 SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
277 memset(&remote, 0, sizeof(remote));
278 len = sizeof(remote);
279 rval = getpeername(sock, (struct sockaddr *)&remote, &len);
283 /* Get host by address */
284 dest = gethostbyaddr((char *)&remote.sin_addr,
285 sizeof(struct in_addr), AF_INET);
289 /* Get same host by name to see that the remote host really is
290 the who it says it is */
291 memset(host_name, 0, sizeof(host_name));
292 memcpy(host_name, dest->h_name, strlen(dest->h_name));
293 dest = gethostbyname(host_name);
297 /* Find the address from list */
298 for (i = 0; dest->h_addr_list[i]; i++)
299 if (!memcmp(dest->h_addr_list[i], &remote.sin_addr,
300 sizeof(struct in_addr)))
302 if (!dest->h_addr_list[i])
305 host_ip = inet_ntoa(remote.sin_addr);
309 *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
310 memcpy(*hostname, host_name, strlen(host_name));
311 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
312 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
313 memcpy(*ip, host_ip, strlen(host_ip));
314 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
317 /* Return name of localhost. */
319 char *silc_net_localhost()
322 if (!gethostname(hostname, sizeof(hostname)))
323 return strdup(hostname);