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/06/30 10:49:48 priikone
24 * Added SOCKS4 and SOCKS5 support for SILC client.
26 * Revision 1.1.1.1 2000/06/27 11:36:55 priikone
27 * Importet from internal CVS/Added Log headers.
32 #include "silcincludes.h"
35 /* This function creates server or daemon or listener or what ever. This
36 does not fork a new process, it must be done by the caller if caller
37 wants to create a child process. This is used by the SILC server.
38 If argument `ip_addr' is NULL `any' address will be used. Returns
39 the created socket or -1 on error. */
41 int silc_net_create_server(int port, char *ip_addr)
44 struct sockaddr_in server;
46 SILC_LOG_DEBUG(("Creating a new server listener"));
48 /* Create the socket */
49 sock = socket(PF_INET, SOCK_STREAM, 0);
51 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
55 /* Set the socket options */
56 rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
58 SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
62 /* Set the socket information for bind() */
63 memset(&server, 0, sizeof(server));
64 server.sin_family = PF_INET;
65 server.sin_port = htons(port);
67 /* Convert IP address to network byte order */
69 inet_aton(ip_addr, &server.sin_addr);
71 server.sin_addr.s_addr = INADDR_ANY;
73 /* Bind the server socket */
74 rval = bind(sock, (struct sockaddr *)&server, sizeof(server));
76 SILC_LOG_ERROR(("Cannot bind socket: %s", strerror(errno)));
80 /* Specify that we are listenning */
81 rval = listen(sock, 5);
83 SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
87 /* Set the server socket to non-blocking mode */
88 silc_net_set_socket_nonblock(sock);
90 SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
95 void silc_net_close_server(int sock)
100 SILC_LOG_DEBUG(("Server socket closed"));
103 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
104 socket or -1 on error. This blocks the process while trying to create
107 int silc_net_create_connection(int port, char *host)
110 struct hostent *dest;
111 struct sockaddr_in desthost;
113 SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
116 dest = gethostbyname(host);
118 SILC_LOG_ERROR(("Network (%s) unreachable", host));
122 /* Set socket information */
123 memset(&desthost, 0, sizeof(desthost));
124 desthost.sin_port = htons(port);
125 desthost.sin_family = PF_INET;
126 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
128 /* Create the connection socket */
129 sock = socket(PF_INET, SOCK_STREAM, 0);
131 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
135 /* Connect to the host */
136 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
138 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
144 /* Set appropriate option */
145 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
147 SILC_LOG_DEBUG(("Connection created"));
152 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
153 socket or -1 on error. This creates non-blocking socket hence the
154 connection returns directly. To get the result of the connect() one
155 must select() the socket and read the result after it's ready. */
157 int silc_net_create_connection_async(int port, char *host)
160 struct hostent *dest;
161 struct sockaddr_in desthost;
163 SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d",
167 dest = gethostbyname(host);
169 SILC_LOG_ERROR(("Network (%s) unreachable", host));
173 /* Set socket information */
174 memset(&desthost, 0, sizeof(desthost));
175 desthost.sin_port = htons(port);
176 desthost.sin_family = PF_INET;
177 memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
179 /* Create the connection socket */
180 sock = socket(PF_INET, SOCK_STREAM, 0);
182 SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
186 /* Set the socket to non-blocking mode */
187 silc_net_set_socket_nonblock(sock);
189 /* Connect to the host */
190 rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
192 if (errno != EINPROGRESS) {
193 SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
200 /* Set appropriate option */
201 silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
203 SILC_LOG_DEBUG(("Connection operation in progress"));
208 /* Closes the connection */
210 void silc_net_close_connection(int sock)
215 /* Accepts a connection from a particular socket */
217 int silc_net_accept_connection(int sock)
219 return accept(sock, 0, 0);
222 /* Set's the socket to non-blocking mode. */
224 int silc_net_set_socket_nonblock(int sock)
226 return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
229 /* Sets a option for a socket. */
231 int silc_net_set_socket_opt(int sock, int level, int option, int on)
233 return setsockopt(sock, level, option, (void *)&on, sizeof(on));
236 /* Checks whether IP address sent as argument is valid IP address. */
238 int silc_net_is_ip(const char *addr)
241 return inet_aton(addr, &tmp);
244 /* Performs lookups for remote name and IP address. */
246 void silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
248 struct sockaddr_in remote;
249 struct hostent *dest;
250 char *host_ip = NULL;
251 char host_name[1024];
258 SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
260 memset(&remote, 0, sizeof(remote));
261 len = sizeof(remote);
262 rval = getpeername(sock, (struct sockaddr *)&remote, &len);
266 /* Get host by address */
267 dest = gethostbyaddr((char *)&remote.sin_addr,
268 sizeof(struct in_addr), AF_INET);
272 /* Get same hsot by name to see that the remote host really is
273 the who it says it is */
274 memset(host_name, 0, sizeof(host_name));
275 memcpy(host_name, dest->h_name, strlen(dest->h_name));
276 dest = gethostbyname(host_name);
280 /* Find the address from list */
281 for (i = 0; dest->h_addr_list[i]; i++)
282 if (!memcmp(dest->h_addr_list[i], &remote.sin_addr,
283 sizeof(struct in_addr)))
285 if (!dest->h_addr_list[i])
288 host_ip = inet_ntoa(remote.sin_addr);
292 *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
293 memcpy(*hostname, host_name, strlen(host_name));
294 SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
295 *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
296 memcpy(*ip, host_ip, strlen(host_ip));
297 SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));