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