Merged silc_1_0_branch to trunk.
[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; version 2 of the License.
12   
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19 /* $Id$ */
20
21 #include "silcincludes.h"
22 #include "silcnet.h"
23
24 /* Accepts a connection from a particular socket */
25
26 int silc_net_accept_connection(int sock)
27 {
28   return accept(sock, 0, 0);
29 }
30
31 /* Sets a option for a socket. */
32
33 int silc_net_set_socket_opt(int sock, int level, int option, int on)
34 {
35   return setsockopt(sock, level, option, (void *)&on, sizeof(on));
36 }
37
38 /* Get socket options */
39
40 int silc_net_get_socket_opt(int sock, int level, int option, 
41                             void *optval, int *opt_len)
42 {
43   return getsockopt(sock, level, option, optval, opt_len);
44 }
45
46 /* Checks whether IP address sent as argument is valid IPv4 address. */
47
48 bool silc_net_is_ip4(const char *addr)
49 {
50   int count = 0;
51
52   while (*addr) {
53     if (*addr != '.' && !isdigit(*addr))
54       return FALSE;
55     if (*addr == '.')
56       count++;
57     addr++;
58   }
59
60   if (count != 3)
61     return FALSE;
62   
63   return TRUE;
64 }
65
66 /* Checks whether IP address sent as argument is valid IPv6 address. */
67
68 bool silc_net_is_ip6(const char *addr)
69 {
70   /* XXX does this work with all kinds of IPv6 addresses? */
71   while (*addr) {
72     if (*addr != ':' && !isxdigit(*addr))
73       return FALSE;
74     addr++;
75   }
76   
77   return TRUE;
78 }
79
80 /* Checks whether IP address sent as argument is valid IP address. */
81
82 bool silc_net_is_ip(const char *addr)
83 {
84   if (silc_net_is_ip4(addr))
85     return TRUE;
86   return silc_net_is_ip6(addr);
87 }
88
89 /* Internal context for async resolving */
90 typedef struct {
91   SilcNetResolveCallback completion;
92   void *context;
93   bool prefer_ipv6;
94   SilcSchedule schedule;
95   char *input;
96   char *result;
97 } *SilcNetResolveContext;
98
99 SILC_TASK_CALLBACK(silc_net_resolve_completion)
100 {
101   SilcNetResolveContext r = (SilcNetResolveContext)context;
102
103   /* Call the completion callback */
104   if (r->completion)
105     (*r->completion)(r->result, r->context);
106
107   silc_free(r->input);
108   silc_free(r->result);
109   silc_free(r);
110 }
111
112 /* Thread function to resolve the address for hostname. */
113
114 static void *silc_net_gethostbyname_thread(void *context)
115 {
116   SilcNetResolveContext r = (SilcNetResolveContext)context;
117   SilcSchedule schedule = r->schedule;
118   char tmp[64];
119
120   if (silc_net_gethostbyname(r->input, r->prefer_ipv6, tmp, sizeof(tmp)))
121     r->result = strdup(tmp);
122
123   silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1,
124                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
125   silc_schedule_wakeup(schedule);
126   return NULL;
127 }
128
129 /* Thread function to resolve the hostname for address. */
130
131 static void *silc_net_gethostbyaddr_thread(void *context)
132 {
133   SilcNetResolveContext r = (SilcNetResolveContext)context;
134   SilcSchedule schedule = r->schedule;
135   char tmp[256];
136
137   if (silc_net_gethostbyaddr(r->input, tmp, sizeof(tmp)))
138     r->result = strdup(tmp);
139
140   silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1,
141                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
142   silc_schedule_wakeup(schedule);
143   return NULL;
144 }
145
146 /* Resolves IP address for hostname. */
147
148 bool silc_net_gethostbyname(const char *name, bool prefer_ipv6, char *address, 
149                             SilcUInt32 address_len)
150 {
151 #ifdef HAVE_IPV6
152   struct addrinfo hints, *ai, *tmp, *ip4 = NULL, *ip6 = NULL;
153
154   memset(&hints, 0, sizeof(hints));
155   hints.ai_socktype = SOCK_STREAM;
156   if (getaddrinfo(name, NULL, &hints, &ai))
157     return FALSE;
158
159   for (tmp = ai; tmp; tmp = tmp->ai_next) {
160     if (tmp->ai_family == AF_INET6) {
161       ip6 = tmp;
162       if (ip4)
163         break;
164       continue;
165     }
166     if (tmp->ai_family == AF_INET) {
167       ip4 = tmp;
168       if (ip6)
169         break;
170       continue;
171     }
172   }
173
174   tmp = (prefer_ipv6 ? (ip6 ? ip6 : ip4) : (ip4 ? ip4 : ip6));
175   if (!tmp) {
176     freeaddrinfo(ai);
177     return FALSE;
178   }
179
180   if (getnameinfo(tmp->ai_addr, tmp->ai_addrlen, address,
181                   address_len, NULL, 0, NI_NUMERICHOST)) {
182     freeaddrinfo(ai);
183     return FALSE;
184   }
185
186   freeaddrinfo(ai);
187 #else
188   struct hostent *hp;
189   struct in_addr ip;
190   char *tmp;
191
192   hp = gethostbyname(name);
193   if (!hp)
194     return FALSE;
195
196   memcpy(&ip.s_addr, hp->h_addr_list[0], 4);
197   tmp = inet_ntoa(ip);
198   if (!tmp)
199     return FALSE;
200   if (address_len < strlen(tmp))
201     return FALSE;
202   memset(address, 0, address_len);
203   strncpy(address, tmp, strlen(tmp));
204 #endif
205   
206   return TRUE;
207 }
208
209 /* Resolves IP address for hostname async. */
210
211 void silc_net_gethostbyname_async(const char *name, 
212                                   bool prefer_ipv6,
213                                   SilcSchedule schedule,
214                                   SilcNetResolveCallback completion,
215                                   void *context)
216 {
217   SilcNetResolveContext r = silc_calloc(1, sizeof(*r));
218
219   r->completion = completion;
220   r->context = context;
221   r->prefer_ipv6 = prefer_ipv6;
222   r->schedule = schedule;
223   r->input = strdup(name);
224
225   silc_thread_create(silc_net_gethostbyname_thread, r, FALSE);
226 }
227
228 /* Resolves hostname by IP address. */
229
230 bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len)
231 {
232 #ifdef HAVE_IPV6
233   struct addrinfo req, *ai;
234   
235   memset(&req, 0, sizeof(req));
236   req.ai_socktype = SOCK_STREAM;
237   req.ai_flags = AI_CANONNAME;
238   
239   if (getaddrinfo(addr, NULL, &req, &ai))
240     return FALSE;
241   if (getnameinfo(ai->ai_addr, ai->ai_addrlen, name, name_len, NULL, 0, 0)) {
242     freeaddrinfo(ai);
243     return FALSE;
244   }
245   freeaddrinfo(ai);
246 #else
247   struct hostent *hp;
248   unsigned char a[16];
249
250   if (!silc_net_addr2bin(addr, a, sizeof(a)))
251     return FALSE;
252
253   hp = gethostbyaddr(a, 4, AF_INET);
254   if (!hp)
255     return FALSE;
256   if (name_len < strlen(hp->h_name))
257     return FALSE;
258   memset(name, 0, name_len);
259   strncpy(name, hp->h_name, strlen(hp->h_name));
260 #endif
261   
262   return TRUE;
263 }
264
265 /* Resolves hostname by IP address async. */
266
267 void silc_net_gethostbyaddr_async(const char *addr, 
268                                   SilcSchedule schedule,
269                                   SilcNetResolveCallback completion,
270                                   void *context)
271 {
272   SilcNetResolveContext r = silc_calloc(1, sizeof(*r));
273
274   r->completion = completion;
275   r->context = context;
276   r->schedule = schedule;
277   r->input = strdup(addr);
278
279   silc_thread_create(silc_net_gethostbyaddr_thread, r, FALSE);
280 }
281
282 /* Performs lookups for remote name and IP address. This peforms reverse
283    lookup as well to verify that the IP has FQDN. */
284
285 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
286 {
287   char host[1024];
288   int rval, len;
289
290 #ifdef HAVE_IPV6
291   struct sockaddr_storage remote;
292   char s[NI_MAXHOST];
293
294   if (hostname)
295     *hostname = NULL;
296   *ip = NULL;
297
298   SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
299
300   memset(&remote, 0, sizeof(remote));
301   memset(&s, 0, sizeof(s));
302   len = sizeof(remote);
303   rval = getpeername(sock, (struct sockaddr *)&remote, &len);
304   if (rval < 0)
305     return FALSE;
306
307   if (getnameinfo((struct sockaddr *)&remote, len, s, sizeof(s), NULL, 0,
308                   NI_NUMERICHOST))
309     return FALSE;
310
311   *ip = silc_memdup(s, strlen(s));
312   if (*ip == NULL)
313     return FALSE;
314 #else
315   struct sockaddr_in remote;
316   char *host_ip;
317
318   if (hostname)
319     *hostname = NULL;
320   *ip = NULL;
321
322   SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
323
324   memset(&remote, 0, sizeof(remote));
325   len = sizeof(remote);
326   rval = getpeername(sock, (struct sockaddr *)&remote, &len);
327   if (rval < 0)
328     return FALSE;
329
330   host_ip = inet_ntoa(remote.sin_addr);
331   if (!host_ip)
332     return FALSE;
333
334   *ip = silc_memdup(host_ip, strlen(host_ip));
335   if (*ip == NULL)
336     return FALSE;
337 #endif
338
339   /* Do reverse lookup if we want hostname too. */
340   if (hostname) {
341     /* Get host by address */
342     if (!silc_net_gethostbyaddr(*ip, host, sizeof(host)))
343       return FALSE;
344
345     *hostname = silc_memdup(host, strlen(host));
346     SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
347
348     /* Reverse */
349     if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host)))
350       return FALSE;
351
352     if (strcmp(*ip, host))
353       return FALSE;
354   }
355
356   SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
357   return TRUE;
358 }
359
360 /* Performs lookups for local name and IP address. This peforms reverse
361    lookup as well to verify that the IP has FQDN. */
362
363 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip)
364 {
365   char host[1024];
366   int rval, len;
367
368 #ifdef HAVE_IPV6
369   struct sockaddr_storage local;
370   char s[NI_MAXHOST];
371
372   if (hostname)
373     *hostname = NULL;
374   *ip = NULL;
375
376   SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
377
378   memset(&local, 0, sizeof(local));
379   memset(&s, 0, sizeof(s));
380   len = sizeof(local);
381   rval = getsockname(sock, (struct sockaddr *)&local, &len);
382   if (rval < 0)
383     return FALSE;
384
385   if (getnameinfo((struct sockaddr *)&local, len, s, sizeof(s), NULL, 0,
386                   NI_NUMERICHOST))
387     return FALSE;
388
389   *ip = silc_memdup(s, strlen(s));
390   if (*ip == NULL)
391     return FALSE;
392 #else
393   struct sockaddr_in local;
394   char *host_ip;
395
396   if (hostname)
397     *hostname = NULL;
398   *ip = NULL;
399
400   SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
401
402   memset(&local, 0, sizeof(local));
403   len = sizeof(local);
404   rval = getsockname(sock, (struct sockaddr *)&local, &len);
405   if (rval < 0)
406     return FALSE;
407
408   host_ip = inet_ntoa(local.sin_addr);
409   if (!host_ip)
410     return FALSE;
411
412   *ip = silc_memdup(host_ip, strlen(host_ip));
413   if (*ip == NULL)
414     return FALSE;
415 #endif
416
417   /* Do reverse lookup if we want hostname too. */
418   if (hostname) {
419     /* Get host by address */
420     if (!silc_net_gethostbyaddr(*ip, host, sizeof(host)))
421       return FALSE;
422
423     *hostname = silc_memdup(host, strlen(host));
424     SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
425
426     /* Reverse */
427     if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host)))
428       return FALSE;
429
430     if (strcmp(*ip, host))
431       return FALSE;
432   }
433
434   SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
435   return TRUE;
436 }
437
438 /* Return remote port by socket. */
439
440 SilcUInt16 silc_net_get_remote_port(int sock)
441 {
442 #ifdef HAVE_IPV6
443   struct sockaddr_storage remote;
444   int len;
445   char s[NI_MAXSERV];
446
447   memset(&remote, 0, sizeof(remote));
448   len = sizeof(remote);
449   if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
450     return 0;
451
452   if (getnameinfo((struct sockaddr *)&remote, len, NULL, 0, s, sizeof(s),
453                   NI_NUMERICSERV))
454     return 0;
455   
456   return atoi(s);
457 #else
458   struct sockaddr_in remote;
459   int len;
460
461   memset(&remote, 0, sizeof(remote));
462   len = sizeof(remote);
463   if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
464     return 0;
465
466   return ntohs(remote.sin_port);
467 #endif
468 }
469
470 /* Return local port by socket. */
471
472 SilcUInt16 silc_net_get_local_port(int sock)
473 {
474 #ifdef HAVE_IPV6
475   struct sockaddr_storage local;
476   int len;
477   char s[NI_MAXSERV];
478
479   memset(&local, 0, sizeof(local));
480   len = sizeof(local);
481   if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
482     return 0;
483
484   if (getnameinfo((struct sockaddr *)&local, len, NULL, 0, s, sizeof(s),
485                   NI_NUMERICSERV))
486     return 0;
487   
488   return atoi(s);
489 #else
490   struct sockaddr_in local;
491   int len;
492
493   memset(&local, 0, sizeof(local));
494   len = sizeof(local);
495   if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
496     return 0;
497
498   return ntohs(local.sin_port);
499 #endif
500 }
501
502 /* Return name of localhost. */
503
504 char *silc_net_localhost(void)
505 {
506   char hostname[256], ip_addr[64];
507
508   if (gethostname(hostname, sizeof(hostname)))
509     return NULL;
510
511   if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr)))
512     return strdup(hostname);
513
514   silc_net_gethostbyaddr(ip_addr, hostname, sizeof(hostname));
515   return strdup(hostname);
516 }
517
518 /* Returns local IP address */
519
520 char *silc_net_localip(void)
521 {
522   char hostname[256], ip_addr[64];
523
524   if (gethostname(hostname, sizeof(hostname)))
525     return NULL;
526
527   if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr)))
528     return NULL;
529
530   return strdup(ip_addr);
531 }