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   SilcSchedule schedule;
95   char *input;
96   char *result;
97 } *SilcNetResolveContext;
98
99 SILC_TASK_CALLBACK(silc_net_resolve_completion)
100 {
101   SilcNetResolveContext r = (SilcNetResolveContext)context;
102
103   /* Call the completion callback */
104   if (r->completion)
105     (*r->completion)(r->result, r->context);
106
107   silc_free(r->input);
108   silc_free(r->result);
109   silc_free(r);
110 }
111
112 /* Thread function to resolve the address for hostname. */
113
114 static void *silc_net_gethostbyname_thread(void *context)
115 {
116   SilcNetResolveContext r = (SilcNetResolveContext)context;
117   char tmp[64];
118
119   if (silc_net_gethostbyname(r->input, tmp, sizeof(tmp)))
120     r->result = strdup(tmp);
121
122   silc_schedule_task_add(r->schedule, 0, silc_net_resolve_completion, r, 0, 1,
123                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
124   silc_schedule_wakeup(r->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   char tmp[256];
134
135   if (silc_net_gethostbyaddr(r->input, tmp, sizeof(tmp)))
136     r->result = strdup(tmp);
137
138   silc_schedule_task_add(r->schedule, 0, silc_net_resolve_completion, r, 0, 1,
139                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
140   silc_schedule_wakeup(r->schedule);
141   return NULL;
142 }
143
144 /* Resolves IP address for hostname. */
145
146 bool silc_net_gethostbyname(const char *name, char *address,
147                             uint32 address_len)
148 {
149 #ifdef HAVE_IPV6
150   struct addrinfo hints, *ai;
151   char hbuf[NI_MAXHOST];
152
153   memset(&hints, 0, sizeof(hints));
154   hints.ai_socktype = SOCK_STREAM;
155   if (getaddrinfo(name, NULL, &hints, &ai))
156     return FALSE;
157
158   if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf,
159                   sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
160     return FALSE;
161
162   if (ai->ai_family == AF_INET) {
163     if (!inet_ntop(ai->ai_family, 
164                    &((struct sockaddr_in *)ai->ai_addr)->sin_addr,
165                    address, address_len))
166       return FALSE;
167   } else {
168     if (!inet_ntop(ai->ai_family, 
169                    &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr,
170                    address, address_len))
171       return FALSE;
172   }
173
174   freeaddrinfo(ai);
175 #else
176   struct hostent *hp;
177   struct in_addr ip;
178   char *tmp;
179
180   hp = gethostbyname(name);
181   if (!hp)
182     return FALSE;
183
184   memcpy(&ip.s_addr, hp->h_addr_list[0], 4);
185   tmp = inet_ntoa(ip);
186   if (!tmp)
187     return FALSE;
188   if (address_len < strlen(tmp))
189     return FALSE;
190   memset(address, 0, address_len);
191   strncpy(address, tmp, strlen(tmp));
192 #endif
193   
194   return TRUE;
195 }
196
197 /* Resolves IP address for hostname async. */
198
199 void silc_net_gethostbyname_async(const char *name, 
200                                   SilcSchedule schedule,
201                                   SilcNetResolveCallback completion,
202                                   void *context)
203 {
204   SilcNetResolveContext r = silc_calloc(1, sizeof(*r));
205
206   r->completion = completion;
207   r->context = context;
208   r->schedule = schedule;
209   r->input = strdup(name);
210
211   silc_thread_create(silc_net_gethostbyname_thread, r, FALSE);
212 }
213
214 /* Resolves hostname by IP address. */
215
216 bool silc_net_gethostbyaddr(const char *addr, char *name, uint32 name_len)
217 {
218 #ifdef HAVE_IPV6
219   struct addrinfo req, *ai;
220   
221   memset(&req, 0, sizeof(req));
222   req.ai_socktype = SOCK_STREAM;
223   req.ai_flags = AI_CANONNAME;
224   
225   if (getaddrinfo(addr, NULL, &req, &ai))
226     return FALSE;
227   if (name_len < strlen(ai->ai_canonname))
228     return FALSE;
229   memset(name, 0, name_len);
230   strncpy(name, ai->ai_canonname, strlen(ai->ai_canonname));
231
232   freeaddrinfo(ai);
233 #else
234   struct hostent *hp;
235
236   hp = gethostbyaddr(addr, strlen(addr), AF_INET);
237   if (!hp)
238     return FALSE;
239   if (name_len < strlen(hp->h_name))
240     return FALSE;
241   memset(name, 0, name_len);
242   strncpy(name, hp->h_name, strlen(hp->h_name));
243 #endif
244   
245   return TRUE;
246 }
247
248 /* Resolves hostname by IP address async. */
249
250 void silc_net_gethostbyaddr_async(const char *addr, 
251                                   SilcSchedule schedule,
252                                   SilcNetResolveCallback completion,
253                                   void *context)
254 {
255   SilcNetResolveContext r = silc_calloc(1, sizeof(*r));
256
257   r->completion = completion;
258   r->context = context;
259   r->schedule = schedule;
260   r->input = strdup(addr);
261
262   silc_thread_create(silc_net_gethostbyaddr_thread, r, FALSE);
263 }
264
265 /* Performs lookups for remote name and IP address. This peforms reverse
266    lookup as well to verify that the IP has FQDN. */
267
268 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
269 {
270   struct sockaddr_in remote;
271   struct hostent *dest;
272   char *host_ip = NULL;
273   char host_name[1024];
274   int rval, len;
275   int i;
276
277   *hostname = NULL;
278   *ip = NULL;
279
280   SILC_LOG_DEBUG(("Resolving remote hostname and IP address"));
281
282   memset(&remote, 0, sizeof(remote));
283   len = sizeof(remote);
284   rval = getpeername(sock, (struct sockaddr *)&remote, &len);
285   if (rval < 0)
286     return FALSE;
287
288   host_ip = inet_ntoa(remote.sin_addr);
289   if (!host_ip)
290     return FALSE;
291
292   *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
293   memcpy(*ip, host_ip, strlen(host_ip));
294
295   /* Get host by address */
296   dest = gethostbyaddr((char *)&remote.sin_addr, 
297                        sizeof(struct in_addr), AF_INET);
298   if (!dest)
299     return FALSE;
300
301   /* Get same host by name to see that the remote host really is
302      the who it says it is */
303   memset(host_name, 0, sizeof(host_name));
304   memcpy(host_name, dest->h_name, strlen(dest->h_name));
305
306   *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
307   memcpy(*hostname, host_name, strlen(host_name));
308   SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
309
310   dest = gethostbyname(host_name);
311   if (!dest)
312     return FALSE;
313
314   /* Find the address from list */
315   for (i = 0; dest->h_addr_list[i]; i++)
316     if (!memcmp(dest->h_addr_list[i], &remote.sin_addr, 
317                 sizeof(struct in_addr)))
318       break;
319   if (!dest->h_addr_list[i])
320     return FALSE;
321
322   silc_free(*ip);
323   *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
324   memcpy(*ip, host_ip, strlen(host_ip));
325   SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
326
327   return TRUE;
328 }
329
330 /* Performs lookups for local name and IP address. This peforms reverse
331    lookup as well to verify that the IP has FQDN. */
332
333 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip)
334 {
335   struct sockaddr_in local;
336   struct hostent *dest;
337   char *host_ip = NULL;
338   char host_name[1024];
339   int rval, len;
340   int i;
341
342   *hostname = NULL;
343   *ip = NULL;
344
345   SILC_LOG_DEBUG(("Resolving local hostname and IP address"));
346
347   memset(&local, 0, sizeof(local));
348   len = sizeof(local);
349   rval = getsockname(sock, (struct sockaddr *)&local, &len);
350   if (rval < 0)
351     return FALSE;
352
353   host_ip = inet_ntoa(local.sin_addr);
354   if (!host_ip)
355     return FALSE;
356
357   *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
358   memcpy(*ip, host_ip, strlen(host_ip));
359
360   /* Get host by address */
361   dest = gethostbyaddr((char *)&local.sin_addr, 
362                        sizeof(struct in_addr), AF_INET);
363   if (!dest)
364     return FALSE;
365
366   /* Get same host by name to see that the local host really is
367      the who it says it is */
368   memset(host_name, 0, sizeof(host_name));
369   memcpy(host_name, dest->h_name, strlen(dest->h_name));
370
371   *hostname = silc_calloc(strlen(host_name) + 1, sizeof(char));
372   memcpy(*hostname, host_name, strlen(host_name));
373   SILC_LOG_DEBUG(("Resolved hostname `%s'", *hostname));
374
375   dest = gethostbyname(host_name);
376   if (!dest)
377     return FALSE;
378
379   /* Find the address from list */
380   for (i = 0; dest->h_addr_list[i]; i++)
381     if (!memcmp(dest->h_addr_list[i], &local.sin_addr, 
382                sizeof(struct in_addr)))
383       break;
384   if (!dest->h_addr_list[i])
385     return FALSE;
386
387   silc_free(*ip);
388   *ip = silc_calloc(strlen(host_ip) + 1, sizeof(char));
389   memcpy(*ip, host_ip, strlen(host_ip));
390   SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
391
392   return TRUE;
393 }
394
395 /* Return remote port by socket. */
396
397 uint16 silc_net_get_remote_port(int sock)
398 {
399   struct sockaddr_in remote;
400   int len;
401
402   memset(&remote, 0, sizeof(remote));
403   len = sizeof(remote);
404   if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
405     return 0;
406
407   return ntohs(remote.sin_port);
408 }
409
410 /* Return local port by socket. */
411
412 uint16 silc_net_get_local_port(int sock)
413 {
414   struct sockaddr_in local;
415   int len;
416
417   memset(&local, 0, sizeof(local));
418   len = sizeof(local);
419   if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
420     return 0;
421
422   return ntohs(local.sin_port);
423 }
424
425 /* Return name of localhost. */
426
427 char *silc_net_localhost(void)
428 {
429   char hostname[256], ip_addr[64];
430
431   if (gethostname(hostname, sizeof(hostname)))
432     return NULL;
433
434   if (!silc_net_gethostbyname(hostname, ip_addr, sizeof(ip_addr)))
435     return strdup(hostname);
436
437   silc_net_gethostbyaddr(ip_addr, hostname, sizeof(hostname));
438   return strdup(hostname);
439 }
440
441 /* Returns local IP address */
442
443 char *silc_net_localip(void)
444 {
445   char hostname[256], ip_addr[64];
446
447   if (gethostname(hostname, sizeof(hostname)))
448     return NULL;
449
450   if (!silc_net_gethostbyname(hostname, ip_addr, sizeof(ip_addr)))
451     return NULL;
452
453   return strdup(ip_addr);
454 }