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.1 2000/09/13 17:45:16 priikone
24 * Splitted SILC core library. Core library includes now only
25 * SILC protocol specific stuff. New utility library includes the
26 * old stuff from core library that is more generic purpose stuff.
28 * Revision 1.3 2000/07/05 06:06:35 priikone
29 * Global cosmetic change.
31 * Revision 1.2 2000/06/30 10:49:48 priikone
32 * Added SOCKS4 and SOCKS5 support for SILC client.
34 * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
35 * Imported from internal CVS/Added Log headers.
40 #include "silcincludes.h"
43 /* This function creates server or daemon or listener or what ever. This
44 does not fork a new process, it must be done by the caller if caller
45 wants to create a child process. This is used by the SILC server.
46 If argument `ip_addr' is NULL `any' address will be used. Returns
47 the created socket or -1 on error. */
49 int silc_net_create_server(int port, char *ip_addr)
52 struct sockaddr_in server;
54 SILC_LOG_DEBUG(("Creating a new server listener"));
56 /* Create the socket */
57 sock = socket(PF_INET, SOCK_STREAM, 0);
59 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
63 /* Set the socket options */
64 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
66 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
70 /* Set the socket information for bind() */
71 memset(&server, 0, sizeof(server));
72 server.sin_family = PF_INET;
73 server.sin_port = htons(port);
75 /* Convert IP address to network byte order */
77 inet_aton(ip_addr, &server.sin_addr);
79 server.sin_addr.s_addr = INADDR_ANY;
81 /* Bind the server socket */
82 rval = bind(sock, (struct sockaddr *)&server, sizeof(server));
84 SILC_LOG_ERROR(("Cannot bind socket: %s", strerror(errno)));
88 /* Specify that we are listenning */
89 rval = listen(sock, 5);
91 SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
95 /* Set the server socket to non-blocking mode */
96 silc_net_set_socket_nonblock(sock);
98 SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
103 void silc_net_close_server(int sock)
108 SILC_LOG_DEBUG(("Server socket closed"));
111 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
112 socket or -1 on error. This blocks the process while trying to create
115 int silc_net_create_connection(int port, char *host)
118 struct hostent *dest;
119 struct sockaddr_in desthost;
121 SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
124 dest = gethostbyname(host);
126 SILC_LOG_ERROR(("Network (%s) unreachable", host));
130 /* Set socket information */
131 memset(&desthost, 0, sizeof(desthost));
132 desthost.sin_port = htons(port);
133 desthost.sin_family = PF_INET;
134 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
136 /* Create the connection socket */
137 sock = socket(PF_INET, SOCK_STREAM, 0);
139 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
143 /* Connect to the host */
144 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
146 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
152 /* Set appropriate option */
153 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
155 SILC_LOG_DEBUG(("Connection created"));
160 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
161 socket or -1 on error. This creates non-blocking socket hence the
162 connection returns directly. To get the result of the connect() one
163 must select() the socket and read the result after it's ready. */
165 int silc_net_create_connection_async(int port, char *host)
168 struct hostent *dest;
169 struct sockaddr_in desthost;
171 SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
175 dest = gethostbyname(host);
177 SILC_LOG_ERROR(("Network (%s) unreachable", host));
181 /* Set socket information */
182 memset(&desthost, 0, sizeof(desthost));
183 desthost.sin_port = htons(port);
184 desthost.sin_family = PF_INET;
185 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
187 /* Create the connection socket */
188 sock = socket(PF_INET, SOCK_STREAM, 0);
190 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
194 /* Set the socket to non-blocking mode */
195 silc_net_set_socket_nonblock(sock);
197 /* Connect to the host */
198 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
200 if (errno != EINPROGRESS) {
201 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
208 /* Set appropriate option */
209 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
211 SILC_LOG_DEBUG(("Connection operation in progress"));
216 /* Closes the connection */
218 void silc_net_close_connection(int sock)
223 /* Accepts a connection from a particular socket */
225 int silc_net_accept_connection(int sock)
227 return accept(sock, 0, 0);
230 /* Set's the socket to non-blocking mode. */
232 int silc_net_set_socket_nonblock(int sock)
234 return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
237 /* Sets a option for a socket. */
239 int silc_net_set_socket_opt(int sock, int level, int option, int on)
241 return setsockopt(sock, level, option, (void *)&on, sizeof(on));
244 /* Checks whether IP address sent as argument is valid IP address. */
246 int silc_net_is_ip(const char *addr)
249 return inet_aton(addr, &tmp);
252 /* Performs lookups for remote name and IP address. */
254 void silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
256 struct sockaddr_in remote;
257 struct hostent *dest;
258 char *host_ip = NULL;
259 char host_name[1024];
266 SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
268 memset(&remote, 0, sizeof(remote));
269 len = sizeof(remote);
270 rval = getpeername(sock, (struct sockaddr *)&remote, &len);
274 /* Get host by address */
275 dest = gethostbyaddr((char *)&remote.sin_addr,
276 sizeof(struct in_addr), AF_INET);
280 /* Get same hsot by name to see that the remote host really is
281 the who it says it is */
282 memset(host_name, 0, sizeof(host_name));
283 memcpy(host_name, dest->h_name, strlen(dest->h_name));
284 dest = gethostbyname(host_name);
288 /* Find the address from list */
289 for (i = 0; dest->h_addr_list[i]; i++)
290 if (!memcmp(dest->h_addr_list[i], &remote.sin_addr,
291 sizeof(struct in_addr)))
293 if (!dest->h_addr_list[i])
296 host_ip = inet_ntoa(remote.sin_addr);
300 *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
301 memcpy(*hostname, host_name, strlen(host_name));
302 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
303 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
304 memcpy(*ip, host_ip, strlen(host_ip));
305 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));