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