Integer type name change.
[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(local));
195   }
196
197   /* Connect to the host */
198   rval = connect(sock, &desthost.sa, sizeof(desthost));
199   if (rval < 0) {
200     SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
201     shutdown(sock, 2);
202     close(sock);
203     return -1;
204   }
205
206   /* Set appropriate options */
207 #if defined(TCP_NODELAY)
208   silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
209 #endif
210   silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
211
212   SILC_LOG_DEBUG(("Connection created"));
213
214   return sock;
215 }
216
217 /* Creates a connection (TCP/IP) to a remote host. Returns the connection
218    socket or -1 on error. This creates non-blocking socket hence the
219    connection returns directly. To get the result of the connect() one
220    must select() the socket and read the result after it's ready. */
221
222 int silc_net_create_connection_async(const char *local_ip, int port, 
223                                      const char *host)
224 {
225   int sock, rval;
226   char ip_addr[64];
227   SilcSockaddr desthost;
228   bool prefer_ipv6 = TRUE;
229
230   SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d", 
231                   host, port));
232
233   /* Do host lookup */
234  retry:
235   if (!silc_net_gethostbyname(host, prefer_ipv6, ip_addr, sizeof(ip_addr))) {
236     SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the "
237                     "IP address", host));
238     return -1;
239   }
240
241   /* Set sockaddr for this connection */
242   if (!silc_net_set_sockaddr(&desthost, ip_addr, port))
243     return -1;
244
245   /* Create the connection socket */
246   sock = socket(desthost.sin.sin_family, SOCK_STREAM, 0);
247   if (sock < 0) {
248     /* If address is IPv6, then fallback to IPv4 and see whether we can do
249        better with that on socket creation. */
250     if (prefer_ipv6 && silc_net_is_ip6(ip_addr)) {
251       prefer_ipv6 = FALSE;
252       goto retry;
253     }
254
255     SILC_LOG_ERROR(("Cannot create socket: %s", strerror(errno)));
256     return -1;
257   }
258
259   /* Bind to the local address if provided */
260   if (local_ip) {
261     SilcSockaddr local;
262
263     /* Set sockaddr for local listener, and try to bind it. */
264     if (silc_net_set_sockaddr(&local, local_ip, 0))
265       bind(sock, &local.sa, sizeof(local));
266   }
267
268   /* Set the socket to non-blocking mode */
269   silc_net_set_socket_nonblock(sock);
270
271   /* Connect to the host */
272   rval = connect(sock, &desthost.sa, sizeof(desthost));
273   if (rval < 0) {
274     if (errno !=  EINPROGRESS) {
275       SILC_LOG_ERROR(("Cannot connect to remote host: %s", strerror(errno)));
276       shutdown(sock, 2);
277       close(sock);
278       return -1;
279     }
280   }
281
282   /* Set appropriate options */
283 #if defined(TCP_NODELAY)
284   silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
285 #endif
286   silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1);
287
288   SILC_LOG_DEBUG(("Connection operation in progress"));
289
290   return sock;
291 }
292
293 /* Closes the connection by closing the socket connection. */
294
295 void silc_net_close_connection(int sock)
296 {
297   close(sock);
298 }
299
300 /* Set's the socket to non-blocking mode. */
301
302 int silc_net_set_socket_nonblock(int sock)
303 {
304   return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
305 }
306
307 /* Converts the IP number string from numbers-and-dots notation to
308    binary form. */
309
310 bool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len)
311 {
312   int ret = 0;
313
314   if (silc_net_is_ip4(addr)) {
315     /* IPv4 address */
316     struct in_addr tmp;
317     ret = inet_aton(addr, &tmp);
318     if (bin_len < 4)
319       return FALSE;
320     
321     memcpy(bin, (unsigned char *)&tmp.s_addr, 4);
322 #ifdef HAVE_IPV6
323   } else {
324     /* IPv6 address */
325     if (bin_len < 16)
326       return FALSE;
327
328     ret = inet_pton(AF_INET6, addr, &bin);
329 #endif /* HAVE_IPV6 */
330   }
331
332   return ret != 0;
333 }