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; 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   unsigned char a[16];
250
251   if (!silc_net_addr2bin(addr, a, sizeof(a)))
252     return FALSE;
253
254   hp = gethostbyaddr(a, 4, AF_INET);
255   if (!hp)
256     return FALSE;
257   if (name_len < strlen(hp->h_name))
258     return FALSE;
259   memset(name, 0, name_len);
260   strncpy(name, hp->h_name, strlen(hp->h_name));
261 #endif
262   
263   return TRUE;
264 }
265
266 /* Resolves hostname by IP address async. */
267
268 void silc_net_gethostbyaddr_async(const char *addr, 
269                                   SilcSchedule schedule,
270                                   SilcNetResolveCallback completion,
271                                   void *context)
272 {
273   SilcNetResolveContext r = silc_calloc(1, sizeof(*r));
274
275   r->completion = completion;
276   r->context = context;
277   r->schedule = schedule;
278   r->input = strdup(addr);
279
280   silc_thread_create(silc_net_gethostbyaddr_thread, r, FALSE);
281 }
282
283 /* Performs lookups for remote name and IP address. This peforms reverse
284    lookup as well to verify that the IP has FQDN. */
285
286 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
287 {
288   char host[1024];
289   int rval, len;
290
291 #ifdef HAVE_IPV6
292   struct sockaddr_storage remote;
293   char s[NI_MAXHOST];
294
295   if (hostname)
296     *hostname = NULL;
297   *ip = NULL;
298
299   SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
300
301   memset(&remote, 0, sizeof(remote));
302   memset(&s, 0, sizeof(s));
303   len = sizeof(remote);
304   rval = getpeername(sock, (struct sockaddr *)&remote, &len);
305   if (rval < 0)
306     return FALSE;
307
308   if (getnameinfo((struct sockaddr *)&remote, len, s, sizeof(s), NULL, 0,
309                   NI_NUMERICHOST))
310     return FALSE;
311
312   *ip = silc_memdup(s, strlen(s));
313   if (*ip == NULL)
314     return FALSE;
315 #else
316   struct sockaddr_in remote;
317   char *host_ip;
318
319   if (hostname)
320     *hostname = NULL;
321   *ip = NULL;
322
323   SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
324
325   memset(&remote, 0, sizeof(remote));
326   len = sizeof(remote);
327   rval = getpeername(sock, (struct sockaddr *)&remote, &len);
328   if (rval < 0)
329     return FALSE;
330
331   host_ip = inet_ntoa(remote.sin_addr);
332   if (!host_ip)
333     return FALSE;
334
335   *ip = silc_memdup(host_ip, strlen(host_ip));
336   if (*ip == NULL)
337     return FALSE;
338 #endif
339
340   /* Do reverse lookup if we want hostname too. */
341   if (hostname) {
342     /* Get host by address */
343     if (!silc_net_gethostbyaddr(*ip, host, sizeof(host)))
344       return FALSE;
345
346     *hostname = silc_memdup(host, strlen(host));
347     SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
348
349     /* Reverse */
350     if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host)))
351       return FALSE;
352
353     if (strcmp(*ip, host))
354       return FALSE;
355   }
356
357   SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
358   return TRUE;
359 }
360
361 /* Performs lookups for local name and IP address. This peforms reverse
362    lookup as well to verify that the IP has FQDN. */
363
364 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip)
365 {
366   char host[1024];
367   int rval, len;
368
369 #ifdef HAVE_IPV6
370   struct sockaddr_storage local;
371   char s[NI_MAXHOST];
372
373   if (hostname)
374     *hostname = NULL;
375   *ip = NULL;
376
377   SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
378
379   memset(&local, 0, sizeof(local));
380   memset(&s, 0, sizeof(s));
381   len = sizeof(local);
382   rval = getsockname(sock, (struct sockaddr *)&local, &len);
383   if (rval < 0)
384     return FALSE;
385
386   if (getnameinfo((struct sockaddr *)&local, len, s, sizeof(s), NULL, 0,
387                   NI_NUMERICHOST))
388     return FALSE;
389
390   *ip = silc_memdup(s, strlen(s));
391   if (*ip == NULL)
392     return FALSE;
393 #else
394   struct sockaddr_in local;
395   char *host_ip;
396
397   if (hostname)
398     *hostname = NULL;
399   *ip = NULL;
400
401   SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
402
403   memset(&local, 0, sizeof(local));
404   len = sizeof(local);
405   rval = getsockname(sock, (struct sockaddr *)&local, &len);
406   if (rval < 0)
407     return FALSE;
408
409   host_ip = inet_ntoa(local.sin_addr);
410   if (!host_ip)
411     return FALSE;
412
413   *ip = silc_memdup(host_ip, strlen(host_ip));
414   if (*ip == NULL)
415     return FALSE;
416 #endif
417
418   /* Do reverse lookup if we want hostname too. */
419   if (hostname) {
420     /* Get host by address */
421     if (!silc_net_gethostbyaddr(*ip, host, sizeof(host)))
422       return FALSE;
423
424     *hostname = silc_memdup(host, strlen(host));
425     SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
426
427     /* Reverse */
428     if (!silc_net_gethostbyname(*hostname, TRUE, host, sizeof(host)))
429       return FALSE;
430
431     if (strcmp(*ip, host))
432       return FALSE;
433   }
434
435   SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
436   return TRUE;
437 }
438
439 /* Return remote port by socket. */
440
441 SilcUInt16 silc_net_get_remote_port(int sock)
442 {
443 #ifdef HAVE_IPV6
444   struct sockaddr_storage remote;
445   int len;
446   char s[NI_MAXSERV];
447
448   memset(&remote, 0, sizeof(remote));
449   len = sizeof(remote);
450   if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
451     return 0;
452
453   if (getnameinfo((struct sockaddr *)&remote, len, NULL, 0, s, sizeof(s),
454                   NI_NUMERICSERV))
455     return 0;
456   
457   return atoi(s);
458 #else
459   struct sockaddr_in remote;
460   int len;
461
462   memset(&remote, 0, sizeof(remote));
463   len = sizeof(remote);
464   if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
465     return 0;
466
467   return ntohs(remote.sin_port);
468 #endif
469 }
470
471 /* Return local port by socket. */
472
473 SilcUInt16 silc_net_get_local_port(int sock)
474 {
475 #ifdef HAVE_IPV6
476   struct sockaddr_storage local;
477   int len;
478   char s[NI_MAXSERV];
479
480   memset(&local, 0, sizeof(local));
481   len = sizeof(local);
482   if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
483     return 0;
484
485   if (getnameinfo((struct sockaddr *)&local, len, NULL, 0, s, sizeof(s),
486                   NI_NUMERICSERV))
487     return 0;
488   
489   return atoi(s);
490 #else
491   struct sockaddr_in local;
492   int len;
493
494   memset(&local, 0, sizeof(local));
495   len = sizeof(local);
496   if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
497     return 0;
498
499   return ntohs(local.sin_port);
500 #endif
501 }
502
503 /* Return name of localhost. */
504
505 char *silc_net_localhost(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 strdup(hostname);
514
515   silc_net_gethostbyaddr(ip_addr, hostname, sizeof(hostname));
516   return strdup(hostname);
517 }
518
519 /* Returns local IP address */
520
521 char *silc_net_localip(void)
522 {
523   char hostname[256], ip_addr[64];
524
525   if (gethostname(hostname, sizeof(hostname)))
526     return NULL;
527
528   if (!silc_net_gethostbyname(hostname, TRUE, ip_addr, sizeof(ip_addr)))
529     return NULL;
530
531   return strdup(ip_addr);
532 }