fromstr.c
3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011-2012, 2014-2016 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <mailutils/cidr.h>
#include <mailutils/errno.h>
#include <mailutils/sockaddr.h>
static void
masklen_to_netmask (unsigned char *buf, size_t len, size_t masklen)
{
int i, cnt;
cnt = masklen / 8;
for (i = 0; i < cnt; i++)
buf[i] = 0xff;
if (i == MU_INADDR_BYTES)
return;
cnt = 8 - masklen % 8;
buf[i++] = (0xff >> cnt) << cnt;
for (; i < MU_INADDR_BYTES; i++)
buf[i] = 0;
}
int
mu_cidr_from_string (struct mu_cidr *pcidr, const char *str)
{
int rc;
char ipbuf[41];
struct mu_cidr cidr;
char *p;
size_t len;
union
{
struct in_addr in;
#ifdef MAILUTILS_IPV6
struct in6_addr in6;
#endif
} inaddr;
p = strchr (str, '/');
if (p)
len = p - str;
else
len = strlen (str);
if (len > sizeof (ipbuf))
return MU_ERR_BUFSPACE;
memcpy (ipbuf, str, len);
ipbuf[len] = 0;
if (mu_str_is_ipv4 (ipbuf))
cidr.family = AF_INET;
#ifdef MAILUTILS_IPV6
else if (mu_str_is_ipv6 (ipbuf))
cidr.family = AF_INET6;
#endif
else
return MU_ERR_FAMILY;
rc = inet_pton (cidr.family, ipbuf, &inaddr);
if (rc == -1)
return MU_ERR_FAMILY;
else if (rc == 0)
return MU_ERR_NONAME;
else if (rc != 1)
return MU_ERR_FAILURE;
cidr.len = _mu_inaddr_to_bytes (cidr.family, &inaddr, cidr.address);
if (cidr.len == 0)
return MU_ERR_FAMILY;
if (p)
{
char *end;
unsigned long masklen;
p++;
masklen = strtoul (p, &end, 10);
if (*end == 0)
masklen_to_netmask (cidr.netmask, cidr.len, masklen);
else if ((cidr.family == AF_INET && mu_str_is_ipv4 (p))
#ifdef MAILUTILS_IPV6
|| (cidr.family == AF_INET6 && mu_str_is_ipv6 (ipbuf))
#endif
)
{
rc = inet_pton (cidr.family, p, &inaddr);
if (rc == -1)
return MU_ERR_FAMILY;
else if (rc == 0)
return MU_ERR_NONAME;
else if (rc != 1)
return MU_ERR_FAILURE;
_mu_inaddr_to_bytes (cidr.family, &inaddr, cidr.netmask);
}
else
return MU_ERR_FAMILY;
}
else
masklen_to_netmask (cidr.netmask, cidr.len, cidr.len * 8);
memcpy (pcidr, &cidr, sizeof (*pcidr));
return 0;
}