Added require_reverse_mapping for ServerParams.
[silc.git] / lib / silcutil / silcnet.c
1 /*
2
3   silcnet.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2000 Pekka Riikonen
8
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.
13   
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.
18
19 */
20 /*
21  * $Id$
22  * $Log$
23  * Revision 1.3  2000/12/17 13:07:35  priikone
24  *      Added require_reverse_mapping for ServerParams.
25  *
26  * Revision 1.2  2000/10/31 19:48:32  priikone
27  *      A LOT updates. Cannot separate. :)
28  *
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.
33  *
34  * Revision 1.3  2000/07/05 06:06:35  priikone
35  *      Global cosmetic change.
36  *
37  * Revision 1.2  2000/06/30 10:49:48  priikone
38  *      Added SOCKS4 and SOCKS5 support for SILC client.
39  *
40  * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
41  *      Imported from internal CVS/Added Log headers.
42  *
43  *
44  */
45
46 #include "silcincludes.h"
47 #include "silcnet.h"
48
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. */
54
55 int silc_net_create_server(int port, char *ip_addr)
56 {
57   int sock, rval;
58   struct sockaddr_in server;
59
60   SILC_LOG_DEBUG(("Creating a new server listener"));
61
62   /* Create the socket */
63   sock = socket(PF_INET, SOCK_STREAM, 0);
64   if (sock < 0) {
65     SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
66     return -1;
67   }
68
69   /* Set the socket options */
70   rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
71   if (rval < 0) {
72     SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
73     return -1;
74   }
75
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);
80
81   /* Convert IP address to network byte order */
82   if (ip_addr)
83     inet_aton(ip_addr, &server.sin_addr);
84   else
85     server.sin_addr.s_addr = INADDR_ANY;
86
87   /* Bind the server socket */
88   rval = bind(sock, (struct sockaddr *)&server, sizeof(server));
89   if (rval < 0) {
90     SILC_LOG_ERROR(("Cannot bind socket: %s", strerror(errno)));
91     return -1;
92   }
93
94   /* Specify that we are listenning */
95   rval = listen(sock, 5);
96   if (rval < 0) {
97     SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
98     return -1;
99   }
100
101   /* Set the server socket to non-blocking mode */
102   silc_net_set_socket_nonblock(sock);
103
104   SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
105
106   return sock;
107 }
108
109 void silc_net_close_server(int sock)
110 {
111   shutdown(sock, 2);
112   close(sock);
113
114   SILC_LOG_DEBUG(("Server socket closed"));
115 }
116
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
119    the connection. */
120
121 int silc_net_create_connection(int port, char *host)
122 {
123   int sock, rval;
124   struct hostent *dest;
125   struct sockaddr_in desthost;
126
127   SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
128
129   /* Do host lookup */
130   dest = gethostbyname(host);
131   if (!dest) {
132     SILC_LOG_ERROR(("Network (%s) unreachable", host));
133     return -1;
134   }
135
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));
141
142   /* Create the connection socket */
143   sock = socket(PF_INET, SOCK_STREAM, 0);
144   if (sock < 0) {
145     SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
146     return -1;
147   }
148
149   /* Connect to the host */
150   rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
151   if (rval < 0) {
152     SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
153     shutdown(sock, 2);
154     close(sock);
155     return -1;
156   }
157
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);
161
162   SILC_LOG_DEBUG(("Connection created"));
163
164   return sock;
165 }
166
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. */
171
172 int silc_net_create_connection_async(int port, char *host)
173 {
174   int sock, rval;
175   struct hostent *dest;
176   struct sockaddr_in desthost;
177
178   SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d", 
179                   host, port));
180
181   /* Do host lookup */
182   dest = gethostbyname(host);
183   if (!dest) {
184     SILC_LOG_ERROR(("Network (%s) unreachable", host));
185     return -1;
186   }
187
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));
193
194   /* Create the connection socket */
195   sock = socket(PF_INET, SOCK_STREAM, 0);
196   if (sock < 0) {
197     SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
198     return -1;
199   }
200
201   /* Set the socket to non-blocking mode */
202   silc_net_set_socket_nonblock(sock);
203
204   /* Connect to the host */
205   rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
206   if (rval < 0) {
207     if (errno !=  EINPROGRESS) {
208       SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
209       shutdown(sock, 2);
210       close(sock);
211       return -1;
212     }
213   }
214
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);
218
219   SILC_LOG_DEBUG(("Connection operation in progress"));
220
221   return sock;
222 }
223
224 /* Closes the connection */
225
226 void silc_net_close_connection(int sock)
227 {
228   close(sock);
229 }
230
231 /* Accepts a connection from a particular socket */
232
233 int silc_net_accept_connection(int sock)
234 {
235   return accept(sock, 0, 0);
236 }
237
238 /* Set's the socket to non-blocking mode. */
239
240 int silc_net_set_socket_nonblock(int sock)
241 {
242   return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
243 }
244
245 /* Sets a option for a socket. */
246
247 int silc_net_set_socket_opt(int sock, int level, int option, int on)
248 {
249   return setsockopt(sock, level, option, (void *)&on, sizeof(on));
250 }
251
252 /* Checks whether IP address sent as argument is valid IP address. */
253
254 int silc_net_is_ip(const char *addr)
255 {
256   struct in_addr tmp;
257   return inet_aton(addr, &tmp);
258 }
259
260 /* Performs lookups for remote name and IP address. This peforms reverse
261    lookup as well to verify that the IP has FQDN. */
262
263 void silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
264 {
265   struct sockaddr_in remote;
266   struct hostent *dest;
267   char *host_ip = NULL;
268   char host_name[1024];
269   int rval, len;
270   int i;
271
272   *hostname = NULL;
273   *ip = NULL;
274
275   SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
276
277   memset(&remote, 0, sizeof(remote));
278   len = sizeof(remote);
279   rval = getpeername(sock, (struct sockaddr *)&remote, &len);
280   if (rval < 0)
281     return;
282
283   /* Get host by address */
284   dest = gethostbyaddr((char *)&remote.sin_addr, 
285                        sizeof(struct in_addr), AF_INET);
286   if (!dest)
287     return;
288
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);
294   if (!dest)
295     return;
296
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)))
301       break;
302   if (!dest->h_addr_list[i])
303     return;
304
305   host_ip = inet_ntoa(remote.sin_addr);
306   if (!host_ip)
307     return;
308
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));
315 }
316
317 /* Return name of localhost. */
318
319 char *silc_net_localhost()
320 {
321   char hostname[256];
322   if (!gethostname(hostname, sizeof(hostname)))
323     return strdup(hostname);
324   return NULL;
325 }