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