diff options
Diffstat (limited to 'libmailutils')
29 files changed, 1899 insertions, 672 deletions
diff --git a/libmailutils/Makefile.am b/libmailutils/Makefile.am index ed1aeb019..5aef3382d 100644 --- a/libmailutils/Makefile.am +++ b/libmailutils/Makefile.am | |||
@@ -16,7 +16,7 @@ | |||
16 | # Public License along with this library. If not, see | 16 | # Public License along with this library. If not, see |
17 | # <http://www.gnu.org/licenses/>. | 17 | # <http://www.gnu.org/licenses/>. |
18 | 18 | ||
19 | SUBDIRS = auth base address cfg diag filter mailbox mailer mime\ | 19 | SUBDIRS = auth base address sockaddr cidr cfg diag filter mailbox mailer mime\ |
20 | server string stream stdstream property url . tests | 20 | server string stream stdstream property url . tests |
21 | 21 | ||
22 | lib_LTLIBRARIES = libmailutils.la | 22 | lib_LTLIBRARIES = libmailutils.la |
@@ -28,6 +28,8 @@ libmailutils_la_LIBADD = \ | |||
28 | auth/libauth.la\ | 28 | auth/libauth.la\ |
29 | base/libbase.la\ | 29 | base/libbase.la\ |
30 | address/libaddress.la\ | 30 | address/libaddress.la\ |
31 | sockaddr/libsockaddr.la\ | ||
32 | cidr/libcidr.la\ | ||
31 | cfg/libcfg.la\ | 33 | cfg/libcfg.la\ |
32 | diag/libdiag.la\ | 34 | diag/libdiag.la\ |
33 | filter/libfilter.la\ | 35 | filter/libfilter.la\ |
diff --git a/libmailutils/cidr/Makefile.am b/libmailutils/cidr/Makefile.am new file mode 100644 index 000000000..408806f79 --- /dev/null +++ b/libmailutils/cidr/Makefile.am | |||
@@ -0,0 +1,27 @@ | |||
1 | # GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | # Copyright (C) 2011 Free Software Foundation, Inc. | ||
3 | # | ||
4 | # This library is free software; you can redistribute it and/or | ||
5 | # modify it under the terms of the GNU Lesser General Public | ||
6 | # License as published by the Free Software Foundation; either | ||
7 | # version 3 of the License, or (at your option) any later version. | ||
8 | # | ||
9 | # This library is distributed in the hope that it will be useful, | ||
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | # Lesser General Public License for more details. | ||
13 | # | ||
14 | # You should have received a copy of the GNU Lesser General | ||
15 | # Public License along with this library. If not, see | ||
16 | # <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | noinst_LTLIBRARIES = libcidr.la | ||
19 | |||
20 | libcidr_la_SOURCES = \ | ||
21 | fromsa.c\ | ||
22 | fromstr.c\ | ||
23 | match.c\ | ||
24 | tosa.c\ | ||
25 | tostr.c | ||
26 | |||
27 | INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils | ||
diff --git a/libmailutils/cidr/fromsa.c b/libmailutils/cidr/fromsa.c new file mode 100644 index 000000000..a2fda6680 --- /dev/null +++ b/libmailutils/cidr/fromsa.c | |||
@@ -0,0 +1,101 @@ | |||
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2011 Free Software Foundation, Inc. | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Lesser General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 3 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Lesser General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Lesser General | ||
15 | Public License along with this library. If not, see | ||
16 | <http://www.gnu.org/licenses/>. */ | ||
17 | |||
18 | #ifdef HAVE_CONFIG_H | ||
19 | # include <config.h> | ||
20 | #endif | ||
21 | #include <sys/types.h> | ||
22 | #include <sys/socket.h> | ||
23 | #include <sys/stat.h> | ||
24 | #include <netinet/in.h> | ||
25 | #include <arpa/inet.h> | ||
26 | #include <netdb.h> | ||
27 | #include <string.h> | ||
28 | #include <stdlib.h> | ||
29 | #include <mailutils/cidr.h> | ||
30 | #include <mailutils/errno.h> | ||
31 | |||
32 | static void | ||
33 | uint32_to_bytes (unsigned char *bytes, uint32_t u) | ||
34 | { | ||
35 | int i; | ||
36 | |||
37 | for (i = 0; i < 4; i++) | ||
38 | { | ||
39 | bytes[i] = u & 0xff; | ||
40 | u >>= 8; | ||
41 | } | ||
42 | } | ||
43 | |||
44 | int | ||
45 | _mu_inaddr_to_bytes (int af, void *buf, unsigned char *bytes) | ||
46 | { | ||
47 | uint32_t u; | ||
48 | |||
49 | switch (af) | ||
50 | { | ||
51 | case AF_INET: | ||
52 | memcpy (&u, buf, sizeof u); | ||
53 | uint32_to_bytes (bytes, u); | ||
54 | return 4; | ||
55 | |||
56 | #ifdef MAILUTILS_IPV6 | ||
57 | case AF_INET6: | ||
58 | memcpy (bytes, buf, 16); | ||
59 | return 16; | ||
60 | #endif | ||
61 | } | ||
62 | return 0; | ||
63 | } | ||
64 | |||
65 | int | ||
66 | _mu_sockaddr_to_bytes (unsigned char *bytes, struct sockaddr const *sa) | ||
67 | { | ||
68 | switch (sa->sa_family) | ||
69 | { | ||
70 | case AF_INET: | ||
71 | uint32_to_bytes (bytes, ((struct sockaddr_in*)sa)->sin_addr.s_addr); | ||
72 | return 4; | ||
73 | |||
74 | #ifdef MAILUTILS_IPV6 | ||
75 | case AF_INET6: | ||
76 | memcpy (bytes, &((struct sockaddr_in6*)sa)->sin6_addr, 16); | ||
77 | return 16; | ||
78 | #endif | ||
79 | } | ||
80 | return 0; | ||
81 | } | ||
82 | |||
83 | int | ||
84 | mu_cidr_from_sockaddr (struct mu_cidr *cidr, const struct sockaddr *sa) | ||
85 | { | ||
86 | unsigned char address[MU_INADDR_BYTES]; | ||
87 | int len; | ||
88 | int i; | ||
89 | |||
90 | len = _mu_sockaddr_to_bytes (address, sa); | ||
91 | if (len == 0) | ||
92 | return MU_ERR_FAMILY; | ||
93 | cidr->family = sa->sa_family; | ||
94 | cidr->len = len; | ||
95 | memcpy (cidr->address, address, sizeof (cidr->address)); | ||
96 | for (i = 0; i < MU_INADDR_BYTES; i++) | ||
97 | cidr->netmask[i] = 0xff; | ||
98 | return 0; | ||
99 | } | ||
100 | |||
101 | |||
diff --git a/libmailutils/cidr/fromstr.c b/libmailutils/cidr/fromstr.c new file mode 100644 index 000000000..4e7a56f80 --- /dev/null +++ b/libmailutils/cidr/fromstr.c | |||
@@ -0,0 +1,128 @@ | |||
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2011 Free Software Foundation, Inc. | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Lesser General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 3 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Lesser General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Lesser General | ||
15 | Public License along with this library. If not, see | ||
16 | <http://www.gnu.org/licenses/>. */ | ||
17 | |||
18 | #ifdef HAVE_CONFIG_H | ||
19 | # include <config.h> | ||
20 | #endif | ||
21 | #include <string.h> | ||
22 | #include <stdlib.h> | ||
23 | #include <arpa/inet.h> | ||
24 | #include <mailutils/cidr.h> | ||
25 | #include <mailutils/errno.h> | ||
26 | #include <mailutils/sockaddr.h> | ||
27 | |||
28 | static void | ||
29 | masklen_to_netmask (unsigned char *buf, size_t len, size_t masklen) | ||
30 | { | ||