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