updates.
[silc.git] / lib / silcutil / silcnet.c
1 /*
2
3   silcnet.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 /* Accepts a connection from a particular socket */
26
27 int silc_net_accept_connection(int sock)
28 {
29   return accept(sock, 0, 0);
30 }
31
32 /* Sets a option for a socket. */
33
34 int silc_net_set_socket_opt(int sock, int level, int option, int on)
35 {
36   return setsockopt(sock, level, option, (void *)&on, sizeof(on));
37 }
38
39 /* Get socket options */
40
41 int silc_net_get_socket_opt(int sock, int level, int option, 
42                             void *optval, int *opt_len)
43 {
44   return getsockopt(sock, level, option, optval, opt_len);
45 }
46
47 /* Checks whether IP address sent as argument is valid IPv4 address. */
48
49 bool silc_net_is_ip4(const char *addr)
50 {
51   int count = 0;
52
53   while (*addr) {
54     if (*addr != '.' && !isdigit(*addr))
55       return FALSE;
56     if (*addr == '.')
57       count++;
58     addr++;
59   }
60
61   if (count != 3)
62     return FALSE;
63   
64   return TRUE;
65 }
66
67 /* Checks whether IP address sent as argument is valid IPv6 address. */
68
69 bool silc_net_is_ip6(const char *addr)
70 {
71   /* XXX does this work with all kinds of IPv6 addresses? */
72   while (*addr) {
73     if (*addr != ':' && !isxdigit(*addr))
74       return FALSE;
75     addr++;
76   }
77   
78   return TRUE;
79 }
80
81 /* Checks whether IP address sent as argument is valid IP address. */
82
83 bool silc_net_is_ip(const char *addr)
84 {
85   if (silc_net_is_ip4(addr))
86     return TRUE;
87   return silc_net_is_ip6(addr);
88 }
89
90 /* Resolves IP address for hostname. */
91
92 bool silc_net_gethostbyname(const char *name, char *address,
93                             uint32 address_len)
94 {
95 #ifdef HAVE_IPV6
96   struct addrinfo hints, *ai;
97   char hbuf[NI_MAXHOST];
98
99   memset(&hints, 0, sizeof(hints));
100   hints.ai_socktype = SOCK_STREAM;
101   if (getaddrinfo(name, NULL, &hints, &ai))
102     return FALSE;
103
104   if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf,
105                   sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
106     return FALSE;
107   
108   if (!inet_ntop(ai->ai_family, ai->ai_addr, address, address_len))
109     return FALSE;
110
111   freeaddrinfo(ai);
112 #else
113   struct hostent *hp;
114   struct in_addr ip;
115   char *tmp;
116
117   hp = gethostbyname(name);
118   if (!hp)
119     return FALSE;
120
121   memcpy(&ip.s_addr, hp->h_addr_list[0], 4);
122   tmp = inet_ntoa(ip);
123   if (!tmp)
124     return FALSE;
125   if (address_len < strlen(tmp))
126     return FALSE;
127   memset(address, 0, address_len);
128   strncpy(address, tmp, strlen(tmp));
129 #endif
130   
131   return TRUE;
132 }
133
134 /* Resolves hostname by IP address. */
135
136 bool silc_net_gethostbyaddr(const char *addr, char *name, uint32 name_len)
137 {
138 #ifdef HAVE_IPV6
139   struct addrinfo req, *ai;
140   
141   memset(&req, 0, sizeof(req));
142   req.ai_socktype = SOCK_STREAM;
143   req.ai_flags = AI_CANONNAME;
144   
145   if (getaddrinfo(addr, NULL, &req, &ai))
146     return FALSE;
147   if (name_len < strlen(ai->ai_canonname))
148     return FALSE;
149   memset(name, 0, name_len);
150   strncpy(name, ai->ai_canonname, strlen(ai->ai_canonname));
151
152   freeaddrinfo(ai);
153 #else
154   struct hostent *hp;
155
156   hp = gethostbyaddr(addr, strlen(addr), AF_INET);
157   if (!hp)
158     return FALSE;
159   if (name_len < strlen(hp->h_name))
160     return FALSE;
161   memset(name, 0, name_len);
162   strncpy(name, hp->h_name, strlen(hp->h_name));
163 #endif
164   
165   return TRUE;
166 }
167
168 /* Performs lookups for remote name and IP address. This peforms reverse
169    lookup as well to verify that the IP has FQDN. */
170
171 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
172 {
173   struct sockaddr_in remote;
174   struct hostent *dest;
175   char *host_ip = NULL;
176   char host_name[1024];
177   int rval, len;
178   int i;
179
180   *hostname = NULL;
181   *ip = NULL;
182
183   SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
184
185   memset(&remote, 0, sizeof(remote));
186   len = sizeof(remote);
187   rval = getpeername(sock, (struct sockaddr *)&remote, &len);
188   if (rval < 0)
189     return FALSE;
190
191   host_ip = inet_ntoa(remote.sin_addr);
192   if (!host_ip)
193     return FALSE;
194
195   *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
196   memcpy(*ip, host_ip, strlen(host_ip));
197
198   /* Get host by address */
199   dest = gethostbyaddr((char *)&remote.sin_addr, 
200                        sizeof(struct in_addr), AF_INET);
201   if (!dest)
202     return FALSE;
203
204   /* Get same host by name to see that the remote host really is
205      the who it says it is */
206   memset(host_name, 0, sizeof(host_name));
207   memcpy(host_name, dest->h_name, strlen(dest->h_name));
208
209   *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
210   memcpy(*hostname, host_name, strlen(host_name));
211   SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
212
213   dest = gethostbyname(host_name);
214   if (!dest)
215     return FALSE;
216
217   /* Find the address from list */
218   for (i = 0; dest->h_addr_list[i]; i++)
219     if (!memcmp(dest->h_addr_list[i], &remote.sin_addr, 
220                 sizeof(struct in_addr)))
221       break;
222   if (!dest->h_addr_list[i])
223     return FALSE;
224
225   silc_free(*ip);
226   *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
227   memcpy(*ip, host_ip, strlen(host_ip));
228   SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
229
230   return TRUE;
231 }
232
233 /* Performs lookups for local name and IP address. This peforms reverse
234    lookup as well to verify that the IP has FQDN. */
235
236 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip)
237 {
238   struct sockaddr_in local;
239   struct hostent *dest;
240   char *host_ip = NULL;
241   char host_name[1024];
242   int rval, len;
243   int i;
244
245   *hostname = NULL;
246   *ip = NULL;
247
248   SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
249
250   memset(&local, 0, sizeof(local));
251   len = sizeof(local);
252   rval = getsockname(sock, (struct sockaddr *)&local, &len);
253   if (rval < 0)
254     return FALSE;
255
256   host_ip = inet_ntoa(local.sin_addr);
257   if (!host_ip)
258     return FALSE;
259
260   *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
261   memcpy(*ip, host_ip, strlen(host_ip));
262
263   /* Get host by address */
264   dest = gethostbyaddr((char *)&local.sin_addr, 
265                        sizeof(struct in_addr), AF_INET);
266   if (!dest)
267     return FALSE;
268
269   /* Get same host by name to see that the local host really is
270      the who it says it is */
271   memset(host_name, 0, sizeof(host_name));
272   memcpy(host_name, dest->h_name, strlen(dest->h_name));
273
274   *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
275   memcpy(*hostname, host_name, strlen(host_name));
276   SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
277
278   dest = gethostbyname(host_name);
279   if (!dest)
280     return FALSE;
281
282   /* Find the address from list */
283   for (i = 0; dest->h_addr_list[i]; i++)
284     if (!memcmp(dest->h_addr_list[i], &local.sin_addr, 
285                sizeof(struct in_addr)))
286       break;
287   if (!dest->h_addr_list[i])
288     return FALSE;
289
290   silc_free(*ip);
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   return TRUE;
296 }
297
298 /* Return remote port by socket. */
299
300 uint16 silc_net_get_remote_port(int sock)
301 {
302   struct sockaddr_in remote;
303   int len;
304
305   memset(&remote, 0, sizeof(remote));
306   len = sizeof(remote);
307   if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
308     return 0;
309
310   return ntohs(remote.sin_port);
311 }
312
313 /* Return local port by socket. */
314
315 uint16 silc_net_get_local_port(int sock)
316 {
317   struct sockaddr_in local;
318   int len;
319
320   memset(&local, 0, sizeof(local));
321   len = sizeof(local);
322   if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
323     return 0;
324
325   return ntohs(local.sin_port);
326 }
327
328 /* Return name of localhost. */
329
330 char *silc_net_localhost(void)
331 {
332   char hostname[256], ip_addr[64];
333
334   if (gethostname(hostname, sizeof(hostname)))
335     return NULL;
336
337   if (!silc_net_gethostbyname(hostname, ip_addr, sizeof(ip_addr)))
338     return strdup(hostname);
339
340   silc_net_gethostbyaddr(ip_addr, hostname, sizeof(hostname));
341   return strdup(hostname);
342 }
343
344 /* Returns local IP address */
345
346 char *silc_net_localip(void)
347 {
348   char hostname[256], ip_addr[64];
349
350   if (gethostname(hostname, sizeof(hostname)))
351     return NULL;
352
353   if (!silc_net_gethostbyname(hostname, ip_addr, sizeof(ip_addr)))
354     return NULL;
355
356   return strdup(ip_addr);
357 }