updates.
[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 /* $Id$ */
21
22 #include "silcincludes.h"
23 #include "silcnet.h"
24
25 /* This function creates server or daemon or listener or what ever. This
26    does not fork a new process, it must be done by the caller if caller
27    wants to create a child process. This is used by the SILC server. 
28    If argument `ip_addr' is NULL `any' address will be used. Returns 
29    the created socket or -1 on error. */
30
31 int silc_net_create_server(int port, char *ip_addr)
32 {
33   int sock, rval;
34   struct sockaddr_in server;
35
36   SILC_LOG_DEBUG(("Creating a new server listener"));
37
38   /* Create the socket */
39   sock = socket(PF_INET, SOCK_STREAM, 0);
40   if (sock < 0) {
41     SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
42     return -1;
43   }
44
45   /* Set the socket options */
46   rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
47   if (rval < 0) {
48     SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
49     return -1;
50   }
51
52   /* Set the socket information for bind() */
53   memset(&server, 0, sizeof(server));
54   server.sin_family = PF_INET;
55   if (port)
56     server.sin_port = htons(port);
57
58   /* Convert IP address to network byte order */
59   if (ip_addr)
60     inet_aton(ip_addr, &server.sin_addr);
61   else
62     server.sin_addr.s_addr = INADDR_ANY;
63
64   /* Bind the server socket */
65   rval = bind(sock, (struct sockaddr *)&server, sizeof(server));
66   if (rval < 0) {
67     SILC_LOG_ERROR(("Cannot bind socket: %s", strerror(errno)));
68     return -1;
69   }
70
71   /* Specify that we are listenning */
72   rval = listen(sock, 5);
73   if (rval < 0) {
74     SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
75     return -1;
76   }
77
78   /* Set the server socket to non-blocking mode */
79   silc_net_set_socket_nonblock(sock);
80
81   SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
82
83   return sock;
84 }
85
86 void silc_net_close_server(int sock)
87 {
88   shutdown(sock, 2);
89   close(sock);
90
91   SILC_LOG_DEBUG(("Server socket closed"));
92 }
93
94 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
95    socket or -1 on error. This blocks the process while trying to create
96    the connection. */
97
98 int silc_net_create_connection(int port, char *host)
99 {
100   int sock, rval;
101   struct hostent *dest;
102   struct sockaddr_in desthost;
103
104   SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
105
106   /* Do host lookup */
107   dest = gethostbyname(host);
108   if (!dest) {
109     SILC_LOG_ERROR(("Network (%s) unreachable", host));
110     return -1;
111   }
112
113   /* Set socket information */
114   memset(&desthost, 0, sizeof(desthost));
115   desthost.sin_port = htons(port);
116   desthost.sin_family = PF_INET;
117   memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
118
119   /* Create the connection socket */
120   sock = socket(PF_INET, SOCK_STREAM, 0);
121   if (sock < 0) {
122     SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
123     return -1;
124   }
125
126   /* Connect to the host */
127   rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
128   if (rval < 0) {
129     SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
130     shutdown(sock, 2);
131     close(sock);
132     return -1;
133   }
134
135   /* Set appropriate options */
136   silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
137   silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
138
139   SILC_LOG_DEBUG(("Connection created"));
140
141   return sock;
142 }
143
144 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
145    socket or -1 on error. This creates non-blocking socket hence the
146    connection returns directly. To get the result of the connect() one
147    must select() the socket and read the result after it's ready. */
148
149 int silc_net_create_connection_async(int port, char *host)
150 {
151   int sock, rval;
152   struct hostent *dest;
153   struct sockaddr_in desthost;
154
155   SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d", 
156                   host, port));
157
158   /* Do host lookup */
159   dest = gethostbyname(host);
160   if (!dest) {
161     SILC_LOG_ERROR(("Network (%s) unreachable", host));
162     return -1;
163   }
164
165   /* Set socket information */
166   memset(&desthost, 0, sizeof(desthost));
167   desthost.sin_port = htons(port);
168   desthost.sin_family = PF_INET;
169   memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr));
170
171   /* Create the connection socket */
172   sock = socket(PF_INET, SOCK_STREAM, 0);
173   if (sock < 0) {
174     SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
175     return -1;
176   }
177
178   /* Set the socket to non-blocking mode */
179   silc_net_set_socket_nonblock(sock);
180
181   /* Connect to the host */
182   rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
183   if (rval < 0) {
184     if (errno !=  EINPROGRESS) {
185       SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
186       shutdown(sock, 2);
187       close(sock);
188       return -1;
189     }
190   }
191
192   /* Set appropriate options */
193   silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
194   silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
195
196   SILC_LOG_DEBUG(("Connection operation in progress"));
197
198   return sock;
199 }
200
201 /* Closes the connection */
202
203 void silc_net_close_connection(int sock)
204 {
205   close(sock);
206 }
207
208 /* Accepts a connection from a particular socket */
209
210 int silc_net_accept_connection(int sock)
211 {
212   return accept(sock, 0, 0);
213 }
214
215 /* Set's the socket to non-blocking mode. */
216
217 int silc_net_set_socket_nonblock(int sock)
218 {
219   return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
220 }
221
222 /* Sets a option for a socket. */
223
224 int silc_net_set_socket_opt(int sock, int level, int option, int on)
225 {
226   return setsockopt(sock, level, option, (void *)&on, sizeof(on));
227 }
228
229 /* Checks whether IP address sent as argument is valid IP address. */
230
231 int silc_net_is_ip(const char *addr)
232 {
233   struct in_addr tmp;
234   return inet_aton(addr, &tmp);
235 }
236
237 /* Performs lookups for remote name and IP address. This peforms reverse
238    lookup as well to verify that the IP has FQDN. */
239
240 void silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
241 {
242   struct sockaddr_in remote;
243   struct hostent *dest;
244   char *host_ip = NULL;
245   char host_name[1024];
246   int rval, len;
247   int i;
248
249   *hostname = NULL;
250   *ip = NULL;
251
252   SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
253
254   memset(&remote, 0, sizeof(remote));
255   len = sizeof(remote);
256   rval = getpeername(sock, (struct sockaddr *)&remote, &len);
257   if (rval < 0)
258     return;
259
260   /* Get host by address */
261   dest = gethostbyaddr((char *)&remote.sin_addr, 
262                        sizeof(struct in_addr), AF_INET);
263   if (!dest)
264     return;
265
266   /* Get same host by name to see that the remote host really is
267      the who it says it is */
268   memset(host_name, 0, sizeof(host_name));
269   memcpy(host_name, dest->h_name, strlen(dest->h_name));
270
271   *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
272   memcpy(*hostname, host_name, strlen(host_name));
273   SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
274
275   dest = gethostbyname(host_name);
276   if (!dest)
277     return;
278
279   /* Find the address from list */
280   for (i = 0; dest->h_addr_list[i]; i++)
281     if (!memcmp(dest->h_addr_list[i], &remote.sin_addr, 
282                sizeof(struct in_addr)))
283       break;
284   if (!dest->h_addr_list[i])
285     return;
286
287   host_ip = inet_ntoa(remote.sin_addr);
288   if (!host_ip)
289     return;
290
291   *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
292   memcpy(*ip, host_ip, strlen(host_ip));
293   SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
294 }
295
296 /* Return remote port by socket. */
297
298 uint16 silc_net_get_remote_port(int sock)
299 {
300   struct sockaddr_in remote;
301   int len;
302
303   memset(&remote, 0, sizeof(remote));
304   len = sizeof(remote);
305   if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
306     return 0;
307
308   return ntohs(remote.sin_port);
309 }
310
311 /* Return local port by socket. */
312
313 uint16 silc_net_get_local_port(int sock)
314 {
315   struct sockaddr_in local;
316   int len;
317
318   memset(&local, 0, sizeof(local));
319   len = sizeof(local);
320   if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
321     return 0;
322
323   return ntohs(local.sin_port);
324 }
325
326 /* Return name of localhost. */
327
328 char *silc_net_localhost()
329 {
330   char hostname[256];
331   if (!gethostname(hostname, sizeof(hostname)))
332     return strdup(hostname);
333   return NULL;
334 }