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