IPv6 fixes around network routines.
[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   char host[1024];
285   int rval, len;
286
287 #ifdef HAVE_IPV6
288   struct sockaddr_storage remote;
289   char s[NI_MAXHOST];
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   memset(&s, 0, sizeof(s));
298   len = sizeof(remote);
299   rval = getpeername(sock, (struct sockaddr *)&remote, &len);
300   if (rval < 0)
301     return FALSE;
302
303   if (getnameinfo((struct sockaddr *)&remote, len, s, sizeof(s), NULL, 0,
304                   NI_NUMERICHOST))
305     return FALSE;
306
307   *ip = silc_memdup(s, strlen(s));
308   if (*ip == NULL)
309     return FALSE;
310 #else
311   struct sockaddr_in remote;
312
313   *hostname = NULL;
314   *ip = NULL;
315
316   SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
317
318   memset(&remote, 0, sizeof(remote));
319   len = sizeof(remote);
320   rval = getpeername(sock, (struct sockaddr *)&remote, &len);
321   if (rval < 0)
322     return FALSE;
323
324   host_ip = inet_ntoa(remote.sin_addr);
325   if (!host_ip)
326     return FALSE;
327
328   *ip = silc_memdup(host_ip, strlen(host_ip));
329   if (*ip == NULL)
330     return FALSE;
331 #endif
332
333   /* Get host by address */
334   if (!silc_net_gethostbyaddr(*ip, host, sizeof(host)))
335     return FALSE;
336
337   *hostname = silc_memdup(host, strlen(host));
338   SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
339
340   /* Reverse */
341   if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host)))
342     return FALSE;
343
344   if (strcmp(*ip, host))
345     return FALSE;
346
347   SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
348   return TRUE;
349 }
350
351 /* Performs lookups for local name and IP address. This peforms reverse
352    lookup as well to verify that the IP has FQDN. */
353
354 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip)
355 {
356   char host[1024];
357   int rval, len;
358
359 #ifdef HAVE_IPV6
360   struct sockaddr_storage local;
361   char s[NI_MAXHOST];
362
363   *hostname = NULL;
364   *ip = NULL;
365
366   SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
367
368   memset(&local, 0, sizeof(local));
369   memset(&s, 0, sizeof(s));
370   len = sizeof(local);
371   rval = getsockname(sock, (struct sockaddr *)&local, &len);
372   if (rval < 0)
373     return FALSE;
374
375   if (getnameinfo((struct sockaddr *)&local, len, s, sizeof(s), NULL, 0,
376                   NI_NUMERICHOST))
377     return FALSE;
378
379   *ip = silc_memdup(s, strlen(s));
380   if (*ip == NULL)
381     return FALSE;
382 #else
383   struct sockaddr_in local;
384
385   *hostname = NULL;
386   *ip = NULL;
387
388   SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
389
390   memset(&local, 0, sizeof(local));
391   len = sizeof(local);
392   rval = getsockname(sock, (struct sockaddr *)&local, &len);
393   if (rval < 0)
394     return FALSE;
395
396   host_ip = inet_ntoa(local.sin_addr);
397   if (!host_ip)
398     return FALSE;
399
400   *ip = silc_memdup(host_ip, strlen(host_ip));
401   if (*ip == NULL)
402     return FALSE;
403 #endif
404
405   /* Get host by address */
406   if (!silc_net_gethostbyaddr(*ip, host, sizeof(host)))
407     return FALSE;
408
409   *hostname = silc_memdup(host, strlen(host));
410   SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
411
412   /* Reverse */
413   if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host)))
414     return FALSE;
415
416   if (strcmp(*ip, host))
417     return FALSE;
418
419   SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
420   return TRUE;
421 }
422
423 /* Return remote port by socket. */
424
425 SilcUInt16 silc_net_get_remote_port(int sock)
426 {
427 #ifdef HAVE_IPV6
428   struct sockaddr_storage remote;
429   int len;
430   char s[NI_MAXSERV];
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   if (getnameinfo((struct sockaddr *)&remote, len, NULL, 0, s, sizeof(s),
438                   NI_NUMERICSERV))
439     return 0;
440   
441   return atoi(s);
442 #else
443   struct sockaddr_in remote;
444   int len;
445
446   memset(&remote, 0, sizeof(remote));
447   len = sizeof(remote);
448   if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
449     return 0;
450
451   return ntohs(remote.sin_port);
452 #endif
453 }
454
455 /* Return local port by socket. */
456
457 SilcUInt16 silc_net_get_local_port(int sock)
458 {
459 #ifdef HAVE_IPV6
460   struct sockaddr_storage local;
461   int len;
462   char s[NI_MAXSERV];
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   if (getnameinfo((struct sockaddr *)&local, len, NULL, 0, s, sizeof(s),
470                   NI_NUMERICSERV))
471     return 0;
472   
473   return atoi(s);
474 #else
475   struct sockaddr_in local;
476   int len;
477
478   memset(&local, 0, sizeof(local));
479   len = sizeof(local);
480   if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
481     return 0;
482
483   return ntohs(local.sin_port);
484 #endif
485 }
486
487 /* Return name of localhost. */
488
489 char *silc_net_localhost(void)
490 {
491   char hostname[256], ip_addr[64];
492
493   if (gethostname(hostname, sizeof(hostname)))
494     return NULL;
495
496   if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr)))
497     return strdup(hostname);
498
499   silc_net_gethostbyaddr(ip_addr, hostname, sizeof(hostname));
500   return strdup(hostname);
501 }
502
503 /* Returns local IP address */
504
505 char *silc_net_localip(void)
506 {
507   char hostname[256], ip_addr[64];
508
509   if (gethostname(hostname, sizeof(hostname)))
510     return NULL;
511
512   if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr)))
513     return NULL;
514
515   return strdup(ip_addr);
516 }