Also free the pointer returned by readline().
[tinc] / 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.16 2000/11/22 18:54:08 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 "net.h"
39 #include "netutl.h"
40
41 #include "system.h"
42
43
44 /*
45   free a queue and all of its elements
46 */
47 void destroy_queue(packet_queue_t *pq)
48 {
49   queue_element_t *p, *q;
50 cp
51   for(p = pq->head; p != NULL; p = q)
52     {
53       q = p->next;
54       if(p->packet)
55         free(p->packet);
56       free(p);
57     }
58
59   free(pq);
60 cp
61 }
62
63
64 char *hostlookup(unsigned long addr)
65 {
66   char *name;
67   struct hostent *host = NULL;
68   struct in_addr in;
69   config_t const *cfg;
70   int lookup_hostname;
71 cp
72   in.s_addr = addr;
73
74   lookup_hostname = 0;
75   if((cfg = get_config_val(config, config_hostnames)) != NULL)
76     if(cfg->data.val == stupid_true)
77       lookup_hostname = 1;
78
79   if(lookup_hostname)
80     host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
81
82   if(!lookup_hostname || !host)
83     {
84       asprintf(&name, "%s", inet_ntoa(in));
85     }
86   else
87     {
88       asprintf(&name, "%s", host->h_name);
89     }
90 cp
91   return name;
92 }
93
94 /*
95   Turn a string into an IP addy with netmask
96   return NULL on failure
97 */
98 ip_mask_t *strtoip(char *str)
99 {
100   ip_mask_t *ip;
101   int masker;
102   char *q, *p;
103   struct hostent *h;
104 cp
105   p = str;
106   if((q = strchr(p, '/')))
107     {
108       *q = '\0';
109       q++; /* q now points to netmask part, or NULL if no mask */
110     }
111
112   if(!(h = gethostbyname(p)))
113     {
114       if(debug_lvl >= DEBUG_ERROR)
115         syslog(LOG_WARNING, _("Error looking up `%s': %s\n"), p, strerror(errno));
116         
117       return NULL;
118     }
119
120   masker = 0;
121   if(q)
122     {
123       masker = strtol(q, &p, 10);
124       if(q == p || (*p))
125         return NULL;
126     }
127
128   ip = xmalloc(sizeof(*ip));
129   ip->address = ntohl(*((ip_t*)(h->h_addr_list[0])));
130
131   ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;
132 cp
133   return ip;
134 }
135