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 /* Internal context for async resolving */
91 typedef struct {
92   SilcNetResolveCallback completion;
93   void *context;
94   bool prefer_ipv6;
95   SilcSchedule schedule;
96   char *input;
97   char *result;
98 } *SilcNetResolveContext;
99
100 SILC_TASK_CALLBACK(silc_net_resolve_completion)
101 {
102   SilcNetResolveContext r = (SilcNetResolveContext)context;
103
104   /* Call the completion callback */
105   if (r->completion)
106     (*r->completion)(r->result, r->context);
107
108   silc_free(r->input);
109   silc_free(r->result);
110   silc_free(r);
111 }
112
113 /* Thread function to resolve the address for hostname. */
114
115 static void *silc_net_gethostbyname_thread(void *context)
116 {
117   SilcNetResolveContext r = (SilcNetResolveContext)context;
118   SilcSchedule schedule = r->schedule;
119   char tmp[64];
120
121   if (silc_net_gethostbyname(r->input, r->prefer_ipv6, tmp, sizeof(tmp)))
122     r->result = strdup(tmp);
123
124   silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1,
125                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
126   silc_schedule_wakeup(schedule);
127   return NULL;
128 }
129
130 /* Thread function to resolve the hostname for address. */
131
132 static void *silc_net_gethostbyaddr_thread(void *context)
133 {
134   SilcNetResolveContext r = (SilcNetResolveContext)context;
135   SilcSchedule schedule = r->schedule;
136   char tmp[256];
137
138   if (silc_net_gethostbyaddr(r->input, tmp, sizeof(tmp)))
139     r->result = strdup(tmp);
140
141   silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1,
142                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
143   silc_schedule_wakeup(schedule);
144   return NULL;
145 }
146
147 /* Resolves IP address for hostname. */
148
149 bool silc_net_gethostbyname(const char *name, bool prefer_ipv6, char *address, 
150                             SilcUInt32 address_len)
151 {
152 #ifdef HAVE_IPV6
153   struct addrinfo hints, *ai, *tmp, *ip4 = NULL, *ip6 = NULL;
154
155   memset(&hints, 0, sizeof(hints));
156   hints.ai_socktype = SOCK_STREAM;
157   if (getaddrinfo(name, NULL, &hints, &ai))
158     return FALSE;
159
160   for (tmp = ai; tmp; tmp = tmp->ai_next) {
161     if (tmp->ai_family == AF_INET6) {
162       ip6 = tmp;
163       if (ip4)
164         break;
165       continue;
166     }
167     if (tmp->ai_family == AF_INET) {
168       ip4 = tmp;
169       if (ip6)
170         break;
171       continue;
172     }
173   }
174
175   tmp = (prefer_ipv6 ? (ip6 ? ip6 : ip4) : (ip4 ? ip4 : ip6));
176   if (!tmp) {
177     freeaddrinfo(ai);
178     return FALSE;
179   }
180
181   if (getnameinfo(tmp->ai_addr, tmp->ai_addrlen, address,
182                   address_len, NULL, 0, NI_NUMERICHOST)) {
183     freeaddrinfo(ai);
184     return FALSE;
185   }
186
187   freeaddrinfo(ai);
188 #else
189   struct hostent *hp;
190   struct in_addr ip;
191   char *tmp;
192
193   hp = gethostbyname(name);
194   if (!hp)
195     return FALSE;
196
197   memcpy(&ip.s_addr, hp->h_addr_list[0], 4);
198   tmp = inet_ntoa(ip);
199   if (!tmp)
200     return FALSE;
201   if (address_len < strlen(tmp))
202     return FALSE;
203   memset(address, 0, address_len);
204   strncpy(address, tmp, strlen(tmp));
205 #endif
206   
207   return TRUE;
208 }
209
210 /* Resolves IP address for hostname async. */
211
212 void silc_net_gethostbyname_async(const char *name, 
213                                   bool prefer_ipv6,
214                                   SilcSchedule schedule,
215                                   SilcNetResolveCallback completion,
216                                   void *context)
217 {
218   SilcNetResolveContext r = silc_calloc(1, sizeof(*r));
219
220   r->completion = completion;
221   r->context = context;
222   r->prefer_ipv6 = prefer_ipv6;
223   r->schedule = schedule;
224   r->input = strdup(name);
225
226   silc_thread_create(silc_net_gethostbyname_thread, r, FALSE);
227 }
228
229 /* Resolves hostname by IP address. */
230
231 bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len)
232 {
233 #ifdef HAVE_IPV6
234   struct addrinfo req, *ai;
235   
236   memset(&req, 0, sizeof(req));
237   req.ai_socktype = SOCK_STREAM;
238   req.ai_flags = AI_CANONNAME;
239   
240   if (getaddrinfo(addr, NULL, &req, &ai))
241     return FALSE;
242   if (getnameinfo(ai->ai_addr, ai->ai_addrlen, name, name_len, NULL, 0, 0)) {
243     freeaddrinfo(ai);
244     return FALSE;
245   }
246   freeaddrinfo(ai);
247 #else
248   struct hostent *hp;
249
250   hp = gethostbyaddr(addr, strlen(addr), AF_INET);
251   if (!hp)
252     return FALSE;
253   if (name_len < strlen(hp->h_name))
254     return FALSE;
255   memset(name, 0, name_len);
256   strncpy(name, hp->h_name, strlen(hp->h_name));
257 #endif
258   
259   return TRUE;
260 }
261
262 /* Resolves hostname by IP address async. */
263
264 void silc_net_gethostbyaddr_async(const char *addr, 
265                                   SilcSchedule schedule,
266                                   SilcNetResolveCallback completion,
267                                   void *context)
268 {
269   SilcNetResolveContext r = silc_calloc(1, sizeof(*r));
270
271   r->completion = completion;
272   r->context = context;
273   r->schedule = schedule;
274   r->input = strdup(addr);
275
276   silc_thread_create(silc_net_gethostbyaddr_thread, r, FALSE);
277 }
278
279 /* Performs lookups for remote name and IP address. This peforms reverse
280    lookup as well to verify that the IP has FQDN. */
281
282 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
283 {
284   struct sockaddr_in remote;
285   struct hostent *dest;
286   char *host_ip = NULL;
287   char host_name[1024];
288   int rval, len;
289   int i;
290
291   *hostname = NULL;
292   *ip = NULL;
293
294   SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
295
296   memset(&remote, 0, sizeof(remote));
297   len = sizeof(remote);
298   rval = getpeername(sock, (struct sockaddr *)&remote, &len);
299   if (rval < 0)
300     return FALSE;
301
302   host_ip = inet_ntoa(remote.sin_addr);
303   if (!host_ip)
304     return FALSE;
305
306   *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
307   memcpy(*ip, host_ip, strlen(host_ip));
308
309   /* Get host by address */
310   dest = gethostbyaddr((char *)&remote.sin_addr, 
311                        sizeof(struct in_addr), AF_INET);
312   if (!dest)
313     return FALSE;
314
315   /* Get same host by name to see that the remote host really is
316      the who it says it is */
317   memset(host_name, 0, sizeof(host_name));
318   memcpy(host_name, dest->h_name, strlen(dest->h_name));
319
320   *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
321   memcpy(*hostname, host_name, strlen(host_name));
322   SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
323
324   dest = gethostbyname(host_name);
325   if (!dest)
326     return FALSE;
327
328   /* Find the address from list */
329   for (i = 0; dest->h_addr_list[i]; i++)
330     if (!memcmp(dest->h_addr_list[i], &remote.sin_addr, 
331                 sizeof(struct in_addr)))
332       break;
333   if (!dest->h_addr_list[i])
334     return FALSE;
335
336   silc_free(*ip);
337   *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
338   memcpy(*ip, host_ip, strlen(host_ip));
339   SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
340
341   return TRUE;
342 }
343
344 /* Performs lookups for local name and IP address. This peforms reverse
345    lookup as well to verify that the IP has FQDN. */
346
347 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip)
348 {
349   struct sockaddr_in local;
350   struct hostent *dest;
351   char *host_ip = NULL;
352   char host_name[1024];
353   int rval, len;
354   int i;
355
356   *hostname = NULL;
357   *ip = NULL;
358
359   SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
360
361   memset(&local, 0, sizeof(local));
362   len = sizeof(local);
363   rval = getsockname(sock, (struct sockaddr *)&local, &len);
364   if (rval < 0)
365     return FALSE;
366
367   host_ip = inet_ntoa(local.sin_addr);
368   if (!host_ip)
369     return FALSE;
370
371   *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
372   memcpy(*ip, host_ip, strlen(host_ip));
373
374   /* Get host by address */
375   dest = gethostbyaddr((char *)&local.sin_addr, 
376                        sizeof(struct in_addr), AF_INET);
377   if (!dest)
378     return FALSE;
379
380   /* Get same host by name to see that the local host really is
381      the who it says it is */
382   memset(host_name, 0, sizeof(host_name));
383   memcpy(host_name, dest->h_name, strlen(dest->h_name));
384
385   *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
386   memcpy(*hostname, host_name, strlen(host_name));
387   SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
388
389   dest = gethostbyname(host_name);
390   if (!dest)
391     return FALSE;
392
393   /* Find the address from list */
394   for (i = 0; dest->h_addr_list[i]; i++)
395     if (!memcmp(dest->h_addr_list[i], &local.sin_addr, 
396                sizeof(struct in_addr)))
397       break;
398   if (!dest->h_addr_list[i])
399     return FALSE;
400
401   silc_free(*ip);
402   *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
403   memcpy(*ip, host_ip, strlen(host_ip));
404   SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
405
406   return TRUE;
407 }
408
409 /* Return remote port by socket. */
410
411 SilcUInt16 silc_net_get_remote_port(int sock)
412 {
413 #ifdef HAVE_IPV6
414   struct sockaddr_storage remote;
415   int len;
416   char s[NI_MAXSERV];
417
418   memset(&remote, 0, sizeof(remote));
419   len = sizeof(remote);
420   if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
421     return 0;
422
423   if (getnameinfo((struct sockaddr *)&remote, len, NULL, 0, s, sizeof(s),
424       NI_NUMERICSERV))
425     return 0;
426   
427   return atoi(s);
428 #else
429   struct sockaddr_in remote;
430   int len;
431
432   memset(&remote, 0, sizeof(remote));
433   len = sizeof(remote);
434   if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
435     return 0;
436
437   return ntohs(remote.sin_port);
438 #endif
439 }
440
441 /* Return local port by socket. */
442
443 SilcUInt16 silc_net_get_local_port(int sock)
444 {
445 #ifdef HAVE_IPV6
446   struct sockaddr_storage local;
447   int len;
448   char s[NI_MAXSERV];
449
450   memset(&local, 0, sizeof(local));
451   len = sizeof(local);
452   if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
453     return 0;
454
455   if (getnameinfo((struct sockaddr *)&local, len, NULL, 0, s, sizeof(s),
456       NI_NUMERICSERV))
457     return 0;
458   
459   return atoi(s);
460 #else
461   struct sockaddr_in local;
462   int len;
463
464   memset(&local, 0, sizeof(local));
465   len = sizeof(local);
466   if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
467     return 0;
468
469   return ntohs(local.sin_port);
470 #endif
471 }
472
473 /* Return name of localhost. */
474
475 char *silc_net_localhost(void)
476 {
477   char hostname[256], ip_addr[64];
478
479   if (gethostname(hostname, sizeof(hostname)))
480     return NULL;
481
482   if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr)))
483     return strdup(hostname);
484
485   silc_net_gethostbyaddr(ip_addr, hostname, sizeof(hostname));
486   return strdup(hostname);
487 }
488
489 /* Returns local IP address */
490
491 char *silc_net_localip(void)
492 {
493   char hostname[256], ip_addr[64];
494
495   if (gethostname(hostname, sizeof(hostname)))
496     return NULL;
497
498   if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr)))
499     return NULL;
500
501   return strdup(ip_addr);
502 }