Merged silc_1_0_branch to trunk.
[silc.git] / lib / silcutil / unix / silcunixnet.c
1 /*
2
3   silcunixnet.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2001 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 #ifdef HAVE_IPV6
26 #define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ?    \
27   sizeof(so.sin6) : sizeof(so.sin))
28 #else
29 #define SIZEOF_SOCKADDR(so) (sizeof(so.sin))
30 #endif
31
32 typedef union {
33   struct sockaddr sa;
34   struct sockaddr_in sin;
35 #ifdef HAVE_IPV6
36   struct sockaddr_in6 sin6;
37 #endif
38 } SilcSockaddr;
39
40 static bool silc_net_set_sockaddr(SilcSockaddr *addr, const char *ip_addr,
41                                   int port)
42 {
43   int len;
44
45   memset(addr, 0, sizeof(*addr));
46
47   /* Check for IPv4 and IPv6 addresses */
48   if (ip_addr) {
49     if (!silc_net_is_ip(ip_addr)) {
50       SILC_LOG_ERROR(("%s is not IP address", ip_addr));
51       return FALSE;
52     }
53
54     if (silc_net_is_ip4(ip_addr)) {
55       /* IPv4 address */
56       len = sizeof(addr->sin.sin_addr);
57       silc_net_addr2bin(ip_addr, 
58                         (unsigned char *)&addr->sin.sin_addr.s_addr, len);
59       addr->sin.sin_family = AF_INET;
60       addr->sin.sin_port = port ? htons(port) : 0;
61     } else {
62 #ifdef HAVE_IPV6
63       /* IPv6 address */
64       len = sizeof(addr->sin6.sin6_addr);
65       silc_net_addr2bin(ip_addr, 
66                         (unsigned char *)&addr->sin6.sin6_addr, len);
67       addr->sin6.sin6_family = AF_INET6;
68       addr->sin6.sin6_port = port ? htons(port) : 0;
69 #else
70       SILC_LOG_ERROR(("IPv6 support is not compiled in"));
71       return FALSE;
72 #endif
73     }
74   } else {
75     /* Any address */
76     addr->sin.sin_family = AF_INET;
77     addr->sin.sin_addr.s_addr = INADDR_ANY;
78     if (port)
79       addr->sin.sin_port = htons(port);
80   }
81
82   return TRUE;
83 }
84
85 /* This function creates server or daemon or listener or what ever. This
86    does not fork a new process, it must be done by the caller if caller
87    wants to create a child process. This is used by the SILC server. 
88    If argument `ip_addr' is NULL `any' address will be used. Returns 
89    the created socket or -1 on error. */
90
91 int silc_net_create_server(int port, const char *ip_addr)
92 {
93   int sock, rval;
94   SilcSockaddr server;
95
96   SILC_LOG_DEBUG(("Creating a new server listener"));
97
98   /* Set sockaddr for server */
99   if (!silc_net_set_sockaddr(&server, ip_addr, port))
100     return -1;
101
102   /* Create the socket */
103   sock = socket(server.sin.sin_family, SOCK_STREAM, 0);
104   if (sock < 0) {
105     SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
106     return -1;
107   }
108
109   /* Set the socket options */
110   rval = silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
111   if (rval < 0) {
112     SILC_LOG_ERROR(("Cannot set socket options: %s", strerror(errno)));
113     return -1;
114   }
115
116   /* Bind the server socket */
117   rval = bind(sock, &server.sa, SIZEOF_SOCKADDR(server));
118   if (rval < 0) {
119     SILC_LOG_DEBUG(("Cannot bind socket: %s", strerror(errno)));
120     return -1;
121   }
122
123   /* Specify that we are listenning */
124   rval = listen(sock, 5);
125   if (rval < 0) {
126     SILC_LOG_ERROR(("Cannot set socket listenning: %s", strerror(errno)));
127     return -1;
128   }
129
130   /* Set the server socket to non-blocking mode */
131   silc_net_set_socket_nonblock(sock);
132
133   SILC_LOG_DEBUG(("Server listener created, fd=%d", sock));
134
135   return sock;
136 }
137
138 /* Closes the server by closing the socket connection. */
139
140 void silc_net_close_server(int sock)
141 {
142   shutdown(sock, 2);
143   close(sock);
144
145   SILC_LOG_DEBUG(("Server socket closed"));
146 }
147
148 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
149    socket or -1 on error. This blocks the process while trying to create
150    the connection. */
151
152 int silc_net_create_connection(const char *local_ip, int port, 
153                                const char *host)
154 {
155   int sock, rval;
156   char ip_addr[64];
157   SilcSockaddr desthost;
158   bool prefer_ipv6 = TRUE;
159
160   SILC_LOG_DEBUG(("Creating connection to host %s port %d", host, port));
161
162   /* Do host lookup */
163  retry:
164   if (!silc_net_gethostbyname(host, prefer_ipv6, ip_addr, sizeof(ip_addr))) {
165     SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
166                     "IP address", host));
167     return -1;
168   }
169
170   /* Set sockaddr for this connection */
171   if (!silc_net_set_sockaddr(&desthost, ip_addr, port))
172     return -1;
173
174   /* Create the connection socket */
175   sock = socket(desthost.sin.sin_family, SOCK_STREAM, 0);
176   if (sock < 0) {
177     /* If address is IPv6, then fallback to IPv4 and see whether we can do
178        better with that on socket creation. */
179     if (prefer_ipv6 && silc_net_is_ip6(ip_addr)) {
180       prefer_ipv6 = FALSE;
181       goto retry;
182     }
183
184     SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
185     return -1;
186   }
187
188   /* Bind to the local address if provided */
189   if (local_ip) {
190     SilcSockaddr local;
191
192     /* Set sockaddr for local listener, and try to bind it. */
193     if (silc_net_set_sockaddr(&local, local_ip, 0))
194       bind(sock, &local.sa, SIZEOF_SOCKADDR(local));
195   }
196
197   /* Connect to the host */
198   rval = connect(sock, &desthost.sa, SIZEOF_SOCKADDR(desthost));
199   if (rval < 0) {
200     /* retry using an IPv4 adress, if IPv6 didn't work */
201     if (prefer_ipv6 && silc_net_is_ip6(ip_addr)) {
202       shutdown(sock, 2);
203       close(sock);
204
205       prefer_ipv6 = FALSE;
206       goto retry;
207     }
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   /* Set appropriate options */
215 #if defined(TCP_NODELAY)
216   silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
217 #endif
218   silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
219
220   SILC_LOG_DEBUG(("Connection created"));
221
222   return sock;
223 }
224
225 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
226    socket or -1 on error. This creates non-blocking socket hence the
227    connection returns directly. To get the result of the connect() one
228    must select() the socket and read the result after it's ready. */
229
230 int silc_net_create_connection_async(const char *local_ip, int port, 
231                                      const char *host)
232 {
233   int sock, rval;
234   char ip_addr[64];
235   SilcSockaddr desthost;
236   bool prefer_ipv6 = TRUE;
237
238   SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d", 
239                   host, port));
240
241   /* Do host lookup */
242  retry:
243   if (!silc_net_gethostbyname(host, prefer_ipv6, ip_addr, sizeof(ip_addr))) {
244     SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
245                     "IP address", host));
246     return -1;
247   }
248
249   /* Set sockaddr for this connection */
250   if (!silc_net_set_sockaddr(&desthost, ip_addr, port))
251     return -1;
252
253   /* Create the connection socket */
254   sock = socket(desthost.sin.sin_family, SOCK_STREAM, 0);
255   if (sock < 0) {
256     /* If address is IPv6, then fallback to IPv4 and see whether we can do
257        better with that on socket creation. */
258     if (prefer_ipv6 && silc_net_is_ip6(ip_addr)) {
259       prefer_ipv6 = FALSE;
260       goto retry;
261     }
262
263     SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
264     return -1;
265   }
266
267   /* Bind to the local address if provided */
268   if (local_ip) {
269     SilcSockaddr local;
270
271     /* Set sockaddr for local listener, and try to bind it. */
272     if (silc_net_set_sockaddr(&local, local_ip, 0))
273       bind(sock, &local.sa, SIZEOF_SOCKADDR(local));
274   }
275
276   /* Set the socket to non-blocking mode */
277   silc_net_set_socket_nonblock(sock);
278
279   /* Connect to the host */
280   rval = connect(sock, &desthost.sa, SIZEOF_SOCKADDR(desthost));
281   if (rval < 0) {
282     if (errno !=  EINPROGRESS) {
283       /* retry using an IPv4 adress, if IPv6 didn't work */
284       if (prefer_ipv6 && silc_net_is_ip6(ip_addr)) {
285         shutdown(sock, 2);
286         close(sock);
287
288         prefer_ipv6 = FALSE;
289         goto retry;
290       }
291
292       SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
293       shutdown(sock, 2);
294       close(sock);
295       return -1;
296     }
297   }
298
299   /* Set appropriate options */
300 #if defined(TCP_NODELAY)
301   silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
302 #endif
303   silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
304
305   SILC_LOG_DEBUG(("Connection operation in progress"));
306
307   return sock;
308 }
309
310 /* Closes the connection by closing the socket connection. */
311
312 void silc_net_close_connection(int sock)
313 {
314   close(sock);
315 }
316
317 /* Set's the socket to non-blocking mode. */
318
319 int silc_net_set_socket_nonblock(int sock)
320 {
321   return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
322 }
323
324 /* Converts the IP number string from numbers-and-dots notation to
325    binary form. */
326
327 bool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len)
328 {
329   int ret = 0;
330
331   if (silc_net_is_ip4(addr)) {
332     /* IPv4 address */
333     struct in_addr tmp;
334     ret = inet_aton(addr, &tmp);
335     if (bin_len < 4)
336       return FALSE;
337     
338     memcpy(bin, (unsigned char *)&tmp.s_addr, 4);
339 #ifdef HAVE_IPV6
340   } else {
341     struct addrinfo hints, *ai;
342     SilcSockaddr *s;
343
344     /* IPv6 address */
345     if (bin_len < 16)
346       return FALSE;
347
348     memset(&hints, 0, sizeof(hints));
349     hints.ai_family = AF_INET6;
350     if (getaddrinfo(addr, NULL, &hints, &ai))
351       return FALSE;
352
353     if (ai) {
354       s = (SilcSockaddr *)ai->ai_addr;
355       memcpy(bin, &s->sin6.sin6_addr, sizeof(s->sin6.sin6_addr));
356       freeaddrinfo(ai);
357     }
358
359     ret = TRUE;
360 #endif /* HAVE_IPV6 */
361   }
362
363   return ret != 0;
364 }