Big and bad commit of my current tree...
[oweals/tinc.git] / src / netutl.c
1 /*
2     netutl.c -- some supporting network utility code
3     Copyright (C) 1998,1999,2000 Ivo Timmermans <itimmermans@bigfoot.com>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id: netutl.c,v 1.12.4.11 2000/10/11 10:35:17 guus Exp $
20 */
21
22 #include "config.h"
23
24 #include <arpa/inet.h>
25 #include <netdb.h>
26 #include <netinet/in.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/socket.h>
31 #include <syslog.h>
32
33 #include <utils.h>
34 #include <xalloc.h>
35
36 #include "errno.h"
37 #include "conf.h"
38 #include "encr.h"
39 #include "net.h"
40 #include "netutl.h"
41
42 #include "system.h"
43
44
45 /*
46   free a queue and all of its elements
47 */
48 void destroy_queue(packet_queue_t *pq)
49 {
50   queue_element_t *p, *q;
51 cp
52   for(p = pq->head; p != NULL; p = q)
53     {
54       q = p->next;
55       if(p->packet)
56         free(p->packet);
57       free(p);
58     }
59
60   free(pq);
61 cp
62 }
63
64
65 char *hostlookup(unsigned long addr)
66 {
67   char *name;
68   struct hostent *host = NULL;
69   struct in_addr in;
70   config_t const *cfg;
71   int lookup_hostname;
72 cp
73   in.s_addr = addr;
74
75   lookup_hostname = 0;
76   if((cfg = get_config_val(resolve_dns)) != NULL)
77     if(cfg->data.val == stupid_true)
78       lookup_hostname = 1;
79
80   if(lookup_hostname)
81     host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
82
83   if(!lookup_hostname || !host)
84     {
85       asprintf(&name, "%s", inet_ntoa(in));
86     }
87   else
88     {
89       asprintf(&name, "%s", host->h_name);
90     }
91 cp
92   return name;
93 }
94
95 /*
96   Turn a string into an IP addy with netmask
97   return NULL on failure
98 */
99 ip_mask_t *strtoip(char *str)
100 {
101   ip_mask_t *ip;
102   int masker;
103   char *q, *p;
104   struct hostent *h;
105 cp
106   p = str;
107   if((q = strchr(p, '/')))
108     {
109       *q = '\0';
110       q++; /* q now points to netmask part, or NULL if no mask */
111     }
112
113   if(!(h = gethostbyname(p)))
114     {
115       fprintf(stderr, _("Error looking up `%s': %s\n"), p, strerror(errno));
116       return NULL;
117     }
118
119   masker = 0;
120   if(q)
121     {
122       masker = strtol(q, &p, 10);
123       if(q == p || (*p))
124         return NULL;
125     }
126
127   ip = xmalloc(sizeof(*ip));
128   ip->ip = ntohl(*((ip_t*)(h->h_addr_list[0])));
129
130   ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;
131 cp
132   return ip;
133 }
134