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