Added SILC Server library.
[silc.git] / lib / silcutil / silcnet.c
1 /*
2
3   silcnet.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2005 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 "silc.h"
22
23 /* Accepts a connection from a particular socket */
24
25 int silc_net_accept_connection(int sock)
26 {
27   return accept(sock, 0, 0);
28 }
29
30 /* Sets a option for a socket. */
31
32 int silc_net_set_socket_opt(int sock, int level, int option, int on)
33 {
34   return setsockopt(sock, level, option, (void *)&on, sizeof(on));
35 }
36
37 /* Get socket options */
38
39 int silc_net_get_socket_opt(int sock, int level, int option,
40                             void *optval, int *opt_len)
41 {
42   return getsockopt(sock, level, option, optval, opt_len);
43 }
44
45 /* Checks whether IP address sent as argument is valid IPv4 address. */
46
47 SilcBool silc_net_is_ip4(const char *addr)
48 {
49   int count = 0;
50
51   while (*addr) {
52     if (*addr != '.' && !isdigit(*addr))
53       return FALSE;
54     if (*addr == '.')
55       count++;
56     addr++;
57   }
58
59   if (count != 3)
60     return FALSE;
61
62   return TRUE;
63 }
64
65 /* Checks whether IP address sent as argument is valid IPv6 address. */
66
67 SilcBool silc_net_is_ip6(const char *addr)
68 {
69   /* XXX does this work with all kinds of IPv6 addresses? */
70   while (*addr) {
71     if (*addr != ':' && !isxdigit(*addr))
72       return FALSE;
73     addr++;
74   }
75
76   return TRUE;
77 }
78
79 /* Checks whether IP address sent as argument is valid IP address. */
80
81 SilcBool silc_net_is_ip(const char *addr)
82 {
83   if (silc_net_is_ip4(addr))
84     return TRUE;
85   return silc_net_is_ip6(addr);
86 }
87
88 /* Internal context for async resolving */
89 typedef struct {
90   SilcNetResolveCallback completion;
91   void *context;
92   SilcBool prefer_ipv6;
93   SilcSchedule schedule;
94   char *input;
95   char *result;
96 } *SilcNetResolveContext;
97
98 SILC_TASK_CALLBACK(silc_net_resolve_completion)
99 {
100   SilcNetResolveContext r = (SilcNetResolveContext)context;
101
102   /* Call the completion callback */
103   if (r->completion)
104     (*r->completion)(r->result, r->context);
105
106   silc_free(r->input);
107   silc_free(r->result);
108   silc_free(r);
109 }
110
111 /* Thread function to resolve the address for hostname. */
112
113 static void *silc_net_gethostbyname_thread(void *context)
114 {
115   SilcNetResolveContext r = (SilcNetResolveContext)context;
116   SilcSchedule schedule = r->schedule;
117   char tmp[64];
118
119   if (silc_net_gethostbyname(r->input, r->prefer_ipv6, tmp, sizeof(tmp)))
120     r->result = strdup(tmp);
121
122   silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1,
123                          SILC_TASK_TIMEOUT);
124   silc_schedule_wakeup(schedule);
125   return NULL;
126 }
127
128 /* Thread function to resolve the hostname for address. */
129
130 static void *silc_net_gethostbyaddr_thread(void *context)
131 {
132   SilcNetResolveContext r = (SilcNetResolveContext)context;
133   SilcSchedule schedule = r->schedule;
134   char tmp[256];
135
136   if (silc_net_gethostbyaddr(r->input, tmp, sizeof(tmp)))
137     r->result = strdup(tmp);
138
139   silc_schedule_task_add(schedule, 0, silc_net_resolve_completion, r, 0, 1,
140                          SILC_TASK_TIMEOUT);
141   silc_schedule_wakeup(schedule);
142   return NULL;
143 }
144
145 /* Resolves IP address for hostname. */
146
147 SilcBool silc_net_gethostbyname(const char *name,
148                                 SilcBool 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                                   SilcBool 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 SilcBool 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 SilcBool 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 SilcBool 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 }