3104b67c72a20b45790acf2b58b004c22968b213
[silc.git] / lib / silccore / 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.2  2000/06/30 10:49:48  priikone
24  *      Added SOCKS4 and SOCKS5 support for SILC client.
25  *
26  * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
27  *      Importet from internal CVS/Added Log headers.
28  *
29  *
30  */
31
32 #include "silcincludes.h"
33 #include "silcnet.h"
34
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. */
40
41 int silc_net_create_server(int port, char *ip_addr)
42 {
43   int sock, rval;
44   struct sockaddr_in server;
45
46   SILC_LOG_DEBUG(("Creating a new server listener"));
47
48   /* Create the socket */
49   sock = socket(PF_INET, SOCK_STREAM, 0);
50   if (sock < 0) {
51     SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
52     return -1;
53   }
54
55   /* Set the socket options */
56   rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
57   if (rval < 0) {
58     SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
59     return -1;
60   }
61
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);
66
67   /* Convert IP address to network byte order */
68   if (ip_addr)
69     inet_aton(ip_addr, &server.sin_addr);
70   else
71     server.sin_addr.s_addr = INADDR_ANY;
72
73   /* Bind the server socket */
74   rval = bind(sock, (struct sockaddr *)&server, sizeof(server));
75   if (rval < 0) {
76     SILC_LOG_ERROR(("Cannot bind socket: %s", strerror(errno)));
77     return -1;
78   }
79
80   /* Specify that we are listenning */
81   rval = listen(sock, 5);
82   if (rval < 0) {
83     SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
84     return -1;
85   }
86
87   /* Set the server socket to non-blocking mode */
88   silc_net_set_socket_nonblock(sock);
89
90   SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
91
92   return sock;
93 }
94
95 void silc_net_close_server(int sock)
96 {
97   shutdown(sock, 2);
98   close(sock);
99
100   SILC_LOG_DEBUG(("Server socket closed"));
101 }
102
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
105    the connection. */
106
107 int silc_net_create_connection(int port, char *host)
108 {
109   int sock, rval;
110   struct hostent *dest;
111   struct sockaddr_in desthost;
112
113   SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
114
115   /* Do host lookup */
116   dest = gethostbyname(host);
117   if (!dest) {
118     SILC_LOG_ERROR(("Network (%s) unreachable", host));
119     return -1;
120   }
121
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));
127
128   /* Create the connection socket */
129   sock = socket(PF_INET, SOCK_STREAM, 0);
130   if (sock < 0) {
131     SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
132     return -1;
133   }
134
135   /* Connect to the host */
136   rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
137   if (rval < 0) {
138     SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
139     shutdown(sock, 2);
140     close(sock);
141     return -1;
142   }
143
144   /* Set appropriate option */
145   silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
146
147   SILC_LOG_DEBUG(("Connection created"));
148
149   return sock;
150 }
151
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. */
156
157 int silc_net_create_connection_async(int port, char *host)
158 {
159   int sock, rval;
160   struct hostent *dest;
161   struct sockaddr_in desthost;
162
163   SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d", 
164                   host, port));
165
166   /* Do host lookup */
167   dest = gethostbyname(host);
168   if (!dest) {
169     SILC_LOG_ERROR(("Network (%s) unreachable", host));
170     return -1;
171   }
172
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));
178
179   /* Create the connection socket */
180   sock = socket(PF_INET, SOCK_STREAM, 0);
181   if (sock < 0) {
182     SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
183     return -1;
184   }
185
186   /* Set the socket to non-blocking mode */
187   silc_net_set_socket_nonblock(sock);
188
189   /* Connect to the host */
190   rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
191   if (rval < 0) {
192     if (errno !=  EINPROGRESS) {
193       SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
194       shutdown(sock, 2);
195       close(sock);
196       return -1;
197     }
198   }
199
200   /* Set appropriate option */
201   silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
202
203   SILC_LOG_DEBUG(("Connection operation in progress"));
204
205   return sock;
206 }
207
208 /* Closes the connection */
209
210 void silc_net_close_connection(int sock)
211 {
212   close(sock);
213 }
214
215 /* Accepts a connection from a particular socket */
216
217 int silc_net_accept_connection(int sock)
218 {
219   return accept(sock, 0, 0);
220 }
221
222 /* Set's the socket to non-blocking mode. */
223
224 int silc_net_set_socket_nonblock(int sock)
225 {
226   return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
227 }
228
229 /* Sets a option for a socket. */
230
231 int silc_net_set_socket_opt(int sock, int level, int option, int on)
232 {
233   return setsockopt(sock, level, option, (void *)&on, sizeof(on));
234 }
235
236 /* Checks whether IP address sent as argument is valid IP address. */
237
238 int silc_net_is_ip(const char *addr)
239 {
240   struct in_addr tmp;
241   return inet_aton(addr, &tmp);
242 }
243
244 /* Performs lookups for remote name and IP address. */
245
246 void silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
247 {
248   struct sockaddr_in remote;
249   struct hostent *dest;
250   char *host_ip = NULL;
251   char host_name[1024];
252   int rval, len;
253   int i;
254
255   *hostname = NULL;
256   *ip = NULL;
257
258   SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
259
260   memset(&remote, 0, sizeof(remote));
261   len = sizeof(remote);
262   rval = getpeername(sock, (struct sockaddr *)&remote, &len);
263   if (rval < 0)
264     return;
265
266   /* Get host by address */
267   dest = gethostbyaddr((char *)&remote.sin_addr, 
268                        sizeof(struct in_addr), AF_INET);
269   if (!dest)
270     return;
271
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);
277   if (!dest)
278     return;
279
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)))
284       break;
285   if (!dest->h_addr_list[i])
286     return;
287
288   host_ip = inet_ntoa(remote.sin_addr);
289   if (!host_ip)
290     return;
291
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));
298 }